1 16 17 package org.springframework.remoting.httpinvoker; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.util.zip.GZIPInputStream ; 23 24 import org.apache.commons.httpclient.Header; 25 import org.apache.commons.httpclient.HttpClient; 26 import org.apache.commons.httpclient.HttpException; 27 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 28 import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; 29 import org.apache.commons.httpclient.methods.PostMethod; 30 31 import org.springframework.remoting.support.RemoteInvocationResult; 32 33 46 public class CommonsHttpInvokerRequestExecutor extends AbstractHttpInvokerRequestExecutor { 47 48 private HttpClient httpClient; 49 50 51 57 public CommonsHttpInvokerRequestExecutor() { 58 this.httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); 59 } 60 61 66 public CommonsHttpInvokerRequestExecutor(HttpClient httpClient) { 67 this.httpClient = httpClient; 68 } 69 70 73 public void setHttpClient(HttpClient httpClient) { 74 this.httpClient = httpClient; 75 } 76 77 80 public HttpClient getHttpClient() { 81 return httpClient; 82 } 83 84 85 95 protected RemoteInvocationResult doExecuteRequest( 96 HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) 97 throws IOException , ClassNotFoundException { 98 99 PostMethod postMethod = createPostMethod(config); 100 try { 101 setRequestBody(config, postMethod, baos); 102 executePostMethod(config, getHttpClient(), postMethod); 103 validateResponse(config, postMethod); 104 InputStream responseBody = getResponseBody(config, postMethod); 105 return readRemoteInvocationResult(responseBody, config.getCodebaseUrl()); 106 } 107 finally { 108 postMethod.releaseConnection(); 110 } 111 } 112 113 122 protected PostMethod createPostMethod(HttpInvokerClientConfiguration config) throws IOException { 123 PostMethod postMethod = new PostMethod(config.getServiceUrl()); 124 if (isAcceptGzipEncoding()) { 125 postMethod.addRequestHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP); 126 } 127 return postMethod; 128 } 129 130 145 protected void setRequestBody( 146 HttpInvokerClientConfiguration config, PostMethod postMethod, ByteArrayOutputStream baos) 147 throws IOException { 148 149 postMethod.setRequestEntity(new ByteArrayRequestEntity(baos.toByteArray(), getContentType())); 150 } 151 152 160 protected void executePostMethod( 161 HttpInvokerClientConfiguration config, HttpClient httpClient, PostMethod postMethod) 162 throws IOException { 163 164 httpClient.executeMethod(postMethod); 165 } 166 167 178 protected void validateResponse(HttpInvokerClientConfiguration config, PostMethod postMethod) 179 throws IOException { 180 181 if (postMethod.getStatusCode() >= 300) { 182 throw new HttpException( 183 "Did not receive successful HTTP response: status code = " + postMethod.getStatusCode() + 184 ", status message = [" + postMethod.getStatusText() + "]"); 185 } 186 } 187 188 203 protected InputStream getResponseBody(HttpInvokerClientConfiguration config, PostMethod postMethod) 204 throws IOException { 205 206 if (isGzipResponse(postMethod)) { 207 return new GZIPInputStream (postMethod.getResponseBodyAsStream()); 208 } 209 else { 210 return postMethod.getResponseBodyAsStream(); 211 } 212 } 213 214 220 protected boolean isGzipResponse(PostMethod postMethod) { 221 Header encodingHeader = postMethod.getResponseHeader(HTTP_HEADER_CONTENT_ENCODING); 222 if (encodingHeader == null || encodingHeader.getValue() == null) { 223 return false; 224 } 225 return (encodingHeader.getValue().toLowerCase().indexOf(ENCODING_GZIP) != -1); 226 } 227 228 } 229 | Popular Tags |