vmassol 01/10/20 04:12:58
Modified: build build.properties.sample
docs/framework/xdocs changes.xml todo.xml
src/framework/share/org/apache/cactus WebRequest.java
src/framework/share/org/apache/cactus/client
HttpClientHelper.java
src/sample/share/org/apache/cactus/sample/unit
TestServletTestCase2.java
Log:
added new WebRequest.setUserData(InputStream) to send arbitrary data in the request
body
Note: the data is not encoded automatically by Cactus at this point. Do we need to
encode it automatically ?
Revision Changes Path
1.15 +6 -2 jakarta-cactus/build/build.properties.sample
Index: build.properties.sample
===================================================================
RCS file: /home/cvs/jakarta-cactus/build/build.properties.sample,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- build.properties.sample 2001/09/16 11:15:06 1.14
+++ build.properties.sample 2001/10/20 11:12:58 1.15
@@ -42,10 +42,14 @@
#resin.home.12 = f:/applis/resin-1.2.8
#tomcat.home.32 = f:/applis/jakarta-tomcat-3.2.3
#weblogic.home.51 = e:/weblogic
-#orion.home.14 = f:/applis/orion-1.4.5
#orion.home.15 = f:/applis/orion-1.5.2
#resin.home.20 = f:/applis/resin-2.0.s010820
#tomcat.home.40 = f:/applis/jakarta-tomcat-4.0-b7
#resin.home.13 = f:/applis/resin-1.3.s010125
#tomcat.home.33 = f:/applis/jakarta-tomcat-3.3-b2
-#enhydra.home.31 = xxx
\ No newline at end of file
+#enhydra.home.31 = xxx
+
+# Note: There seems to be a bug in Orion 1.4 preventing it to run correctly
+# the testSendUserData() test. It runs fine in Orion 1.5 however.
+
+#orion.home.14 = f:/applis/orion-1.4.5
1.57 +9 -1 jakarta-cactus/docs/framework/xdocs/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/docs/framework/xdocs/changes.xml,v
retrieving revision 1.56
retrieving revision 1.57
diff -u -r1.56 -r1.57
--- changes.xml 2001/10/20 10:21:58 1.56
+++ changes.xml 2001/10/20 11:12:58 1.57
@@ -119,8 +119,16 @@
<release version="1.3 in CVS">
<action dev="VMA" type="add">
+ Added new method : <code>WebRequest.setUserData(InputStream)</code> to
+ allow sending arbitrary data in the request body. Note that calling this
+ method will invalidate any parameters that were set to be sent in the
+ request body.
+ </action>
+ <action dev="VMA" type="add">
Added support for testing code which uses
- <code>HttpServletRequest.getReader()</code>.
+ <code>HttpServletRequest.getReader()</code>. Note: there seems to be
+ a bug in Orion 1.4 for using <code>getReader()</code>. It runs fine
+ on Orion 1.5 however.
</action>
<action dev="VMA" type="remove">
The <code>WebRequest.setMethod()</code> has been removed as deprecating
1.63 +0 -10 jakarta-cactus/docs/framework/xdocs/todo.xml
Index: todo.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/docs/framework/xdocs/todo.xml,v
retrieving revision 1.62
retrieving revision 1.63
diff -u -r1.62 -r1.63
--- todo.xml 2001/10/19 21:10:46 1.62
+++ todo.xml 2001/10/20 11:12:58 1.63
@@ -97,16 +97,6 @@
Willkomm</link>.
</action>
<action>
- Add an API in <code>WebRequest</code> to send data to the URL
- connection output stream. This is, for example, to easily test code
- that upload files to a servlet. This would need managing
- "multipart/form-data" MIME type in the HTTP connection. A
- convenience method, like
- <code>WebRequest.uploadFile(InputStream)</code> should be added. We
- should wait till Cactus moves to using commons-httpclient for HTTP
- connections (in replacement of <code>HttpURLConnection</code>).
- </action>
- <action>
Design and implement a mechanism for better supporting unit testing of
EJBs (they are currently supported). There are several possible
mechanisms :
1.7 +25 -1
jakarta-cactus/src/framework/share/org/apache/cactus/WebRequest.java
Index: WebRequest.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/src/framework/share/org/apache/cactus/WebRequest.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- WebRequest.java 2001/10/19 23:06:34 1.6
+++ WebRequest.java 2001/10/20 11:12:58 1.7
@@ -54,6 +54,7 @@
package org.apache.cactus;
import java.util.*;
+import java.io.*;
import org.apache.cactus.util.*;
@@ -81,7 +82,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
- * @version $Id: WebRequest.java,v 1.6 2001/10/19 23:06:34 vmassol Exp $
+ * @version $Id: WebRequest.java,v 1.7 2001/10/20 11:12:58 vmassol Exp $
*/
public class WebRequest
{
@@ -124,6 +125,29 @@
* Automatic session creation flag (default is true).
*/
private boolean isAutomaticSession = true;
+
+ /**
+ * Binary data to send in the request body (if any)
+ */
+ private InputStream dataStream;
+
+ /**
+ * Allow the user to send arbitrary data in the request body
+ *
+ * @param theDataStream the stream on which the data are put by the user
+ */
+ public void setUserData(InputStream theDataStream)
+ {
+ this.dataStream = theDataStream;
+ }
+
+ /**
+ * @return the data stream set up by the user
+ */
+ public InputStream getUserData()
+ {
+ return this.dataStream;
+ }
/**
* @param isAutomaticSession whether the redirector servlet will
1.13 +66 -22
jakarta-cactus/src/framework/share/org/apache/cactus/client/HttpClientHelper.java
Index: HttpClientHelper.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/src/framework/share/org/apache/cactus/client/HttpClientHelper.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- HttpClientHelper.java 2001/10/19 23:06:35 1.12
+++ HttpClientHelper.java 2001/10/20 11:12:58 1.13
@@ -70,7 +70,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
- * @version $Id: HttpClientHelper.java,v 1.12 2001/10/19 23:06:35 vmassol Exp $
+ * @version $Id: HttpClientHelper.java,v 1.13 2001/10/20 11:12:58 vmassol Exp $
*/
public class HttpClientHelper
{
@@ -131,7 +131,10 @@
// 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 (theRequest.getParameterNamesPost().hasMoreElements()) {
+ // - If user data has been specified, then we are doing a POST
+ if (theRequest.getParameterNamesPost().hasMoreElements() ||
+ (theRequest.getUserData() != null)) {
+
connection.setDoOutput(true);
} else {
connection.setDoOutput(false);
@@ -145,8 +148,13 @@
// Add the cookies
addCookies(theRequest, connection);
- // Add the POST parameters
- addParametersPost(theRequest, connection);
+ // Add the POST parameters if no user data has been specified (user data
+ // overried post parameters)
+ if (theRequest.getUserData() != null) {
+ addUserData(theRequest, connection);
+ } else {
+ addParametersPost(theRequest, connection);
+ }
// Open the connection and get the result
connection.connect();
@@ -156,6 +164,33 @@
}
/**
+ * Add user data in the request body.
+ *
+ * @param theRequest the request containing all data to pass to the server
+ * redirector.
+ * @param theConnection the HTTP connection
+ */
+ private void addUserData(WebRequest theRequest,
+ URLConnection theConnection) throws Throwable
+ {
+ // If no user data, then exit
+ if (theRequest.getUserData() == null) {
+ return;
+ }
+
+ OutputStream out = getConnectionStream(theConnection);
+ InputStream stream = theRequest.getUserData();
+
+ byte[] buffer = new byte[2048];
+ int length;
+ while ((length = stream.read(buffer)) != -1) {
+ out.write(buffer, 0, length);
+ }
+
+ out.close();
+ }
+
+ /**
* Add the HTTP parameters that need to be passed in the query string of
* the URL.
*
@@ -232,26 +267,9 @@
if (!theRequest.getParameterNamesPost().hasMoreElements()) {
return;
}
-
- PrintWriter out;
- try {
- out = new PrintWriter(theConnection.getOutputStream());
- } catch (ConnectException e) {
- // Cannot connect to server, try to explain why ...
- String reason = "Cannot connect to URL [" + theConnection.getURL() +
- "]. Reason : [" + e.getMessage() + "]\r\n";
- reason += "Possible reasons :\r\n";
- reason += "\t- The server is not running,\r\n";
- reason += "\t- The server redirector is not correctly mapped in " +
- "web.xml,\r\n";
- reason += "\t- Something else ... !";
-
- throw new Exception(reason);
- }
-
+ PrintWriter out = new PrintWriter(getConnectionStream(theConnection));
StringBuffer queryString = new StringBuffer();
-
Enumeration keys = theRequest.getParameterNamesPost();
if (keys.hasMoreElements()) {
@@ -281,6 +299,32 @@
out.print(queryString.toString());
out.close();
+ }
+
+ /**
+ * @param theConnection the HTTP connection
+ * @return an output stream to write in the request body
+ */
+ private OutputStream getConnectionStream(URLConnection theConnection)
+ throws Throwable
+ {
+ OutputStream out;
+ try {
+ out = theConnection.getOutputStream();
+ } catch (IOException e) {
+ // Cannot connect to server, try to explain why ...
+ String reason = "Cannot connect to URL [" + theConnection.getURL() +
+ "]. Reason : [" + e.getMessage() + "]\r\n";
+ reason += "Possible reasons :\r\n";
+ reason += "\t- The server is not running,\r\n";
+ reason += "\t- The server redirector is not correctly mapped in " +
+ "web.xml,\r\n";
+ reason += "\t- Something else ... !";
+
+ throw new Exception(reason);
+ }
+
+ return out;
}
/**
1.19 +33 -1
jakarta-cactus/src/sample/share/org/apache/cactus/sample/unit/TestServletTestCase2.java
Index: TestServletTestCase2.java
===================================================================
RCS file:
/home/cvs/jakarta-cactus/src/sample/share/org/apache/cactus/sample/unit/TestServletTestCase2.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- TestServletTestCase2.java 2001/10/20 10:18:06 1.18
+++ TestServletTestCase2.java 2001/10/20 11:12:58 1.19
@@ -77,7 +77,7 @@
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Massol</a>
*
- * @version $Id: TestServletTestCase2.java,v 1.18 2001/10/20 10:18:06 vmassol Exp $
+ * @version $Id: TestServletTestCase2.java,v 1.19 2001/10/20 11:12:58 vmassol Exp $
*/
public class TestServletTestCase2 extends ServletTestCase
{
@@ -619,6 +619,38 @@
}
assertEquals("test=something", body.toString());
+ }
+
+ //-------------------------------------------------------------------------
+
+ /**
+ * Verify that we can send arbitrary data in the request body.
+ *
+ * @param theRequest the request object that serves to initialize the
+ * HTTP connection to the server redirector.
+ */
+ public void beginSendUserData(WebRequest theRequest)
+ {
+ ByteArrayInputStream bais = new ByteArrayInputStream(
+ "some data to send in the body".getBytes());
+
+ theRequest.setUserData(bais);
+ }
+
+ /**
+ * Verify that we can send arbitrary data in the request body.
+ */
+ public void testSendUserData() throws Exception
+ {
+ String buffer;
+ StringBuffer body = new StringBuffer();
+
+ BufferedReader reader = request.getReader();
+ while ((buffer = reader.readLine()) != null) {
+ body.append(buffer);
+ }
+
+ assertEquals("some data to send in the body", body.toString());
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]