You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xmlrpc-auto@ws.apache.org by jo...@apache.org on 2006/12/14 22:37:58 UTC

svn commit: r487358 - in /webservices/xmlrpc/trunk: client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransport.java client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransportFactory.java src/site/apt/advanced.apt

Author: jochen
Date: Thu Dec 14 13:37:57 2006
New Revision: 487358

URL: http://svn.apache.org/viewvc?view=rev&rev=487358
Log:
Added support for cookies with XmlRpcCommonsTransport.

Modified:
    webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransport.java
    webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransportFactory.java
    webservices/xmlrpc/trunk/src/site/apt/advanced.apt

Modified: webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransport.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransport.java?view=diff&rev=487358&r1=487357&r2=487358
==============================================================================
--- webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransport.java (original)
+++ webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransport.java Thu Dec 14 13:37:57 2006
@@ -1,179 +1,184 @@
-/*
- * Copyright 1999,2005 The Apache Software Foundation.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.xmlrpc.client;
-
-import java.io.BufferedOutputStream;
-import java.io.FilterOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-
-import org.apache.commons.httpclient.Credentials;
-import org.apache.commons.httpclient.Header;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpException;
-import org.apache.commons.httpclient.HttpVersion;
-import org.apache.commons.httpclient.UsernamePasswordCredentials;
-import org.apache.commons.httpclient.auth.AuthScope;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.RequestEntity;
-import org.apache.commons.httpclient.params.HttpMethodParams;
-import org.apache.xmlrpc.XmlRpcException;
-import org.apache.xmlrpc.XmlRpcRequest;
-import org.apache.xmlrpc.common.XmlRpcStreamConfig;
-import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
-import org.apache.xmlrpc.util.HttpUtil;
-import org.apache.xmlrpc.util.XmlRpcIOException;
-import org.xml.sax.SAXException;
-
-
-/** An HTTP transport factory, which is based on the Jakarta Commons
- * HTTP Client.
- */
-public class XmlRpcCommonsTransport extends XmlRpcHttpTransport {
-	private final HttpClient client = newHttpClient();
-	private static final String userAgent = USER_AGENT + " (Jakarta Commons httpclient Transport)";
-	private PostMethod method;
-	private int contentLength = -1;
-	private XmlRpcHttpClientConfig config;      
-
-	/** Creates a new instance.
-	 * @param pClient The client, which will be invoking the transport.
-	 */
-	public XmlRpcCommonsTransport(XmlRpcClient pClient) {
-		super(pClient, userAgent);
-	}
-
-	protected void setContentLength(int pLength) {
-		contentLength = pLength;
-	}
-
-    protected HttpClient newHttpClient() {
-        return new HttpClient();
-    }
-
-    protected void initHttpHeaders(XmlRpcRequest pRequest) throws XmlRpcClientException {
-        config = (XmlRpcHttpClientConfig) pRequest.getConfig();
-        method = new PostMethod(config.getServerURL().toString());
-        super.initHttpHeaders(pRequest);
-        
-        if (config.getConnectionTimeout() != 0)
-            client.getHttpConnectionManager().getParams().setConnectionTimeout(config.getConnectionTimeout());
-        
-        if (config.getReplyTimeout() != 0)
-            client.getHttpConnectionManager().getParams().setSoTimeout(config.getReplyTimeout());
-        
-        method.getParams().setVersion(HttpVersion.HTTP_1_1);
-    }
-
-	protected void setRequestHeader(String pHeader, String pValue) {
-		method.setRequestHeader(new Header(pHeader, pValue));
-	}
-
-	protected boolean isResponseGzipCompressed() {
-		Header h = method.getResponseHeader( "Content-Encoding" );
-		if (h == null) {
-			return false;
-		} else {
-			return HttpUtil.isUsingGzipEncoding(h.getValue());
-		}
-	}
-
-	protected InputStream getInputStream() throws XmlRpcException {
-        try {
-            return method.getResponseBodyAsStream();
-		} catch (HttpException e) {
-			throw new XmlRpcClientException("Error in HTTP transport: " + e.getMessage(), e);
-		} catch (IOException e) {
-			throw new XmlRpcClientException("I/O error in server communication: " + e.getMessage(), e);
-		}
-	}
-
-	protected void setCredentials(XmlRpcHttpClientConfig pConfig) throws XmlRpcClientException {
-		String userName = pConfig.getBasicUserName();
-		if (userName != null) {
-            String enc = pConfig.getBasicEncoding();
-            if (enc == null) {
-                enc = XmlRpcStreamConfig.UTF8_ENCODING;
-            }
-            client.getParams().setParameter(HttpMethodParams.CREDENTIAL_CHARSET, enc);
-			Credentials creds = new UsernamePasswordCredentials(userName, pConfig.getBasicPassword());
-			AuthScope scope = new AuthScope(null, AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME);
-			client.getState().setCredentials(scope, creds);
-            client.getParams().setAuthenticationPreemptive(true);
-        }
-	}
-
-	protected void close() throws XmlRpcClientException {
-		method.releaseConnection();
-	}
-
-	protected boolean isResponseGzipCompressed(XmlRpcStreamRequestConfig pConfig) {
-		Header h = method.getResponseHeader( "Content-Encoding" );
-		if (h == null) {
-			return false;
-		} else {
-			return HttpUtil.isUsingGzipEncoding(h.getValue());
-		}
-	}
-
-	protected void writeRequest(final ReqWriter pWriter) throws XmlRpcException {
-		method.setRequestEntity(new RequestEntity(){
-			public boolean isRepeatable() { return contentLength != -1; }
-			public void writeRequest(OutputStream pOut) throws IOException {
-				try {
-                    /* Make sure, that the socket is not closed by replacing it with our
-                     * own BufferedOutputStream.
-                     */
-                    OutputStream ostream;
-                    if (isUsingByteArrayOutput(config)) {
-                        // No need to buffer the output.
-                        ostream = new FilterOutputStream(pOut){
-                            public void close() throws IOException {
-                                flush();
-                            }
-                        };
-                    } else {
-                        ostream = new BufferedOutputStream(pOut){
-                            public void close() throws IOException {
-                                flush();
-                            }
-                        };
-                    }
-					pWriter.write(ostream);
-				} catch (XmlRpcException e) {
-					throw new XmlRpcIOException(e);
-				} catch (SAXException e) {
-                    throw new XmlRpcIOException(e);
-                }
-			}
-			public long getContentLength() { return contentLength; }
-			public String getContentType() { return "text/xml"; }
-		});
-		try {
-			client.executeMethod(method);
-		} catch (XmlRpcIOException e) {
-			Throwable t = e.getLinkedException();
-			if (t instanceof XmlRpcException) {
-				throw (XmlRpcException) t;
-			} else {
-				throw new XmlRpcException("Unexpected exception: " + t.getMessage(), t);
-			}
-		} catch (IOException e) {
-			throw new XmlRpcException("I/O error while communicating with HTTP server: " + e.getMessage(), e);
-		}
-	}
-}
+/*
+ * Copyright 1999,2005 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.xmlrpc.client;
+
+import java.io.BufferedOutputStream;
+import java.io.FilterOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpVersion;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.RequestEntity;
+import org.apache.commons.httpclient.params.HttpMethodParams;
+import org.apache.xmlrpc.XmlRpcException;
+import org.apache.xmlrpc.XmlRpcRequest;
+import org.apache.xmlrpc.common.XmlRpcStreamConfig;
+import org.apache.xmlrpc.common.XmlRpcStreamRequestConfig;
+import org.apache.xmlrpc.util.HttpUtil;
+import org.apache.xmlrpc.util.XmlRpcIOException;
+import org.xml.sax.SAXException;
+
+
+/** An HTTP transport factory, which is based on the Jakarta Commons
+ * HTTP Client.
+ */
+public class XmlRpcCommonsTransport extends XmlRpcHttpTransport {
+    private final HttpClient client;
+	private static final String userAgent = USER_AGENT + " (Jakarta Commons httpclient Transport)";
+	private PostMethod method;
+	private int contentLength = -1;
+	private XmlRpcHttpClientConfig config;      
+
+	/** Creates a new instance.
+	 * @param pFactory The factory, which created this transport.
+	 */
+	public XmlRpcCommonsTransport(XmlRpcCommonsTransportFactory pFactory) {
+		super(pFactory.getClient(), userAgent);
+        HttpClient httpClient = pFactory.getHttpClient();
+        if (httpClient == null) {
+            httpClient = newHttpClient();
+        }
+        client = httpClient;
+     }
+
+	protected void setContentLength(int pLength) {
+		contentLength = pLength;
+	}
+
+    protected HttpClient newHttpClient() {
+        return new HttpClient();
+    }
+
+    protected void initHttpHeaders(XmlRpcRequest pRequest) throws XmlRpcClientException {
+        config = (XmlRpcHttpClientConfig) pRequest.getConfig();
+        method = new PostMethod(config.getServerURL().toString());
+        super.initHttpHeaders(pRequest);
+        
+        if (config.getConnectionTimeout() != 0)
+            client.getHttpConnectionManager().getParams().setConnectionTimeout(config.getConnectionTimeout());
+        
+        if (config.getReplyTimeout() != 0)
+            client.getHttpConnectionManager().getParams().setSoTimeout(config.getReplyTimeout());
+        
+        method.getParams().setVersion(HttpVersion.HTTP_1_1);
+    }
+
+	protected void setRequestHeader(String pHeader, String pValue) {
+		method.setRequestHeader(new Header(pHeader, pValue));
+	}
+
+	protected boolean isResponseGzipCompressed() {
+		Header h = method.getResponseHeader( "Content-Encoding" );
+		if (h == null) {
+			return false;
+		} else {
+			return HttpUtil.isUsingGzipEncoding(h.getValue());
+		}
+	}
+
+	protected InputStream getInputStream() throws XmlRpcException {
+        try {
+            return method.getResponseBodyAsStream();
+		} catch (HttpException e) {
+			throw new XmlRpcClientException("Error in HTTP transport: " + e.getMessage(), e);
+		} catch (IOException e) {
+			throw new XmlRpcClientException("I/O error in server communication: " + e.getMessage(), e);
+		}
+	}
+
+	protected void setCredentials(XmlRpcHttpClientConfig pConfig) throws XmlRpcClientException {
+		String userName = pConfig.getBasicUserName();
+		if (userName != null) {
+            String enc = pConfig.getBasicEncoding();
+            if (enc == null) {
+                enc = XmlRpcStreamConfig.UTF8_ENCODING;
+            }
+            client.getParams().setParameter(HttpMethodParams.CREDENTIAL_CHARSET, enc);
+			Credentials creds = new UsernamePasswordCredentials(userName, pConfig.getBasicPassword());
+			AuthScope scope = new AuthScope(null, AuthScope.ANY_PORT, null, AuthScope.ANY_SCHEME);
+			client.getState().setCredentials(scope, creds);
+            client.getParams().setAuthenticationPreemptive(true);
+        }
+	}
+
+	protected void close() throws XmlRpcClientException {
+		method.releaseConnection();
+	}
+
+	protected boolean isResponseGzipCompressed(XmlRpcStreamRequestConfig pConfig) {
+		Header h = method.getResponseHeader( "Content-Encoding" );
+		if (h == null) {
+			return false;
+		} else {
+			return HttpUtil.isUsingGzipEncoding(h.getValue());
+		}
+	}
+
+	protected void writeRequest(final ReqWriter pWriter) throws XmlRpcException {
+		method.setRequestEntity(new RequestEntity(){
+			public boolean isRepeatable() { return contentLength != -1; }
+			public void writeRequest(OutputStream pOut) throws IOException {
+				try {
+                    /* Make sure, that the socket is not closed by replacing it with our
+                     * own BufferedOutputStream.
+                     */
+                    OutputStream ostream;
+                    if (isUsingByteArrayOutput(config)) {
+                        // No need to buffer the output.
+                        ostream = new FilterOutputStream(pOut){
+                            public void close() throws IOException {
+                                flush();
+                            }
+                        };
+                    } else {
+                        ostream = new BufferedOutputStream(pOut){
+                            public void close() throws IOException {
+                                flush();
+                            }
+                        };
+                    }
+					pWriter.write(ostream);
+				} catch (XmlRpcException e) {
+					throw new XmlRpcIOException(e);
+				} catch (SAXException e) {
+                    throw new XmlRpcIOException(e);
+                }
+			}
+			public long getContentLength() { return contentLength; }
+			public String getContentType() { return "text/xml"; }
+		});
+		try {
+			client.executeMethod(method);
+		} catch (XmlRpcIOException e) {
+			Throwable t = e.getLinkedException();
+			if (t instanceof XmlRpcException) {
+				throw (XmlRpcException) t;
+			} else {
+				throw new XmlRpcException("Unexpected exception: " + t.getMessage(), t);
+			}
+		} catch (IOException e) {
+			throw new XmlRpcException("I/O error while communicating with HTTP server: " + e.getMessage(), e);
+		}
+	}
+}

Modified: webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransportFactory.java
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransportFactory.java?view=diff&rev=487358&r1=487357&r2=487358
==============================================================================
--- webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransportFactory.java (original)
+++ webservices/xmlrpc/trunk/client/src/main/java/org/apache/xmlrpc/client/XmlRpcCommonsTransportFactory.java Thu Dec 14 13:37:57 2006
@@ -15,12 +15,16 @@
  */
 package org.apache.xmlrpc.client;
 
+import org.apache.commons.httpclient.HttpClient;
+
 
 /** An HTTP transport factory, which is based on the Jakarta Commons
  * HTTP Client.
  */
 public class XmlRpcCommonsTransportFactory extends XmlRpcTransportFactoryImpl {
-	/** Creates a new instance.
+    private HttpClient httpClient;
+
+    /** Creates a new instance.
 	 * @param pClient The client, which is controlling the factory.
 	 */
 	public XmlRpcCommonsTransportFactory(XmlRpcClient pClient) {
@@ -28,6 +32,32 @@
 	}
 
 	public XmlRpcTransport getTransport() {
-		return new XmlRpcCommonsTransport(getClient());
+		return new XmlRpcCommonsTransport(this);
 	}
+
+	/**
+     * <p>Sets the factories {@link HttpClient}. By default, a new instance
+     * of {@link HttpClient} is created for any request.</p>
+     * <p>Reusing the {@link HttpClient} is required, if you want to preserve
+     * some state between requests. This applies, in particular, if you want
+     * to use cookies: In that case, create an instance of {@link HttpClient},
+     * give it to the factory, and use {@link HttpClient#getState()} to
+     * read or set cookies.
+	 */
+    public void setHttpClient(HttpClient pHttpClient) {
+        httpClient = pHttpClient;
+    }
+
+    /**
+     * <p>Returns the factories {@link HttpClient}. By default, a new instance
+     * of {@link HttpClient} is created for any request.</p>
+     * <p>Reusing the {@link HttpClient} is required, if you want to preserve
+     * some state between requests. This applies, in particular, if you want
+     * to use cookies: In that case, create an instance of {@link HttpClient},
+     * give it to the factory, and use {@link HttpClient#getState()} to
+     * read or set cookies.
+     */
+    public HttpClient getHttpClient() {
+        return httpClient;
+    }
 }

Modified: webservices/xmlrpc/trunk/src/site/apt/advanced.apt
URL: http://svn.apache.org/viewvc/webservices/xmlrpc/trunk/src/site/apt/advanced.apt?view=diff&rev=487358&r1=487357&r2=487358
==============================================================================
--- webservices/xmlrpc/trunk/src/site/apt/advanced.apt (original)
+++ webservices/xmlrpc/trunk/src/site/apt/advanced.apt Thu Dec 14 13:37:57 2006
@@ -137,28 +137,24 @@
 
   * The XmlRpcCommonsHttpTransport requires that the HttpClient is being
     reused. (By default, a new HttpClient is created for any connection.)
-    To reuse the HttpClient, use a transport factory like the following:
+    To reuse the HttpClient, set it on the transport factory:
 
 -----------------------------------------------------------------------------------
     import org.apache.commons.httpclient.HttpClient;
+    import org.apache.commons.httpclient.HttpState;
     import org.apache.xmlrpc.client.XmlRpcClient;
-    import org.apache.xmlrpc.client.XmlRpcCommonsHttpTransport;
-    import org.apache.xmlrpc.client.XmlRpcTransport;
-    import org.apache.xmlrpc.client.XmlRpcTransportFactory;
+    import org.apache.xmlrpc.client.XmlRpcCommonsTransport;
+    import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;
 
     final XmlRpcClient client = new XmlRpcClient();
     final HttpClient httpClient = new HttpClient();
-    XmlRpcTransportFactory factory = new XmlRpcTransportFactory(){
-        public XmlRpcTransport getTransport(){
-            return new XmlRpcCommonsHttpTransport(client){
-               protected HttpClient newHttpClient() {
-                   return httpClient;
-               }      
-            };
-        }
-    };
+    final XmlRpcCommonsTransportFactory factory = new XmlRpcCommonsTransportFactory(client);
+    factory.setHttpClient(httpClient);
     client.setTransportFactory(factory);
+    final HttpState httpState = client.getState();
 -----------------------------------------------------------------------------------
+
+    Cookies may now be read or set on the httpState object.
 
     Note, that this means losing the XmlRpcClients multithreading abilities!
     The factory above is obviously bound to the HttpClient, which must be