View Javadoc

1   /*
2    * $Id: MultiPartRequest.java 421776 2006-07-14 00:47:56Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.dispatcher.multipart;
19  
20  import java.io.File;
21  import java.util.Enumeration;
22  import java.util.List;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  
30  /***
31   * Abstract wrapper class HTTP requests to handle multi-part data. <p>
32   *
33   */
34  public abstract class MultiPartRequest {
35  
36      protected static Log log = LogFactory.getLog(MultiPartRequest.class);
37  
38  
39      /***
40       * Returns <tt>true</tt> if the request is multipart form data, <tt>false</tt> otherwise.
41       *
42       * @param request the http servlet request.
43       * @return <tt>true</tt> if the request is multipart form data, <tt>false</tt> otherwise.
44       */
45      public static boolean isMultiPart(HttpServletRequest request) {
46          String content_type = request.getContentType();
47          return content_type != null && content_type.indexOf("multipart/form-data") != -1;
48      }
49  
50      /***
51       * Returns an enumeration of the parameter names for uploaded files
52       *
53       * @return an enumeration of the parameter names for uploaded files
54       */
55      public abstract Enumeration<String> getFileParameterNames();
56  
57      /***
58       * Returns the content type(s) of the file(s) associated with the specified field name
59       * (as supplied by the client browser), or <tt>null</tt> if no files are associated with the
60       * given field name.
61       *
62       * @param fieldName input field name
63       * @return an array of content encoding for the specified input field name or <tt>null</tt> if
64       *         no content type was specified.
65       */
66      public abstract String[] getContentType(String fieldName);
67  
68      /***
69       * Returns a {@link java.io.File} object for the filename specified or <tt>null</tt> if no files
70       * are associated with the given field name.
71       *
72       * @param fieldName input field name
73       * @return a File[] object for files associated with the specified input field name
74       */
75      public abstract File[] getFile(String fieldName);
76  
77      /***
78       * Returns a String[] of file names for files associated with the specified input field name
79       *
80       * @param fieldName input field name
81       * @return a String[] of file names for files associated with the specified input field name
82       */
83      public abstract String[] getFileNames(String fieldName);
84  
85      /***
86       * Returns the file system name(s) of files associated with the given field name or
87       * <tt>null</tt> if no files are associated with the given field name.
88       *
89       * @param fieldName input field name
90       * @return the file system name(s) of files associated with the given field name
91       */
92      public abstract String[] getFilesystemName(String fieldName);
93  
94      /***
95       * Returns the specified request parameter.
96       *
97       * @param name the name of the parameter to get
98       * @return the parameter or <tt>null</tt> if it was not found.
99       */
100     public abstract String getParameter(String name);
101 
102     /***
103      * Returns an enumeration of String parameter names.
104      *
105      * @return an enumeration of String parameter names.
106      */
107     public abstract Enumeration<String> getParameterNames();
108 
109     /***
110      * Returns a list of all parameter values associated with a parameter name. If there is only
111      * one parameter value per name the resulting array will be of length 1.
112      *
113      * @param name the name of the parameter.
114      * @return an array of all values associated with the parameter name.
115      */
116     public abstract String[] getParameterValues(String name);
117 
118     /***
119      * Returns a list of error messages that may have occurred while processing the request.
120      * If there are no errors, an empty list is returned. If the underlying implementation
121      * (ie: pell, cos, jakarta, etc) cannot support providing these errors, an empty list is
122      * also returned. This list of errors is repoted back to the
123      * {@link MultiPartRequestWrapper}'s errors field.
124      *
125      * @return a list of Strings that represent various errors during parsing
126      */
127     public abstract List getErrors();
128 }