KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > web > MultipartRequest


1 /***
2 * jwma Java WebMail
3 * Copyright (c) 2001 Dieter Wimberger
4 *
5 * jwma is free software; you can distribute and use this source
6 * under the terms of the BSD-style license received along with
7 * the distribution.
8 ***/

9 package org.lucane.server.web;
10
11
12 import java.io.IOException JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.Hashtable JavaDoc;
15
16 import javax.mail.MessagingException JavaDoc;
17 import javax.mail.internet.MimeMultipart JavaDoc;
18 import javax.servlet.ServletRequest JavaDoc;
19
20 /**
21  * Class that encapsulates a MultipartRequest, internally
22  * handling it.
23  * <p>
24  * The protocol to access the parameters resembles the one
25  * of the <code>ServletRequest</code> class.
26  *
27  * @author Dieter Wimberger
28  * @version 0.9.5 04/05/2001
29  */

30 public class MultipartRequest {
31
32     //instance attributes
33
private int m_Limit;
34     
35     private ServletRequest JavaDoc m_Request;
36     private Hashtable JavaDoc m_Parameters;
37     private FormdataMultipart m_FormdataMultipart;
38  
39     /**
40      * Constructs a <code>MultipartRequest</code> instance.
41      *
42      * @return the newly constructed <code>MultipartRequest</code> instance.
43      */

44      public MultipartRequest(ServletRequest JavaDoc request, int limit)
45             throws IOException JavaDoc {
46         
47         //Create with size from Kernel which comes in kb
48
m_Limit=limit;
49                 
50         m_Request=request;
51         processRequest();
52      }//constructor
53

54  
55     /**
56      * Returns the names of all the parameters as an Enumeration of
57      * strings.<br>
58      * It returns an empty Enumeration if there are no parameters.
59      *
60      * @return Enumeration of the names of all the parameters as strings.
61      */

62      public Enumeration JavaDoc getParameterNames(){
63         return m_Parameters.keys();
64      }//getParameterNames
65

66     /**
67      * Returns the value of a given parameter as String, or null if
68      * the control was not successful or non existant.
69      * <i><b>Note:</b><br>
70      * Mimics servlet request, do not call for multi value
71      * parameters.</i>
72      *
73      * @param name of the parameter to be retrieved as <code>String</code>.
74      *
75      * @return the parameter's value as <code>String</code>.
76      */

77      public String JavaDoc getParameter(String JavaDoc name){
78         if(m_Parameters.containsKey(name)) {
79             return ((String JavaDoc[])m_Parameters.get(name))[0];
80         } else {
81             return null;
82         }
83      }//getParameter
84

85     /**
86      * Returns all values of a given parameter as an array of strings.
87      *
88      * If this MultipartRequest does not contain any values for this
89      * parameter name, then this method returns null.
90      * Otherwise the array contains one <code>String</code> for each value
91      * of this parameter.
92      *
93      * @param name of the parameter to be retrieved as <code>String</code>.
94      *
95      * @return an array of strings,each representing a value of the
96      * parameter.
97      */

98      public String JavaDoc[] getParameterValues(String JavaDoc name){
99         if(m_Parameters.containsKey(name)) {
100             return (String JavaDoc[])m_Parameters.get(name);
101         } else {
102             return null;
103         }
104      }//getParameterValues
105

106     /**
107      * Tests if this <code>MultipartRequest</code> has attachments.
108      *
109      * @return true if it has attachments, false otherwise.
110      */

111      public boolean hasAttachments() {
112         try {
113             return (m_FormdataMultipart.getCount()>0);
114         } catch (MessagingException JavaDoc ex) {
115             return false;
116         }
117      }//hasAttachments
118

119     /**
120      * Returns the attachments as <code>MimeMultipart</code>.
121      *
122      * @return the attachments contained within in a <code>MimeMultipart</code>
123      * instance.
124      */

125      public MimeMultipart JavaDoc getAttachments() {
126         return (MimeMultipart JavaDoc) m_FormdataMultipart;
127      }//getAttachments
128

129     /**
130      * Parses the incoming multipart/form-data by reading it from the
131      * given request's input stream.
132      *
133      * <i><b>Note:</b>Does not handle multiparts contained in multiparts.</i>
134      *
135      * @throws IOException if uploaded content larger than allowed
136      * or parsing failed.
137      */

138      public void processRequest()
139             throws IOException JavaDoc {
140         
141         //first check on content length
142
int length=m_Request.getContentLength();
143         if (length>m_Limit) {
144             throw new IOException JavaDoc("Posted data exceeds limit of " +
145             m_Limit + " bytes.");
146         }
147         //then check for the content type and contained boundary
148
String JavaDoc ctype=m_Request.getContentType();
149         if(ctype.indexOf("--")==-1) {
150             throw new IOException JavaDoc("Data malformed, missing multipart boundary:"+ctype);
151         }
152         if(ctype.indexOf("multipart/form-data")==-1) {
153             throw new IOException JavaDoc("Can only handle an incoming multipart/form-data stream:"+ctype);
154         }
155         
156         MultipartInputStream m_InputStream= new MultipartInputStream(
157             m_Request.getInputStream(),ctype,m_Limit
158         );
159         try {
160             m_FormdataMultipart=new FormdataMultipart(m_InputStream);
161             m_Parameters=m_FormdataMultipart.getParameters();
162             
163         } catch (MessagingException JavaDoc mex) {
164             throw new IOException JavaDoc(mex.getMessage());
165         }
166      }//processRequest
167

168     
169      //constant definitions
170
/**
171       * Defines the size limit of the upload.
172       */

173      private static final int FORMDATA_LIMIT=1024 * 1024; // 1 Meg
174

175 }//class MultipartRequest
176

177
178
179
180
Popular Tags