Ejemplo n.º 1
0
 /**
  * This method generates the web page based on the contents of the request packet.
  *
  * <p>This method will be overwritten to enable various actions to take place as the information
  * is uploaded.
  *
  * <p>For the result of File requests, write the contents to a file.
  *
  * <p>I am having a problem with the loading of binary files.
  *
  * @param thisPage Information on this HTTP request
  * @throws IOException if io errors
  */
 protected void processor(ThisPage thisPage) throws IOException {
   Enumeration<String> keys = thisPage.getPartNames();
   HttpServletRequest req = thisPage.getRequest();
   ServletConfig config = thisPage.getConfig();
   String targetDirectory = config.getInitParameter("directory");
   GenericPrinter output = thisPage.getPrinter();
   output.println("<html><head>");
   output.println("<title>Dummy Upload Program</title>");
   output.println("</head><body>");
   output.println("<p>It is assumed that the processor method of the ");
   output.println("bradleyross.library.servlets.UploadServlet class will ");
   output.println("be overridden to provide the desired function.  This ");
   output.println("sample version is designed for testing the applications ");
   output.println("and to allow demonstration of the capabilities.</p>");
   output.println("<p>Target directory for upload tests is " + targetDirectory + "</p>");
   output.println("<h2>Headers</h2>");
   output.println("<table border=\"1\">");
   Enumeration<?> list1 = req.getHeaderNames();
   while (list1.hasMoreElements()) {
     String name = (String) list1.nextElement();
     Enumeration<?> list2 = req.getHeaders(name);
     while (list2.hasMoreElements()) {
       String value = (String) list2.nextElement();
       output.println("<tr><td>" + name + "</td><td>" + value + "</td></tr>");
     }
   }
   output.println("</table>");
   output.println("<h2>Parts of form</h2>");
   output.println("<p>The following are the parts of the multipart form</p>");
   output.println("<ul>");
   while (keys.hasMoreElements()) {
     String name = keys.nextElement();
     String mime = thisPage.getElement(name).getMime();
     String fileName = thisPage.getElement(name).getFilename();
     String encoding = thisPage.getElement(name).getTransferEncoding();
     output.println("<li><p>" + name + "</p>");
     if (fileName == null) {
       output.println("<p>Filename not specified</p>");
     } else {
       output.println("<p>Filename is " + fileName + "</p>");
     }
     if (mime == null) {
       output.println("<p>Content type not specified</p>");
       mime = new String();
     } else {
       output.println("<p>Content-type: " + mime + "</p>");
     }
     if (encoding == null) {
       output.println("<p>Transfer encoding not specified</p>");
     } else {
       output.println("<p>Content-transfer-encoding: " + encoding + "</p>");
     }
     if (mime.toUpperCase().startsWith("TEXT")) {
       output.println(
           "<p>" + StringHelpers.escapeHTML(thisPage.getElement(name).toString()) + "</p></li>");
     }
     output.println(
         "<p>Size of contents: " + Integer.toString(thisPage.getElement(name).getContentsSize()));
     if (fileName != null && targetDirectory != null) {
       try {
         boolean validEntry = true;
         File outputFile = null;
         if (targetDirectory.length() == 0 || fileName.length() == 0) {
           validEntry = false;
         }
         if (validEntry) {
           outputFile = new File(targetDirectory, fileName);
           output.println(
               "<p>Name of file is "
                   + StringHelpers.escapeHTML(outputFile.getCanonicalPath())
                   + "</p>");
         }
         if (!validEntry) {;
         } else if (outputFile == null) {
           output.println("<p>Unable to open output file</p>");
         } else {
           FileOutputStream outputStream = new FileOutputStream(outputFile);
           byte transfer[] = thisPage.getElement(name).getContents();
           if (transfer == null) {
             output.println("<p>Unable to get file contents</p>");
           } else {
             outputStream.write(thisPage.getElement(name).getContents());
           }
           outputStream.close();
         }
       } catch (IOException e) {
         output.println("<p>Error while writing file</p>");
         output.println(
             StringHelpers.escapeHTML(
                 "<p>" + e.getClass().getName() + " " + e.getMessage() + "</p>"));
       }
     } else {;
     }
   }
   output.println("</ul>");
   output.println("<h2>Messages</h2>");
   output.println(
       "<p>These messages are normally printed only if an error occurs during the processing ");
   output.println(
       "of the HTTP transaction.  They are included here to test the behavior of the servlet.</p><ol>");
   Vector<String> messages = thisPage.getMessageList();
   messages.trimToSize();
   for (int i = 0; i < messages.size(); i++) {
     output.println("<li><p>" + StringHelpers.escapeHTML(messages.elementAt(i)) + "</p></li>");
   }
   output.println("</ol>");
   output.println("</body></html>");
   thisPage.sendContents();
 }