Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package org.apache.xmlrpc.client;
00020
00021 import java.io.ByteArrayOutputStream;
00022 import java.io.IOException;
00023 import java.io.InputStream;
00024 import java.io.OutputStream;
00025 import java.io.UnsupportedEncodingException;
00026 import java.lang.reflect.UndeclaredThrowableException;
00027 import java.net.URL;
00028 import java.util.Properties;
00029
00030 import org.apache.xmlrpc.XmlRpcException;
00031 import org.apache.xmlrpc.XmlRpcRequest;
00032 import org.apache.xmlrpc.util.HttpUtil;
00033 import org.xml.sax.SAXException;
00034
00035
00040 public abstract class XmlRpcHttpTransport extends XmlRpcStreamTransport {
00041 protected class ByteArrayReqWriter implements ReqWriter {
00042 private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
00043 ByteArrayReqWriter(XmlRpcRequest pRequest)
00044 throws XmlRpcException, IOException, SAXException {
00045 new ReqWriterImpl(pRequest).write(baos);
00046 }
00047
00048 protected int getContentLength() {
00049 return baos.size();
00050 }
00051
00052 public void write(OutputStream pStream) throws IOException {
00053 try {
00054 baos.writeTo(pStream);
00055 pStream.close();
00056 pStream = null;
00057 } finally {
00058 if (pStream != null) { try { pStream.close(); } catch (Throwable ignore) {} }
00059 }
00060 }
00061 }
00062
00065 public static final String USER_AGENT;
00066 static {
00067 final String p = "XmlRpcClient.properties";
00068 final URL url = XmlRpcHttpTransport.class.getResource(p);
00069 if (url == null) {
00070 throw new IllegalStateException("Failed to locate resource: " + p);
00071 }
00072 InputStream stream = null;
00073 try {
00074 stream = url.openStream();
00075 final Properties props = new Properties();
00076 props.load(stream);
00077 USER_AGENT = props.getProperty("user.agent");
00078 if (USER_AGENT == null || USER_AGENT.trim().length() == 0) {
00079 throw new IllegalStateException("The property user.agent is not set.");
00080 }
00081 stream.close();
00082 stream = null;
00083 } catch (IOException e) {
00084 throw new UndeclaredThrowableException(e, "Failed to load resource " + url + ": " + e.getMessage());
00085 } finally {
00086 if (stream != null) { try { stream.close(); } catch (Throwable t) { } }
00087 }
00088 }
00089
00090 private String userAgent;
00091
00092
00093 protected XmlRpcHttpTransport(XmlRpcClient pClient, String pUserAgent) {
00094 super(pClient);
00095 userAgent = pUserAgent;
00096 }
00097
00098 protected String getUserAgent() { return userAgent; }
00099
00100 protected abstract void setRequestHeader(String pHeader, String pValue);
00101
00102 protected void setCredentials(XmlRpcHttpClientConfig pConfig)
00103 throws XmlRpcClientException {
00104 String auth;
00105 try {
00106 auth = HttpUtil.encodeBasicAuthentication(pConfig.getBasicUserName(),
00107 pConfig.getBasicPassword(),
00108 pConfig.getBasicEncoding());
00109 } catch (UnsupportedEncodingException e) {
00110 throw new XmlRpcClientException("Unsupported encoding: " + pConfig.getBasicEncoding(), e);
00111 }
00112 if (auth != null) {
00113 setRequestHeader("Authorization", "Basic " + auth);
00114 }
00115 }
00116
00117 protected void setContentLength(int pLength) {
00118 setRequestHeader("Content-Length", Integer.toString(pLength));
00119 }
00120
00121 protected void setCompressionHeaders(XmlRpcHttpClientConfig pConfig) {
00122 if (pConfig.isGzipCompressing()) {
00123 setRequestHeader("Content-Encoding", "gzip");
00124 }
00125 if (pConfig.isGzipRequesting()) {
00126 setRequestHeader("Accept-Encoding", "gzip");
00127 }
00128 }
00129
00130 protected void initHttpHeaders(XmlRpcRequest pRequest) throws XmlRpcClientException {
00131 XmlRpcHttpClientConfig config = (XmlRpcHttpClientConfig) pRequest.getConfig();
00132 setRequestHeader("Content-Type", "text/xml");
00133 if(config.getUserAgent() != null)
00134 setRequestHeader("User-Agent", config.getUserAgent());
00135 else
00136 setRequestHeader("User-Agent", getUserAgent());
00137 setCredentials(config);
00138 setCompressionHeaders(config);
00139 }
00140
00141 public Object sendRequest(XmlRpcRequest pRequest) throws XmlRpcException {
00142 initHttpHeaders(pRequest);
00143 return super.sendRequest(pRequest);
00144 }
00145
00146 protected boolean isUsingByteArrayOutput(XmlRpcHttpClientConfig pConfig) {
00147 return !pConfig.isEnabledForExtensions()
00148 || !pConfig.isContentLengthOptional();
00149 }
00150
00151 protected ReqWriter newReqWriter(XmlRpcRequest pRequest)
00152 throws XmlRpcException, IOException, SAXException {
00153 final XmlRpcHttpClientConfig config = (XmlRpcHttpClientConfig) pRequest.getConfig();
00154 if (isUsingByteArrayOutput(config)) {
00155 ByteArrayReqWriter reqWriter = new ByteArrayReqWriter(pRequest);
00156 setContentLength(reqWriter.getContentLength());
00157 if (isCompressingRequest(config)) {
00158 return new GzipReqWriter(reqWriter);
00159 }
00160 return reqWriter;
00161 } else {
00162 return super.newReqWriter(pRequest);
00163 }
00164 }
00165 }