Coverage Report - org.apache.xmlrpc.client.XmlRpcHttpTransport
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlRpcHttpTransport
83% 
100% 
1,6
 
 1  
 package org.apache.xmlrpc.client;
 2  
 
 3  
 import java.io.InputStream;
 4  
 import java.io.UnsupportedEncodingException;
 5  
 
 6  
 import org.apache.xmlrpc.XmlRpcException;
 7  
 import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
 8  
 import org.apache.xmlrpc.util.HttpUtil;
 9  
 
 10  
 
 11  
 /** Abstract base implementation of an HTTP transport. Base class for the
 12  
  * concrete implementations, like {@link org.apache.xmlrpc.client.XmlRpcSunHttpTransport},
 13  
  * or {@link org.apache.xmlrpc.client.XmlRpcCommonsTransport}.
 14  
  */
 15  
 public abstract class XmlRpcHttpTransport extends XmlRpcStreamTransport {
 16  
         /** The user agent string.
 17  
          */
 18  
         public static final String USER_AGENT = "Apache XML RPC 3.0";
 19  
 
 20  
         protected XmlRpcHttpTransport(XmlRpcClient pClient) {
 21  412
                 super(pClient);
 22  412
         }
 23  
 
 24  
         protected abstract void setRequestHeader(Object pConnection, String pHeader, String pValue);
 25  
 
 26  
         protected void setCredentials(XmlRpcHttpClientConfig pConfig, Object pConnection)
 27  
                         throws XmlRpcClientException {
 28  
                 String auth;
 29  
                 try {
 30  576
                         auth = HttpUtil.encodeBasicAuthentication(pConfig.getBasicUserName(),
 31  288
                                                                                                           pConfig.getBasicPassword(),
 32  288
                                                                                                           pConfig.getBasicEncoding());
 33  0
                 } catch (UnsupportedEncodingException e) {
 34  0
                         throw new XmlRpcClientException("Unsupported encoding: " + pConfig.getBasicEncoding(), e);
 35  
                 }
 36  288
                 if (auth != null) {
 37  0
                         setRequestHeader(pConnection, "Authorization", "Basic " + auth);
 38  
                 }
 39  288
         }
 40  
 
 41  
         protected void setContentLength(Object pConnection, int pLength) {
 42  138
                 setRequestHeader(pConnection, "Content-Length", Integer.toString(pLength));
 43  138
         }
 44  
 
 45  
         protected InputStream getInputStream(XmlRpcStreamRequestConfig pConfig, Object pConnection, byte[] pContent) throws XmlRpcException {
 46  245
                 if (pContent != null) {
 47  173
                         setContentLength(pConnection, pContent.length);
 48  
                 }
 49  245
                 return super.getInputStream(pConfig, pConnection, pContent);
 50  
         }
 51  
 
 52  
         protected void setCompressionHeaders(XmlRpcHttpClientConfig pConfig, Object pConnection) {
 53  336
                 if (pConfig.isGzipCompressing()) {
 54  0
                         setRequestHeader(pConnection, "Content-Encoding", "gzip");
 55  
                 }
 56  336
                 if (pConfig.isGzipRequesting()) {
 57  0
                         setRequestHeader(pConnection, "Accept-Encoding", "gzip");
 58  
                 }
 59  336
         }
 60  
 
 61  412
         protected String getUserAgent() { return USER_AGENT; }
 62  
 
 63  
         protected void initConnection(XmlRpcStreamRequestConfig pConfig, Object pConnection) throws XmlRpcClientException {
 64  336
                 super.initConnection(pConfig, pConnection);
 65  336
                 XmlRpcHttpClientConfig config = (XmlRpcHttpClientConfig) pConfig;
 66  336
                 setRequestHeader(pConnection, "Content-Type", "text/xml");
 67  336
                 setRequestHeader(pConnection, "User-Agent", getUserAgent());
 68  336
                 setCredentials(config, pConnection);
 69  336
                 setCompressionHeaders(config, pConnection);
 70  336
         }
 71  
 
 72  
         protected abstract boolean isResponseGzipCompressed(XmlRpcStreamRequestConfig pConfig, Object pConnection);
 73  
 
 74  
         protected boolean isUsingByteArrayOutput(XmlRpcStreamRequestConfig pConfig) {
 75  504
                 return !pConfig.isEnabledForExtensions()
 76  144
                         || !((XmlRpcHttpClientConfig) pConfig).isContentLengthOptional();
 77  
         }
 78  
 }