INT-3991: Add IntegrationGraphController
JIRA: https://jira.spring.io/browse/INT-3991 * Add `IntegrationGraphController` `@RestController` over `IntegrationGraphServer` bean * Add `@EnableIntegrationGraphController` and `<int-http:graph-controller>` to register `IntegrationGraphController` if the `DispatcherServlet` is in classpath. * Also register the `IntegrationGraphServer` bean from the same place, if that isn't presented in the application context yet. * Allow to configure the "root" `path` for the `IntegrationGraphController` as a placeholder value via `spring.integration.graph.controller.request.mapping.path` property * Add tests for both `@EnableIntegrationGraphController` and `<int-http:graph-controller>` cases based on the `MockMvc`
This commit is contained in:
committed by
Gary Russell
parent
b84747b090
commit
8df487c96f
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.http.config;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
import org.springframework.integration.http.management.IntegrationGraphController;
|
||||
import org.springframework.integration.http.support.HttpContextUtils;
|
||||
|
||||
/**
|
||||
* Enables the {@link IntegrationGraphController} if {@code DispatcherServlet} is present in the classpath.
|
||||
*
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 4.3
|
||||
*
|
||||
* @see IntegrationGraphController
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
@Import(IntegrationGraphControllerRegistrarImportSelector.class)
|
||||
public @interface EnableIntegrationGraphController {
|
||||
|
||||
/**
|
||||
* Specify the Request Mapping path for the {@link IntegrationGraphController}.
|
||||
* Defaults to {@value HttpContextUtils#GRAPH_CONTROLLER_DEFAULT_PATH}.
|
||||
* @return The Request Mapping path for the {@link IntegrationGraphController}
|
||||
*/
|
||||
@AliasFor("path")
|
||||
String value() default HttpContextUtils.GRAPH_CONTROLLER_DEFAULT_PATH;
|
||||
|
||||
/**
|
||||
* Specify the Request Mapping path for the {@link IntegrationGraphController}.
|
||||
* Defaults to {@value HttpContextUtils#GRAPH_CONTROLLER_DEFAULT_PATH}.
|
||||
* @return The Request Mapping path for the {@link IntegrationGraphController}
|
||||
*/
|
||||
@AliasFor("value")
|
||||
String path() default HttpContextUtils.GRAPH_CONTROLLER_DEFAULT_PATH;
|
||||
|
||||
}
|
||||
@@ -28,7 +28,6 @@ import org.springframework.integration.config.IntegrationConfigurationInitialize
|
||||
import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
|
||||
import org.springframework.integration.http.inbound.IntegrationRequestMappingHandlerMapping;
|
||||
import org.springframework.integration.http.support.HttpContextUtils;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* The HTTP Integration infrastructure {@code beanFactory} initializer.
|
||||
@@ -40,9 +39,6 @@ public class HttpIntegrationConfigurationInitializer implements IntegrationConfi
|
||||
|
||||
private static final Log logger = LogFactory.getLog(HttpIntegrationConfigurationInitializer.class);
|
||||
|
||||
private static final boolean servletPresent = ClassUtils.isPresent("javax.servlet.Servlet",
|
||||
HttpIntegrationConfigurationInitializer.class.getClassLoader());
|
||||
|
||||
@Override
|
||||
public void initialize(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
if (beanFactory instanceof BeanDefinitionRegistry) {
|
||||
@@ -65,7 +61,8 @@ public class HttpIntegrationConfigurationInitializer implements IntegrationConfi
|
||||
* the HTTP server components.
|
||||
*/
|
||||
private void registerRequestMappingHandlerMappingIfNecessary(BeanDefinitionRegistry registry) {
|
||||
if (servletPresent && !registry.containsBeanDefinition(HttpContextUtils.HANDLER_MAPPING_BEAN_NAME)) {
|
||||
if (HttpContextUtils.SERVLET_PRESENT &&
|
||||
!registry.containsBeanDefinition(HttpContextUtils.HANDLER_MAPPING_BEAN_NAME)) {
|
||||
BeanDefinitionBuilder requestMappingBuilder =
|
||||
BeanDefinitionBuilder.genericBeanDefinition(IntegrationRequestMappingHandlerMapping.class);
|
||||
requestMappingBuilder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,15 +22,18 @@ import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHa
|
||||
* Namespace handler for Spring Integration's <em>http</em> namespace.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 1.0.2
|
||||
*/
|
||||
public class HttpNamespaceHandler extends AbstractIntegrationNamespaceHandler {
|
||||
|
||||
public void init() {
|
||||
this.registerBeanDefinitionParser("inbound-channel-adapter", new HttpInboundEndpointParser(false));
|
||||
this.registerBeanDefinitionParser("inbound-gateway", new HttpInboundEndpointParser(true));
|
||||
this.registerBeanDefinitionParser("outbound-channel-adapter", new HttpOutboundChannelAdapterParser());
|
||||
this.registerBeanDefinitionParser("outbound-gateway", new HttpOutboundGatewayParser());
|
||||
registerBeanDefinitionParser("inbound-channel-adapter", new HttpInboundEndpointParser(false));
|
||||
registerBeanDefinitionParser("inbound-gateway", new HttpInboundEndpointParser(true));
|
||||
registerBeanDefinitionParser("outbound-channel-adapter", new HttpOutboundChannelAdapterParser());
|
||||
registerBeanDefinitionParser("outbound-gateway", new HttpOutboundGatewayParser());
|
||||
registerBeanDefinitionParser("graph-controller", new IntegrationGraphControllerParser());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.http.config;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.core.type.StandardAnnotationMetadata;
|
||||
import org.springframework.integration.http.support.HttpContextUtils;
|
||||
|
||||
/**
|
||||
* The {@link BeanDefinitionParser} for the {@code <int-http:graph-controller>} component.
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
public class IntegrationGraphControllerParser implements BeanDefinitionParser {
|
||||
|
||||
private final IntegrationGraphControllerRegistrar graphControllerRegistrar =
|
||||
new IntegrationGraphControllerRegistrar();
|
||||
|
||||
@Override
|
||||
public BeanDefinition parse(final Element element, ParserContext parserContext) {
|
||||
if (HttpContextUtils.SERVLET_PRESENT) {
|
||||
this.graphControllerRegistrar.registerBeanDefinitions(
|
||||
new StandardAnnotationMetadata(IntegrationGraphControllerParser.class) {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getAnnotationAttributes(String annotationType) {
|
||||
return Collections.<String, Object>singletonMap("value", element.getAttribute("path"));
|
||||
}
|
||||
|
||||
}, parserContext.getRegistry());
|
||||
}
|
||||
else {
|
||||
parserContext.getReaderContext().warning("The 'IntegrationGraphController' isn't registered " +
|
||||
"with the application context because" +
|
||||
" there is no 'org.springframework.web.servlet.DispatcherServlet' in the classpath.", element);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.http.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.integration.http.management.IntegrationGraphController;
|
||||
import org.springframework.integration.http.support.HttpContextUtils;
|
||||
import org.springframework.integration.support.management.graph.IntegrationGraphServer;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
* @since 4.3
|
||||
*/
|
||||
class IntegrationGraphControllerRegistrar implements ImportBeanDefinitionRegistrar {
|
||||
|
||||
@Override
|
||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
|
||||
Map<String, Object> annotationAttributes =
|
||||
importingClassMetadata.getAnnotationAttributes(EnableIntegrationGraphController.class.getName());
|
||||
|
||||
if (!registry.containsBeanDefinition(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME)) {
|
||||
registry.registerBeanDefinition(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME,
|
||||
new RootBeanDefinition(IntegrationGraphServer.class));
|
||||
}
|
||||
|
||||
if (!registry.containsBeanDefinition(HttpContextUtils.GRAPH_CONTROLLER_BEAN_NAME)) {
|
||||
AbstractBeanDefinition controllerPropertiesPopulator =
|
||||
BeanDefinitionBuilder.genericBeanDefinition(GraphControllerPropertiesPopulator.class)
|
||||
.addConstructorArgValue(annotationAttributes)
|
||||
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
|
||||
.getBeanDefinition();
|
||||
BeanDefinitionReaderUtils.registerWithGeneratedName(controllerPropertiesPopulator, registry);
|
||||
|
||||
BeanDefinition graphController =
|
||||
BeanDefinitionBuilder.genericBeanDefinition(IntegrationGraphController.class)
|
||||
.addConstructorArgReference(IntegrationContextUtils.INTEGRATION_GRAPH_SERVER_BEAN_NAME)
|
||||
.getBeanDefinition();
|
||||
|
||||
registry.registerBeanDefinition(HttpContextUtils.GRAPH_CONTROLLER_BEAN_NAME, graphController);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class GraphControllerPropertiesPopulator
|
||||
implements BeanFactoryPostProcessor, EnvironmentAware {
|
||||
|
||||
private final Map<String, Object> properties = new HashMap<String, Object>();
|
||||
|
||||
private GraphControllerPropertiesPopulator(Map<String, Object> annotationAttributes) {
|
||||
Object graphControllerPath = annotationAttributes.get(AnnotationUtils.VALUE);
|
||||
this.properties.put(HttpContextUtils.GRAPH_CONTROLLER_PATH_PROPERTY, graphControllerPath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
((ConfigurableEnvironment) environment)
|
||||
.getPropertySources()
|
||||
.addLast(new MapPropertySource(HttpContextUtils.GRAPH_CONTROLLER_BEAN_NAME + "_properties",
|
||||
this.properties));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.http.config;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.context.annotation.ImportSelector;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.integration.http.support.DefaultHttpHeaderMapper;
|
||||
import org.springframework.integration.http.support.HttpContextUtils;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
* @since 4.3
|
||||
*/
|
||||
class IntegrationGraphControllerRegistrarImportSelector implements ImportSelector {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DefaultHttpHeaderMapper.class);
|
||||
|
||||
@Override
|
||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||
if (HttpContextUtils.SERVLET_PRESENT) {
|
||||
return new String[] { IntegrationGraphControllerRegistrar.class.getName() };
|
||||
}
|
||||
else {
|
||||
logger.warn("The 'IntegrationGraphController' isn't registered with the application context because" +
|
||||
" there is no 'org.springframework.web.servlet.DispatcherServlet' in the classpath.");
|
||||
return new String[0];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.http.management;
|
||||
|
||||
import org.springframework.integration.http.support.HttpContextUtils;
|
||||
import org.springframework.integration.support.management.graph.Graph;
|
||||
import org.springframework.integration.support.management.graph.IntegrationGraphServer;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* The REST Controller to provide the management API over {@link IntegrationGraphServer}.
|
||||
*
|
||||
* @author Artem Bilan
|
||||
*
|
||||
* @since 4.3
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping(IntegrationGraphController.REQUEST_MAPPING_PATH_VARIABLE)
|
||||
public class IntegrationGraphController {
|
||||
|
||||
final static String REQUEST_MAPPING_PATH_VARIABLE =
|
||||
"${" + HttpContextUtils.GRAPH_CONTROLLER_PATH_PROPERTY + ":" +
|
||||
HttpContextUtils.GRAPH_CONTROLLER_DEFAULT_PATH + "}";
|
||||
|
||||
private final IntegrationGraphServer integrationGraphServer;
|
||||
|
||||
public IntegrationGraphController(IntegrationGraphServer integrationGraphServer) {
|
||||
this.integrationGraphServer = integrationGraphServer;
|
||||
}
|
||||
|
||||
@GetMapping(name = "getGraph")
|
||||
public Graph getGraph() {
|
||||
return this.integrationGraphServer.getGraph();
|
||||
}
|
||||
|
||||
@GetMapping(path = "/refresh", name = "refreshGraph")
|
||||
public Graph refreshGraph() {
|
||||
return this.integrationGraphServer.rebuild();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Provides classes related to management support.
|
||||
*/
|
||||
package org.springframework.integration.http.management;
|
||||
@@ -16,8 +16,11 @@
|
||||
|
||||
package org.springframework.integration.http.support;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Utility class for accessing HTTP integration components from the {@link org.springframework.beans.factory.BeanFactory}.
|
||||
* Utility class for accessing HTTP integration components
|
||||
* from the {@link org.springframework.beans.factory.BeanFactory}.
|
||||
*
|
||||
* @author Artem Bilan
|
||||
* @author Gary Russell
|
||||
@@ -29,9 +32,34 @@ public final class HttpContextUtils {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code boolean} flag to indicate if the {@code org.springframework.web.servlet.DispatcherServlet}
|
||||
* is present in the CLASSPATH to allow to register the Integration server components,
|
||||
* e.g. {@code IntegrationGraphController}.
|
||||
*/
|
||||
public static final boolean SERVLET_PRESENT =
|
||||
ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet",
|
||||
HttpContextUtils.class.getClassLoader());
|
||||
|
||||
/**
|
||||
* @see org.springframework.integration.http.config.HttpInboundEndpointParser
|
||||
*/
|
||||
public static final String HANDLER_MAPPING_BEAN_NAME = "integrationRequestMappingHandlerMapping";
|
||||
|
||||
/**
|
||||
* Represents the environment property for the {@code IntegrationGraphController} request mapping path.
|
||||
*/
|
||||
public static final String GRAPH_CONTROLLER_PATH_PROPERTY =
|
||||
"spring.integration.graph.controller.request.mapping.path";
|
||||
|
||||
/**
|
||||
* Represents the default request mapping path for the {@code IntegrationGraphController}.
|
||||
*/
|
||||
public static final String GRAPH_CONTROLLER_DEFAULT_PATH = "/integration";
|
||||
|
||||
/**
|
||||
* Represents the bean name for the default {@code IntegrationGraphController}.
|
||||
*/
|
||||
public static final String GRAPH_CONTROLLER_BEAN_NAME = "integrationGraphController";
|
||||
|
||||
}
|
||||
|
||||
@@ -492,6 +492,28 @@
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="graph-controller">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
Configures a
|
||||
'org.springframework.integration.http.management.IntegrationGraphController' bean
|
||||
to expose a REST API for the
|
||||
'org.springframework.integration.support.management.graph.IntegrationGraphServer' bean.
|
||||
Note: Spring Web MVC must be present in the application to enable and register this component.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="path" type="xsd:string" default="/integration">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation>
|
||||
The root request mapping path for the 'IntegrationGraphController'.
|
||||
Defaults to /integration.
|
||||
</xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:complexType name="uriVariableType">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
|
||||
Reference in New Issue
Block a user