hgomez 2005/04/15 02:00:01
Modified: src/java/org/apache/xmlrpc CommonsXmlRpcTransport.java Log: Add support for Gzip compression (reply AND/OR request) Add basic authentification support Revision Changes Path 1.4 +89 -4 ws-xmlrpc/src/java/org/apache/xmlrpc/CommonsXmlRpcTransport.java Index: CommonsXmlRpcTransport.java =================================================================== RCS file: /home/cvs/ws-xmlrpc/src/java/org/apache/xmlrpc/CommonsXmlRpcTransport.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- CommonsXmlRpcTransport.java 30 Mar 2005 11:19:18 -0000 1.3 +++ CommonsXmlRpcTransport.java 15 Apr 2005 09:00:01 -0000 1.4 @@ -56,13 +56,19 @@ */ import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.IOException; import java.net.URL; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HostConfiguration; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.URI; +import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.methods.PostMethod; /** @@ -105,32 +111,111 @@ private HttpClient client; private final Header userAgentHeader = new Header("User-Agent", XmlRpc.version); private boolean http11 = false; // defaults to HTTP 1.0 + private boolean gzip = false; + private boolean rgzip = false; + private Credentials creds; public InputStream sendXmlRpc(byte[] request) throws IOException, XmlRpcClientException { method = new PostMethod(url.toString()); method.setHttp11(http11); method.setRequestHeader(new Header("Content-Type", "text/xml")); + + if (rgzip) + method.setRequestHeader(new Header("Content-Encoding", "gzip")); + + if (gzip) + method.setRequestHeader(new Header("Accept-Encoding", "gzip")); + method.setRequestHeader(userAgentHeader); - // TODO: authentication not implemented yet - method.setRequestBody(new ByteArrayInputStream(request)); + + if (rgzip) + { + ByteArrayOutputStream lBo = new ByteArrayOutputStream(); + GZIPOutputStream lGzo = new GZIPOutputStream(lBo); + lGzo.write(request); + lGzo.finish(); + lGzo.close(); + byte[] lArray = lBo.toByteArray(); + method.setRequestBody(new ByteArrayInputStream(lArray)); + method.setRequestContentLength(-1); + } + else + method.setRequestBody(new ByteArrayInputStream(request)); + URI hostURI = new URI(url.toString()); HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setHost(hostURI); client.executeMethod(hostConfig, method); - return method.getResponseBodyAsStream(); + + boolean lgzipo = false; + + Header lHeader = method.getResponseHeader( "Content-Encoding" ); + if ( lHeader != null ) { + String lValue = lHeader.getValue(); + if ( lValue != null ) + lgzipo = (lValue.indexOf( "gzip" ) >= 0); + } + + if (lgzipo) + return( new GZIPInputStream( method.getResponseBodyAsStream() ) ); + else + return method.getResponseBodyAsStream(); } + /** + * Make use of HTTP 1.1 + * + * @param http11 HTTP 1.1 will be used if http11 is true + */ public void setHttp11(boolean http11) { this.http11 = http11; } + /** + * Transport make use of the 'Accept-Encoding: gzip', so compliant HTTP servers + * could return HTTP reply compressed with gzip + * + * @param gzip Gzip compression will be used if gzip is true + */ + public void setGzip(boolean gzip) { + this.gzip = gzip; + } + + /** + * Transport make use of the 'Content-Encoding: gzip' and send HTTP request + * compressed with gzip : works only with some compliant HTTP servers like Apache 2.x + * using SetInputFilter DEFLATE. + * + * @param gzip Compress request with gzip if gzip is true + */ + public void setRGzip(boolean gzip) { + this.rgzip = gzip; + } + + /** + * Set the UserAgent for this client + * + * @param userAgent + */ public void setUserAgent(String userAgent) { userAgentHeader.setValue(userAgent); } + /** + * Sets Authentication for this client, very basic for now user/password + * + * @param user + * @param password + */ + public void setBasicAuthentication(String user, String password) + { + creds = new UsernamePasswordCredentials(user, password); + client.getState().setCredentials(null, null, creds); + } + public void endClientRequest() throws XmlRpcClientException {