package com.silverwrist.util.test;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log4j.*;
import com.silverwrist.util.*;

public class FormDataTest extends HttpServlet
{
  private static Category logger = Category.getInstance(FormDataTest.class.getName());

  public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<HTML><HEAD><TITLE>Form Test</TITLE></HEAD><BODY>");
    out.println("<H1>Form Test</H1>");
    out.println("<FORM METHOD=POST ENCTYPE=\"multipart/form-data\" ACTION=\"/venice/testformdata\">");
    out.println("Text field: <INPUT TYPE=TEXT NAME=\"text\" VALUE=\"foo\" SIZE=32 MAXLENGTH=128><P>");
    out.println("File field: <INPUT TYPE=FILE NAME=\"file\" SIZE=32 MAXLENGTH=255><P>");
    out.println("<INPUT TYPE=SUBMIT VALUE=\"sumbit\">");
    out.println("</FORM></BODY></HTML>");

  } // end doGet

  public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
  {
    final String save_dir = "/home/erbo";
    String errmsg = null;
    ServletMultipartHandler smh = null;
    boolean file_check = false;
    String text_value = null;
    String file_name = null;
    String file_type = null;
    int file_size = -1;

    try
    { // create the multipart handler
      smh = new ServletMultipartHandler(request);

      // retrieve the text parameter value
      text_value = smh.getValue("text");
      if (logger.isDebugEnabled())
	logger.debug("text param value = \"" + text_value + "\"");

      // check to make sure the file parameter is really a file!
      file_check = smh.isFileParam("file");
      if (file_check)
      { // get the file name and type
	file_name = smh.getValue("file");
	file_type = smh.getContentType("file");
	file_size = smh.getContentSize("file");
	if (logger.isDebugEnabled())
	  logger.debug("file param = \"" + file_name + "\", ctype = " + file_type + ", size = " + file_size);

	if ((file_name!=null) && (file_name.length()>0))
	{ // build the file name and open a stream to save it
	  String real_name = save_dir + "/" + file_name;
	  FileOutputStream stm = new FileOutputStream(real_name);
	  InputStream in = smh.getFileContentStream("file");
	  byte[] copybuf = new byte[1024];

	  // go into a copy loop
	  int ct = in.read(copybuf);
	  int nbytes = 0;
	  while (ct>=0)
	  { // copy loop...
	    if (logger.isDebugEnabled())
	      logger.debug("read blksize = " + ct);
	    if (ct>0)
	    { // write out copy buffer data
	      stm.write(copybuf,0,ct);
	      nbytes += ct;

	    } // end if

	    ct = in.read(copybuf);
	    
	  } // end while

	  // all done - file saved
	  stm.close();
	  in.close();
	  if (nbytes!=file_size)
	    logger.warn("YIKES! expected " + file_size + " bytes but got " + nbytes + "!");
	  if (logger.isDebugEnabled())
	    logger.debug("saved " + nbytes + " bytes to file " + real_name);

	} // end if

      } // end if
      else  // this shouldn't happen
	logger.warn("file param was not a file?!?!?!?");

    } // end try
    catch (ServletMultipartException e)
    { // load error message
      logger.error("caught ServletMultipartException: " + e.getMessage(),e);
      errmsg = e.getMessage();

    } // end catch

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<HTML><HEAD><TITLE>Form Test Results</TITLE></HEAD><BODY>");
    out.println("<H1>Form Test Results</H1>");
    if (errmsg==null)
    { // print data
      out.println("Text value: " + text_value + "<P>");
      if (file_check)
      { // get file name and type
	out.println("File name: " + file_name + "<P>");
	out.println("File type: " + file_type + "<P>");
	out.println("File size: " + file_size + " bytes<P>");
	out.println("Saved to directory: " + save_dir + "<P>");

      } // end if
      else
	out.println("Not a file.<P>");

    } // end if
    else
      out.println("ERROR: " + errmsg + "<P>");
    out.println("<A HREF=\"testformdata\">Go back.</A>");
    out.println("</BODY></HTML>");

  } // end doPost

} // end class FormDataTest
