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/26 20:50:29 UTC
cvs commit: jakarta-cactus/sample-servlet/src/unit/share/org/apache/cactus/unit TestServletTestCase2.java TestServletTestCase3.java
vmassol 2002/07/26 11:50:29
Modified: documentation/docs/xdocs changes.xml todo.xml
framework/src/java/share/org/apache/cactus WebRequest.java
framework/src/java/share/org/apache/cactus/client
ConnectionHelperFactory.java
framework/src/java/share/org/apache/cactus/util
Configuration.java
sample-servlet/src/unit/share/org/apache/cactus/unit
TestServletTestCase2.java TestServletTestCase3.java
Added: framework/src/java/share/org/apache/cactus/client
HttpClientConnectionHelper.java
framework/src/java/share/org/apache/cactus/util
HttpURLConnection.java
Log:
- Added an HttpClient implementation of ConnectionHelper and use it as the Cactus default
- Re-enabled Cactus response code tests as it now works with HttpClient (except for 401)
- Ability to switch implementations of ConnectionHelper by configuring a Cactus property (see changes.xml). Default one is HttpClientConnectionHelper
Revision Changes Path
1.34 +24 -0 jakarta-cactus/documentation/docs/xdocs/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/documentation/docs/xdocs/changes.xml,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -r1.33 -r1.34
--- changes.xml 22 Jul 2002 20:59:58 -0000 1.33
+++ changes.xml 26 Jul 2002 18:50:29 -0000 1.34
@@ -49,6 +49,30 @@
<release version="1.4 in CVS">
<action dev="VMA" type="add">
+ It is now possible to assert response codes in
+ <code>endXXX()</code>. For example, you can verify that you servlet
+ has returned a 500 response code. See the tests provided in the Sample
+ Servlet application which is part of the Cactus distribution. This
+ change was possible because we moved the underlying implementation from
+ <code>HttpURLConnection</code> to Jakarta Commons
+ <link href="http://jakarta.apache.org/commons/httpclient">
+ HttpClient</link>. Note: It is still not possible to test a 401
+ response code (this limitation has been raised to the HttpClient team).
+ </action>
+ <action dev="VMA" type="update">
+ Refactored internal code to be able to use different HTTP connection
+ implementation. 2 are currently provided : one using the JDK
+ <code>HttpURLConnection</code> and one using Jakarta Commons
+ <link href="http://jakarta.apache.org/commons/httpclient">
+ HttpClient</link> (the default one). It can be modified by setting
+ the following System property :
+ <code>cactus.connectionHelper.classname =
+ org.apache.cactus.client.JdkConnectionHelper</code> (for the JDK
+ <code>HttpURLConnection</code>). Note that the Servlet Sample test
+ that assert response code will fail with the JDK
+ <code>HttpURLConnection</code> implementation.
+ </action>
+ <action dev="VMA" type="add">
Cactus now requires the Commons Logging library
(<code>commons-logging.jar</code>). It is needed as Commons HttpClient
is now using Commons Logging for logging and Cactus depends on
1.15 +0 -11 jakarta-cactus/documentation/docs/xdocs/todo.xml
Index: todo.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/documentation/docs/xdocs/todo.xml,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- todo.xml 22 Jul 2002 20:59:59 -0000 1.14
+++ todo.xml 26 Jul 2002 18:50:29 -0000 1.15
@@ -136,17 +136,6 @@
in a subsequent release as it involves some changes in the build
process).
</action>
- <action>
- Consider moving from HttpURLConnection to Commons HttpClient. This
- will solve the problem of asserting code that returns an error
- response code. Note: this has been fixed in JDK 1.3.1 I believe for
- HttpURLConnection but as we're already using HttpClient for handling
- cookies (because the JDK does not seem to provide any API for that).
- Review JDK 1.4's HttpURLConnection to check if the JDK is making any
- progress in this area. In term of design, isolate the implementation
- with an interface so that different implementation can be plugged in
- without affecting any other business classes.
- </action>
</category>
</version>
1.5 +8 -1 jakarta-cactus/framework/src/java/share/org/apache/cactus/WebRequest.java
Index: WebRequest.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/WebRequest.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- WebRequest.java 21 Jul 2002 12:09:16 -0000 1.4
+++ WebRequest.java 26 Jul 2002 18:50:29 -0000 1.5
@@ -557,6 +557,13 @@
*/
public void addHeader(String theName, String theValue)
{
+ // If the header is "Content-type", then call setContentType() instead.
+ // This is to prevent the content type to be set twice.
+ if (theName.equalsIgnoreCase("Content-type")) {
+ setContentType(theValue);
+ return;
+ }
+
// If there is already a header of the same name, add the
// new header to the Vector. If not, create a Vector an add it to the
// hashtable
1.2 +27 -7 jakarta-cactus/framework/src/java/share/org/apache/cactus/client/ConnectionHelperFactory.java
Index: ConnectionHelperFactory.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/client/ConnectionHelperFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ConnectionHelperFactory.java 24 Jul 2002 20:46:48 -0000 1.1
+++ ConnectionHelperFactory.java 26 Jul 2002 18:50:29 -0000 1.2
@@ -56,10 +56,16 @@
*/
package org.apache.cactus.client;
+import org.apache.cactus.util.Configuration;
+import org.apache.cactus.util.ChainedRuntimeException;
+
+import java.lang.reflect.Constructor;
+
/**
* Factory that returns the <code>ConnectionHelper</code> specified in Cactus
- * configuration or the default one (<code>JdkConnectionHelper</code>) if none
- * has been specified.
+ * configuration or the default one if none has been specified.
+ *
+ * @see Configuration
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
@@ -68,14 +74,28 @@
public class ConnectionHelperFactory
{
/**
- * @return the <code>ConnectionHelper</code> specified in Cactus
- * configuration or the default one
+ * @return a <code>ConnectionHelper</code> instance of the type 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);
+ // Load the corresponding class
+ ConnectionHelper connectionHelper;
+ try {
+ Class connectionHelperClass =
+ Class.forName(Configuration.getConnectionHelper());
+ Constructor constructor = connectionHelperClass.getConstructor(
+ new Class[] {String.class});
+ connectionHelper = (ConnectionHelper) constructor.newInstance(
+ new Object[] {theUrl});
+ } catch (Exception e) {
+ throw new ChainedRuntimeException("Failed to load the ["
+ + Configuration.getConnectionHelper() + "] ConnectionHelper "
+ + "class", e);
+ }
+
+ return connectionHelper;
}
}
1.1 jakarta-cactus/framework/src/java/share/org/apache/cactus/client/HttpClientConnectionHelper.java
Index: HttpClientConnectionHelper.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 java.net.URL;
import java.net.HttpURLConnection;
import java.util.Enumeration;
import java.io.InputStream;
import java.io.IOException;
import java.io.ByteArrayOutputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.cactus.WebRequest;
import org.apache.cactus.client.authentication.AbstractAuthentication;
/**
* Implementation of <code>ConnectionHelper</code> using Jakarta Commons
* HttpClient.
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
* @version $Id: HttpClientConnectionHelper.java,v 1.1 2002/07/26 18:50:29 vmassol Exp $
*/
public class HttpClientConnectionHelper extends AbstractConnectionHelper
{
/**
* The <code>HttpMethod</code> used to connect to the HTTP server. It is
* either a <code>GetMethod</code> or a <code>PostMethod</code>.
*/
private GetMethod method;
/**
* The URL that will be used for the HTTP connection.
*/
private String url;
/**
* @param theURL the URL that will be used for the HTTP connection.
*/
public HttpClientConnectionHelper(String theURL)
{
this.url = theURL;
}
/**
* @see ConnectionHelper#connect(WebRequest)
*/
public HttpURLConnection connect(WebRequest theRequest)
throws Throwable
{
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);
// Choose the method that we will use to post data :
// - If at least one parameter is to be sent in the request body, then
// we are doing a POST.
// - If user data has been specified, then we are doing a POST
if (theRequest.getParameterNamesPost().hasMoreElements()
|| (theRequest.getUserData() != null)) {
this.method = new PostMethod();
} else {
this.method = new GetMethod();
}
this.method.setUseDisk(false);
this.method.setFollowRedirects(false);
this.method.setPath(url.getPath());
this.method.setQueryString(url.getQuery());
// Sets the content type
this.method.setRequestHeader("Content-type",
theRequest.getContentType());
// Add the other header fields
addHeaders(theRequest);
// Add the cookies
String cookieString = getCookieString(theRequest, url);
if (cookieString != null) {
this.method.addRequestHeader("Cookie", cookieString);
}
// Add the POST parameters if no user data has been specified (user data
// overried post parameters)
if (theRequest.getUserData() != null) {
addUserData(theRequest);
} else {
addParametersPost(theRequest);
}
// Open the connection and get the result
HttpClient client = new HttpClient();
client.startSession(url.getHost(), url.getPort());
client.executeMethod(this.method);
// Wrap the HttpClient method in a java.net.HttpURLConnection object
return new org.apache.cactus.util.HttpURLConnection(this.method, url);
}
/**
* 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
* redirector.
*/
private void addParametersPost(WebRequest theRequest)
{
// If no parameters, then exit
if (!theRequest.getParameterNamesPost().hasMoreElements()) {
return;
}
Enumeration keys = theRequest.getParameterNamesPost();
if (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String[] values = theRequest.getParameterValuesPost(key);
for (int i = 0; i < values.length; i++) {
((PostMethod) this.method).addParameter(key, values[i]);
}
}
}
/**
* Add the Headers to the request.
*
* @param theRequest the request containing all data to pass to the server
* redirector.
*/
private void addHeaders(WebRequest theRequest)
{
Enumeration keys = theRequest.getHeaderNames();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String[] values = theRequest.getHeaderValues(key);
StringBuffer fullHeaderValue = new StringBuffer(values[0]);
for (int i = 1; i < values.length; i++) {
fullHeaderValue.append("," + values[i]);
}
this.method.addRequestHeader(key, fullHeaderValue.toString());
}
}
/**
* Add user data in the request body.
*
* @param theRequest the request containing all data to pass to the server
* redirector.
* @exception IOException if we fail to read the user data
*/
private void addUserData(WebRequest theRequest)
throws IOException
{
// If no user data, then exit
if (theRequest.getUserData() == null) {
return;
}
InputStream stream = theRequest.getUserData();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int length;
while ((length = stream.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
((PostMethod) this.method).setRequestBody(baos.toString());
}
}
1.10 +38 -5 jakarta-cactus/framework/src/java/share/org/apache/cactus/util/Configuration.java
Index: Configuration.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/util/Configuration.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Configuration.java 22 Jul 2002 18:40:19 -0000 1.9
+++ Configuration.java 26 Jul 2002 18:50:29 -0000 1.10
@@ -56,6 +56,8 @@
*/
package org.apache.cactus.util;
+import org.apache.cactus.client.HttpClientConnectionHelper;
+
import java.io.FileInputStream;
import java.io.IOException;
import java.util.PropertyResourceBundle;
@@ -78,7 +80,7 @@
* Name of the Cactus configuration file if cactus is to look for it in
* the classpath.
*/
- private static final String CONFIG_DEFAULT_NAME = "cactus";
+ private static final String DEFAULT_CONFIG_NAME = "cactus";
/**
* Name of the java property for specifying the location of the cactus
@@ -96,6 +98,20 @@
"cactus.contextURL";
/**
+ * Name of the Cactus property for overriding the default
+ * {@link org.apache.cactus.client.ConnectionHelper}. Defaults to
+ * {@link org.apache.cactus.client.HttpClientConnectionHelper}
+ */
+ private static final String CACTUS_CONNECTION_HELPER_CLASSNAME_PROPERTY =
+ "cactus.connectionHelper.classname";
+
+ /**
+ * Default {@link org.apache.cactus.client.ConnectionHelper} to use.
+ */
+ public static final String DEFAULT_CACTUS_CONNECTION_HELPER_CLASSNAME =
+ HttpClientConnectionHelper.class.getName();
+
+ /**
* True if the Cactus configuration file has already been read.
* @see #initialize()
*/
@@ -104,7 +120,7 @@
/**
* Read the cactus configuration file from the java property defined
* on the command line (named CACTUS_CONFIG_PROPERTY) and if none has been
- * defined tries to read the CONFIG_DEFAULT_NAME file from the classpath.
+ * defined tries to read the DEFAULT_CONFIG_NAME file from the classpath.
* All properties found are exported as java system properties.
*/
public static final void initialize()
@@ -120,7 +136,7 @@
// Try to read the default cactus configuration file from the
// classpath
config = ClassLoaderUtils.loadPropertyResourceBundle(
- CONFIG_DEFAULT_NAME, Configuration.class);
+ DEFAULT_CONFIG_NAME, Configuration.class);
} else {
// Try to read from specified properties file
try {
@@ -160,10 +176,27 @@
// from the Cactus configuration file.
String contextURL = System.getProperty(CACTUS_CONTEXT_URL_PROPERTY);
if (contextURL == null) {
- new ChainedRuntimeException("Missing Cactus property ["
+ throw new ChainedRuntimeException("Missing Cactus property ["
+ CACTUS_CONTEXT_URL_PROPERTY + "]");
}
return contextURL;
}
+ /**
+ * @return the {@link org.apache.cactus.client.ConnectionHelper} classname
+ * to use for opening the HTTP connection
+ */
+ public static String getConnectionHelper()
+ {
+ // Try to read it from a System property first and then if not defined
+ // use the default.
+ String connectionHelperClassname =
+ System.getProperty(CACTUS_CONNECTION_HELPER_CLASSNAME_PROPERTY);
+ if (connectionHelperClassname == null) {
+ connectionHelperClassname =
+ DEFAULT_CACTUS_CONNECTION_HELPER_CLASSNAME;
+ }
+
+ return connectionHelperClassname;
+ }
}
1.1 jakarta-cactus/framework/src/java/share/org/apache/cactus/util/HttpURLConnection.java
Index: HttpURLConnection.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.util;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.Header;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.ProtocolException;
import java.security.Permission;
/**
* Provides a <code>HttpURLConnection</code> wrapper around HttpClient
* <code>HttpMethod</code>. This allows existing code to easily switch to
* HttpClieht without breaking existing interfaces using the JDK
* <code>HttpURLConnection<code>.
*
* Note: It is a best try effort as different version of the JDK have different
* behaviours for <code>HttpURLConnection</code> (And I'm not even including
* the numerous <code>HttpURLConnection</code> bugs!).
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
* @version $Id: HttpURLConnection.java,v 1.1 2002/07/26 18:50:29 vmassol Exp $
*/
public class HttpURLConnection extends java.net.HttpURLConnection
{
/**
* The <code>HttpMethod</code> object that was used to connect to the
* HTTP server. It contains all the returned data.
*/
private HttpMethod method;
/**
* The URL to which we are connected
*/
private URL url;
/**
* Creates an <code>HttpURLConnection</code> from a
* <code>HttpMethod</code>.
*
* @param theMethod the theMethod that was used to connect to the HTTP
* server and which contains the returned data.
* @param theURL the URL to which we are connected (includes query string)
*/
public HttpURLConnection(HttpMethod theMethod, URL theURL)
{
super(theURL);
this.method = theMethod;
this.url = theURL;
}
/**
* @see java.net.HttpURLConnection#HttpURLConnection(URL)
*/
protected HttpURLConnection(URL theURL)
{
super(theURL);
throw new RuntimeException("An HTTP URL connection can only be "
+ "constructed from a HttpMethod class");
}
/**
* @see java.net.HttpURLConnection#getInputStream()
*/
public InputStream getInputStream() throws IOException
{
return this.method.getResponseBodyAsStream();
}
/**
* @see java.net.HttpURLConnection#getErrorStream()
*/
public InputStream getErrorStream()
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#disconnect()
*/
public void disconnect()
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#connect()
*/
public void connect() throws IOException
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#usingProxy()
*/
public boolean usingProxy()
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#getRequestMethod()
*/
public String getRequestMethod()
{
return this.method.getName();
}
/**
* @see java.net.HttpURLConnection#getResponseCode()
*/
public int getResponseCode() throws IOException
{
return this.method.getStatusCode();
}
/**
* @see java.net.HttpURLConnection#getResponseMessage()
*/
public String getResponseMessage() throws IOException
{
return this.method.getStatusText();
}
/**
* @see java.net.HttpURLConnection#getHeaderField(String)
*/
public String getHeaderField(String theName)
{
// Note: Return the last matching header in the Header[] array, as in
// the JDK implementation.
Header[] headers = this.method.getResponseHeaders();
for (int i = headers.length - 1; i >= 0; i--) {
if (headers[i].getName().equalsIgnoreCase(theName)) {
return headers[i].getValue();
}
}
return null;
}
/**
* @see java.net.HttpURLConnection#getHeaderFieldKey(int)
*/
public String getHeaderFieldKey(int theKeyPosition)
{
// Note: I hope the header fields are kept in the correct order when
// calling getRequestHeaders.
Header[] headers = this.method.getResponseHeaders();
if (theKeyPosition < 0 || theKeyPosition >= headers.length) {
return null;
}
return headers[theKeyPosition].getName();
}
/**
* @see java.net.HttpURLConnection#getHeaderField(int)
*/
public String getHeaderField(int thePosition)
{
// Note: I hope the header fields are kept in the correct order when
// calling getRequestHeaders.
Header[] headers = this.method.getResponseHeaders();
if (thePosition < 0 || thePosition >= headers.length) {
return null;
}
return headers[thePosition].getValue();
}
/**
* @see java.net.HttpURLConnection#getURL()
*/
public URL getURL()
{
return this.url;
}
// Note: We don't implement the following methods so that they default to
// the JDK implementation. They will all call
// <code>getHeaderField(String)</code> which we have overridden.
// java.net.HttpURLConnection#getHeaderFieldDate(String, long)
// java.net.HttpURLConnection#getContentLength()
// java.net.HttpURLConnection#getContentType()
// java.net.HttpURLConnection#getContentEncoding()
// java.net.HttpURLConnection#getDate()
// java.net.HttpURLConnection#getHeaderFieldInt(String, int)
// java.net.HttpURLConnection#getExpiration()
// java.net.HttpURLConnection#getLastModified()
/**
* @see java.net.HttpURLConnection#setInstanceFollowRedirects(boolean)
*/
public void setInstanceFollowRedirects(boolean isFollowingRedirects)
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#getInstanceFollowRedirects()
*/
public boolean getInstanceFollowRedirects()
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#setRequestMethod(String)
*/
public void setRequestMethod(String theMethod) throws ProtocolException
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#getPermission()
*/
public Permission getPermission() throws IOException
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#getContent()
*/
public Object getContent() throws IOException
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#getContent(Class[])
*/
public Object getContent(Class[] theClasses) throws IOException
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#getOutputStream()
*/
public OutputStream getOutputStream() throws IOException
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#setDoInput(boolean)
*/
public void setDoInput(boolean isInput)
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#getDoInput()
*/
public boolean getDoInput()
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#setDoOutput(boolean)
*/
public void setDoOutput(boolean isOutput)
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#getDoOutput()
*/
public boolean getDoOutput()
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#setAllowUserInteraction(boolean)
*/
public void setAllowUserInteraction(boolean isAllowInteraction)
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#getAllowUserInteraction()
*/
public boolean getAllowUserInteraction()
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#setUseCaches(boolean)
*/
public void setUseCaches(boolean isUsingCaches)
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#getUseCaches()
*/
public boolean getUseCaches()
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#setIfModifiedSince(long)
*/
public void setIfModifiedSince(long theModificationDate)
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#getIfModifiedSince()
*/
public long getIfModifiedSince()
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#getDefaultUseCaches()
*/
public boolean getDefaultUseCaches()
{
throw new RuntimeException("Not implemented yet");
}
/**
* @see java.net.HttpURLConnection#setDefaultUseCaches(boolean)
*/
public void setDefaultUseCaches(boolean isUsingCaches)
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#setRequestProperty(String, String)
*/
public void setRequestProperty(String theKey, String theValue)
{
throw new RuntimeException("This class can only be used with already"
+ "retrieved data");
}
/**
* @see java.net.HttpURLConnection#getRequestProperty(String)
*/
public String getRequestProperty(String theKey)
{
throw new RuntimeException("Not implemented yet");
}
}
1.7 +6 -14 jakarta-cactus/sample-servlet/src/unit/share/org/apache/cactus/unit/TestServletTestCase2.java
Index: TestServletTestCase2.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/sample-servlet/src/unit/share/org/apache/cactus/unit/TestServletTestCase2.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TestServletTestCase2.java 23 Jul 2002 22:07:52 -0000 1.6
+++ TestServletTestCase2.java 26 Jul 2002 18:50:29 -0000 1.7
@@ -548,15 +548,11 @@
* status code (> 400).
*
* Note: HttpURLConnection will return a FileNotFoundException if the
- * status code is > 400 and the request does not end with a "/" ! In
- * order to prevent, this ensure the Servlet Redirector URL in
- * cactus.properties does end with a "/" and modify the servlet mapping
- * in web.xml to include the trailing slash.
+ * status code is > 400 and the request does not end with a "/" !
*/
public void testStatusCode()
{
-// FIXME: use HttpClient for http connection
-// response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
+ response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
/**
@@ -564,19 +560,15 @@
* status code (> 400).
*
* Note: HttpURLConnection will return a FileNotFoundException if the
- * status code is > 400 and the request does not end with a "/" ! In
- * order to prevent, this ensure the Servlet Redirector URL in
- * cactus.properties does end with a "/" and modify the servlet mapping
- * in web.xml to include the trailing slash.
+ * status code is > 400 and the request does not end with a "/" !
*
* @param theResponse the response from the server side.
*/
public void endStatusCode(WebResponse theResponse)
throws IOException
{
-// FIXME: use HttpClient for http connection
-// assertEquals(HttpServletResponse.SC_UNAUTHORIZED,
-// theResponse.getConnection().getResponseCode());
+ assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+ theResponse.getConnection().getResponseCode());
}
//-------------------------------------------------------------------------
1.3 +3 -3 jakarta-cactus/sample-servlet/src/unit/share/org/apache/cactus/unit/TestServletTestCase3.java
Index: TestServletTestCase3.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/sample-servlet/src/unit/share/org/apache/cactus/unit/TestServletTestCase3.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestServletTestCase3.java 10 Apr 2002 00:26:20 -0000 1.2
+++ TestServletTestCase3.java 26 Jul 2002 18:50:29 -0000 1.3
@@ -135,7 +135,7 @@
*/
protected void tearDown()
{
- response.setHeader("teardownheader", "tear down header");
+ response.setHeader("Teardownheader", "tear down header");
}
/**
@@ -157,7 +157,7 @@
public void endTearDown(WebResponse theResponse)
{
assertEquals("tear down header",
- theResponse.getConnection().getHeaderField("teardownheader"));
+ theResponse.getConnection().getHeaderField("Teardownheader"));
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>