Author: sagara Date: Wed Apr 11 14:23:20 2012 New Revision: 1324778 URL: http://svn.apache.org/viewvc?rev=1324778&view=rev Log: * Applied patch for AXIS2-5281 with few changes. * Added few test cases for AXIS2-5281.
Modified: axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/HTTPSender.java axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java Modified: axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/HTTPSender.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/HTTPSender.java?rev=1324778&r1=1324777&r2=1324778&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/HTTPSender.java (original) +++ axis/axis2/java/core/trunk/modules/transport/http/src/org/apache/axis2/transport/http/HTTPSender.java Wed Apr 11 14:23:20 2012 @@ -264,18 +264,19 @@ public class HTTPSender extends Abstract private void handleResponse(MessageContext msgContext, HttpMethodBase method) throws IOException { int statusCode = method.getStatusCode(); + HTTPStatusCodeFamily family = getHTTPStatusCodeFamily(statusCode); log.trace("Handling response - " + statusCode); - if (statusCode == HttpStatus.SC_OK) { - // Save the HttpMethod so that we can release the connection when cleaning up - msgContext.setProperty(HTTPConstants.HTTP_METHOD, method); - processResponse(method, msgContext); - } else if (statusCode == HttpStatus.SC_ACCEPTED) { + if (statusCode == HttpStatus.SC_ACCEPTED) { /* When an HTTP 202 Accepted code has been received, this will be the case of an execution * of an in-only operation. In such a scenario, the HTTP response headers should be returned, * i.e. session cookies. */ obtainHTTPHeaderInformation(method, msgContext); // Since we don't expect any content with a 202 response, we must release the connection method.releaseConnection(); + } else if (HTTPStatusCodeFamily.SUCCESSFUL.equals(family)) { + // Save the HttpMethod so that we can release the connection when cleaning up + msgContext.setProperty(HTTPConstants.HTTP_METHOD, method); + processResponse(method, msgContext); } else if (statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR || statusCode == HttpStatus.SC_BAD_REQUEST) { // Save the HttpMethod so that we can release the connection when cleaning up @@ -312,4 +313,25 @@ public class HTTPSender extends Abstract method.getStatusText())); } } + + /** + * Used to determine the family of HTTP status codes to which the given code belongs. + * + * @param statusCode - The HTTP status code + */ + private HTTPStatusCodeFamily getHTTPStatusCodeFamily(int statusCode) { + switch(statusCode/100) { + case 1: return HTTPStatusCodeFamily.INFORMATIONAL; + case 2: return HTTPStatusCodeFamily.SUCCESSFUL; + case 3: return HTTPStatusCodeFamily.REDIRECTION; + case 4: return HTTPStatusCodeFamily.CLIENT_ERROR; + case 5: return HTTPStatusCodeFamily.SERVER_ERROR; + default: return HTTPStatusCodeFamily.OTHER; + } + } + + /** + * The set of HTTP status code families. + */ + private enum HTTPStatusCodeFamily {INFORMATIONAL, SUCCESSFUL, REDIRECTION, CLIENT_ERROR, SERVER_ERROR, OTHER} } Modified: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java?rev=1324778&r1=1324777&r2=1324778&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java (original) +++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/HTTPSenderTest.java Wed Apr 11 14:23:20 2012 @@ -31,6 +31,7 @@ import org.apache.axis2.context.Configur import org.apache.axis2.context.MessageContext; import org.apache.axis2.context.OperationContext; import org.apache.axis2.transport.http.mock.server.AbstractHTTPServerTest; +import org.apache.axis2.transport.http.mock.server.BasicHttpServer; /** * The Class HTTPSenderTest. @@ -241,6 +242,46 @@ public class HTTPSenderTest extends Abst } + public void testHandleResponseHTTPStatusCode200() throws Exception { + httpSender = new HTTPSender(); + int port = getBasicHttpServer().getPort(); + getBasicHttpServer().setResponseTemplate(BasicHttpServer.RESPONSE_HTTP_200); + sendViaHTTP(Constants.Configuration.HTTP_METHOD_POST, "urn:postService", + "http://localhost:" + port + "/postService", true); + } + + public void testHandleResponseHTTPStatusCode201() throws Exception { + httpSender = new HTTPSender(); + int port = getBasicHttpServer().getPort(); + getBasicHttpServer().setResponseTemplate(BasicHttpServer.RESPONSE_HTTP_201); + sendViaHTTP(Constants.Configuration.HTTP_METHOD_POST, "urn:postService", + "http://localhost:" + port + "/postService", true); + } + + public void testHandleResponseHTTPStatusCode202() throws Exception { + httpSender = new HTTPSender(); + int port = getBasicHttpServer().getPort(); + getBasicHttpServer().setResponseTemplate(BasicHttpServer.RESPONSE_HTTP_202); + sendViaHTTP(Constants.Configuration.HTTP_METHOD_POST, "urn:postService", + "http://localhost:" + port + "/postService", true); + } + + public void testHandleResponseHTTPStatusCode400() throws Exception { + httpSender = new HTTPSender(); + int port = getBasicHttpServer().getPort(); + getBasicHttpServer().setResponseTemplate(BasicHttpServer.RESPONSE_HTTP_400); + sendViaHTTP(Constants.Configuration.HTTP_METHOD_POST, "urn:postService", + "http://localhost:" + port + "/postService", true); + } + + public void testHandleResponseHTTPStatusCode500() throws Exception { + httpSender = new HTTPSender(); + int port = getBasicHttpServer().getPort(); + getBasicHttpServer().setResponseTemplate(BasicHttpServer.RESPONSE_HTTP_500); + sendViaHTTP(Constants.Configuration.HTTP_METHOD_POST, "urn:postService", + "http://localhost:" + port + "/postService", true); + } + } Modified: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java?rev=1324778&r1=1324777&r2=1324778&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java (original) +++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServer.java Wed Apr 11 14:23:20 2012 @@ -148,5 +148,11 @@ public interface BasicHttpServer { public static final String RESPONSE_HTTP_404 = "response.http.404"; public static final String RESPONSE_HTTP_OK_XML = "response.http.ok.xml"; public static final String RESPONSE_HTTP_OK_LOOP_BACK = "response.http.ok.loop.back"; + public static final String RESPONSE_HTTP_200 = "response.http.200"; + public static final String RESPONSE_HTTP_201 = "response.http.201"; + public static final String RESPONSE_HTTP_202 = "response.http.202"; + public static final String RESPONSE_HTTP_400 = "response.http.400"; + public static final String RESPONSE_HTTP_500 = "response.http.500"; + } Modified: axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java?rev=1324778&r1=1324777&r2=1324778&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java (original) +++ axis/axis2/java/core/trunk/modules/transport/http/test/org/apache/axis2/transport/http/mock/server/BasicHttpServerImpl.java Wed Apr 11 14:23:20 2012 @@ -238,7 +238,66 @@ public class BasicHttpServerImpl impleme } }); - } + + } else if (server.getResponseTemplate().equals(BasicHttpServer.RESPONSE_HTTP_200)) { + response.setStatusCode(HttpStatus.SC_OK); + body = new EntityTemplate(new ContentProducer() { + + public void writeTo(final OutputStream outstream) throws IOException { + OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); + writer.write("<Response> SC_ACCEPTED 202 <Response>"); + writer.flush(); + } + + }); + + } else if (server.getResponseTemplate().equals(BasicHttpServer.RESPONSE_HTTP_201)) { + response.setStatusCode(HttpStatus.SC_CREATED); + body = new EntityTemplate(new ContentProducer() { + + public void writeTo(final OutputStream outstream) throws IOException { + OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); + //writer.write("<Response> SC_ACCEPTED 202 <Response>"); + writer.flush(); + } + + }); + + } else if (server.getResponseTemplate().equals(BasicHttpServer.RESPONSE_HTTP_202)) { + response.setStatusCode(HttpStatus.SC_ACCEPTED); + body = new EntityTemplate(new ContentProducer() { + public void writeTo(final OutputStream outstream) throws IOException { + OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); + //writer.write("<Response> SC_ACCEPTED 202 <Response>"); + writer.flush(); + } + + }); + + } else if (server.getResponseTemplate().equals(BasicHttpServer.RESPONSE_HTTP_400)) { + response.setStatusCode(HttpStatus.SC_BAD_REQUEST); + body = new EntityTemplate(new ContentProducer() { + public void writeTo(final OutputStream outstream) throws IOException { + OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); + //writer.write("<Response> SC_ACCEPTED 202 <Response>"); + writer.flush(); + } + + }); + + } else if (server.getResponseTemplate().equals(BasicHttpServer.RESPONSE_HTTP_500)) { + response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR); + body = new EntityTemplate(new ContentProducer() { + public void writeTo(final OutputStream outstream) throws IOException { + OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); + writer.write(" Server Error"); + writer.flush(); + } + + }); + + } + // TODO - customize to send content type depend on expectations. body.setContentType("text/html; charset=UTF-8"); response.setEntity(body);