5b1871fa3b9070ad841b944d2cc17777df2a8afc
[mir.git] / source / mir / misc / WebdbMultipartRequest.java
1 package mir.misc;
2
3 import java.util.*;
4 import java.io.*;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7 import com.oreilly.servlet.multipart.*;
8 import com.oreilly.servlet.*;
9
10 /**
11  * Title:
12  * Description:
13  * Copyright:    Copyright (c) 2001
14  * Company:      Indymedia
15  * @author
16  * @version 1.0
17  */
18
19 public class WebdbMultipartRequest
20 {
21   HttpServletRequest    req=null;
22   Hashtable             parameters = new Hashtable();
23   MultipartParser       mp=null;
24   public static ArrayList requestList;
25
26   public WebdbMultipartRequest(HttpServletRequest theReq) throws IOException
27   {
28     req=theReq;
29     int maxSize = Integer.parseInt(MirConfig.getProp("MaxMediaUploadSize"));
30     mp = new MultipartParser(req, 1024*maxSize);
31     requestList = new ArrayList();
32     _evaluateRequest();
33   }
34
35
36   /**
37    * The following comment and some code was adapted from the Oreilley cos.jar 
38    * package. -mh 2001.09.20
39    *
40    * Returns all the parameters as a HashMap of Strings, any parameter 
41    * that sent without a value will be null.  A value 
42    * is guaranteed to be in its normal, decoded form.  If A parameter 
43    * has multiple values, only the last one is returned (for backward 
44    * compatibility).  For parameters with multiple values, it's possible
45    * the last "value" may be null.
46    *
47    * @return A HashMap of String representations of the  parameter values.
48    */
49   public HashMap getParameters(){
50     HashMap pHash = new HashMap();
51     String value = new String();
52
53     Enumeration Keys = parameters.keys();
54     while(Keys.hasMoreElements()) {
55       String KeyNm = (String)Keys.nextElement();
56       Vector values = (Vector)parameters.get(KeyNm);
57       if (values == null || values.size() == 0) {
58         value = null;
59       } else {
60         value = (String)values.elementAt(values.size() - 1);
61       } //endif
62       pHash.put(KeyNm, value);
63     } // end while
64     return pHash;
65   }
66
67   /**
68    * The following code and comment stolen from oreilley cos.jar.
69    * -mh. 2001.09.20
70    *
71    * Returns the values of the named parameter as a String array, or null if 
72    * the parameter was not sent.  The array has one entry for each parameter 
73    * field sent.  If any field was sent without a value that entry is stored 
74    * in the array as a null.  The values are guaranteed to be in their 
75    * normal, decoded form.  A single value is returned as a one-element array.
76    *
77    * @param name the parameter name.
78    * @return the parameter values.
79    */
80   public String[] getParameterValues(String name) {
81     try {
82       Vector values = (Vector)parameters.get(name);
83       if (values == null || values.size() == 0) {
84         return null;
85       }
86       String[] valuesArray = new String[values.size()];
87       values.copyInto(valuesArray);
88       return valuesArray;
89     }
90     catch (Exception e) {
91       return null;
92     }
93   }
94
95   private void _evaluateRequest() throws IOException{
96
97     Part part;
98     int i = 0;
99     while ((part = mp.readNextPart()) != null) {
100       String name = part.getName();
101       if (part.isParam()) {
102         // It's a parameter part, add it to the vector of values
103         ParamPart paramPart = (ParamPart) part;
104         String value = paramPart.getStringValue();
105         Vector existingValues = (Vector)parameters.get(name);
106         if (existingValues == null) {
107           existingValues = new Vector();
108           parameters.put(name, existingValues);
109         }
110         existingValues.addElement(value);
111       }
112       else if (part.isFile()) {
113         // nur das erste uploadfile beruecksichtigen
114         FilePart filePart = (FilePart) part;
115         String fn = filePart.getFileName();
116         if (filePart.getFileName() != null) {
117           ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
118           filePart.writeTo(byteStream);
119           requestList.add(i,new MpRequest(byteStream.toByteArray(),filePart.getFileName(),filePart.getContentType()));
120           i++;
121         }
122       }
123     } // while */
124   }
125
126 }