[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit MimeEncodedMessageBody.java,1.1,1.2 PostM
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2001-04-02 19:07:40
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory usw-pr-cvs1:/tmp/cvs-serv29110/src/com/meterware/httpunit
Modified Files:
MimeEncodedMessageBody.java PostMethodWebRequest.java
Log Message:
Reimplement file upload w/o JavaMail
Index: MimeEncodedMessageBody.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/MimeEncodedMessageBody.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MimeEncodedMessageBody.java 2001/01/05 14:00:36 1.1
+++ MimeEncodedMessageBody.java 2001/04/02 19:07:36 1.2
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2000, Russell Gold
+* Copyright (c) 2000-2001, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
@@ -19,6 +19,7 @@
* DEALINGS IN THE SOFTWARE.
*
*******************************************************************************************************************/
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -27,19 +28,9 @@
import java.util.Dictionary;
import java.util.Enumeration;
-import javax.activation.*;
-
-import javax.mail.MessagingException;
-
-import javax.mail.internet.MimeBodyPart;
-import javax.mail.internet.MimeMultipart;
-
-
/**
* A POST-method message body which is MIME-encoded. This is used when uploading files, and is selected when the enctype
* parameter of a form is set to "multi-part/form-data".
- *
- * Note that this class is only compiled if the JavaMail and Java Activation Framework are accessible.
**/
class MimeEncodedMessageBody extends MessageBody {
@@ -53,7 +44,7 @@
* Updates the headers for this request as needed.
**/
void updateHeaders( URLConnection connection ) throws IOException {
- connection.setRequestProperty( "Content-type", getMimeBody().getContentType() );
+ connection.setRequestProperty( "Content-type", "multipart/form-data;boundary=\"" + BOUNDARY + "\"" );
}
@@ -61,43 +52,54 @@
* Transmits the body of this request as a sequence of bytes.
**/
void writeTo( OutputStream outputStream ) throws IOException {
- try {
- getMimeBody().writeTo( outputStream );
- } catch (MessagingException e) {
- throw new IOException( e.toString() );
+ for (Enumeration e = getRequest().getParameterNames(); e.hasMoreElements();) {
+ String name = (String) e.nextElement();
+ String[] values = getRequest().getParameterValues( name );
+ for (int i = 0; i < values.length; i++) {
+ writeLn( outputStream, "--" + BOUNDARY );
+ writeLn( outputStream, "Content-Disposition: form-data; name=\"" + name + '"' ); // XXX need to handle non-ascii names here
+ writeLn( outputStream, "Content-Type: text/plain; charset=" + getRequest().getCharacterSet() );
+ writeLn( outputStream, "" );
+ writeLn( outputStream, values[i], getRequest().getCharacterSet() );
+ }
}
+
+ Dictionary files = getRequest().getSelectedFiles();
+ for (Enumeration e = files.keys(); e.hasMoreElements();) {
+ String name = (String) e.nextElement();
+ WebRequest.UploadFileSpec spec = (WebRequest.UploadFileSpec) files.get( name );
+ writeLn( outputStream, "--" + BOUNDARY );
+ writeLn( outputStream, "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + spec.getFile().getName() + '"' ); // XXX need to handle non-ascii names here
+// writeLn( outputStream, "Content-Type: application/octet-stream" ); // XXX want to support real content types
+ writeLn( outputStream, "" );
+
+ FileInputStream in = new FileInputStream( spec.getFile() );
+ byte[] buffer = new byte[8 * 1024];
+ int count = 0;
+ do {
+ outputStream.write( buffer, 0, count );
+ count = in.read( buffer, 0, buffer.length );
+ } while (count != -1);
+
+ in.close();
+ writeLn( outputStream, "" );
+ }
+ writeLn( outputStream, "--" + BOUNDARY + "--" );
}
- private MimeMultipart _mimeMultipart;
+ private final static String BOUNDARY = "--HttpUnit-part0-aSgQ2M";
+ private final static byte[] CRLF = { 0x0d, 0x0A };
- private MimeMultipart getMimeBody() throws IOException {
- if (_mimeMultipart == null) {
- try {
- _mimeMultipart = new MimeMultipart( "form-data" );
- for (Enumeration e = getRequest().getParameterNames(); e.hasMoreElements();) {
- String name = (String) e.nextElement();
- MimeBodyPart mbp = new MimeBodyPart();
- mbp.setDisposition( "form-data; name=\"" + name + '"' ); // XXX need to handle non-ascii names here
- mbp.setText( getRequest().getParameter( name ), getRequest().getCharacterSet() );
- _mimeMultipart.addBodyPart( mbp );
- }
-
- Dictionary files = getRequest().getSelectedFiles();
- for (Enumeration e = files.keys(); e.hasMoreElements();) {
- String name = (String) e.nextElement();
- WebRequest.UploadFileSpec spec = (WebRequest.UploadFileSpec) files.get( name );
- FileDataSource fds = new FileDataSource( spec.getFile() );
- MimeBodyPart mbp = new MimeBodyPart();
- mbp.setDisposition( "form-data; name=\"" + name + "\"; filename=\"" + spec.getFile().getName() + '"' ); // XXX need to handle non-ascii names here
- mbp.setDataHandler( new DataHandler( fds ) );
- _mimeMultipart.addBodyPart( mbp );
- }
- } catch (MessagingException e) {
- throw new IOException( e.toString() );
- }
- }
- return _mimeMultipart;
+
+ private void writeLn( OutputStream os, String value, String encoding ) throws IOException {
+ os.write( value.getBytes( encoding ) );
+ os.write( CRLF );
+ }
+
+
+ private void writeLn( OutputStream os, String value ) throws IOException {
+ writeLn( os, value, getRequest().getCharacterSet() );
}
}
Index: PostMethodWebRequest.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/PostMethodWebRequest.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- PostMethodWebRequest.java 2001/01/05 14:00:36 1.9
+++ PostMethodWebRequest.java 2001/04/02 19:07:36 1.10
@@ -2,7 +2,7 @@
/********************************************************************************************************************
* $Id$
*
-* Copyright (c) 2000, Russell Gold
+* Copyright (c) 2000-2001, Russell Gold
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
@@ -107,73 +107,11 @@
private MessageBody newMessageBody() {
if (isMimeEncoded()) {
- return newMimeEncodedMessageBody( this );
+ return new MimeEncodedMessageBody( this );
} else {
return new URLEncodedMessageBody( this );
}
}
-
-
- private static Constructor _bodyConstructor;
-
-
- static private MessageBody newMimeEncodedMessageBody( PostMethodWebRequest request ) {
- try {
- return (MessageBody) getMimeEncodedMessageBodyConstructor().newInstance( new Object[] { request } );
- } catch (IllegalAccessException e) {
- throw new RuntimeException( "Programming error: no access to desired message body constructor. Please report this problem." );
- } catch (InstantiationException e) {
- throw new RuntimeException( "Programming error: message body class is abstract. Please report this problem." );
- } catch (InvocationTargetException e) {
- if (e.getTargetException() instanceof RuntimeException) {
- throw (RuntimeException) e.getTargetException();
- } else if (e.getTargetException() instanceof Error) {
- throw (Error) e.getTargetException();
- } else {
- throw new RuntimeException( "Error during construction of MimeEncodedMessageBody: " + e.getTargetException() );
- }
-
- }
- }
-
-
- static private Constructor getMimeEncodedMessageBodyConstructor() {
- if (_bodyConstructor == null) {
- try {
- confirmJavaMail();
- confirmJavaActivationFramework();
- Class bodyClass = Class.forName( "com.meterware.httpunit.MimeEncodedMessageBody" );
- _bodyConstructor = bodyClass.getConstructor( new Class[] { PostMethodWebRequest.class } );
- } catch (ClassNotFoundException e) {
- throw new RuntimeException( "Multi-part form support was not compiled.\n" +
- "Please rebuild HttpUnit or obtain a version which has this capability included." );
- } catch (NoSuchMethodException e) {
- throw new RuntimeException( "Programming error: cannot find desired message body constructor. Please report this problem." );
- }
- }
- return _bodyConstructor;
- }
-
-
- static private void confirmJavaMail() {
- try {
- Class.forName( "javax.mail.MessagingException" );
- } catch (ClassNotFoundException e) {
- throw new RuntimeException( "Multi-part form support requires the Java Mail and Java Activation Framework extensions.\n" +
- "The Java Mail extension is not in your classpath." );
- }
- }
-
-
- static private void confirmJavaActivationFramework() {
- try {
- Class.forName( "javax.activation.DataSource" );
- } catch (ClassNotFoundException e) {
- throw new RuntimeException( "Multi-part form support requires the Java Mail and Java Activation Framework extensions.\n" +
- "The Java Activation Framework extension is not in your classpath." );
- }
- }
-
}
|