jsdever 2003/01/31 22:46:10
Modified: httpclient/src/java/org/apache/commons/httpclient/methods
EntityEnclosingMethod.java PostMethod.java
PutMethod.java
httpclient/src/test-webapp/src/org/apache/commons/httpclient
ParamServlet.java
Log:
Some cleanups to the shiny new EntityEnclosingMethod and the revamped
PostMethod.
Contributed by: Jeff Dever
Revision Changes Path
1.2 +35 -69
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java
Index: EntityEnclosingMethod.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/EntityEnclosingMethod.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- EntityEnclosingMethod.java 1 Feb 2003 00:01:01 -0000 1.1
+++ EntityEnclosingMethod.java 1 Feb 2003 06:46:10 -0000 1.2
@@ -2,11 +2,12 @@
* $Header$
* $Revision$
* $Date$
+ *
* ====================================================================
*
* The Apache Software License, Version 1.1
*
- * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
+ * Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -85,17 +86,12 @@
* This abstract class serves as a foundation for all HTTP methods
* that can enclose an entity within requests
*
- * @author <a href="mailto:[EMAIL PROTECTED]">Remy Maucherat</a>
- * @author <a href="mailto:[EMAIL PROTECTED]">Doug Sale</a>
- * @author <a href="mailto:[EMAIL PROTECTED]">Jeff Dever</a>
- * @author Ortwin Gl�ck
* @author <a href="mailto:[EMAIL PROTECTED]">Oleg Kalnichevski</a>
*
* @since 2.0beta1
*/
+public abstract class EntityEnclosingMethod extends GetMethod {
-public abstract class EntityEnclosingMethod extends GetMethod
-{
// ----------------------------------------- Static variables/initializers
/**
@@ -127,6 +123,7 @@
*/
private int requestContentLength = CONTENT_LENGTH_AUTO;
+
private boolean useExpectHeader = true;
// ----------------------------------------------------------- Constructors
@@ -161,8 +158,8 @@
*
* @since 2.0
*/
- public EntityEnclosingMethod(String uti, String tempDir) {
- super(uti, tempDir);
+ public EntityEnclosingMethod(String uri, String tempDir) {
+ super(uri, tempDir);
setFollowRedirects(false);
}
@@ -182,7 +179,8 @@
/**
- * Entity enclosing requests cannot be redirected without user intervention
according to RFC 2616.
+ * Entity enclosing requests cannot be redirected without user intervention
+ * according to RFC 2616.
*
* @return <code>false</code>.
*
@@ -194,11 +192,10 @@
/**
- * Entity enclosing requests cannot be redirected without user intervention
according to RFC 2616.
+ * Entity enclosing requests cannot be redirected without user intervention
+ * according to RFC 2616.
*
* @param followRedirects must always be <code>false</code>
- *
- * @throws IllegalArgumentException if <code>true</code> is given
*/
public void setFollowRedirects(boolean followRedirects) {
if (followRedirects == true) {
@@ -216,17 +213,15 @@
* Returns the useExpectHeader.
* @return boolean
*/
- public boolean getUseExpectHeader()
- {
+ public boolean getUseExpectHeader() {
return this.useExpectHeader;
}
/**
* Sets the useExpectHeader.
- * @param useExpectHeader The useExpectHeader to set
+ * @param value The useExpectHeader to set
*/
- public void setUseExpectHeader(boolean value)
- {
+ public void setUseExpectHeader(boolean value) {
this.useExpectHeader = value;
}
@@ -249,7 +244,6 @@
* If CONTENT_LENGTH_AUTO is specified the request will be buffered
* before it is sent over the network.
*
- * @since 2.0
*/
public void setRequestContentLength(int length) {
LOG.trace("enter EntityEnclosingMethod.setRequestContentLength(int)");
@@ -261,8 +255,6 @@
* to return the length of the request body.
*
* @return number of bytes in the request body
- *
- * @since 2.0
*/
protected int getRequestContentLength() {
LOG.trace("enter EntityEnclosingMethod.getRequestContentLength()");
@@ -271,20 +263,14 @@
return this.requestContentLength;
}
bufferContent();
- if (this.buffer != null) {
- return this.buffer.length;
- }
- else {
- return 0;
- }
+
+ return (this.buffer == null) ? 0 : this.buffer.length;
}
/**
* Sets the request body to be the specified inputstream.
*
* @param body Request body content as {@link java.io.InputStream}
- *
- * @since 2.0
*/
public void setRequestBody(InputStream body) {
LOG.trace("enter EntityEnclosingMethod.setRequestBody(InputStream)");
@@ -296,17 +282,12 @@
* Gets the request body as a stream.
*
* @return The request body {@link java.io.InputStream} if it has been set.
- *
- * @throws IllegalStateException if request body is not buferred
- *
- * @since 2.0
*/
public InputStream getRequestBody() {
LOG.trace("enter EntityEnclosingMethod.getRequestBody()");
if (this.buffer != null) {
return new ByteArrayInputStream(this.buffer);
- }
- else {
+ } else {
return this.requestBodyStream;
}
}
@@ -315,8 +296,6 @@
* Sets the request body to be the specified string.
*
* @param body Request body content as a string
- *
- * @since 2.0
*/
public void setRequestBody(String body) {
LOG.trace("enter EntityEnclosingMethod.setRequestBody(String)");
@@ -330,50 +309,46 @@
}
/**
- * Gets the request body as a string.
+ * Gets the request body as a String.
*
* @return the request body as a string
*
* @throws IOException when i/o errors occur reading the request
- * @throws IllegalStateException if request body is not buferred
- *
- * @since 2.0
*/
-
public String getRequestBodyAsString() throws IOException {
LOG.trace("enter EntityEnclosingMethod.getRequestBodyAsString()");
+
Reader instream = null;
try {
instream = new InputStreamReader(getRequestBody(), getRequestCharSet());
- }
- catch(UnsupportedEncodingException e) {
+ } catch (UnsupportedEncodingException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("Unsupported encoding: " + e.getMessage());
}
instream = new InputStreamReader(getRequestBody());
}
+
StringBuffer buffer = new StringBuffer();
char[] tmp = new char[4096];
int l = 0;
- while((l = instream.read(tmp)) >= 0) {
+ while ((l = instream.read(tmp)) >= 0) {
buffer.append(tmp, 0, l);
}
+
return buffer.toString();
}
/**
- * Override the method of {@link HttpMethodBase}
- * to set the <tt>Expect</tt> header if it has
- * not already been set, in addition to the "standard"
- * set of headers.
+ * Set the <tt>Expect</tt> header if it has not already been set,
+ * in addition to the "standard" set of headers.
+ *
+ * @param state the client state
+ * @param conn the connection to write to
*
* @throws HttpException when a protocol error occurs or state is invalid
* @throws IOException when i/o errors occur reading the response
- *
- * @since 2.0
*/
-
protected void addRequestHeaders(HttpState state, HttpConnection conn)
throws IOException, HttpException {
LOG.trace("enter EntityEnclosingMethod.addRequestHeaders(HttpState,
HttpConnection)");
@@ -384,7 +359,7 @@
super.addRequestHeaders(state, conn);
// Send expectation header, provided there's something to be sent
- if(isHttp11() && getUseExpectHeader() && (this.requestBodyStream != null)) {
+ if (isHttp11() && getUseExpectHeader() && (this.requestBodyStream != null))
{
if (getRequestHeader("Expect") == null) {
setRequestHeader("Expect", "100-continue");
}
@@ -402,8 +377,6 @@
* @return <tt>true</tt>
* @throws IOException when i/o errors occur reading the response
* @throws HttpException when a protocol error occurs or state is invalid
- *
- * @since 2.0
*/
protected boolean writeRequestBody(HttpState state, HttpConnection conn)
throws IOException, HttpException {
@@ -415,7 +388,7 @@
LOG.debug("Expecting response");
return false;
}
- if(getStatusLine().getStatusCode() != HttpStatus.SC_CONTINUE) {
+ if (getStatusLine().getStatusCode() != HttpStatus.SC_CONTINUE) {
LOG.debug("Expecting 100-continue");
return false;
}
@@ -459,7 +432,7 @@
}
// This is hardly the most elegant solution to closing chunked stream
if (outstream instanceof ChunkedOutputStream) {
- ((ChunkedOutputStream)outstream).writeClosingChunk();
+ ((ChunkedOutputStream) outstream).writeClosingChunk();
}
if ((contentLength > 0) && (total < contentLength)) {
throw new IOException("Unexpected end of input stream after "
@@ -472,25 +445,20 @@
/**
* Override method of {@link org.apache.commons.httpclient.HttpMethodBase}
* to clear my request body.
- *
- * @since 2.0
*/
public void recycle() {
LOG.trace("enter EntityEnclosingMethod.recycle()");
- super.recycle();
this.requestContentLength = CONTENT_LENGTH_AUTO;
this.requestBodyStream = null;
this.buffer = null;
this.repeatCount = 0;
+ super.recycle();
}
/**
* Buffers the request body and calculates the content length. If the
* method was called earlier it returns immediately.
- *
- * @since 2.0
*/
-
protected void bufferContent() {
LOG.trace("enter EntityEnclosingMethod.bufferContent()");
@@ -510,9 +478,7 @@
this.buffer = tmp.toByteArray();
this.requestBodyStream = null;
} catch (IOException e) {
- if (LOG.isErrorEnabled()) {
- LOG.error(e.toString(), e );
- }
+ LOG.error(e.getMessage(), e);
this.buffer = null;
this.requestBodyStream = null;
}
1.36 +91 -79
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java
Index: PostMethod.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PostMethod.java,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -r1.35 -r1.36
--- PostMethod.java 31 Jan 2003 23:23:16 -0000 1.35
+++ PostMethod.java 1 Feb 2003 06:46:10 -0000 1.36
@@ -2,6 +2,7 @@
* $Header$
* $Revision$
* $Date$
+ *
* ====================================================================
*
* The Apache Software License, Version 1.1
@@ -65,7 +66,6 @@
import java.io.InputStream;
import java.util.Vector;
import java.util.Iterator;
-import java.util.List;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpConnection;
@@ -117,7 +117,7 @@
// -------------------------------------------------------------- Constants
/** Log object for this class. */
- private static final Log log = LogFactory.getLog(PostMethod.class);
+ private static final Log LOG = LogFactory.getLog(PostMethod.class);
/** Custom content encoding. */
private static final int CUSTOM_CONTENT = 0;
@@ -126,14 +126,18 @@
private static final int URL_ENCODED_CONTENT = 1;
/** My content encoding. */
- private int contentEnconding = CUSTOM_CONTENT;
+ private int contentEncoding = CUSTOM_CONTENT;
/** The Content-Type header for www-form-urlcoded. */
- static final Header URL_ENCODED_CONTENT_TYPE = new Header("Content-Type",
+ public static final Header URL_ENCODED_CONTENT_TYPE = new Header("Content-Type",
"application/x-www-form-urlencoded");
- /** The buffered request body consisting of <code>NameValuePair</code>s */
- protected Vector deprecated_parameters = new Vector();
+ /**
+ * The buffered request body consisting of <code>NameValuePair</code>s.
+ * @deprecated Parameters will not be buffered in the future but converted
+ * into an InputStream immeadiately.
+ */
+ private Vector deprecated_parameters = new Vector();
// ----------------------------------------------------------- Constructors
@@ -165,8 +169,8 @@
*
* @since 1.0
*/
- public PostMethod(String uti, String tempDir) {
- super(uti, tempDir);
+ public PostMethod(String uri, String tempDir) {
+ super(uri, tempDir);
}
/**
@@ -207,7 +211,7 @@
* @deprecated use {@link #setRequestBody(NameValuePair[])}.
*/
public void setParameter(String parameterName, String parameterValue) {
- log.trace("enter PostMethod.setParameter(String, String)");
+ LOG.trace("enter PostMethod.setParameter(String, String)");
removeParameter(parameterName, parameterValue);
addParameter(parameterName, parameterValue);
@@ -228,7 +232,7 @@
* {@link #getRequestBodyAsString()}.
*/
public NameValuePair getParameter(String paramName) {
- log.trace("enter PostMethod.getParameter(String)");
+ LOG.trace("enter PostMethod.getParameter(String)");
if (paramName == null) {
return null;
@@ -250,7 +254,7 @@
* Gets the parameters currently added to the PostMethod. If there are no
* parameters, a valid array is returned with zero elements. The returned
* array object contains an array of pointers to the internal data
- * members. TODO: is it ok to return internal data?
+ * members.
*
* @return An array of the current parameters
*
@@ -260,7 +264,7 @@
* {@link #getRequestBodyAsString()}.
*/
public NameValuePair[] getParameters() {
- log.trace("enter PostMethod.getParameters()");
+ LOG.trace("enter PostMethod.getParameters()");
int numPairs = deprecated_parameters.size();
Object[] objectArr = deprecated_parameters.toArray();
@@ -285,8 +289,9 @@
*
* @deprecated use {@link #setRequestBody(NameValuePair[])}.
*/
- public void addParameter(String paramName, String paramValue) {
- log.trace("enter PostMethod.addParameter(String, String)");
+ public void addParameter(String paramName, String paramValue)
+ throws IllegalArgumentException {
+ LOG.trace("enter PostMethod.addParameter(String, String)");
if ((paramName == null) || (paramValue == null)) {
throw new IllegalArgumentException(
@@ -309,8 +314,9 @@
*
* @deprecated use {@link #setRequestBody(NameValuePair[])}.
*/
- public void addParameter(NameValuePair param) {
- log.trace("enter PostMethod.addParameter(NameValuePair)");
+ public void addParameter(NameValuePair param)
+ throws IllegalArgumentException {
+ LOG.trace("enter PostMethod.addParameter(NameValuePair)");
if (param == null) {
throw new IllegalArgumentException("NameValuePair may not be null");
@@ -330,10 +336,10 @@
* @deprecated use {@link #setRequestBody(NameValuePair[])}.
*/
public void addParameters(NameValuePair[] parameters) {
- log.trace("enter PostMethod.addParameters(NameValuePair[])");
+ LOG.trace("enter PostMethod.addParameters(NameValuePair[])");
if (parameters == null) {
- log.warn("Attempt to addParameters(null) ignored");
+ LOG.warn("Attempt to addParameters(null) ignored");
} else {
for (int i = 0; i < parameters.length; i++) {
addParameter(parameters[i]);
@@ -357,8 +363,9 @@
*
* @deprecated use {@link #setRequestBody(NameValuePair[])}.
*/
- public boolean removeParameter(String paramName) {
- log.trace("enter PostMethod.removeParameter(String)");
+ public boolean removeParameter(String paramName)
+ throws IllegalArgumentException {
+ LOG.trace("enter PostMethod.removeParameter(String)");
if (paramName == null) {
throw new IllegalArgumentException(
@@ -395,8 +402,9 @@
*
* @deprecated use {@link #setRequestBody(NameValuePair[])}.
*/
- public boolean removeParameter(String paramName, String paramValue) {
- log.trace("enter PostMethod.removeParameter(String, String)");
+ public boolean removeParameter(String paramName, String paramValue)
+ throws IllegalArgumentException {
+ LOG.trace("enter PostMethod.removeParameter(String, String)");
if (paramName == null) {
throw new IllegalArgumentException("Parameter name may not be null");
@@ -416,48 +424,29 @@
return true;
}
}
- setRequestBody(getParameters());
- return false;
- }
-
- /**
- * Set an Array of parameters to be used in the POST request body
- *
- * @param parameters The array of parameters to add.
- *
- * @throws IllegalArgumentException when param parameters are null
- *
- * @since 2.0beta1
- */
- public void setRequestBody(NameValuePair[] parameters) {
- log.trace("enter PostMethod.setRequestBody(NameValuePair[])");
-
- if (parameters == null) {
- throw new IllegalArgumentException("Array of parameters may not be
null");
- }
- super.setRequestBody(generateRequestBody(parameters));
- this.contentEnconding = URL_ENCODED_CONTENT;
+ return false;
}
/**
* Encode the list of parameters into a query string.
*
- * @param params the list of query name and value
+ * @param parameters the list of query name and value
*
- * @return the query string
+ * @return url encoded form of the parameters
*
- * @since 2.0
+ * @throws IllegalArgumentException if parameters is null
*/
- protected static String generateRequestBody(NameValuePair[] parameters) {
- log.trace("enter PostMethod.generateRequestBodyAsString(NameValuePair[])");
+ protected static String generateRequestBody(NameValuePair[] parameters)
+ throws IllegalArgumentException {
+ LOG.trace("enter PostMethod.generateRequestBody(NameValuePair[])");
if (parameters == null) {
throw new IllegalArgumentException("Array of parameters may not be
null");
}
StringBuffer buff = new StringBuffer();
- for(int i = 0; i < parameters.length; i++) {
+ for (int i = 0; i < parameters.length; i++) {
if (i > 0) {
buff.append("&");
}
@@ -468,58 +457,83 @@
try {
queryName = URIUtil.encodeWithinQuery(parameter.getName());
} catch (URIException urie) {
- log.error("encoding error within query name", urie);
+ LOG.error("encoding error within query name", urie);
queryName = parameter.getName();
}
+
buff.append(queryName).append("=");
String queryValue = null;
+
try {
queryValue = URIUtil.encodeWithinQuery(parameter.getValue());
} catch (URIException e) {
- log.error("Encoding error within query value", e);
+ LOG.error("Encoding error within query value", e);
queryValue = parameter.getValue();
}
buff.append(queryValue);
}
+
return buff.toString();
}
/**
* Sets the request body to be the specified string.
- *
- * <p>
* Once this method has been invoked, the request parameters cannot be
* altered until I am {@link #recycle recycled}.
- * </p>
*
- * @param body Request body content as a string
+ * @param stringBody Request body content as a string
*
- * @since 2.0
+ * @throws IllegalArgumentException if stringBody is null
*/
- public void setRequestBody(String body) {
- log.trace("enter PostMethod.setRequestBody(String)");
+ public void setRequestBody(String stringBody)
+ throws IllegalArgumentException {
+ LOG.trace("enter PostMethod.setRequestBody(String)");
- super.setRequestBody(body);
- this.contentEnconding = CUSTOM_CONTENT;
+ if (stringBody == null) {
+ throw new IllegalArgumentException("String body not be null");
+ }
+ super.setRequestBody(stringBody);
+ this.contentEncoding = CUSTOM_CONTENT;
}
/**
* Sets the request body to be the specified inputstream.
- *
- * <p>
* Once this method has been invoked, the request parameters cannot be
* altered until I am {@link #recycle recycled}.
- * </p>
*
- * @param body Request body content as {@link java.io.InputStream}
+ * @param streamBody Request body content as {@link java.io.InputStream}
*
- * @since 2.0
+ * @throws IllegalArgumentException if streamBody is null
*/
- public void setRequestBody(InputStream body) {
- log.trace("enter PostMethod.getRequestBody(InputStream)");
+ public void setRequestBody(InputStream streamBody)
+ throws IllegalArgumentException {
+ LOG.trace("enter PostMethod.setRequestBody(InputStream)");
- super.setRequestBody(body);
- this.contentEnconding = CUSTOM_CONTENT;
+ if (streamBody == null) {
+ throw new IllegalArgumentException("Stream body may not be null");
+ }
+ super.setRequestBody(streamBody);
+ this.contentEncoding = CUSTOM_CONTENT;
+ }
+
+ /**
+ * Set an Array of parameters to be used in the POST request body
+ *
+ * @param parametersBody The array of parameters to add.
+ *
+ * @throws IllegalArgumentException when param parameters are null
+ *
+ * @since 2.0beta1
+ */
+ public void setRequestBody(NameValuePair[] parametersBody)
+ throws IllegalArgumentException {
+ LOG.trace("enter PostMethod.setRequestBody(NameValuePair[])");
+
+ if (parametersBody == null) {
+ throw new IllegalArgumentException("Array of parameters may not be
null");
+ }
+ super.setRequestBody(generateRequestBody(parametersBody));
+ this.contentEncoding = URL_ENCODED_CONTENT;
}
/**
@@ -534,28 +548,26 @@
*
* @since 2.0
*/
-
protected void addRequestHeaders(HttpState state, HttpConnection conn)
throws IOException, HttpException {
super.addRequestHeaders(state, conn);
- if (this.contentEnconding == URL_ENCODED_CONTENT) {
+ if (this.contentEncoding == URL_ENCODED_CONTENT) {
//there are some parameters, so set the contentType header
setRequestHeader(URL_ENCODED_CONTENT_TYPE);
}
}
/**
- * Override method of {@link org.apache.commons.httpclient.HttpMethodBase}
- * to clear my request body.
+ * Prepare the method for reuse.
*
* @since 1.0
*/
public void recycle() {
- log.trace("enter PostMethod.recycle()");
- super.recycle();
- this.contentEnconding = CUSTOM_CONTENT;
+ LOG.trace("enter PostMethod.recycle()");
+ this.contentEncoding = CUSTOM_CONTENT;
this.deprecated_parameters.clear();
+ super.recycle();
}
}
1.22 +4 -3
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PutMethod.java
Index: PutMethod.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/PutMethod.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- PutMethod.java 31 Jan 2003 23:23:16 -0000 1.21
+++ PutMethod.java 1 Feb 2003 06:46:10 -0000 1.22
@@ -2,6 +2,7 @@
* $Header$
* $Revision$
* $Date$
+ *
* ====================================================================
*
* The Apache Software License, Version 1.1
1.4 +10 -7
jakarta-commons/httpclient/src/test-webapp/src/org/apache/commons/httpclient/ParamServlet.java
Index: ParamServlet.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test-webapp/src/org/apache/commons/httpclient/ParamServlet.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ParamServlet.java 23 Jan 2003 22:48:49 -0000 1.3
+++ ParamServlet.java 1 Feb 2003 06:46:10 -0000 1.4
@@ -68,7 +68,10 @@
import java.util.*;
public class ParamServlet extends MultiMethodServlet {
- protected void genericService(HttpServletRequest request, HttpServletResponse
response) throws IOException, ServletException {
+
+ protected void genericService(HttpServletRequest request, HttpServletResponse
response)
+ throws IOException, ServletException {
+
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
@@ -86,9 +89,9 @@
out.println("</p>");
out.println("<p>Parameters</p>");
- Enumeration enum = request.getParameterNames();
- while(enum.hasMoreElements()) {
- String name = (String)(enum.nextElement());
+ Enumeration e = request.getParameterNames();
+ while(e.hasMoreElements()) {
+ String name = (String)(e.nextElement());
String[] values = request.getParameterValues(name);
if(null == values || values.length < 1) {
out.println("name=\"" + name + "\";value=null<br>");
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]