001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2001 Dieter Wimberger
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package org.lucane.server.web;
009:
010: import java.io.IOException;
011: import java.util.Enumeration;
012: import java.util.Hashtable;
013:
014: import javax.mail.MessagingException;
015: import javax.mail.internet.MimeMultipart;
016: import javax.servlet.ServletRequest;
017:
018: /**
019: * Class that encapsulates a MultipartRequest, internally
020: * handling it.
021: * <p>
022: * The protocol to access the parameters resembles the one
023: * of the <code>ServletRequest</code> class.
024: *
025: * @author Dieter Wimberger
026: * @version 0.9.5 04/05/2001
027: */
028: public class MultipartRequest {
029:
030: //instance attributes
031: private int m_Limit;
032:
033: private ServletRequest m_Request;
034: private Hashtable m_Parameters;
035: private FormdataMultipart m_FormdataMultipart;
036:
037: /**
038: * Constructs a <code>MultipartRequest</code> instance.
039: *
040: * @return the newly constructed <code>MultipartRequest</code> instance.
041: */
042: public MultipartRequest(ServletRequest request, int limit)
043: throws IOException {
044:
045: //Create with size from Kernel which comes in kb
046: m_Limit = limit;
047:
048: m_Request = request;
049: processRequest();
050: }//constructor
051:
052: /**
053: * Returns the names of all the parameters as an Enumeration of
054: * strings.<br>
055: * It returns an empty Enumeration if there are no parameters.
056: *
057: * @return Enumeration of the names of all the parameters as strings.
058: */
059: public Enumeration getParameterNames() {
060: return m_Parameters.keys();
061: }//getParameterNames
062:
063: /**
064: * Returns the value of a given parameter as String, or null if
065: * the control was not successful or non existant.
066: * <i><b>Note:</b><br>
067: * Mimics servlet request, do not call for multi value
068: * parameters.</i>
069: *
070: * @param name of the parameter to be retrieved as <code>String</code>.
071: *
072: * @return the parameter's value as <code>String</code>.
073: */
074: public String getParameter(String name) {
075: if (m_Parameters.containsKey(name)) {
076: return ((String[]) m_Parameters.get(name))[0];
077: } else {
078: return null;
079: }
080: }//getParameter
081:
082: /**
083: * Returns all values of a given parameter as an array of strings.
084: *
085: * If this MultipartRequest does not contain any values for this
086: * parameter name, then this method returns null.
087: * Otherwise the array contains one <code>String</code> for each value
088: * of this parameter.
089: *
090: * @param name of the parameter to be retrieved as <code>String</code>.
091: *
092: * @return an array of strings,each representing a value of the
093: * parameter.
094: */
095: public String[] getParameterValues(String name) {
096: if (m_Parameters.containsKey(name)) {
097: return (String[]) m_Parameters.get(name);
098: } else {
099: return null;
100: }
101: }//getParameterValues
102:
103: /**
104: * Tests if this <code>MultipartRequest</code> has attachments.
105: *
106: * @return true if it has attachments, false otherwise.
107: */
108: public boolean hasAttachments() {
109: try {
110: return (m_FormdataMultipart.getCount() > 0);
111: } catch (MessagingException ex) {
112: return false;
113: }
114: }//hasAttachments
115:
116: /**
117: * Returns the attachments as <code>MimeMultipart</code>.
118: *
119: * @return the attachments contained within in a <code>MimeMultipart</code>
120: * instance.
121: */
122: public MimeMultipart getAttachments() {
123: return (MimeMultipart) m_FormdataMultipart;
124: }//getAttachments
125:
126: /**
127: * Parses the incoming multipart/form-data by reading it from the
128: * given request's input stream.
129: *
130: * <i><b>Note:</b>Does not handle multiparts contained in multiparts.</i>
131: *
132: * @throws IOException if uploaded content larger than allowed
133: * or parsing failed.
134: */
135: public void processRequest() throws IOException {
136:
137: //first check on content length
138: int length = m_Request.getContentLength();
139: if (length > m_Limit) {
140: throw new IOException("Posted data exceeds limit of "
141: + m_Limit + " bytes.");
142: }
143: //then check for the content type and contained boundary
144: String ctype = m_Request.getContentType();
145: if (ctype.indexOf("--") == -1) {
146: throw new IOException(
147: "Data malformed, missing multipart boundary:"
148: + ctype);
149: }
150: if (ctype.indexOf("multipart/form-data") == -1) {
151: throw new IOException(
152: "Can only handle an incoming multipart/form-data stream:"
153: + ctype);
154: }
155:
156: MultipartInputStream m_InputStream = new MultipartInputStream(
157: m_Request.getInputStream(), ctype, m_Limit);
158: try {
159: m_FormdataMultipart = new FormdataMultipart(m_InputStream);
160: m_Parameters = m_FormdataMultipart.getParameters();
161:
162: } catch (MessagingException mex) {
163: throw new IOException(mex.getMessage());
164: }
165: }//processRequest
166:
167: //constant definitions
168: /**
169: * Defines the size limit of the upload.
170: */
171: private static final int FORMDATA_LIMIT = 1024 * 1024; // 1 Meg
172:
173: }//class MultipartRequest
|