*** empty log message ***
This commit is contained in:
115
src/com/silverwrist/util/DOMElementHelper.java
Normal file
115
src/com/silverwrist/util/DOMElementHelper.java
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
* (the "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
|
||||
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
|
||||
* language governing rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is the Venice Web Community System.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
|
||||
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
|
||||
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
package com.silverwrist.util;
|
||||
|
||||
import org.w3c.dom.*;
|
||||
|
||||
public class DOMElementHelper
|
||||
{
|
||||
private Element elt; // element housed by this helper class
|
||||
|
||||
public DOMElementHelper(Element elt)
|
||||
{
|
||||
this.elt = elt;
|
||||
|
||||
} // end constructor
|
||||
|
||||
protected void finalize()
|
||||
{
|
||||
elt = null;
|
||||
|
||||
} // end finalize
|
||||
|
||||
private static String getTextOfElement(Element e)
|
||||
{
|
||||
NodeList kids = e.getChildNodes();
|
||||
if (kids==null)
|
||||
return null; // no text?
|
||||
|
||||
StringBuffer b = null;
|
||||
for (int i=0; i<kids.getLength(); i++)
|
||||
{ // look for an ELEMENT_NODE node matching the desired name
|
||||
Node t = kids.item(i);
|
||||
if ((t.getNodeType()==Node.TEXT_NODE) || (t.getNodeType()==Node.CDATA_SECTION_NODE))
|
||||
{ // append to the string under construction
|
||||
if (b==null)
|
||||
b = new StringBuffer();
|
||||
else
|
||||
b.append(' ');
|
||||
b.append(t.getNodeValue());
|
||||
|
||||
} // end if
|
||||
|
||||
} // end for
|
||||
|
||||
if (b==null)
|
||||
return null; // no TEXT nodes
|
||||
else
|
||||
return b.toString(); // return the concatenation
|
||||
|
||||
} // end getTextOfElement
|
||||
|
||||
public Element getElement()
|
||||
{
|
||||
return elt;
|
||||
|
||||
} // end getElement
|
||||
|
||||
public Element getSubElement(String name)
|
||||
{
|
||||
NodeList kids = elt.getChildNodes();
|
||||
if (kids==null)
|
||||
return null; // no children?
|
||||
for (int i=0; i<kids.getLength(); i++)
|
||||
{ // look for an ELEMENT_NODE node matching the desired name
|
||||
Node t = kids.item(i);
|
||||
if ((t.getNodeType()==Node.ELEMENT_NODE) && (t.getNodeName().equals(name)))
|
||||
return (Element)t;
|
||||
|
||||
} // end for
|
||||
|
||||
return null; // not found
|
||||
|
||||
} // end getSubElement
|
||||
|
||||
public String getElementText()
|
||||
{
|
||||
return getTextOfElement(elt);
|
||||
|
||||
} // end getElementText
|
||||
|
||||
public String getSubElementText(String name)
|
||||
{
|
||||
Element se = getSubElement(name);
|
||||
if (se==null)
|
||||
return null;
|
||||
else
|
||||
return getTextOfElement(se);
|
||||
|
||||
} // end getSubElementText
|
||||
|
||||
public boolean hasChildElement(String name)
|
||||
{
|
||||
Element tmp = getSubElement(name);
|
||||
return (tmp==null) ? false : true;
|
||||
|
||||
} // end hasChildElement
|
||||
|
||||
} // end DOMElementHelper
|
||||
|
||||
|
||||
64
src/com/silverwrist/util/ServletMultipartException.java
Normal file
64
src/com/silverwrist/util/ServletMultipartException.java
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
* (the "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
|
||||
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
|
||||
* language governing rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is the Venice Web Community System.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
|
||||
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
|
||||
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
package com.silverwrist.util;
|
||||
|
||||
public class ServletMultipartException extends Exception
|
||||
{
|
||||
// Attributes
|
||||
private Exception e = null; // internal "root cause" exception
|
||||
|
||||
public ServletMultipartException()
|
||||
{
|
||||
super();
|
||||
|
||||
} // end constructor
|
||||
|
||||
public ServletMultipartException(String msg)
|
||||
{
|
||||
super(msg);
|
||||
|
||||
} // end constructor
|
||||
|
||||
public ServletMultipartException(Exception e)
|
||||
{
|
||||
super(e.getMessage());
|
||||
this.e = e;
|
||||
|
||||
} // end constructor
|
||||
|
||||
public ServletMultipartException(String msg, Exception e)
|
||||
{
|
||||
super(msg);
|
||||
this.e = e;
|
||||
|
||||
} // end constructor
|
||||
|
||||
protected void finalize() throws Throwable
|
||||
{
|
||||
e = null;
|
||||
super.finalize();
|
||||
|
||||
} // end finalize
|
||||
|
||||
public Exception getException()
|
||||
{
|
||||
return e;
|
||||
|
||||
} // end getException
|
||||
|
||||
} // end class ServletMultipartException
|
||||
379
src/com/silverwrist/util/ServletMultipartHandler.java
Normal file
379
src/com/silverwrist/util/ServletMultipartHandler.java
Normal file
@@ -0,0 +1,379 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
* (the "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
|
||||
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
|
||||
* language governing rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is the Venice Web Community System.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
|
||||
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
|
||||
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
package com.silverwrist.util;
|
||||
|
||||
import java.io.*;
|
||||
import java.sql.Blob;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
import javax.activation.DataSource;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.*;
|
||||
import javax.servlet.*;
|
||||
|
||||
// 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
|
||||
// absolutely bodge-tastic!
|
||||
|
||||
public class ServletMultipartHandler
|
||||
{
|
||||
private MimeMultipart multipart; // holds all the multipart data
|
||||
private Hashtable param_byname; // parameters by name
|
||||
private Vector param_order; // parameters in order
|
||||
|
||||
class ServletDataSource implements DataSource
|
||||
{
|
||||
private ServletRequest request;
|
||||
private InputStream istm = null;
|
||||
|
||||
public ServletDataSource(ServletRequest request)
|
||||
{
|
||||
this.request = request;
|
||||
|
||||
} // end constructor
|
||||
|
||||
public InputStream getInputStream() throws IOException
|
||||
{
|
||||
if (istm==null)
|
||||
istm = request.getInputStream();
|
||||
return istm;
|
||||
|
||||
} // end getInputStream
|
||||
|
||||
public OutputStream getOutputStream() throws IOException
|
||||
{
|
||||
throw new IOException("tried to get OutputStream on servlet input?!?!?");
|
||||
|
||||
} // end getOutputStream
|
||||
|
||||
public String getContentType()
|
||||
{
|
||||
return request.getContentType();
|
||||
|
||||
} // end getContentType
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return "servlet";
|
||||
|
||||
} // end getName
|
||||
|
||||
} // end class ServletDataSource
|
||||
|
||||
static class MultipartDataValue implements Blob
|
||||
{
|
||||
private byte[] actual_data; // the actual data we contain
|
||||
|
||||
public MultipartDataValue(MimeBodyPart part) throws MessagingException, IOException
|
||||
{
|
||||
InputStream in = part.getInputStream();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
byte[] copybuf = new byte[1024];
|
||||
int ct = in.read(copybuf);
|
||||
while (ct>=0)
|
||||
{ // do a simple read and write
|
||||
if (ct>0)
|
||||
out.write(copybuf,0,ct);
|
||||
ct = in.read(copybuf);
|
||||
|
||||
} // end while
|
||||
|
||||
in.close();
|
||||
actual_data = out.toByteArray();
|
||||
out.close();
|
||||
|
||||
} // end constructor
|
||||
|
||||
public long length()
|
||||
{
|
||||
return actual_data.length;
|
||||
|
||||
} // end length
|
||||
|
||||
public byte[] getBytes(long pos, int length)
|
||||
{
|
||||
byte[] rc = new byte[length];
|
||||
System.arraycopy(actual_data,(int)pos,rc,0,length);
|
||||
return rc;
|
||||
|
||||
} // end getBytes
|
||||
|
||||
public InputStream getBinaryStream()
|
||||
{
|
||||
return new ByteArrayInputStream(actual_data);
|
||||
|
||||
} // end getBinaryStream
|
||||
|
||||
public long position(byte[] pattern, long start) throws SQLException
|
||||
{
|
||||
throw new SQLException("function not implemented");
|
||||
|
||||
} // end position
|
||||
|
||||
public long position(Blob pattern, long start) throws SQLException
|
||||
{
|
||||
return position(pattern.getBytes(0,(int)(pattern.length())),start);
|
||||
|
||||
} // end position
|
||||
|
||||
} // end class MultipartDataValue
|
||||
|
||||
class MultipartParameter
|
||||
{
|
||||
private MimeBodyPart part; // the actual body part data
|
||||
private String name; // the parameter name
|
||||
private String filename; // the filename
|
||||
private MultipartDataValue cached_value = null;
|
||||
|
||||
public MultipartParameter(MimeBodyPart part) throws MessagingException
|
||||
{
|
||||
this.part = part; // save part reference
|
||||
|
||||
// Parse the Content-Disposition header.
|
||||
String[] cdstr = part.getHeader("Content-Disposition");
|
||||
ContentDisposition cdisp = new ContentDisposition(cdstr[0]);
|
||||
name = cdisp.getParameter("name");
|
||||
filename = cdisp.getParameter("filename");
|
||||
|
||||
if (filename!=null)
|
||||
{ // Strip off everything but the base filename, if the browser happened to pass that.
|
||||
int sep = filename.lastIndexOf('\\');
|
||||
if (sep>=0)
|
||||
filename = filename.substring(sep+1);
|
||||
sep = filename.lastIndexOf('/');
|
||||
if (sep>=0)
|
||||
filename = filename.substring(sep+1);
|
||||
|
||||
} // end if
|
||||
|
||||
} // end constructor
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
|
||||
} // end getName
|
||||
|
||||
public boolean isFile()
|
||||
{
|
||||
return (filename!=null);
|
||||
|
||||
} // end isFile
|
||||
|
||||
public String getValue()
|
||||
{
|
||||
if (filename!=null)
|
||||
return filename;
|
||||
try
|
||||
{ // Retrieve the part's actual content and convert it to a String. (Since non-file
|
||||
// fields are of type text/plain, the Object "val" should actually be a String, in
|
||||
// which case the toString() call is actually a no-op. But this is safe.)
|
||||
Object val = part.getContent();
|
||||
return val.toString();
|
||||
|
||||
} // end try
|
||||
catch (Exception e)
|
||||
{ // turn any exception returns here into null returns
|
||||
return null;
|
||||
|
||||
} // end catch
|
||||
|
||||
} // end getValue
|
||||
|
||||
public String getContentType()
|
||||
{
|
||||
try
|
||||
{ // pass through to the interior part
|
||||
return part.getContentType();
|
||||
|
||||
} // end try
|
||||
catch (Exception e)
|
||||
{ // just dump a null on error
|
||||
return null;
|
||||
|
||||
} // end catch
|
||||
|
||||
} // end getContentType
|
||||
|
||||
public int getSize()
|
||||
{
|
||||
try
|
||||
{ // pass through to the interior part
|
||||
return part.getSize();
|
||||
|
||||
} // end try
|
||||
catch (Exception e)
|
||||
{ // just dump a -1 on error
|
||||
return -1;
|
||||
|
||||
} // end catch
|
||||
|
||||
} // end getSize
|
||||
|
||||
public MultipartDataValue getContent() throws ServletMultipartException
|
||||
{
|
||||
if (filename==null)
|
||||
return null;
|
||||
|
||||
if (cached_value==null)
|
||||
{ // we don't have the value cached yet
|
||||
try
|
||||
{ // extract the value
|
||||
cached_value = new MultipartDataValue(part);
|
||||
|
||||
} // end try
|
||||
catch (MessagingException me)
|
||||
{ // translate exception here
|
||||
throw new ServletMultipartException("Error getting data content: " + me.getMessage(),me);
|
||||
|
||||
} // end catch
|
||||
catch (IOException ioe)
|
||||
{ // translate exception here
|
||||
throw new ServletMultipartException("Error getting data content: " + ioe.getMessage(),ioe);
|
||||
|
||||
} // end catch
|
||||
|
||||
} // end if
|
||||
|
||||
return cached_value;
|
||||
|
||||
} // end getContent
|
||||
|
||||
} // end class MultipartParameter
|
||||
|
||||
public ServletMultipartHandler(ServletRequest request) throws ServletMultipartException
|
||||
{
|
||||
if (!canHandle(request))
|
||||
throw new ServletMultipartException("not a multipart/form-data request");
|
||||
|
||||
try
|
||||
{ // build the MimeMultipart based on the ServletDataSource
|
||||
multipart = new MimeMultipart(new ServletDataSource(request));
|
||||
int count = multipart.getCount();
|
||||
|
||||
// allocate the multipart parameters and slot them into our arrays
|
||||
param_byname = new Hashtable();
|
||||
param_order = new Vector();
|
||||
for (int i=0; i<count; i++)
|
||||
{ // create the MultipartParameter structures and stash them for later use
|
||||
MultipartParameter parm = new MultipartParameter((MimeBodyPart)(multipart.getBodyPart(i)));
|
||||
param_byname.put(parm.getName(),parm);
|
||||
param_order.addElement(parm);
|
||||
|
||||
} // end count
|
||||
|
||||
} // end try
|
||||
catch (MessagingException me)
|
||||
{ // translate a MessagingException into the nicer ServletMultipartException
|
||||
throw new ServletMultipartException("Error parsing request data: " + me.getMessage(),me);
|
||||
|
||||
} // end catch
|
||||
|
||||
} // end constructor
|
||||
|
||||
/**
|
||||
* Returns <CODE>true</CODE> if the given <CODE>ServletRequest</CODE> can be handled by
|
||||
* the <CODE>ServletMultipartHandler</CODE>, <CODE>false</CODE> if not.
|
||||
*
|
||||
* @param request The <CODE>ServletRequest</CODE> to be checked.
|
||||
* @return <CODE>true</CODE> if the given <CODE>ServletRequest</CODE> can be handled by
|
||||
* the <CODE>ServletMultipartHandler</CODE>, <CODE>false</CODE> if not.
|
||||
*/
|
||||
public static boolean canHandle(ServletRequest request)
|
||||
{
|
||||
String ctype = request.getContentType();
|
||||
return (ctype.startsWith("multipart/form-data"));
|
||||
|
||||
} // end canHandle
|
||||
|
||||
public Enumeration getNames()
|
||||
{
|
||||
Vector tmp_vector = new Vector();
|
||||
Enumeration enum = param_order.elements();
|
||||
while (enum.hasMoreElements())
|
||||
{ // add each name to the temporary vector
|
||||
MultipartParameter parm = (MultipartParameter)(enum.nextElement());
|
||||
tmp_vector.addElement(parm.getName());
|
||||
|
||||
} // end while
|
||||
|
||||
return tmp_vector.elements(); // and enumerate it
|
||||
|
||||
} // end getNames
|
||||
|
||||
public boolean isFileParam(String name)
|
||||
{
|
||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||
if (parm==null)
|
||||
return false;
|
||||
else
|
||||
return parm.isFile();
|
||||
|
||||
} // end isFileParam
|
||||
|
||||
public String getValue(String name)
|
||||
{
|
||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||
if (parm==null)
|
||||
return null;
|
||||
else
|
||||
return parm.getValue();
|
||||
|
||||
} // end getValue
|
||||
|
||||
public String getContentType(String name)
|
||||
{
|
||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||
if (parm==null)
|
||||
return null;
|
||||
else
|
||||
return parm.getContentType();
|
||||
|
||||
} // end getContentType
|
||||
|
||||
public int getContentSize(String name)
|
||||
{
|
||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||
if (parm==null)
|
||||
return -1;
|
||||
else
|
||||
return parm.getSize();
|
||||
|
||||
} // end getContentSize
|
||||
|
||||
public Blob getFileContentBlob(String name) throws ServletMultipartException
|
||||
{
|
||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||
if (parm==null)
|
||||
return null;
|
||||
else
|
||||
return parm.getContent();
|
||||
|
||||
} // end getFileContentBlob
|
||||
|
||||
public InputStream getFileContentStream(String name) throws ServletMultipartException
|
||||
{
|
||||
MultipartParameter parm = (MultipartParameter)(param_byname.get(name));
|
||||
if (parm==null)
|
||||
return null;
|
||||
MultipartDataValue val = parm.getContent();
|
||||
return val.getBinaryStream();
|
||||
|
||||
} // end getFileContentStream
|
||||
|
||||
} // end class ServletMultipartHandler
|
||||
122
src/com/silverwrist/util/StringUtil.java
Normal file
122
src/com/silverwrist/util/StringUtil.java
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
||||
* (the "License"); you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
|
||||
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
|
||||
* language governing rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is the Venice Web Community System.
|
||||
*
|
||||
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
|
||||
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
|
||||
* Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
*/
|
||||
package com.silverwrist.util;
|
||||
|
||||
public class StringUtil
|
||||
{
|
||||
public static String encodeStringSQL(String str)
|
||||
{
|
||||
if (str==null)
|
||||
return null;
|
||||
int ndx = str.indexOf('\'');
|
||||
if (ndx<0)
|
||||
return str;
|
||||
StringBuffer buf = new StringBuffer();
|
||||
while (ndx>=0)
|
||||
{ // convert each single quote mark to a pair of them
|
||||
if (ndx>0)
|
||||
buf.append(str.substring(0,ndx));
|
||||
buf.append("''");
|
||||
str = str.substring(ndx+1);
|
||||
ndx = str.indexOf('\'');
|
||||
|
||||
} // end while
|
||||
|
||||
buf.append(str);
|
||||
return buf.toString();
|
||||
|
||||
} // end encodeStringSQL
|
||||
|
||||
public static String encodeHTML(String str)
|
||||
{
|
||||
if (str==null)
|
||||
return null;
|
||||
StringBuffer buf = new StringBuffer();
|
||||
for (int i=0; i<str.length(); i++)
|
||||
{ // loop through the string encoding each character in turn
|
||||
switch (str.charAt(i))
|
||||
{
|
||||
case '"':
|
||||
buf.append(""");
|
||||
break;
|
||||
|
||||
case '&':
|
||||
buf.append("&");
|
||||
break;
|
||||
|
||||
case '<':
|
||||
buf.append("<");
|
||||
break;
|
||||
|
||||
case '>':
|
||||
buf.append(">");
|
||||
break;
|
||||
|
||||
default:
|
||||
buf.append(str.charAt(i));
|
||||
break;
|
||||
|
||||
} // end switch
|
||||
|
||||
} // end for
|
||||
|
||||
return buf.toString();
|
||||
|
||||
} // end encodeHTML
|
||||
|
||||
public static boolean isStringEmpty(String s)
|
||||
{
|
||||
return ((s==null) || (s.length()==0));
|
||||
|
||||
} // end isStringEmpty
|
||||
|
||||
public static String replaceAllInstances(String base, String find, String replace)
|
||||
{
|
||||
String work = base;
|
||||
int ndx = work.indexOf(find);
|
||||
if (ndx<0)
|
||||
return work; // trivial case
|
||||
StringBuffer b = new StringBuffer();
|
||||
while (ndx>=0)
|
||||
{ // break off the first part of the string, then insert the replacement
|
||||
if (ndx>0)
|
||||
b.append(work.substring(0,ndx));
|
||||
b.append(replace);
|
||||
|
||||
// break off the tail end
|
||||
ndx += find.length();
|
||||
if (ndx==work.length())
|
||||
work = null;
|
||||
else
|
||||
work = work.substring(ndx);
|
||||
|
||||
// do the next find
|
||||
if (work!=null)
|
||||
ndx = work.indexOf(find);
|
||||
else
|
||||
ndx = -1;
|
||||
|
||||
} // end while
|
||||
|
||||
if (work!=null)
|
||||
b.append(work);
|
||||
return b.toString();
|
||||
|
||||
} // end replaceAllInstances
|
||||
|
||||
} // end class StringUtil
|
||||
132
src/com/silverwrist/util/test/FormDataTest.java
Normal file
132
src/com/silverwrist/util/test/FormDataTest.java
Normal file
@@ -0,0 +1,132 @@
|
||||
package com.silverwrist.util.test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import javax.servlet.*;
|
||||
import javax.servlet.http.*;
|
||||
import com.silverwrist.util.*;
|
||||
|
||||
public class FormDataTest extends HttpServlet
|
||||
{
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException, ServletException
|
||||
{
|
||||
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
|
||||
|
||||
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)
|
||||
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;
|
||||
|
||||
try
|
||||
{ // create the multipart handler
|
||||
smh = new ServletMultipartHandler(request);
|
||||
text_value = smh.getValue("text");
|
||||
file_check = smh.isFileParam("file");
|
||||
if (file_check)
|
||||
{ // copy out to file
|
||||
file_name = smh.getValue("file");
|
||||
file_type = smh.getContentType("file");
|
||||
if ((file_name!=null) && (file_name.length()>0))
|
||||
{ // build the file name and save it
|
||||
FileOutputStream stm = new FileOutputStream(save_dir + "/" + file_name);
|
||||
InputStream in = smh.getFileContentStream("file");
|
||||
byte[] copybuf = new byte[1024];
|
||||
int ct = in.read(copybuf);
|
||||
while (ct>=0)
|
||||
{ // copy loop...
|
||||
if (ct>0)
|
||||
stm.write(copybuf,0,ct);
|
||||
ct = in.read(copybuf);
|
||||
|
||||
} // end while
|
||||
|
||||
stm.close();
|
||||
in.close();
|
||||
|
||||
} // end if
|
||||
|
||||
} // end if
|
||||
|
||||
} // end try
|
||||
catch (ServletMultipartException e)
|
||||
{ // load error message
|
||||
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)
|
||||
{ // 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("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=\"/venice/testformdata\">Go back.</A>");
|
||||
out.println("</BODY></HTML>");
|
||||
|
||||
} // end doPost
|
||||
|
||||
} // end class FormDataTest
|
||||
121
src/com/silverwrist/util/test/TestMultiHandler.java
Normal file
121
src/com/silverwrist/util/test/TestMultiHandler.java
Normal file
@@ -0,0 +1,121 @@
|
||||
package com.silverwrist.util.test;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
import javax.activation.DataSource;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.*;
|
||||
import javax.servlet.*;
|
||||
|
||||
public class TestMultiHandler
|
||||
{
|
||||
private MimeMultipart multipart;
|
||||
private int count;
|
||||
private Vector params;
|
||||
|
||||
static class ServletDataSource implements DataSource
|
||||
{
|
||||
private ServletRequest request;
|
||||
private InputStream istm = null;
|
||||
|
||||
public ServletDataSource(ServletRequest request)
|
||||
{
|
||||
this.request = request;
|
||||
|
||||
} // end constructor
|
||||
|
||||
public InputStream getInputStream() throws IOException
|
||||
{
|
||||
if (istm==null)
|
||||
istm = request.getInputStream();
|
||||
return istm;
|
||||
|
||||
} // end getInputStream
|
||||
|
||||
public OutputStream getOutputStream() throws IOException
|
||||
{
|
||||
throw new IOException("tried to get OutputStream on servlet input?!?!?");
|
||||
|
||||
} // end getOutputStream
|
||||
|
||||
public String getContentType()
|
||||
{
|
||||
return request.getContentType();
|
||||
|
||||
} // end getContentType
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return "servlet";
|
||||
|
||||
} // end getName
|
||||
|
||||
} // end class ServletDataSource
|
||||
|
||||
public TestMultiHandler(ServletRequest request) throws IOException
|
||||
{
|
||||
if (!canHandle(request))
|
||||
throw new UnsupportedOperationException("data type doesn't match");
|
||||
|
||||
try
|
||||
{ // OK, this is the tricky part...
|
||||
ServletDataSource ds = new ServletDataSource(request);
|
||||
multipart = new MimeMultipart(ds);
|
||||
count = multipart.getCount();
|
||||
params = new Vector(count);
|
||||
// OK, that's enough for now
|
||||
|
||||
} // end try
|
||||
catch (MessagingException e)
|
||||
{ // better run like hell, 'cos this may not work
|
||||
throw new IOException("Got MessagingException: " + e.getMessage());
|
||||
|
||||
} // end catch
|
||||
|
||||
} // end constructor
|
||||
|
||||
public static boolean canHandle(ServletRequest request)
|
||||
{
|
||||
String ctype = request.getContentType();
|
||||
return (ctype.startsWith("multipart/form-data"));
|
||||
|
||||
} // end canHandle
|
||||
|
||||
public int getCount()
|
||||
{
|
||||
return count;
|
||||
|
||||
} // end getCount
|
||||
|
||||
public void dump(PrintWriter out)
|
||||
{
|
||||
try
|
||||
{ // print out all parts
|
||||
int ct = multipart.getCount();
|
||||
out.println("Number of parts: " + String.valueOf(ct));
|
||||
|
||||
for (int i=0; i<ct; i++)
|
||||
{ // loop through all the parts
|
||||
out.println("--- PART " + String.valueOf(i) + " ---");
|
||||
MimeBodyPart part = (MimeBodyPart)(multipart.getBodyPart(i));
|
||||
out.println("got it");
|
||||
String foo = part.getContentType();
|
||||
out.println("Content type: " + foo);
|
||||
foo = part.getDisposition();
|
||||
out.println("Content disposition: " + foo);
|
||||
String[] disphdr = part.getHeader("Content-Disposition");
|
||||
for (int j=0; j<disphdr.length; j++)
|
||||
out.println("Content-Disposition header[" + String.valueOf(j) + "]: " + disphdr[j]);
|
||||
|
||||
} // end for
|
||||
|
||||
} // end try
|
||||
catch (MessagingException e)
|
||||
{ // whoops!
|
||||
out.println("THREW MessagingException: " + e.getMessage());
|
||||
|
||||
} // end catch
|
||||
|
||||
} // end dump
|
||||
|
||||
} // end class TestMultiHandler
|
||||
Reference in New Issue
Block a user