did some extra instrumentation of these files (with LOG4J), at the request of
Amos and Laurie at Narex
This commit is contained in:
parent
2e455b4bdd
commit
a258f6ee80
|
@ -25,6 +25,7 @@ import javax.activation.DataSource;
|
||||||
import javax.mail.*;
|
import javax.mail.*;
|
||||||
import javax.mail.internet.*;
|
import javax.mail.internet.*;
|
||||||
import javax.servlet.*;
|
import javax.servlet.*;
|
||||||
|
import org.apache.log4j.*;
|
||||||
|
|
||||||
// This class was built in a process I call "Java Junkyard Wars," in which I put together
|
// This class was built in a process I call "Java Junkyard Wars," in which I put together
|
||||||
// a bunch of APIs in ways that their designers would never have anticipated. It's
|
// a bunch of APIs in ways that their designers would never have anticipated. It's
|
||||||
|
@ -83,19 +84,29 @@ public class ServletMultipartHandler
|
||||||
|
|
||||||
static class MultipartDataValue implements Blob
|
static class MultipartDataValue implements Blob
|
||||||
{
|
{
|
||||||
|
private static final int BLKSIZE = 4096;
|
||||||
|
|
||||||
private byte[] actual_data; // the actual data we contain
|
private byte[] actual_data; // the actual data we contain
|
||||||
|
|
||||||
public MultipartDataValue(MimeBodyPart part) throws MessagingException, IOException
|
public MultipartDataValue(MimeBodyPart part) throws MessagingException, IOException
|
||||||
{
|
{
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
logger.debug("creating new MultipartDataValue");
|
||||||
|
|
||||||
|
// set up the streams to copy between
|
||||||
InputStream in = part.getInputStream();
|
InputStream in = part.getInputStream();
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
byte[] copybuf = new byte[4096];
|
byte[] copybuf = new byte[BLKSIZE];
|
||||||
int ct = in.read(copybuf);
|
|
||||||
|
// begin the copy loop
|
||||||
|
int ct = in.read(copybuf,0,BLKSIZE);
|
||||||
while (ct>=0)
|
while (ct>=0)
|
||||||
{ // do a simple read and write
|
{ // do a simple read and write
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
logger.debug("read blksize = " + ct);
|
||||||
if (ct>0)
|
if (ct>0)
|
||||||
out.write(copybuf,0,ct);
|
out.write(copybuf,0,ct);
|
||||||
ct = in.read(copybuf);
|
ct = in.read(copybuf,0,BLKSIZE);
|
||||||
|
|
||||||
} // end while
|
} // end while
|
||||||
|
|
||||||
|
@ -103,6 +114,9 @@ public class ServletMultipartHandler
|
||||||
actual_data = out.toByteArray();
|
actual_data = out.toByteArray();
|
||||||
out.close();
|
out.close();
|
||||||
|
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
logger.debug("finished copying, " + actual_data.length + " bytes transferred");
|
||||||
|
|
||||||
} // end constructor
|
} // end constructor
|
||||||
|
|
||||||
public long length()
|
public long length()
|
||||||
|
@ -127,6 +141,7 @@ public class ServletMultipartHandler
|
||||||
|
|
||||||
public long position(byte[] pattern, long start) throws SQLException
|
public long position(byte[] pattern, long start) throws SQLException
|
||||||
{
|
{
|
||||||
|
logger.warn("position() function is not implemented for MultipartDataValue");
|
||||||
throw new SQLException("function not implemented");
|
throw new SQLException("function not implemented");
|
||||||
|
|
||||||
} // end position
|
} // end position
|
||||||
|
@ -172,6 +187,15 @@ public class ServletMultipartHandler
|
||||||
|
|
||||||
} // end if
|
} // end if
|
||||||
|
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
{ // tell us what kind of parameter we have
|
||||||
|
if (filename!=null)
|
||||||
|
logger.debug("new file parameter \"" + name + "\" defined (filename = " + filename + ")");
|
||||||
|
else
|
||||||
|
logger.debug("new text parameter \"" + name + "\" defined");
|
||||||
|
|
||||||
|
} // end if
|
||||||
|
|
||||||
} // end constructor
|
} // end constructor
|
||||||
|
|
||||||
public String getName()
|
public String getName()
|
||||||
|
@ -201,6 +225,7 @@ public class ServletMultipartHandler
|
||||||
} // end try
|
} // end try
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{ // turn any exception returns here into null returns
|
{ // turn any exception returns here into null returns
|
||||||
|
logger.warn("parameter getValue() method threw a " + e.getClass().getName(),e);
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
} // end catch
|
} // end catch
|
||||||
|
@ -216,6 +241,7 @@ public class ServletMultipartHandler
|
||||||
} // end try
|
} // end try
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{ // just dump a null on error
|
{ // just dump a null on error
|
||||||
|
logger.warn("parameter getContentType() method threw a " + e.getClass().getName(),e);
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
} // end catch
|
} // end catch
|
||||||
|
@ -231,6 +257,7 @@ public class ServletMultipartHandler
|
||||||
} // end try
|
} // end try
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{ // just dump a -1 on error
|
{ // just dump a -1 on error
|
||||||
|
logger.warn("parameter getSize() method threw a " + e.getClass().getName(),e);
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
} // end catch
|
} // end catch
|
||||||
|
@ -244,6 +271,9 @@ public class ServletMultipartHandler
|
||||||
|
|
||||||
if (cached_value==null)
|
if (cached_value==null)
|
||||||
{ // we don't have the value cached yet
|
{ // we don't have the value cached yet
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
logger.debug("getting MultipartDataValue for parameter \"" + name + "\"");
|
||||||
|
|
||||||
try
|
try
|
||||||
{ // extract the value
|
{ // extract the value
|
||||||
cached_value = new MultipartDataValue(part);
|
cached_value = new MultipartDataValue(part);
|
||||||
|
@ -251,11 +281,13 @@ public class ServletMultipartHandler
|
||||||
} // end try
|
} // end try
|
||||||
catch (MessagingException me)
|
catch (MessagingException me)
|
||||||
{ // translate exception here
|
{ // translate exception here
|
||||||
|
logger.error("MIME parse error getting data content: " + me.getMessage(),me);
|
||||||
throw new ServletMultipartException("Error getting data content: " + me.getMessage(),me);
|
throw new ServletMultipartException("Error getting data content: " + me.getMessage(),me);
|
||||||
|
|
||||||
} // end catch
|
} // end catch
|
||||||
catch (IOException ioe)
|
catch (IOException ioe)
|
||||||
{ // translate exception here
|
{ // translate exception here
|
||||||
|
logger.error("IO error getting data: " + ioe.getMessage(),ioe);
|
||||||
throw new ServletMultipartException("Error getting data content: " + ioe.getMessage(),ioe);
|
throw new ServletMultipartException("Error getting data content: " + ioe.getMessage(),ioe);
|
||||||
|
|
||||||
} // end catch
|
} // end catch
|
||||||
|
@ -268,6 +300,13 @@ public class ServletMultipartHandler
|
||||||
|
|
||||||
} // end class MultipartParameter
|
} // end class MultipartParameter
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------------
|
||||||
|
* Static data members
|
||||||
|
*--------------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
private static Category logger = Category.getInstance(ServletMultipartHandler.class.getName());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------------
|
/*--------------------------------------------------------------------------------
|
||||||
* Attributes
|
* Attributes
|
||||||
*--------------------------------------------------------------------------------
|
*--------------------------------------------------------------------------------
|
||||||
|
@ -284,13 +323,22 @@ public class ServletMultipartHandler
|
||||||
|
|
||||||
public ServletMultipartHandler(ServletRequest request) throws ServletMultipartException
|
public ServletMultipartHandler(ServletRequest request) throws ServletMultipartException
|
||||||
{
|
{
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
logger.debug("creating new ServletMultipartHandler");
|
||||||
|
|
||||||
if (!canHandle(request))
|
if (!canHandle(request))
|
||||||
|
{ // we can't handle ordinary requests, just multipart/form-data ones
|
||||||
|
logger.error("this request is not of type multipart/form-data");
|
||||||
throw new ServletMultipartException("not a multipart/form-data request");
|
throw new ServletMultipartException("not a multipart/form-data request");
|
||||||
|
|
||||||
|
} // end if
|
||||||
|
|
||||||
try
|
try
|
||||||
{ // build the MimeMultipart based on the ServletDataSource
|
{ // build the MimeMultipart based on the ServletDataSource
|
||||||
multipart = new MimeMultipart(new ServletDataSource(request));
|
multipart = new MimeMultipart(new ServletDataSource(request));
|
||||||
int count = multipart.getCount();
|
int count = multipart.getCount();
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
logger.debug("retrieved " + count + " parameter(s) from request");
|
||||||
|
|
||||||
// allocate the multipart parameters and slot them into our arrays
|
// allocate the multipart parameters and slot them into our arrays
|
||||||
param_byname = new Hashtable();
|
param_byname = new Hashtable();
|
||||||
|
@ -306,6 +354,7 @@ public class ServletMultipartHandler
|
||||||
} // end try
|
} // end try
|
||||||
catch (MessagingException me)
|
catch (MessagingException me)
|
||||||
{ // translate a MessagingException into the nicer ServletMultipartException
|
{ // translate a MessagingException into the nicer ServletMultipartException
|
||||||
|
logger.error("multipart parsing failed: " + me.getMessage(),me);
|
||||||
throw new ServletMultipartException("Error parsing request data: " + me.getMessage(),me);
|
throw new ServletMultipartException("Error parsing request data: " + me.getMessage(),me);
|
||||||
|
|
||||||
} // end catch
|
} // end catch
|
||||||
|
@ -356,7 +405,11 @@ public class ServletMultipartHandler
|
||||||
{
|
{
|
||||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||||
if (parm==null)
|
if (parm==null)
|
||||||
|
{ // no such parameter!
|
||||||
|
logger.warn("parameter \"" + name + "\" not found");
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
} // end if
|
||||||
else
|
else
|
||||||
return parm.isFile();
|
return parm.isFile();
|
||||||
|
|
||||||
|
@ -366,7 +419,11 @@ public class ServletMultipartHandler
|
||||||
{
|
{
|
||||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||||
if (parm==null)
|
if (parm==null)
|
||||||
|
{ // no such parameter!
|
||||||
|
logger.warn("parameter \"" + name + "\" not found");
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
} // end if
|
||||||
else
|
else
|
||||||
return parm.getValue();
|
return parm.getValue();
|
||||||
|
|
||||||
|
@ -376,7 +433,11 @@ public class ServletMultipartHandler
|
||||||
{
|
{
|
||||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||||
if (parm==null)
|
if (parm==null)
|
||||||
|
{ // no such parameter!
|
||||||
|
logger.warn("parameter \"" + name + "\" not found");
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
} // end if
|
||||||
else
|
else
|
||||||
return parm.getContentType();
|
return parm.getContentType();
|
||||||
|
|
||||||
|
@ -386,7 +447,11 @@ public class ServletMultipartHandler
|
||||||
{
|
{
|
||||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||||
if (parm==null)
|
if (parm==null)
|
||||||
|
{ // no such parameter!
|
||||||
|
logger.warn("parameter \"" + name + "\" not found");
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
} // end if
|
||||||
else
|
else
|
||||||
return parm.getSize();
|
return parm.getSize();
|
||||||
|
|
||||||
|
@ -396,7 +461,11 @@ public class ServletMultipartHandler
|
||||||
{
|
{
|
||||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||||
if (parm==null)
|
if (parm==null)
|
||||||
|
{ // no such parameter!
|
||||||
|
logger.warn("parameter \"" + name + "\" not found");
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
} // end if
|
||||||
else
|
else
|
||||||
return parm.getContent();
|
return parm.getContent();
|
||||||
|
|
||||||
|
@ -406,7 +475,12 @@ public class ServletMultipartHandler
|
||||||
{
|
{
|
||||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||||
if (parm==null)
|
if (parm==null)
|
||||||
|
{ // no such parameter!
|
||||||
|
logger.warn("parameter \"" + name + "\" not found");
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
|
} // end if
|
||||||
|
|
||||||
MultipartDataValue val = parm.getContent();
|
MultipartDataValue val = parm.getContent();
|
||||||
return val.getBinaryStream();
|
return val.getBinaryStream();
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,13 @@ import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import javax.servlet.*;
|
import javax.servlet.*;
|
||||||
import javax.servlet.http.*;
|
import javax.servlet.http.*;
|
||||||
|
import org.apache.log4j.*;
|
||||||
import com.silverwrist.util.*;
|
import com.silverwrist.util.*;
|
||||||
|
|
||||||
public class FormDataTest extends HttpServlet
|
public class FormDataTest extends HttpServlet
|
||||||
{
|
{
|
||||||
|
private static Category logger = Category.getInstance(FormDataTest.class.getName());
|
||||||
|
|
||||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||||
throws IOException, ServletException
|
throws IOException, ServletException
|
||||||
{
|
{
|
||||||
|
@ -22,41 +25,6 @@ public class FormDataTest extends HttpServlet
|
||||||
|
|
||||||
} // end doGet
|
} // end doGet
|
||||||
|
|
||||||
private void old_doPost(HttpServletRequest request, HttpServletResponse response)
|
|
||||||
throws IOException, ServletException
|
|
||||||
{
|
|
||||||
TestMultiHandler tmh = null;
|
|
||||||
String errmsg = null;
|
|
||||||
int count = -1;
|
|
||||||
|
|
||||||
try
|
|
||||||
{ // attempt to launch the multi handler
|
|
||||||
tmh = new TestMultiHandler(request);
|
|
||||||
count = tmh.getCount();
|
|
||||||
|
|
||||||
} // end try
|
|
||||||
catch (IOException e)
|
|
||||||
{ // we got an error from the parser
|
|
||||||
errmsg = e.getMessage();
|
|
||||||
|
|
||||||
} // end catch
|
|
||||||
|
|
||||||
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)
|
|
||||||
out.println("Success!<P>");
|
|
||||||
else
|
|
||||||
out.println("ERROR: " + errmsg + "<P>");
|
|
||||||
out.println("Data count was: " + String.valueOf(count) + "<P>");
|
|
||||||
out.println("<PRE>");
|
|
||||||
tmh.dump(out);
|
|
||||||
out.println("</PRE>");
|
|
||||||
out.println("<A HREF=\"/venice/testformdata\">Go back.</A>");
|
|
||||||
out.println("</BODY></HTML>");
|
|
||||||
|
|
||||||
} // end old_doPost
|
|
||||||
|
|
||||||
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||||
throws IOException, ServletException
|
throws IOException, ServletException
|
||||||
{
|
{
|
||||||
|
@ -67,40 +35,70 @@ public class FormDataTest extends HttpServlet
|
||||||
String text_value = null;
|
String text_value = null;
|
||||||
String file_name = null;
|
String file_name = null;
|
||||||
String file_type = null;
|
String file_type = null;
|
||||||
|
int file_size = -1;
|
||||||
|
|
||||||
try
|
try
|
||||||
{ // create the multipart handler
|
{ // create the multipart handler
|
||||||
smh = new ServletMultipartHandler(request);
|
smh = new ServletMultipartHandler(request);
|
||||||
|
|
||||||
|
// retrieve the text parameter value
|
||||||
text_value = smh.getValue("text");
|
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");
|
file_check = smh.isFileParam("file");
|
||||||
if (file_check)
|
if (file_check)
|
||||||
{ // copy out to file
|
{ // get the file name and type
|
||||||
file_name = smh.getValue("file");
|
file_name = smh.getValue("file");
|
||||||
file_type = smh.getContentType("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))
|
if ((file_name!=null) && (file_name.length()>0))
|
||||||
{ // build the file name and save it
|
{ // build the file name and open a stream to save it
|
||||||
FileOutputStream stm = new FileOutputStream(save_dir + "/" + file_name);
|
String real_name = save_dir + "/" + file_name;
|
||||||
|
FileOutputStream stm = new FileOutputStream(real_name);
|
||||||
InputStream in = smh.getFileContentStream("file");
|
InputStream in = smh.getFileContentStream("file");
|
||||||
byte[] copybuf = new byte[1024];
|
byte[] copybuf = new byte[1024];
|
||||||
|
|
||||||
|
// go into a copy loop
|
||||||
int ct = in.read(copybuf);
|
int ct = in.read(copybuf);
|
||||||
|
int nbytes = 0;
|
||||||
while (ct>=0)
|
while (ct>=0)
|
||||||
{ // copy loop...
|
{ // copy loop...
|
||||||
|
if (logger.isDebugEnabled())
|
||||||
|
logger.debug("read blksize = " + ct);
|
||||||
if (ct>0)
|
if (ct>0)
|
||||||
|
{ // write out copy buffer data
|
||||||
stm.write(copybuf,0,ct);
|
stm.write(copybuf,0,ct);
|
||||||
|
nbytes += ct;
|
||||||
|
|
||||||
|
} // end if
|
||||||
|
|
||||||
ct = in.read(copybuf);
|
ct = in.read(copybuf);
|
||||||
|
|
||||||
} // end while
|
} // end while
|
||||||
|
|
||||||
|
// all done - file saved
|
||||||
stm.close();
|
stm.close();
|
||||||
in.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
|
||||||
|
|
||||||
} // end if
|
} // end if
|
||||||
|
else // this shouldn't happen
|
||||||
|
logger.warn("file param was not a file?!?!?!?");
|
||||||
|
|
||||||
} // end try
|
} // end try
|
||||||
catch (ServletMultipartException e)
|
catch (ServletMultipartException e)
|
||||||
{ // load error message
|
{ // load error message
|
||||||
|
logger.error("caught ServletMultipartException: " + e.getMessage(),e);
|
||||||
errmsg = e.getMessage();
|
errmsg = e.getMessage();
|
||||||
|
|
||||||
} // end catch
|
} // end catch
|
||||||
|
@ -115,6 +113,7 @@ public class FormDataTest extends HttpServlet
|
||||||
{ // get file name and type
|
{ // get file name and type
|
||||||
out.println("File name: " + file_name + "<P>");
|
out.println("File name: " + file_name + "<P>");
|
||||||
out.println("File type: " + file_type + "<P>");
|
out.println("File type: " + file_type + "<P>");
|
||||||
|
out.println("File size: " + file_size + " bytes<P>");
|
||||||
out.println("Saved to directory: " + save_dir + "<P>");
|
out.println("Saved to directory: " + save_dir + "<P>");
|
||||||
|
|
||||||
} // end if
|
} // end if
|
||||||
|
@ -124,7 +123,7 @@ public class FormDataTest extends HttpServlet
|
||||||
} // end if
|
} // end if
|
||||||
else
|
else
|
||||||
out.println("ERROR: " + errmsg + "<P>");
|
out.println("ERROR: " + errmsg + "<P>");
|
||||||
out.println("<A HREF=\"/venice/testformdata\">Go back.</A>");
|
out.println("<A HREF=\"testformdata\">Go back.</A>");
|
||||||
out.println("</BODY></HTML>");
|
out.println("</BODY></HTML>");
|
||||||
|
|
||||||
} // end doPost
|
} // end doPost
|
||||||
|
|
Loading…
Reference in New Issue
Block a user