Migrated HttpInvoker adapter and parser code from "org.springframework.integration.adapter" to the new "org.springframework.integration.httpinvoker" module, and added a dedicated spring-integration-httpinvoker-1.0.xsd schema and HttpInvokerNamespaceHandler. Also refactored base and support classes for remoting-based Messaging Gateways (including both HttpInvoker and RMI adapters).

This commit is contained in:
Mark Fisher
2008-09-20 19:32:56 +00:00
parent 870ab7b9f4
commit 890d7b227a
28 changed files with 261 additions and 224 deletions

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2002-2008 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.httpinvoker;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.adapter.RemotingInboundGatewaySupport;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.MessagingException;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.web.HttpRequestHandler;
/**
* An inbound gateway adapter for HttpInvoker-based remoting. Since this class implements
* {@link HttpRequestHandler}, it can be configured with a delegating Servlet where the
* servlet-name matches this adapter's bean name. For example, the following servlet can
* be defined in web.xml:
*
* <pre class="code">
* &lt;servlet&gt;
* &lt;servlet-name&gt;httpInvokerGateway&lt;/servlet-name&gt;
* &lt;servlet-class&gt;org.springframework.web.context.support.HttpRequestHandlerServlet&lt;/servlet-class&gt;
* &lt;/servlet&gt;
* </pre>
*
* And, this would match the following bean definition in the application context loaded
* by a {@link org.springframework.web.context.ContextLoaderListener}:
*
* <pre class="code">
* &lt;bean id="httpInvokerGateway" class="org.springframework.integration.adapter.httpinvoker.HttpInvokerGateway"&gt;
* &lt;constructor-arg ref="requestChannel"/&gt;
* &lt;/bean&gt;
* </pre>
*
* <p>
* Alternatively, in a Spring MVC application, the DispatcherServlet can delegate to the
* "httpInvokerGateway" bean based on a handler mapping configuration. In that case,
* the HttpRequestHandlerServlet would not be necessary.
* </p>
*
* @author Mark Fisher
*/
public class HttpInvokerInboundGateway extends RemotingInboundGatewaySupport implements HttpRequestHandler, InitializingBean {
private volatile HttpInvokerServiceExporter exporter;
public void afterPropertiesSet() {
HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
exporter.setService(this);
exporter.setServiceInterface(MessageHandler.class);
exporter.afterPropertiesSet();
this.exporter = exporter;
}
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (this.exporter == null) {
throw new MessagingException("adapter has not been initialized");
}
this.exporter.handleRequest(request, response);
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2002-2008 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.httpinvoker;
import org.springframework.integration.adapter.AbstractRemotingOutboundGateway;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
/**
* A MessageHandler adapter for HttpInvoker-based remoting.
*
* @author Mark Fisher
*/
public class HttpInvokerOutboundGateway extends AbstractRemotingOutboundGateway {
public HttpInvokerOutboundGateway(String url) {
super(url);
}
@Override
protected MessageHandler createHandlerProxy(String url) {
HttpInvokerProxyFactoryBean proxyFactory = new HttpInvokerProxyFactoryBean();
proxyFactory.setServiceInterface(MessageHandler.class);
proxyFactory.setServiceUrl(url);
proxyFactory.afterPropertiesSet();
return (MessageHandler) proxyFactory.getObject();
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2002-2008 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.httpinvoker.config;
import org.w3c.dom.Element;
import org.springframework.integration.adapter.config.AbstractRemotingGatewayParser;
import org.springframework.integration.httpinvoker.HttpInvokerInboundGateway;
/**
* Parser for the &lt;httpinvoker-gateway/&gt; element.
*
* @author Mark Fisher
*/
public class HttpInvokerInboundGatewayParser extends AbstractRemotingGatewayParser {
@Override
protected Class<?> getBeanClass(Element element) {
return HttpInvokerInboundGateway.class;
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2002-2008 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.httpinvoker.config;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
/**
* Namespace handler for Spring Integration's <em>httpinvoker</em> namespace.
*
* @author Mark Fisher
*/
public class HttpInvokerNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
this.registerBeanDefinitionParser("inbound-gateway", new HttpInvokerInboundGatewayParser());
this.registerBeanDefinitionParser("outbound-gateway", new HttpInvokerOutboundGatewayParser());
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2002-2008 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.httpinvoker.config;
import org.w3c.dom.Element;
import org.springframework.integration.adapter.config.AbstractRemotingOutboundGatewayParser;
import org.springframework.integration.httpinvoker.HttpInvokerOutboundGateway;
/**
* Parser for the &lt;outbound-gateway/&gt; element of the 'httpinvoker' namespace.
*
* @author Mark Fisher
*/
public class HttpInvokerOutboundGatewayParser extends AbstractRemotingOutboundGatewayParser {
@Override
protected Class<?> getBeanClass(Element element) {
return HttpInvokerOutboundGateway.class;
}
}

View File

@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.springframework.org/schema/integration/httpinvoker"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.springframework.org/schema/integration/httpinvoker"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:import namespace="http://www.springframework.org/schema/tool"/>
<xsd:annotation>
<xsd:documentation><![CDATA[
Defines the configuration elements for Spring Integration's HttpInvoker adapters.
]]></xsd:documentation>
</xsd:annotation>
<xsd:element name="inbound-gateway">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines an HttpInvoker-based inbound Messaging Gateway.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="gatewayType">
<xsd:attribute name="expect-reply" type="xsd:boolean" default="true"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="outbound-gateway">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines an HttpInvoker-based outbound Messaging Gateway.
</xsd:documentation>
</xsd:annotation>
<xsd:complexContent>
<xsd:extension base="gatewayType">
<xsd:attribute name="url" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="gatewayType">
<xsd:annotation>
<xsd:documentation>
Defines common configuration for gateway adapters.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:ID"/>
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="request-channel" type="xsd:string" use="required"/>
<xsd:attribute name="reply-channel" type="xsd:string"/>
<xsd:attribute name="request-timeout" type="xsd:long"/>
<xsd:attribute name="reply-timeout" type="xsd:long"/>
</xsd:complexType>
</xsd:schema>

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration/httpinvoker=org.springframework.integration.httpinvoker.config.HttpInvokerNamespaceHandler

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration/httpinvoker/spring-integration-httpinvoker-1.0.xsd=org/springframework/integration/httpinvoker/config/spring-integration-httpinvoker-1.0.xsd

View File

@@ -0,0 +1,111 @@
/*
* Copyright 2002-2008 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.httpinvoker;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.concurrent.Executors;
import org.junit.Test;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.httpinvoker.HttpInvokerInboundGateway;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.remoting.support.RemoteInvocation;
import org.springframework.remoting.support.RemoteInvocationResult;
/**
* @author Mark Fisher
*/
public class HttpInvokerInboundGatewayTests {
@Test
public void testRequestOnly() throws Exception {
QueueChannel channel = new QueueChannel();
HttpInvokerInboundGateway gateway = new HttpInvokerInboundGateway();
gateway.setRequestChannel(channel);
gateway.setExpectReply(false);
gateway.afterPropertiesSet();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(createRequestContent(new StringMessage("test")));
gateway.handleRequest(request, response);
Message<?> message = channel.receive(500);
assertNotNull(message);
assertEquals("test", message.getPayload());
}
@Test
public void testRequestReply() throws Exception {
final QueueChannel channel = new QueueChannel();
Executors.newSingleThreadExecutor().execute(new Runnable() {
public void run() {
Message<?> message = channel.receive();
MessageChannel replyChannel = (MessageChannel) message.getHeaders().getReturnAddress();
replyChannel.send(new StringMessage(message.getPayload().toString().toUpperCase()));
}
});
HttpInvokerInboundGateway gateway = new HttpInvokerInboundGateway();
gateway.setRequestChannel(channel);
gateway.setExpectReply(true);
gateway.afterPropertiesSet();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(createRequestContent(new StringMessage("test")));
gateway.handleRequest(request, response);
Message<?> reply = extractMessageFromResponse(response);
assertEquals("TEST", reply.getPayload());
}
private static byte[] createRequestContent(Message<?> message) throws IOException {
RemoteInvocation invocation = new RemoteInvocation(
"handle", new Class[] { Message.class }, new Object[] { message });
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
ObjectOutputStream oos = new ObjectOutputStream(baos);
try {
oos.writeObject(invocation);
oos.flush();
}
finally {
oos.close();
}
return baos.toByteArray();
}
private static Message<?> extractMessageFromResponse(MockHttpServletResponse response) throws IOException, ClassNotFoundException {
byte[] responseContent = response.getContentAsByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(responseContent);
ObjectInputStream ois = new ObjectInputStream(bais);
RemoteInvocationResult remoteResult = (RemoteInvocationResult) ois.readObject();
Object resultValue = remoteResult.getValue();
assertTrue(resultValue instanceof Message);
return (Message<?>) resultValue;
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2002-2008 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.httpinvoker.config;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.httpinvoker.HttpInvokerInboundGateway;
import org.springframework.integration.message.MessageChannelTemplate;
/**
* @author Mark Fisher
*/
public class HttpInvokerInboundGatewayParserTests {
@Test
public void gatewayWithDefaults() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"httpInvokerInboundGatewayParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
HttpInvokerInboundGateway gateway = (HttpInvokerInboundGateway) context.getBean("gatewayWithDefaults");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
assertEquals(true, accessor.getPropertyValue("expectReply"));
assertEquals(channel, accessor.getPropertyValue("requestChannel"));
MessageChannelTemplate template = (MessageChannelTemplate)
accessor.getPropertyValue("channelTemplate");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
assertEquals(-1L, templateAccessor.getPropertyValue("sendTimeout"));
assertEquals(-1L, templateAccessor.getPropertyValue("receiveTimeout"));
}
@Test
public void gatewayWithName() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"httpInvokerInboundGatewayParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
HttpInvokerInboundGateway gateway = (HttpInvokerInboundGateway) context.getBean("/gateway/with/name");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
assertEquals(true, accessor.getPropertyValue("expectReply"));
assertEquals(channel, accessor.getPropertyValue("requestChannel"));
MessageChannelTemplate template = (MessageChannelTemplate)
accessor.getPropertyValue("channelTemplate");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
assertEquals(-1L, templateAccessor.getPropertyValue("sendTimeout"));
assertEquals(-1L, templateAccessor.getPropertyValue("receiveTimeout"));
}
@Test
public void gatewayWithCustomProperties() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"httpInvokerInboundGatewayParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
HttpInvokerInboundGateway gateway = (HttpInvokerInboundGateway) context.getBean("gatewayWithCustomProperties");
DirectFieldAccessor accessor = new DirectFieldAccessor(gateway);
assertEquals(false, accessor.getPropertyValue("expectReply"));
assertEquals(channel, accessor.getPropertyValue("requestChannel"));
MessageChannelTemplate template = (MessageChannelTemplate)
accessor.getPropertyValue("channelTemplate");
DirectFieldAccessor templateAccessor = new DirectFieldAccessor(template);
assertEquals(123L, templateAccessor.getPropertyValue("sendTimeout"));
assertEquals(456L, templateAccessor.getPropertyValue("receiveTimeout"));
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2002-2008 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.httpinvoker.config;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.httpinvoker.HttpInvokerOutboundGateway;
/**
* @author Mark Fisher
*/
public class HttpInvokerOutboundGatewayParserTests {
@Test
public void testHttpInvokerOutboundGatewayParser() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"httpInvokerOutboundGatewayParserTests.xml", this.getClass());
Object gateway = context.getBean("gateway");
assertEquals(HttpInvokerOutboundGateway.class, gateway.getClass());
}
}

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:httpinvoker="http://www.springframework.org/schema/integration/httpinvoker"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/integration/httpinvoker
http://www.springframework.org/schema/integration/httpinvoker/spring-integration-httpinvoker-1.0.xsd">
<message-bus/>
<channel id="testChannel"/>
<httpinvoker:inbound-gateway id="gatewayWithDefaults" request-channel="testChannel"/>
<httpinvoker:inbound-gateway name="/gateway/with/name" request-channel="testChannel"/>
<httpinvoker:inbound-gateway id="gatewayWithCustomProperties"
request-channel="testChannel"
request-timeout="123"
expect-reply="false"
reply-timeout="456"/>
</beans:beans>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:httpinvoker="http://www.springframework.org/schema/integration/httpinvoker"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
http://www.springframework.org/schema/integration/httpinvoker
http://www.springframework.org/schema/integration/httpinvoker/spring-integration-httpinvoker-1.0.xsd">
<message-bus/>
<channel id="testChannel"/>
<httpinvoker:outbound-gateway id="gateway" url="http://localhost:8080/test" request-channel="testChannel"/>
</beans:beans>