some needed date-converters
[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     mp = new MultipartParser(req, 1024*8192); // maximum eight megabyte
30     requestList = new ArrayList();
31     _evaluateRequest();
32   }
33
34
35   /**
36    * The following comment and some code was adapted from the Oreilley cos.jar 
37    * package. -mh 2001.09.20
38    *
39    * Returns all the parameters as a HashMap of Strings, any parameter 
40    * that sent without a value will be null.  A value 
41    * is guaranteed to be in its normal, decoded form.  If A parameter 
42    * has multiple values, only the last one is returned (for backward 
43    * compatibility).  For parameters with multiple values, it's possible
44    * the last "value" may be null.
45    *
46    * @return A HashMap of String representations of the  parameter values.
47    */
48   public HashMap getParameters(){
49     HashMap pHash = new HashMap();
50     String value = new String();
51
52     Enumeration Keys = parameters.keys();
53     while(Keys.hasMoreElements()) {
54       String KeyNm = (String)Keys.nextElement();
55       Vector values = (Vector)parameters.get(KeyNm);
56       if (values == null || values.size() == 0) {
57         value = null;
58       } else {
59         value = (String)values.elementAt(values.size() - 1);
60       } //endif
61       pHash.put(KeyNm, value);
62     } // end while
63     return pHash;
64   }
65
66   /**
67    * The following code and comment stolen from oreilley cos.jar.
68    * -mh. 2001.09.20
69    *
70    * Returns the values of the named parameter as a String array, or null if 
71    * the parameter was not sent.  The array has one entry for each parameter 
72    * field sent.  If any field was sent without a value that entry is stored 
73    * in the array as a null.  The values are guaranteed to be in their 
74    * normal, decoded form.  A single value is returned as a one-element array.
75    *
76    * @param name the parameter name.
77    * @return the parameter values.
78    */
79   public String[] getParameterValues(String name) {
80     try {
81       Vector values = (Vector)parameters.get(name);
82       if (values == null || values.size() == 0) {
83         return null;
84       }
85       String[] valuesArray = new String[values.size()];
86       values.copyInto(valuesArray);
87       return valuesArray;
88     }
89     catch (Exception e) {
90       return null;
91     }
92   }
93
94   private void _evaluateRequest() throws IOException{
95
96     Part part;
97     int i = 0;
98     while ((part = mp.readNextPart()) != null) {
99       String name = part.getName();
100       if (part.isParam()) {
101         // It's a parameter part, add it to the vector of values
102         ParamPart paramPart = (ParamPart) part;
103         String value = paramPart.getStringValue();
104         Vector existingValues = (Vector)parameters.get(name);
105         if (existingValues == null) {
106           existingValues = new Vector();
107           parameters.put(name, existingValues);
108         }
109         existingValues.addElement(value);
110       }
111       else if (part.isFile()) {
112         // nur das erste uploadfile beruecksichtigen
113         FilePart filePart = (FilePart) part;
114         String fn = filePart.getFileName();
115         if (filePart.getFileName() != null) {
116           ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
117           filePart.writeTo(byteStream);
118           requestList.add(i,new MpRequest(byteStream.toByteArray(),filePart.getFileName(),filePart.getContentType()));
119           i++;
120         }
121       }
122     } // while */
123   }
124   
125 }