Coverage Report - org.apache.xmlrpc.client.XmlRpcCommonsTransport
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlRpcCommonsTransport
73% 
100% 
1,923
 
 1  
 /*
 2  
  * Copyright 1999,2005 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.apache.xmlrpc.client;
 17  
 
 18  
 import java.io.IOException;
 19  
 import java.io.InputStream;
 20  
 import java.io.OutputStream;
 21  
 
 22  
 import org.apache.commons.httpclient.Credentials;
 23  
 import org.apache.commons.httpclient.Header;
 24  
 import org.apache.commons.httpclient.HostConfiguration;
 25  
 import org.apache.commons.httpclient.HttpClient;
 26  
 import org.apache.commons.httpclient.HttpException;
 27  
 import org.apache.commons.httpclient.HttpVersion;
 28  
 import org.apache.commons.httpclient.URI;
 29  
 import org.apache.commons.httpclient.URIException;
 30  
 import org.apache.commons.httpclient.UsernamePasswordCredentials;
 31  
 import org.apache.commons.httpclient.auth.AuthScope;
 32  
 import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
 33  
 import org.apache.commons.httpclient.methods.PostMethod;
 34  
 import org.apache.xmlrpc.XmlRpcException;
 35  
 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
 36  
 import org.apache.xmlrpc.util.HttpUtil;
 37  
 
 38  
 
 39  
 /** An HTTP transport factory, which is based on the Jakarta Commons
 40  
  * HTTP Client.
 41  
  */
 42  
 public class XmlRpcCommonsTransport extends XmlRpcHttpTransport {
 43  
         private class CommonsConnection {
 44  48
                 final HttpClient client = new HttpClient();
 45  
                 final PostMethod method;
 46  48
                 CommonsConnection(XmlRpcHttpClientConfig pConfig) {
 47  48
                         method = new PostMethod(pConfig.getServerURL().toString());
 48  48
                         method.getParams().setVersion(HttpVersion.HTTP_1_1);
 49  48
                 }
 50  
         }
 51  
 
 52  22
         private String userAgent = super.getUserAgent() + " (Jakarta Commons httpclient Transport)";
 53  
 
 54  
         /** Creates a new instance.
 55  
          * @param pClient The client, which will be invoking the transport.
 56  
          */
 57  
         public XmlRpcCommonsTransport(XmlRpcClient pClient) {
 58  22
                 super(pClient);
 59  22
         }
 60  
 
 61  48
         protected String getUserAgent() { return userAgent; }
 62  
 
 63  
         protected void setRequestHeader(Object pConnection, String pHeader, String pValue) {
 64  96
                 PostMethod method = ((CommonsConnection) pConnection).method;
 65  96
                 method.setRequestHeader(new Header(pHeader, pValue));
 66  96
         }
 67  
 
 68  
         protected boolean isResponseGzipCompressed(XmlRpcStreamRequestConfig pConfig, Object pConnection) {
 69  35
                 Header h = ((CommonsConnection) pConnection).method.getResponseHeader( "Content-Encoding" );
 70  35
                 if (h == null) {
 71  35
                         return false;
 72  
                 } else {
 73  0
                         return HttpUtil.isUsingGzipEncoding(h.getValue());
 74  
                 }
 75  
         }
 76  
 
 77  
         protected Object newConnection(XmlRpcStreamRequestConfig pConfig) throws XmlRpcClientException {
 78  48
                 return new CommonsConnection((XmlRpcHttpClientConfig) pConfig);
 79  
         }
 80  
 
 81  
         protected void closeConnection(Object pConnection) throws XmlRpcClientException {
 82  48
                 ((CommonsConnection) pConnection).method.releaseConnection();
 83  48
         }
 84  
 
 85  
         protected OutputStream newOutputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) throws XmlRpcClientException {
 86  0
                 throw new IllegalStateException("Not implemented");
 87  
         }
 88  
 
 89  48
         protected boolean isUsingByteArrayOutput(XmlRpcStreamRequestConfig pConfig) { return true; }
 90  
 
 91  
         protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection) throws XmlRpcException {
 92  0
                 throw new IllegalStateException("Not implemented");
 93  
         }
 94  
 
 95  
         protected void setContentLength(Object pConnection, int pLength) {
 96  35
         }
 97  
 
 98  
         protected InputStream newInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection, byte[] pContents)
 99  
                         throws XmlRpcException {
 100  35
                 XmlRpcHttpClientConfig config = (XmlRpcHttpClientConfig) pConfig;
 101  35
                 CommonsConnection conn = (CommonsConnection) pConnection;
 102  35
                 PostMethod method = conn.method;
 103  35
                 method.setRequestEntity(new ByteArrayRequestEntity(pContents, "text/xml"));
 104  
                 HostConfiguration hostConfig;
 105  
                 try {
 106  35
                         URI hostURI = new URI(config.getServerURL().toString(), false);
 107  35
                         hostConfig = new HostConfiguration();
 108  35
                         hostConfig.setHost(hostURI);
 109  0
                 } catch (URIException e) {
 110  0
                         throw new XmlRpcClientException("Failed to parse URL: " + config.getServerURL().toString(), e);
 111  
                 }
 112  
                 try {
 113  35
                         conn.client.executeMethod(hostConfig, method);
 114  35
                         return method.getResponseBodyAsStream();
 115  0
                 } catch (HttpException e) {
 116  0
                         throw new XmlRpcClientException("Error in HTTP transport: " + e.getMessage(), e);
 117  0
                 } catch (IOException e) {
 118  0
                         throw new XmlRpcClientException("I/O error in server communication: " + e.getMessage(), e);
 119  
                 }
 120  
         }
 121  
 
 122  
         protected void setCredentials(XmlRpcHttpClientConfig pConfig, Object pConnection) throws XmlRpcClientException {
 123  48
                 String userName = pConfig.getBasicUserName();
 124  48
                 if (userName != null) {
 125  0
                         Credentials creds = new UsernamePasswordCredentials(userName, pConfig.getBasicPassword());
 126  0
                         AuthScope scope = new AuthScope(null, AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME);
 127  0
                         ((CommonsConnection) pConnection).client.getState().setCredentials(scope, creds);
 128  
                 }
 129  48
         }
 130  
 }