/**
  * Carries out processing for a HTTP transaction.
  *
  * @see #starter(ThisPage)
  * @see #processor(ThisPage)
  * @see #ender(ThisPage)
  * @param requestValue Request object
  * @param responseValue Response object
  * @throws IOException
  */
 public void service(HttpServletRequest requestValue, HttpServletResponse responseValue)
     throws IOException {
   ThisPage thisPage = new ThisPage(requestValue, responseValue, getConfig());
   starter(thisPage);
   if (thisPage.getTerminateRequest()) {
     return;
   }
   processor(thisPage);
   if (thisPage.getTerminateRequest()) {
     return;
   }
   ender(thisPage);
   if (thisPage.getTerminateRequest()) {
     return;
   }
   String address = thisPage.getRedirectAddress();
   if (address != null) {
     thisPage.getResponse().sendRedirect(thisPage.getResponse().encodeRedirectURL(address));
   }
 }
Example #2
0
  /**
   * Process the call to the servlet.
   *
   * <p>With regard to reading data from the request, a {@link javax.servlet.ServletInputStream}
   * object for binary data can be obtained by {@link HttpServletRequest#getInputStream()} or a
   * {@link java.io.BufferedReader} can be obtained for character data by using {@link
   * HttpServletRequest#getReader}.
   *
   * <p>With regard to writing data to the response a {@link javax.servlet.ServletOutputStream}
   * object for binary data can be obtained using {@link HttpServletResponse#getOutputStream()}
   * while a {@link java.io.PrintWriter} object can be obtained using {@link
   * HttpServletResponse#getWriter()}.
   *
   * <p>The {@link java.io.ByteArrayOutputStream} class can be used as a means of collecting the
   * bytes contained in the attached file.
   *
   * <p>It would be desirable to have tests for enctype and method.
   *
   * @param req Request object
   * @param res Response object
   * @throws IOException if io problems
   */
  public void service(HttpServletRequest req, HttpServletResponse res) throws IOException {
    ThisPage thisPage = new ThisPage(req, res, config);
    GenericPrinter output = thisPage.getPrinter();
    if (!req.getMethod().equalsIgnoreCase("post")) {
      output.println("<html><head>");
      output.println("<title>Must use POST method</title>");
      output.println("</head><body>");
      output.println("<h1>Must use POST method</h1>");
      output.println("<p>Request used " + req.getMethod() + " method</p>");
      output.println("<p>Must use POST method</p>");
      output.println("</body></html>");
      thisPage.sendContents();
      return;
    } else if (req.getHeader("content-type") == null) {
      output.println("<html><head>");
      output.println("<title>Missing content-type header</title>");
      output.println("</head><body>");
      output.println("<h1>Missing content-type header</h1>");
      output.println(
          "<p>Must use content-type header " + "to specify multipart/form-data encoding</p>");
      output.println("</body></html>");
      thisPage.sendContents();
      return;
    } else if (!req.getHeader("content-type").toLowerCase().startsWith("multipart/form-data")) {
      output.println("<html><head>");
      output.println("<title>Must use multipart/form-data encoding</title>");
      output.println("</head><body>");
      output.println("<h1>Must use multipart/form-data encoding</h1>");
      output.println("<p>content-type is " + req.getHeader("content-type") + " </p>");
      output.println("<p>Must use multipart/form-data encoding</p>");
      output.println("</body></html>");
      thisPage.sendContents();
      return;
    }
    String boundary = extractBoundary(req);
    if (boundary == null) {
      thisPage.addMessage("Unable to extract boundary value");
      thisPage.addMessage(req.getHeader("content-type"));
      thisPage.errorMessage();
      return;
    }
    int counter = 0;
    byte buffer[] = new byte[4096];
    byte extract[];
    int bytesRead = -1;
    ServletInputStream input = req.getInputStream();
    bytesRead = input.readLine(buffer, 0, buffer.length);
    if (!new String(buffer, 0, bytesRead, "ISO8859_1").startsWith("--" + boundary)) {
      thisPage.addMessage(
          "Should be separator "
              + "--"
              + boundary
              + " : "
              + " found "
              + new String(buffer, 0, bytesRead));
      thisPage.errorMessage();
      return;
    }
    while (true) {
      counter++;
      Contents part = new Contents();
      thisPage.addMessage("Starting part " + Integer.toString(counter) + " of form");
      while (true) {

        bytesRead = input.readLine(buffer, 0, buffer.length);
        if (bytesRead < 0) {
          thisPage.addMessage("Unexpected end of packet");
          thisPage.errorMessage();
          return;
        } else {
          String value = new String(buffer, 0, bytesRead, "ISO8859_1");
          if (value.endsWith("\r\n")) {
            value = stripEOL(value);
          }
          if (value.length() == 0) {
            extract = readPart(input, thisPage, part.getTransferEncoding());
            if (thisPage.getTerminateRequest()) {
              return;
            }
            part.setContents(extract);
            thisPage.addElement(part);
            break;
          } else {
            thisPage.addMessage("service method - Parsing line: " + value);
            part.parseLine(value);
          }
        }
      }
      if (thisPage.getEndOfPacket()) {
        break;
      }
    }
    if (thisPage.getTerminateRequest()) {
      return;
    }
    starter(thisPage);
    if (thisPage.getTerminateRequest()) {
      return;
    }
    processor(thisPage);
    if (thisPage.getTerminateRequest()) {
      return;
    }
    ender(thisPage);
    if (thisPage.getTerminateRequest()) {
      return;
    }
    if (thisPage.getRedirectAddress() != null) {
      thisPage
          .getResponse()
          .sendRedirect(thisPage.getResponse().encodeRedirectURL(thisPage.getRedirectAddress()));
    }
  }