1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.upload;
24  
25  import com.liferay.portal.kernel.upload.UploadPortletRequest;
26  import com.liferay.portal.kernel.upload.UploadServletRequest;
27  
28  import java.io.File;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.Enumeration;
33  import java.util.HashMap;
34  import java.util.List;
35  import java.util.Map;
36  
37  import javax.servlet.http.HttpServletRequestWrapper;
38  
39  /**
40   * <a href="UploadPortletRequestImpl.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Harry Mark
44   *
45   */
46  public class UploadPortletRequestImpl
47      extends HttpServletRequestWrapper implements UploadPortletRequest {
48  
49      public UploadPortletRequestImpl(
50          UploadServletRequest uploadRequest, String namespace) {
51  
52          super(uploadRequest);
53  
54          _uploadRequest = uploadRequest;
55          _namespace = namespace;
56      }
57  
58      public void cleanUp() {
59          _uploadRequest.cleanUp();
60      }
61  
62      public String getContentType(String name) {
63          String contentType = _uploadRequest.getContentType(_namespace + name);
64  
65          if (contentType == null) {
66              contentType = _uploadRequest.getContentType(name);
67          }
68  
69          return contentType;
70      }
71  
72      public File getFile(String name) {
73          File file = _uploadRequest.getFile(_namespace + name);
74  
75          if (file == null) {
76              file = _uploadRequest.getFile(name);
77          }
78  
79          return file;
80      }
81  
82      public String getFileName(String name) {
83          String fileName = _uploadRequest.getFileName(_namespace + name);
84  
85          if (fileName == null) {
86              fileName = _uploadRequest.getFileName(name);
87          }
88  
89          return fileName;
90      }
91  
92      public String getFullFileName(String name) {
93          String fullFileName = _uploadRequest.getFullFileName(_namespace + name);
94  
95          if (fullFileName == null) {
96              fullFileName = _uploadRequest.getFullFileName(name);
97          }
98  
99          return fullFileName;
100     }
101 
102     public String getParameter(String name) {
103         String parameter = _uploadRequest.getParameter(_namespace + name);
104 
105         if (parameter == null) {
106             parameter = _uploadRequest.getParameter(name);
107         }
108 
109         return parameter;
110     }
111 
112     public Map<String, String[]> getParameterMap() {
113         Map<String, String[]> map = new HashMap<String, String[]>();
114 
115         Enumeration<String> enu = getParameterNames();
116 
117         while (enu.hasMoreElements()) {
118             String name = enu.nextElement();
119 
120             map.put(name, getParameterValues(name));
121         }
122 
123         return map;
124     }
125 
126     public Enumeration<String> getParameterNames() {
127         List<String> parameterNames = new ArrayList<String>();
128 
129         Enumeration<String> enu = _uploadRequest.getParameterNames();
130 
131         while (enu.hasMoreElements()) {
132             String name = enu.nextElement();
133 
134             if (name.startsWith(_namespace)) {
135                 parameterNames.add(
136                     name.substring(_namespace.length(), name.length()));
137             }
138             else {
139                 parameterNames.add(name);
140             }
141         }
142 
143         return Collections.enumeration(parameterNames);
144     }
145 
146     public String[] getParameterValues(String name) {
147         String[] parameterValues = _uploadRequest.getParameterValues(
148             _namespace + name);
149 
150         if (parameterValues == null) {
151             parameterValues = _uploadRequest.getParameterValues(name);
152         }
153 
154         return parameterValues;
155     }
156 
157     public boolean isFormField(String name) {
158         Boolean formField = _uploadRequest.isFormField(_namespace + name);
159 
160         if (formField == null) {
161             formField = _uploadRequest.isFormField(name);
162         }
163 
164         if (formField == null) {
165             return true;
166         }
167         else {
168             return formField.booleanValue();
169         }
170     }
171 
172     private UploadServletRequest _uploadRequest;
173     private String _namespace;
174 
175 }