[Zope] Posting a document to Zope via a Java Program

D Bamud nougain@cad.delhi.nic.in
Thu, 10 Oct 2002 18:46:00 +0530


My idea is to submit a document using a Java Client and not the browser. I
could do this successfully using my *JAVA HTTP Client program* written by
me. The server I used is Tomcat4.0.3 and a JSP program Upload.jsp (that uses
com.orelly.servlet.MultiPart). If it works fine here it should work fine
with any HTTP server including Zope. But when I try "Zope" server (with
program http://localhost:8080/Examples/FileLibrary/addFile) it gives me a
500 error. I am posting the error message below if you can give some clue...
(I used the specs from RFC1867)

null: HTTP/1.1 500 Internal Server Error
Server: Zope/(Zope 2.5.1 (binary release, python 2.1, win32-x86), python
2.1.3, win32) ZServer/1.1b1
Date: Thu, 10 Oct 2002 12:57:34 GMT
Bobo-Exception-File: C:\Zope\bin\lib\cgi.py
Content-Type: text/html
Bobo-Exception-Type: ValueError
Bobo-Exception-Value: bobo exception
Etag:
Content-Length: 1704
Bobo-Exception-Line: 603

getRequestMethod   : POST
getResponseCode    : 500
getResponseMessage : Internal Server Error


---------------------------------------------------------
Here is my entire working Java HTTP Client program
---------------------------------------------------------
import java.io.*;
import java.net.*;

/**
*    RFC 1867
*    ========
*        Content-type: multipart/form-data, boundary=AaB03x
*
*        --AaB03x
*        content-disposition: form-data; name="field1"
*
*        Joe Blow
*        --AaB03x
*        content-disposition: form-data; name="pics"; filename="file1.txt"
*        Content-Type: text/plain
*
*         ... contents of file1.txt ...
*        --AaB03x--
*/
public class PostDocument {
   public static void main(String[] args) throws Exception {
       //
       // CONSTANTS
       //
       String url = "http://localhost:8080/Examples/FileLibrary/addFile";
// <--- Zope/Python
       // String url =
"http://localhost:8080/rcn/jsp/UploadFile.jsp?action=upload"; // <---
Tomcat/JSP
       String docPath = "D:\\rcn\\java\\HttpURLConnection\\testdoc.txt";
       String bndry = "AaB03x";
       String paramName = "file";
       String fileName = "testdoc.txt";

       //
       // CREATE AN HTTP CONNECTION
       //
       HttpURLConnection httpcon = (HttpURLConnection) ((new
URL(url).openConnection()));
       httpcon.setDoOutput(true);
       httpcon.setUseCaches(false); // ??? Not Required?
       httpcon.setRequestMethod("POST");
       httpcon.setRequestProperty("Content-type", "multipart/form-data,
boundary=" +bndry); // this is new line
       httpcon.connect();

       //
       // OPEN THE READ AND WRITE STREAMS
       //
       System.out.println("Posting " +docPath +"...");
       File file = new File(docPath);
       FileInputStream is = new FileInputStream(file);
       OutputStream os = httpcon.getOutputStream();

       //
       // WRITE THE FIRST/START BOUNDARY
       //
       String disptn = "--" +bndry +"\r\ncontent-disposition: form-data;
name=\"" +paramName +"\"; filename=\"" +fileName +"\"\r\nContent-Type:
text/plain\r\n\r\n";
       System.out.print(disptn);
       os.write(disptn.getBytes());

       //
       // WRITE THE FILE CONTENT
       //
       byte[] buffer = new byte[4096];
       int bytes_read;
       while((bytes_read = is.read(buffer)) != -1) {
           os.write(buffer, 0, bytes_read);
           System.out.print(new String(buffer, 0, bytes_read));
       }

       //
       // WRITE THE CLOSING BOUNDARY
       //
       String boundar = "\r\n--" +bndry +"--";
       System.out.print(boundar);
       os.write(boundar.getBytes()); // another 2 new lines

       //
       // FLUSH / CLOSE THE STREAMS
       //
       os.flush();
       os.close();
       is.close();

       // DEBUG
       System.out.println("\n....Done!!!...\n\n");
       dump(httpcon);
   }

   public static void dump(HttpURLConnection httpcon) throws IOException {
       int n=0; // n=0 has no key, and the HTTP return status in the value
field
       String headerKey;
       String headerVal;

       while (true){
           headerKey = httpcon.getHeaderFieldKey(n);
           headerVal = httpcon.getHeaderField(n);

           if (headerKey != null || headerVal != null) {
               System.out.println(headerKey +": " +headerVal);
           }
           else {
               break;
           }

           n++;
       }

       System.out.println();
       System.out.println("getRequestMethod   : "
+httpcon.getRequestMethod());
       System.out.println("getResponseCode    : "
+httpcon.getResponseCode());
       System.out.println("getResponseMessage : "
+httpcon.getResponseMessage());
   }
}