This commit is contained in:
Arjen Poutsma
2008-02-17 02:35:20 +00:00
parent c73d3df3e1
commit a17d6fc6ce
16 changed files with 219 additions and 357 deletions

View File

@@ -0,0 +1,164 @@
/*
* Copyright 2007 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.ws.transport.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import com.sun.net.httpserver.HttpExchange;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.transport.AbstractReceiverConnection;
import org.springframework.ws.transport.EndpointAwareWebServiceConnection;
import org.springframework.ws.transport.FaultAwareWebServiceConnection;
import org.springframework.ws.transport.WebServiceConnection;
/**
* Implementation of {@link WebServiceConnection} that is based on the Java 6 HttpServer {@link HttpExchange}.
*
* @author Arjen Poutsma
* @since 1.5.0
*/
public class HttpExchangeConnection extends AbstractReceiverConnection
implements EndpointAwareWebServiceConnection, FaultAwareWebServiceConnection {
private final HttpExchange httpExchange;
private ByteArrayOutputStream responseBuffer;
private int responseStatusCode = HttpTransportConstants.STATUS_ACCEPTED;
private boolean chunkedEncoding;
/** Constructs a new exchange connection with the given <code>HttpExchange</code>. */
protected HttpExchangeConnection(HttpExchange httpExchange) {
Assert.notNull(httpExchange, "'httpExchange' must not be null");
this.httpExchange = httpExchange;
}
/** Returns the <code>HttpExchange</code> for this connection. */
public HttpExchange getHttpExchange() {
return httpExchange;
}
public URI getUri() throws URISyntaxException {
return httpExchange.getRequestURI();
}
void setChunkedEncoding(boolean chunkedEncoding) {
this.chunkedEncoding = chunkedEncoding;
}
public void endpointNotFound() {
responseStatusCode = HttpTransportConstants.STATUS_NOT_FOUND;
}
/*
* Errors
*/
public boolean hasError() throws IOException {
return false;
}
public String getErrorMessage() throws IOException {
return null;
}
/*
* Receiving request
*/
protected Iterator getRequestHeaderNames() throws IOException {
return httpExchange.getRequestHeaders().keySet().iterator();
}
protected Iterator getRequestHeaders(String name) throws IOException {
List headers = httpExchange.getRequestHeaders().get(name);
return headers != null ? headers.iterator() : Collections.EMPTY_LIST.iterator();
}
protected InputStream getRequestInputStream() throws IOException {
return httpExchange.getRequestBody();
}
/*
* Sending response
*/
protected void addResponseHeader(String name, String value) throws IOException {
httpExchange.getResponseHeaders().add(name, value);
}
protected OutputStream getResponseOutputStream() throws IOException {
if (chunkedEncoding) {
httpExchange.sendResponseHeaders(responseStatusCode, 0);
return httpExchange.getResponseBody();
}
else {
if (responseBuffer == null) {
responseBuffer = new ByteArrayOutputStream();
}
return responseBuffer;
}
}
protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
if (!chunkedEncoding) {
byte[] buf = responseBuffer.toByteArray();
httpExchange.sendResponseHeaders(responseStatusCode, buf.length);
OutputStream responseBody = httpExchange.getResponseBody();
FileCopyUtils.copy(buf, responseBody);
}
responseBuffer = null;
}
public void onClose() throws IOException {
if (responseStatusCode == HttpTransportConstants.STATUS_ACCEPTED ||
responseStatusCode == HttpTransportConstants.STATUS_NOT_FOUND) {
httpExchange.sendResponseHeaders(responseStatusCode, -1);
}
httpExchange.close();
}
/*
* Faults
*/
public boolean hasFault() throws IOException {
return responseStatusCode == HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR;
}
public void setFault(boolean fault) throws IOException {
if (fault) {
responseStatusCode = HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR;
}
else {
responseStatusCode = HttpTransportConstants.STATUS_OK;
}
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2007 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.ws.transport.http;
import java.io.IOException;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.springframework.ws.transport.support.SimpleWebServiceMessageReceiverObjectSupport;
/**
* {@link HttpHandler} that can be used to handle incoming {@link HttpExchange} service requests. Designed for Sun's JRE
* 1.6 HTTP server.
* <p/>
* Requires a {@link org.springframework.ws.WebServiceMessageFactory} which is used to convert the incoming {@link
* HttpExchange} into a {@link org.springframework.ws.WebServiceMessage}, and passes that to the {@link
* org.springframework.ws.transport.WebServiceMessageReceiver} {@link #setMessageReceiver(org.springframework.ws.transport.WebServiceMessageReceiver)
* registered}.
*
* @author Arjen Poutsma
* @see org.springframework.remoting.support.SimpleHttpServerFactoryBean
* @since 1.5.0
*/
public class WebServiceHttpHandler extends SimpleWebServiceMessageReceiverObjectSupport implements HttpHandler {
private boolean chunkedEncoding = false;
/** Enables chunked encoding on response bodies. Defaults to <code>false</code>. */
public void setChunkedEncoding(boolean chunkedEncoding) {
this.chunkedEncoding = chunkedEncoding;
}
public void handle(HttpExchange httpExchange) throws IOException {
if (HttpTransportConstants.METHOD_POST.equals(httpExchange.getRequestMethod())) {
HttpExchangeConnection connection = new HttpExchangeConnection(httpExchange);
connection.setChunkedEncoding(chunkedEncoding);
try {
handleConnection(connection);
}
catch (Exception ex) {
logger.error(ex);
}
}
else {
httpExchange.sendResponseHeaders(HttpTransportConstants.STATUS_METHOD_NOT_ALLOWED, -1);
httpExchange.close();
}
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2007 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.ws.transport.http;
import java.util.Locale;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.MessageEndpoint;
import org.springframework.ws.soap.SoapMessage;
public class FaultEndpoint implements MessageEndpoint {
public void invoke(MessageContext messageContext) throws Exception {
SoapMessage response = (SoapMessage) messageContext.getResponse();
response.getSoapBody().addServerOrReceiverFault("Something went wrong", Locale.ENGLISH);
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright 2007 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.ws.transport.http;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.MessageEndpoint;
/** @author Arjen Poutsma */
public class NoResponseEndpoint implements MessageEndpoint {
public void invoke(MessageContext messageContext) throws Exception {
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2007 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.ws.transport.http;
import org.springframework.ws.context.MessageContext;
import org.springframework.ws.server.endpoint.MessageEndpoint;
/** @author Arjen Poutsma */
public class ResponseEndpoint implements MessageEndpoint {
public void invoke(MessageContext messageContext) throws Exception {
messageContext.getResponse();
}
}

View File

@@ -0,0 +1,100 @@
/*
* Copyright ${YEAR} 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.ws.transport.http;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springframework.ws.transport.TransportConstants;
public class WebServiceHttpHandlerIntegrationTest extends AbstractDependencyInjectionSpringContextTests {
private HttpClient client;
protected String[] getConfigLocations() {
return new String[]{"classpath:org/springframework/ws/transport/http/httpserver-applicationContext.xml"};
}
protected void onSetUp() throws Exception {
client = new HttpClient();
}
public void testInvalidMethod() throws IOException {
GetMethod getMethod = new GetMethod("http://localhost:8888/service");
client.executeMethod(getMethod);
assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_METHOD_NOT_ALLOWED,
getMethod.getStatusCode());
assertEquals("Response retrieved", 0, getMethod.getResponseContentLength());
}
public void testNoResponse() throws IOException {
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
"http://springframework.org/spring-ws/NoResponse");
Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
client.executeMethod(postMethod);
assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_ACCEPTED, postMethod.getStatusCode());
assertEquals("Response retrieved", 0, postMethod.getResponseContentLength());
}
public void testResponse() throws IOException {
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
"http://springframework.org/spring-ws/Response");
Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
client.executeMethod(postMethod);
assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_OK, postMethod.getStatusCode());
assertTrue("No Response retrieved", postMethod.getResponseContentLength() > 0);
}
public void testNoEndpoint() throws IOException {
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
postMethod.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION,
"http://springframework.org/spring-ws/NoEndpoint");
Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
client.executeMethod(postMethod);
assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_NOT_FOUND, postMethod.getStatusCode());
assertEquals("Response retrieved", 0, postMethod.getResponseContentLength());
}
public void testFault() throws IOException {
PostMethod postMethod = new PostMethod("http://localhost:8888/service");
postMethod.addRequestHeader(HttpTransportConstants.HEADER_CONTENT_TYPE, "text/xml");
postMethod
.addRequestHeader(TransportConstants.HEADER_SOAP_ACTION, "http://springframework.org/spring-ws/Fault");
Resource soapRequest = new ClassPathResource("soapRequest.xml", WebServiceHttpHandlerIntegrationTest.class);
postMethod.setRequestEntity(new InputStreamRequestEntity(soapRequest.getInputStream()));
client.executeMethod(postMethod);
assertEquals("Invalid Response Code", HttpTransportConstants.STATUS_INTERNAL_SERVER_ERROR,
postMethod.getStatusCode());
assertTrue("No Response retrieved", postMethod.getResponseContentLength() > 0);
}
}

View File

@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="httpServer" class="org.springframework.remoting.support.SimpleHttpServerFactoryBean">
<property name="port" value="8888"/>
<property name="contexts">
<map>
<entry key="/service" value-ref="webServiceHandler"/>
</map>
</property>
</bean>
<bean id="webServiceHandler" class="org.springframework.ws.transport.http.WebServiceHttpHandler">
<property name="chunkedEncoding" value="true"/>
<property name="messageFactory" ref="messageFactory"/>
<property name="messageReceiver" ref="messageDispatcher"/>
</bean>
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/>
<bean id="messageDispatcher" class="org.springframework.ws.soap.server.SoapMessageDispatcher">
<property name="endpointMappings" ref="payloadMapping"/>
</bean>
<bean id="payloadMapping" class="org.springframework.ws.soap.server.endpoint.mapping.SoapActionEndpointMapping">
<property name="mappings">
<props>
<prop key="http://springframework.org/spring-ws/NoResponse">noResponseEndpoint</prop>
<prop key="http://springframework.org/spring-ws/Response">responseEndpoint</prop>
<prop key="http://springframework.org/spring-ws/Fault">faultEndpoint</prop>
</props>
</property>
</bean>
<bean id="noResponseEndpoint"
class="org.springframework.ws.transport.http.NoResponseEndpoint"/>
<bean id="responseEndpoint"
class="org.springframework.ws.transport.http.ResponseEndpoint"/>
<bean id="faultEndpoint"
class="org.springframework.ws.transport.http.FaultEndpoint"/>
</beans>

View File

@@ -0,0 +1,7 @@
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'>
<SOAP-ENV:Body>
<m:GetLastTradePrice xmlns:m='http://www.springframework.org/spring-ws'>
<symbol>DIS</symbol>
</m:GetLastTradePrice>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>