You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by vm...@apache.org on 2002/07/24 22:46:48 UTC
cvs commit: jakarta-cactus/framework/src/java/share/org/apache/cactus/client/authentication AbstractAuthentication.java BasicAuthentication.java
vmassol 2002/07/24 13:46:48
Modified: framework/src/java/share/org/apache/cactus/client
AbstractHttpClient.java JdkConnectionHelper.java
framework/src/java/share/org/apache/cactus/client/authentication
AbstractAuthentication.java
BasicAuthentication.java
Added: framework/src/java/share/org/apache/cactus/client
AbstractConnectionHelper.java
ConnectionHelperFactory.java
Log:
refactorings to allow different implementations for HTTP connection to the Cactus server side (next step is to provide the implementation using HttpClient)
Revision Changes Path
1.10 +5 -5 jakarta-cactus/framework/src/java/share/org/apache/cactus/client/AbstractHttpClient.java
Index: AbstractHttpClient.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/client/AbstractHttpClient.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- AbstractHttpClient.java 23 Jul 2002 22:03:53 -0000 1.9
+++ AbstractHttpClient.java 24 Jul 2002 20:46:48 -0000 1.10
@@ -180,8 +180,8 @@
// Open the first connection to the redirector to execute the test on
// the server side
- ConnectionHelper helper =
- new JdkConnectionHelper(getRedirectorURL(theRequest));
+ ConnectionHelper helper = ConnectionHelperFactory.getConnectionHelper(
+ getRedirectorURL(theRequest));
HttpURLConnection connection = helper.connect(theRequest);
@@ -219,8 +219,8 @@
resultsRequest.setAuthentication(theAuthentication);
// Open the second connection to get the test results
- ConnectionHelper helper =
- new JdkConnectionHelper(getRedirectorURL(resultsRequest));
+ ConnectionHelper helper = ConnectionHelperFactory.getConnectionHelper(
+ getRedirectorURL(resultsRequest));
HttpURLConnection resultConnection = helper.connect(resultsRequest);
1.3 +14 -145 jakarta-cactus/framework/src/java/share/org/apache/cactus/client/JdkConnectionHelper.java
Index: JdkConnectionHelper.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/client/JdkConnectionHelper.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- JdkConnectionHelper.java 23 Jul 2002 22:35:36 -0000 1.2
+++ JdkConnectionHelper.java 24 Jul 2002 20:46:48 -0000 1.3
@@ -64,16 +64,12 @@
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
-import java.net.MalformedURLException;
import java.util.Enumeration;
-import java.util.Vector;
-import org.apache.commons.httpclient.Header;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.apache.cactus.WebRequest;
-import org.apache.cactus.Cookie;
import org.apache.cactus.client.authentication.AbstractAuthentication;
import org.apache.cactus.util.ChainedRuntimeException;
@@ -86,7 +82,7 @@
*
* @version $Id$
*/
-public class JdkConnectionHelper implements ConnectionHelper
+public class JdkConnectionHelper extends AbstractConnectionHelper
{
/**
* The logger
@@ -122,6 +118,14 @@
{
URL url = new URL(this.url);
+ // Add Authentication headers, if necessary. This is the first
+ // step to allow authentication to add extra headers, HTTP parameters,
+ // etc.
+ AbstractAuthentication authentication = theRequest.getAuthentication();
+ if (authentication != null) {
+ authentication.configure(theRequest);
+ }
+
// Add the parameters that need to be passed as part of the URL
url = addParametersGet(theRequest, url);
@@ -147,17 +151,14 @@
connection.setRequestProperty("Content-type",
theRequest.getContentType());
- // Add Authentication headers, if necessary
- AbstractAuthentication authentication = theRequest.getAuthentication();
- if (authentication != null) {
- authentication.configure(connection);
- }
-
// Add the other header fields
addHeaders(theRequest, connection);
// Add the cookies
- addCookies(theRequest, connection);
+ String cookieString = getCookieString(theRequest, url);
+ if (cookieString != null) {
+ connection.setRequestProperty("Cookie", cookieString);
+ }
// Add the POST parameters if no user data has been specified (user data
// overried post parameters)
@@ -205,70 +206,6 @@
}
/**
- * Add the HTTP parameters that need to be passed in the query string of
- * the URL.
- *
- * @param theRequest the request containing all data to pass to the server
- * redirector.
- * @param theURL the URL used to connect to the server redirector.
- * @return the new URL
- * @exception MalformedURLException if the URL is malformed
- */
- private URL addParametersGet(WebRequest theRequest, URL theURL)
- throws MalformedURLException
- {
- // If no parameters, then exit
- if (!theRequest.getParameterNamesGet().hasMoreElements()) {
- return theURL;
- }
-
- StringBuffer queryString = new StringBuffer();
-
- Enumeration keys = theRequest.getParameterNamesGet();
-
- if (keys.hasMoreElements()) {
- String key = (String) keys.nextElement();
- String[] values = theRequest.getParameterValuesGet(key);
- queryString.append(key);
- queryString.append('=');
- queryString.append(URLEncoder.encode(values[0]));
- for (int i = 1; i < values.length; i++) {
- queryString.append('&');
- queryString.append(key);
- queryString.append('=');
- queryString.append(URLEncoder.encode(values[i]));
- }
- }
-
- while (keys.hasMoreElements()) {
- String key = (String) keys.nextElement();
- String[] values = theRequest.getParameterValuesGet(key);
- for (int i = 0; i < values.length; i++) {
- queryString.append('&');
- queryString.append(key);
- queryString.append('=');
- queryString.append(URLEncoder.encode(values[i]));
- }
- }
-
- String file = theURL.getFile();
-
- // Remove the trailing "/" if there is one
- if (file.endsWith("/")) {
- file = file.substring(0, file.length() - 1);
- }
-
- if (theURL.toString().indexOf("?") > 0) {
- file = file + "&" + queryString.toString();
- } else {
- file = file + "?" + queryString.toString();
- }
-
- return new URL(theURL.getProtocol(), theURL.getHost(),
- theURL.getPort(), file);
- }
-
- /**
* Add the HTTP parameters that need to be passed in the request body.
*
* @param theRequest the request containing all data to pass to the server
@@ -339,74 +276,6 @@
}
return out;
- }
-
- /**
- * Add the Cookies to the request.
- *
- * @param theRequest the request containing all data to pass to the server
- * redirector.
- * @param theConnection the HTTP connection
- */
- private void addCookies(WebRequest theRequest,
- URLConnection theConnection)
- {
- // If no Cookies, then exit
- Vector cookies = theRequest.getCookies();
- if (!cookies.isEmpty()) {
-
- // transform the Cactus cookies into HttpClient cookies
- org.apache.commons.httpclient.Cookie[] httpclientCookies =
- new org.apache.commons.httpclient.Cookie[cookies.size()];
- for (int i = 0; i < cookies.size(); i++) {
- org.apache.cactus.Cookie cactusCookie =
- (org.apache.cactus.Cookie) cookies.elementAt(i);
-
- // If no domain has been specified, use a default one
- String domain;
- if (cactusCookie.getDomain() == null) {
- domain = Cookie.getCookieDomain(theRequest,
- theConnection.getURL().getHost());
- } else {
- domain = cactusCookie.getDomain();
- }
-
- // If not path has been specified , use a default one
- String path;
- if (cactusCookie.getPath() == null) {
- path = Cookie.getCookiePath(theRequest,
- theConnection.getURL().getFile());
- } else {
- path = cactusCookie.getPath();
- }
-
- httpclientCookies[i] =
- new org.apache.commons.httpclient.Cookie(
- domain, cactusCookie.getName(),
- cactusCookie.getValue());
-
- httpclientCookies[i].setComment(cactusCookie.getComment());
- httpclientCookies[i].setExpiryDate(
- cactusCookie.getExpiryDate());
- httpclientCookies[i].setPath(path);
- httpclientCookies[i].setSecure(cactusCookie.isSecure());
- }
-
- // and create the cookie header to send
- Header cookieHeader =
- org.apache.commons.httpclient.Cookie.createCookieHeader(
- Cookie.getCookieDomain(theRequest,
- theConnection.getURL().getHost()),
- Cookie.getCookiePath(theRequest,
- theConnection.getURL().getFile()),
- httpclientCookies);
-
- LOGGER.debug("Cookie string = [" + cookieHeader.getValue()
- + "]");
-
- theConnection.setRequestProperty("Cookie",
- cookieHeader.getValue());
- }
}
/**
1.1 jakarta-cactus/framework/src/java/share/org/apache/cactus/client/AbstractConnectionHelper.java
Index: AbstractConnectionHelper.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
* Foundation" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.cactus.client;
import org.apache.cactus.WebRequest;
import org.apache.cactus.Cookie;
import org.apache.commons.httpclient.Header;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Vector;
/**
* Common helper methods for implementing <code>ConnectionHelper</code>. These
* methods are common to any implementation.
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
* @version $Id: AbstractConnectionHelper.java,v 1.1 2002/07/24 20:46:48 vmassol Exp $
*/
public abstract class AbstractConnectionHelper implements ConnectionHelper
{
/**
* Add the HTTP parameters that need to be passed in the query string of
* the URL.
*
* @param theRequest the request containing all data to pass to the server
* redirector.
* @param theURL the URL used to connect to the server redirector.
* @return the new URL
* @exception MalformedURLException if the URL is malformed
*/
protected URL addParametersGet(WebRequest theRequest, URL theURL)
throws MalformedURLException
{
// If no parameters, then exit
if (!theRequest.getParameterNamesGet().hasMoreElements()) {
return theURL;
}
StringBuffer queryString = new StringBuffer();
Enumeration keys = theRequest.getParameterNamesGet();
if (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String[] values = theRequest.getParameterValuesGet(key);
queryString.append(key);
queryString.append('=');
queryString.append(URLEncoder.encode(values[0]));
for (int i = 1; i < values.length; i++) {
queryString.append('&');
queryString.append(key);
queryString.append('=');
queryString.append(URLEncoder.encode(values[i]));
}
}
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String[] values = theRequest.getParameterValuesGet(key);
for (int i = 0; i < values.length; i++) {
queryString.append('&');
queryString.append(key);
queryString.append('=');
queryString.append(URLEncoder.encode(values[i]));
}
}
String file = theURL.getFile();
// Remove the trailing "/" if there is one
if (file.endsWith("/")) {
file = file.substring(0, file.length() - 1);
}
if (theURL.toString().indexOf("?") > 0) {
file = file + "&" + queryString.toString();
} else {
file = file + "?" + queryString.toString();
}
return new URL(theURL.getProtocol(), theURL.getHost(),
theURL.getPort(), file);
}
/**
* @return the cookie string which will be added as a HTTP "Cookie" header
* or null if no cookie has been set
* @param theRequest the request containing all data to pass to the server
* redirector.
* @param theUrl the URL to connect to
*/
public String getCookieString(WebRequest theRequest, URL theUrl)
{
// If no Cookies, then exit
Vector cookies = theRequest.getCookies();
if (!cookies.isEmpty()) {
// transform the Cactus cookies into HttpClient cookies
org.apache.commons.httpclient.Cookie[] httpclientCookies =
new org.apache.commons.httpclient.Cookie[cookies.size()];
for (int i = 0; i < cookies.size(); i++) {
Cookie cactusCookie = (Cookie) cookies.elementAt(i);
// If no domain has been specified, use a default one
String domain;
if (cactusCookie.getDomain() == null) {
domain = Cookie.getCookieDomain(theRequest,
theUrl.getHost());
} else {
domain = cactusCookie.getDomain();
}
// If not path has been specified , use a default one
String path;
if (cactusCookie.getPath() == null) {
path = Cookie.getCookiePath(theRequest, theUrl.getFile());
} else {
path = cactusCookie.getPath();
}
httpclientCookies[i] =
new org.apache.commons.httpclient.Cookie(
domain, cactusCookie.getName(),
cactusCookie.getValue());
httpclientCookies[i].setComment(cactusCookie.getComment());
httpclientCookies[i].setExpiryDate(
cactusCookie.getExpiryDate());
httpclientCookies[i].setPath(path);
httpclientCookies[i].setSecure(cactusCookie.isSecure());
}
// and create the cookie header to send
Header cookieHeader =
org.apache.commons.httpclient.Cookie.createCookieHeader(
Cookie.getCookieDomain(theRequest, theUrl.getHost()),
Cookie.getCookiePath(theRequest, theUrl.getFile()),
httpclientCookies);
return cookieHeader.getValue();
}
return null;
}
}
1.1 jakarta-cactus/framework/src/java/share/org/apache/cactus/client/ConnectionHelperFactory.java
Index: ConnectionHelperFactory.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
* Foundation" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.cactus.client;
/**
* Factory that returns the <code>ConnectionHelper</code> specified in Cactus
* configuration or the default one (<code>JdkConnectionHelper</code>) if none
* has been specified.
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
* @version $Id: ConnectionHelperFactory.java,v 1.1 2002/07/24 20:46:48 vmassol Exp $
*/
public class ConnectionHelperFactory
{
/**
* @return the <code>ConnectionHelper</code> specified in Cactus
* configuration or the default one
* (<code>JdkConnectionHelper</code>)
* @param theUrl the URL to connect to as a String
*/
public static ConnectionHelper getConnectionHelper(String theUrl)
{
// ATM only return the default one.
return new JdkConnectionHelper(theUrl);
}
}
1.2 +6 -5 jakarta-cactus/framework/src/java/share/org/apache/cactus/client/authentication/AbstractAuthentication.java
Index: AbstractAuthentication.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/client/authentication/AbstractAuthentication.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AbstractAuthentication.java 1 Mar 2002 00:43:46 -0000 1.1
+++ AbstractAuthentication.java 24 Jul 2002 20:46:48 -0000 1.2
@@ -56,7 +56,7 @@
*/
package org.apache.cactus.client.authentication;
-import java.net.HttpURLConnection;
+import org.apache.cactus.WebRequest;
/**
* This class was designed with the simple assumption that ALL authentication
@@ -145,12 +145,13 @@
protected abstract void validatePassword(String thePassword);
/**
- * Modify the <code>HttpURLConnection</code> passed as parameter so
+ * Modify the <code>WebRequest</code> passed as parameter so
* that it will carry authentication information.
*
- * @param theConnection the HTTP connection to the server URL
+ * @param theRequest the request object that will be sent to the Cactus
+ * Redirector over HTTP
*/
- public abstract void configure(HttpURLConnection theConnection);
+ public abstract void configure(WebRequest theRequest);
/**
* @return the user password of the Credential
1.4 +6 -5 jakarta-cactus/framework/src/java/share/org/apache/cactus/client/authentication/BasicAuthentication.java
Index: BasicAuthentication.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/client/authentication/BasicAuthentication.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- BasicAuthentication.java 21 Jul 2002 12:09:16 -0000 1.3
+++ BasicAuthentication.java 24 Jul 2002 20:46:48 -0000 1.4
@@ -56,7 +56,8 @@
*/
package org.apache.cactus.client.authentication;
-import java.net.HttpURLConnection;
+import org.apache.cactus.WebRequest;
+
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
@@ -202,9 +203,9 @@
}
/**
- * @see AbstractAuthentication#configure(HttpURLConnection)
+ * @see AbstractAuthentication#configure(WebRequest)
*/
- public void configure(HttpURLConnection theConnection)
+ public void configure(WebRequest theRequest)
{
// According to HTTP 1.0 Spec:
// basic-credentials = "Basic" SP basic-cookie
@@ -218,7 +219,7 @@
String basicCredentials = "Basic "
+ new String(base64Encode(basicCookie.getBytes()));
- theConnection.setRequestProperty("Authorization", basicCredentials);
+ theRequest.addHeader("Authorization", basicCredentials);
}
/**
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>