You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/11/05 18:50:22 UTC

cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestPartsNoHost.java

jsdever     2002/11/05 09:50:22

  Modified:    httpclient/src/java/org/apache/commons/httpclient/methods
                        MultipartPostMethod.java
               httpclient/src/java/org/apache/commons/httpclient/methods/multipart
                        FilePart.java Part.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestPartsNoHost.java
  Log:
  Many fixes for MultiPartPost
  
  - Fixes the tests for the Parts classes.
  - Adds logging to FilePart and Part as well as fixing the null pointer exception in FilePart.
  - Adds logging to MultipartPostMethod and fixes the problem with it closing the connection.
  
  Patches were applied from bug:
  http://nagoya.apache.org/bugzilla/show_bug.cgi?id=14108
  
  Contributed by: Adrian Sutton
  Committed by: Jeff Dever
  
  Revision  Changes    Path
  1.3       +18 -5     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/MultipartPostMethod.java
  
  Index: MultipartPostMethod.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/MultipartPostMethod.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- MultipartPostMethod.java	11 Oct 2002 05:16:32 -0000	1.2
  +++ MultipartPostMethod.java	5 Nov 2002 17:50:22 -0000	1.3
  @@ -71,17 +71,23 @@
   import java.io.FileNotFoundException;
   import org.apache.commons.httpclient.*;
   import org.apache.commons.httpclient.methods.multipart.*;
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
   
   /**
    * POST Method for Multipart encoded forms.
    *
    * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
    * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  + * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
    *
    * @since 2.0
    */
   public class MultipartPostMethod extends GetMethod {
   
  +    /** Log object for this class. */
  +    private static final Log log = LogFactory.getLog(MultipartPostMethod.class);
  +
       private List parameters = new ArrayList();
   
       /**
  @@ -131,12 +137,14 @@
        * Clear my request body.
        */
       public void recycle() {
  +        log.trace("enter recycle()");
           super.recycle();
           parameters.clear();
       }
   
   
       public void addParameter(String parameterName, String parameterValue) {
  +        log.trace("enter addParameter(String parameterName, String parameterValue)");
           Part param = new StringPart(parameterName, parameterValue);
           parameters.add(param);
       }
  @@ -144,6 +152,7 @@
   
       public void addParameter(String parameterName, File parameterFile) 
       throws FileNotFoundException {
  +        log.trace("enter addParameter(String parameterName, File parameterFile)");
           Part param = new FilePart(parameterName, parameterFile);
           parameters.add(param);
       }
  @@ -152,11 +161,13 @@
        * Adds another part to this post.
        */
       public void addPart( Part part ) {
  +        log.trace("enter addPart(Part part)");
           parameters.add(part);
       }
   
       protected void addRequestHeaders(HttpState state, HttpConnection conn) 
       throws IOException, HttpException {
  +        log.trace("enter addRequestHeaders(HttpState state, HttpConnection conn)");
           super.addRequestHeaders(state,conn);
           
           if (! parameters.isEmpty()) 
  @@ -173,6 +184,7 @@
        */
       protected boolean writeRequestBody(HttpState state, HttpConnection conn) 
       throws IOException, HttpException {
  +        log.trace("enter writeRequestBody(HttpState state, HttpConnection conn)");
           OutputStream out = conn.getRequestOutputStream();
           
           for (Iterator it = parameters.iterator(); it.hasNext();) {
  @@ -181,9 +193,9 @@
           }
           
           Part.sendLastBoundary(out);
  +
  +        out.flush();
           
  -        out.close();
  -            
           return true;
       }
   
  @@ -196,6 +208,7 @@
        * until I am {@link #recycle recycled}.
        */
       protected int getRequestContentLength() {
  +        log.trace("enter getRequestContentLength()");
           long length = 0;
           
           try {
  
  
  
  1.4       +23 -6     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java
  
  Index: FilePart.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FilePart.java	29 Oct 2002 06:40:15 -0000	1.3
  +++ FilePart.java	5 Nov 2002 17:50:22 -0000	1.4
  @@ -70,6 +70,8 @@
   import java.io.FileNotFoundException;
   import java.io.IOException;
   import java.security.InvalidParameterException;
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
   
   
   /**
  @@ -90,6 +92,9 @@
    */
   public class FilePart extends Part {
   
  +    /** Log object for this class. */
  +    private static final Log log = LogFactory.getLog(FilePart.class);
  +
       //TODO: make this configurable
       static int MAX_BUFF_SIZE = 1 * 1024 * 1024;  // 1 MiBs
       
  @@ -164,13 +169,16 @@
           // We need to cast fileLength to an int to call mark(int) on the
           // input stream, so we must ensure that the file length will fit
           // into an int before trying to use mark and reset.
  -        this.markSupported = fileInputStream.markSupported() &&
  -            fileLength <= Integer.MAX_VALUE;
  -        this.bufferData = !markSupported;
  +        if (fileInputStream != null) {
  +            this.markSupported = fileInputStream.markSupported() &&
  +                fileLength <= Integer.MAX_VALUE;
  +            this.bufferData = !markSupported;
  +        }
       }
           
       protected void sendHeader(OutputStream out) 
       throws IOException {
  +        log.trace("enter sendHeader(OutputStream out)");
           super.sendHeader(out);
           sendFilename(out);
           sendContentType(out);
  @@ -178,6 +186,7 @@
       
       protected void sendFilename(OutputStream out) 
       throws IOException {
  +        log.trace("enter sendFilename(OutputStream out)");
           String filename = "; filename=\"" + fileName + "\"";
           out.write(filename.getBytes());
       }
  @@ -185,6 +194,7 @@
       
       protected void sendContentType(OutputStream out) 
       throws IOException {
  +        log.trace("enter sendContentType(OutputStream out)");
           out.write(CRLF_bytes);
           out.write("Content-Type: application/octet-stream".getBytes());
       }    
  @@ -195,6 +205,7 @@
       
       protected void sendData(OutputStream out) 
       throws IOException {
  +        log.trace("enter sendData(OutputStream out)");
           
           byte[] buff;
           
  @@ -203,6 +214,7 @@
               // this file contains no data, so there is nothing to send.
               // we don't want to create a zero length buffer as this will
               // cause an infinite loop when reading.
  +            log.debug("No data to send.");
               return;
               
           } else if (lengthOfData() > MAX_BUFF_SIZE) {
  @@ -217,6 +229,7 @@
           }
           if (file != null) {
               is = new FileInputStream(file);
  +            dataBuf = null;
           }
           if (dataBuf != null) {
               // Send the buffered data from a previous send.
  @@ -240,6 +253,9 @@
                   fileStream.reset();
               }
           }
  +        if (file != null) {
  +            is.close();
  +        }
       }
       
       protected long lengthOfData() 
  @@ -258,6 +274,7 @@
        * @see #getBufferData()
        */
       public void setBufferData(boolean bufferData) {
  +        log.trace("enter setBufferData(boolean bufferData)");
           this.bufferData = bufferData;
       }
   
  
  
  
  1.3       +21 -3     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/Part.java
  
  Index: Part.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/methods/multipart/Part.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Part.java	13 Oct 2002 18:57:10 -0000	1.2
  +++ Part.java	5 Nov 2002 17:50:22 -0000	1.3
  @@ -65,17 +65,23 @@
   import java.io.OutputStream;
   import java.io.ByteArrayOutputStream;
   import java.io.IOException;
  +import org.apache.commons.logging.Log;
  +import org.apache.commons.logging.LogFactory;
   
   /**
    * Abstract class for one Part of a multipart post object.
    *
    * @author <a href="mailto:mattalbright@yahoo.com">Matthew Albright</a>
    * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
  + * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
    *
    * @since 2.0
    */
   public abstract class Part {
   
  +    /** Log object for this class. */
  +    private static final Log log = LogFactory.getLog(Part.class);
  +
       static String boundary = "----------------314159265358979323846";
       static byte[] boundary_bytes = boundary.getBytes();
       static String CRLF = "\r\n";
  @@ -89,6 +95,7 @@
       
       public static void sendLastBoundary(OutputStream out)
       throws IOException {
  +        log.trace("enter sendLastBoundary(OutputStream out)");
           out.write(extra_bytes);
           out.write(boundary_bytes);
           out.write(extra_bytes);
  @@ -97,6 +104,7 @@
       
       public static int lengthOfLastBoundary()
       throws IOException {
  +        log.trace("enter lengthOfLastBoundary()");
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           
           sendLastBoundary(out);
  @@ -108,6 +116,7 @@
       
       protected void sendStart(OutputStream out) 
       throws IOException {
  +        log.trace("enter sendStart(OutputStream out)");
           out.write(extra_bytes);
           out.write(boundary_bytes);
           out.write(CRLF_bytes);
  @@ -115,6 +124,7 @@
       
       protected int lengthOfStart()
       throws IOException {
  +        log.trace("enter lengthOfStart()");
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           sendStart(out);
           return(out.size());
  @@ -122,6 +132,7 @@
       
       protected void sendHeader(OutputStream out) 
       throws IOException {
  +        log.trace("enter sendHeader(OutputStream out)");
           String content_dispos = "Content-Disposition: form-data; name=\"" 
               + getName() + "\"";
       
  @@ -130,6 +141,7 @@
       
       protected int lengthOfHeader()
       throws IOException {
  +        log.trace("enter lengthOfHeader()");
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           sendHeader(out);
           return(out.size());
  @@ -138,12 +150,14 @@
       
       protected void sendEndOfHeader(OutputStream out) 
       throws IOException {
  +        log.trace("enter sendEndOfHeader(OutputStream out)");
           out.write(CRLF_bytes);
           out.write(CRLF_bytes);
       }
       
       protected int lengthOfEndOfHeader()
       throws IOException {
  +        log.trace("enter lengthOfEndOfHeader()");
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           sendEndOfHeader(out);
           return out.size();
  @@ -156,11 +170,13 @@
       
       protected void sendEnd(OutputStream out) 
       throws IOException {
  +        log.trace("enter sendEnd(OutputStream out)");
           out.write(CRLF_bytes);
       }
       
       protected int lengthOfEnd()
       throws IOException {
  +        log.trace("enter lengthOfEnd()");
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           sendEnd(out);
           return out.size();
  @@ -172,6 +188,7 @@
        */
   
       final public void send(OutputStream out) throws IOException {
  +        log.trace("enter send(OutputStream out)");
           sendStart(out);
           sendHeader(out);
           sendEndOfHeader(out);
  @@ -180,6 +197,7 @@
       }
       
       final public long length() throws IOException {
  +        log.trace("enter length()");
           return lengthOfStart() + 
                  lengthOfHeader() + 
                  lengthOfEndOfHeader() +
  
  
  
  1.2       +8 -6      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPartsNoHost.java
  
  Index: TestPartsNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestPartsNoHost.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestPartsNoHost.java	29 Oct 2002 06:40:15 -0000	1.1
  +++ TestPartsNoHost.java	5 Nov 2002 17:50:22 -0000	1.2
  @@ -96,7 +96,7 @@
       // ------------------------------------------------------- TestCase Methods
   
       public static Test suite() {
  -        return new TestSuite(TestMethodsNoHost.class);
  +        return new TestSuite(TestPartsNoHost.class);
       }
   
       // ----------------------------------------------------------------- Tests
  @@ -145,7 +145,8 @@
   
       public void testFilePartNoBuffer() throws Exception {
           File file = createTempTestFile();
  -        FilePart part = new FilePart(NAME, file);
  +        FilePart part = new FilePart(NAME,
  +                new FileInputStream(file), file.getName(), file.length());
           part.setBufferData(false);
           ByteArrayOutputStream stream = new ByteArrayOutputStream();
           part.send(stream);
  @@ -163,6 +164,7 @@
           part.send(stream);
           String resp1 = stream.toString();
           stream = new ByteArrayOutputStream();
  +        part.send(stream);
           String resp2 = stream.toString();
           assertEquals(resp1, resp2);
       }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>