1 16 17 18 package org.apache.xmlrpc; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.zip.GZIPInputStream ; 26 import java.util.zip.GZIPOutputStream ; 27 28 import org.apache.commons.httpclient.Credentials; 29 import org.apache.commons.httpclient.Header; 30 import org.apache.commons.httpclient.HostConfiguration; 31 import org.apache.commons.httpclient.HttpClient; 32 import org.apache.commons.httpclient.URI; 33 import org.apache.commons.httpclient.UsernamePasswordCredentials; 34 import org.apache.commons.httpclient.methods.PostMethod; 35 36 47 public class CommonsXmlRpcTransport implements XmlRpcTransport 48 { 49 50 protected PostMethod method; 51 52 53 public CommonsXmlRpcTransport(URL url, HttpClient client) 54 { 55 this.url = url; 56 if (client == null) 57 { 58 HttpClient newClient = new HttpClient(); 59 this.client = newClient; 60 } 61 else 62 { 63 this.client = client; 64 } 65 } 66 67 public CommonsXmlRpcTransport(URL url) 68 { 69 this(url, null); 70 } 71 72 private URL url; 73 private HttpClient client; 74 private final Header userAgentHeader = new Header("User-Agent", XmlRpc.version); 75 private boolean http11 = false; private boolean gzip = false; 77 private boolean rgzip = false; 78 private Credentials creds; 79 80 public InputStream sendXmlRpc(byte[] request) throws IOException , XmlRpcClientException 81 { 82 method = new PostMethod(url.toString()); 83 method.setHttp11(http11); 84 method.setRequestHeader(new Header("Content-Type", "text/xml")); 85 86 if (rgzip) 87 method.setRequestHeader(new Header("Content-Encoding", "gzip")); 88 89 if (gzip) 90 method.setRequestHeader(new Header("Accept-Encoding", "gzip")); 91 92 method.setRequestHeader(userAgentHeader); 93 94 if (rgzip) 95 { 96 ByteArrayOutputStream lBo = new ByteArrayOutputStream (); 97 GZIPOutputStream lGzo = new GZIPOutputStream (lBo); 98 lGzo.write(request); 99 lGzo.finish(); 100 lGzo.close(); 101 byte[] lArray = lBo.toByteArray(); 102 method.setRequestBody(new ByteArrayInputStream (lArray)); 103 method.setRequestContentLength(-1); 104 } 105 else 106 method.setRequestBody(new ByteArrayInputStream (request)); 107 108 URI hostURI = new URI(url.toString()); 109 HostConfiguration hostConfig = new HostConfiguration(); 110 hostConfig.setHost(hostURI); 111 client.executeMethod(hostConfig, method); 112 113 boolean lgzipo = false; 114 115 Header lHeader = method.getResponseHeader( "Content-Encoding" ); 116 if ( lHeader != null ) { 117 String lValue = lHeader.getValue(); 118 if ( lValue != null ) 119 lgzipo = (lValue.indexOf( "gzip" ) >= 0); 120 } 121 122 if (lgzipo) 123 return( new GZIPInputStream ( method.getResponseBodyAsStream() ) ); 124 else 125 return method.getResponseBodyAsStream(); 126 } 127 128 133 public void setHttp11(boolean http11) 134 { 135 this.http11 = http11; 136 } 137 138 144 public void setGzip(boolean gzip) { 145 this.gzip = gzip; 146 } 147 148 155 public void setRGzip(boolean gzip) { 156 this.rgzip = gzip; 157 } 158 159 164 public void setUserAgent(String userAgent) 165 { 166 userAgentHeader.setValue(userAgent); 167 } 168 169 175 public void setBasicAuthentication(String user, String password) 176 { 177 creds = new UsernamePasswordCredentials(user, password); 178 client.getState().setCredentials(null, null, creds); 179 } 180 181 186 public void endClientRequest() 187 throws XmlRpcClientException 188 { 189 method.releaseConnection(); 190 } 191 } 192 | Popular Tags |