Added HttpInvokerSourceAdapter (INT-92).

This commit is contained in:
Mark Fisher
2008-03-18 20:52:27 +00:00
parent 4b067668d4
commit cfa6ff79a7
5 changed files with 182 additions and 1 deletions

View File

@@ -6,6 +6,7 @@
<classpathentry kind="var" path="IVY_CACHE/javax.activation/activation/activation-1.1.jar" sourcepath="/IVY_CACHE/javax.activation/activation/activation-sources-1.1.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.jms/jms/jms-1.1.jar" sourcepath="/IVY_CACHE/javax.jms/jms/jms-sources-1.1.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.mail/mail/mail-1.4.jar" sourcepath="/IVY_CACHE/javax.mail/mail/mail-sources-1.4.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.servlet/servlet-api/servlet-api-2.4.jar" sourcepath="/IVY_CACHE/javax.servlet/servlet-api/servlet-api-sources-2.4.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.aopalliance/aopalliance/aopalliance-1.0.jar" sourcepath="IVY_CACHE/org.aopalliance/aopalliance/aopalliance-sources-1.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.apache/commons-logging/commons-logging-1.1.jar" sourcepath="/IVY_CACHE/org.apache/commons-logging/commons-logging-sources-1.1.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.apache/commons-pool/commons-pool-1.3.jar"/>
@@ -18,6 +19,7 @@
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-jms/spring-jms-2.5.2.jar" sourcepath="/IVY_CACHE/org.springframework/spring-jms/spring-jms-sources-2.5.2.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-test/spring-test-2.5.2.jar" sourcepath="/IVY_CACHE/org.springframework/spring-test/spring-test-sources-2.5.2.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-tx/spring-tx-2.5.2.jar" sourcepath="/IVY_CACHE/org.springframework/spring-tx/spring-tx-sources-2.5.2.jar"/>
<classpathentry kind="var" path="IVY_CACHE/org.springframework/spring-web/spring-web-2.5.2.jar" sourcepath="/IVY_CACHE/org.springframework/spring-web/spring-web-sources-2.5.2.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/spring-integration-core"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -23,6 +23,7 @@
<dependency org="javax.activation" name="activation" rev="1.1" conf="compile->default"/>
<dependency org="javax.jms" name="jms" rev="1.1" conf="compile->default"/>
<dependency org="javax.mail" name="mail" rev="1.4" conf="compile->default"/>
<dependency org="javax.servlet" name="servlet-api" rev="2.4" conf="compile->default"/>
<dependency org="org.junit" name="junit" rev="4.4" conf="test->default"/>
<dependency org="org.springframework.integration" name="spring-integration-core" rev="latest.integration" conf="compile"/>
<dependency org="org.springframework" name="spring-context" rev="2.5.2" conf="compile->default"/>
@@ -30,6 +31,7 @@
<dependency org="org.springframework" name="spring-jms" rev="2.5.2" conf="compile->default"/>
<dependency org="org.springframework" name="spring-test" rev="2.5.2" conf="test->default"/>
<dependency org="org.springframework" name="spring-tx" rev="2.5.2" conf="compile->default"/>
<dependency org="org.springframework" name="spring-web" rev="2.5.2" conf="compile->default"/>
</dependencies>
</ivy-module>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* 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.
@@ -75,6 +75,10 @@ public abstract class AbstractMessageHandlingSourceAdapter implements SourceAdap
}
public final void afterPropertiesSet() throws Exception {
if (this.channel == null) {
throw new MessagingConfigurationException("The 'channel' property of '" +
this.getClass().getName() + "' must not be null.");
}
synchronized (this.lifecycleMonitor) {
if (this.initialized) {
return;

View File

@@ -0,0 +1,65 @@
/*
* 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.adapter.httpinvoker;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.integration.adapter.AbstractMessageHandlingSourceAdapter;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;
import org.springframework.util.Assert;
import org.springframework.web.HttpRequestHandler;
/**
* A source channel adapter for HttpInvoker-based remoting.
*
* @author Mark Fisher
*/
public class HttpInvokerSourceAdapter extends AbstractMessageHandlingSourceAdapter implements HttpRequestHandler {
private volatile HttpInvokerServiceExporter exporter;
public HttpInvokerSourceAdapter(MessageChannel channel) {
Assert.notNull(channel, "'channel' must not be null");
this.setChannel(channel);
}
public void initialize() {
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 MessageHandlingException("adapter has not been initialized");
}
this.exporter.handleRequest(request, response);
}
}

View File

@@ -0,0 +1,108 @@
/*
* 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.adapter.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.SimpleChannel;
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 HttpInvokerSourceAdapterTests {
@Test
public void testRequestOnly() throws Exception {
MessageChannel channel = new SimpleChannel();
HttpInvokerSourceAdapter adapter = new HttpInvokerSourceAdapter(channel);
adapter.setExpectReply(false);
adapter.afterPropertiesSet();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(createRequestContent(new StringMessage("test")));
adapter.handleRequest(request, response);
Message<?> message = channel.receive(500);
assertNotNull(message);
assertEquals("test", message.getPayload());
}
@Test
public void testRequestExpectingReply() throws Exception {
final MessageChannel channel = new SimpleChannel();
Executors.newSingleThreadExecutor().execute(new Runnable() {
public void run() {
Message<?> message = channel.receive();
MessageChannel replyChannel = (MessageChannel) message.getHeader().getReturnAddress();
replyChannel.send(new StringMessage(message.getPayload().toString().toUpperCase()));
}
});
HttpInvokerSourceAdapter adapter = new HttpInvokerSourceAdapter(channel);
adapter.setExpectReply(true);
adapter.afterPropertiesSet();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(createRequestContent(new StringMessage("test")));
adapter.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;
}
}