001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.upload;
016    
017    import com.liferay.portal.kernel.upload.FileItem;
018    import com.liferay.portal.kernel.upload.UploadPortletRequest;
019    import com.liferay.portal.kernel.upload.UploadServletRequest;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.MimeTypesUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.io.File;
025    import java.io.IOException;
026    import java.io.InputStream;
027    
028    import java.util.ArrayList;
029    import java.util.Collections;
030    import java.util.Enumeration;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    import javax.servlet.http.HttpServletRequestWrapper;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Harry Mark
040     */
041    public class UploadPortletRequestImpl
042            extends HttpServletRequestWrapper implements UploadPortletRequest {
043    
044            public UploadPortletRequestImpl(
045                    UploadServletRequest uploadServletRequest, String namespace) {
046    
047                    super(uploadServletRequest);
048    
049                    _uploadServletRequest = uploadServletRequest;
050                    _namespace = namespace;
051            }
052    
053            public void cleanUp() {
054                    _uploadServletRequest.cleanUp();
055            }
056    
057            public String getContentType(String name) {
058                    String contentType = _uploadServletRequest.getContentType(
059                            _namespace.concat(name));
060    
061                    if (contentType == null) {
062                            contentType = _uploadServletRequest.getContentType(name);
063                    }
064    
065                    if (Validator.isNull(contentType) ||
066                            contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
067    
068                            contentType = MimeTypesUtil.getContentType(getFile(name));
069                    }
070    
071                    return contentType;
072            }
073    
074            public File getFile(String name) {
075                    return getFile(name, false);
076            }
077    
078            public File getFile(String name, boolean forceCreate) {
079                    File file = _uploadServletRequest.getFile(
080                            _namespace.concat(name), forceCreate);
081    
082                    if (file == null) {
083                            file = _uploadServletRequest.getFile(name, forceCreate);
084                    }
085    
086                    return file;
087            }
088    
089            public InputStream getFileAsStream(String name) throws IOException {
090                    return getFileAsStream(name, true);
091            }
092    
093            public InputStream getFileAsStream(String name, boolean deleteOnClose)
094                    throws IOException {
095    
096                    InputStream inputStream = _uploadServletRequest.getFileAsStream(
097                            _namespace.concat(name), deleteOnClose);
098    
099                    if (inputStream == null) {
100                            inputStream = _uploadServletRequest.getFileAsStream(
101                                    name, deleteOnClose);
102                    }
103    
104                    return inputStream;
105            }
106    
107            public String getFileName(String name) {
108                    String fileName = _uploadServletRequest.getFileName(
109                            _namespace.concat(name));
110    
111                    if (fileName == null) {
112                            fileName = _uploadServletRequest.getFileName(name);
113                    }
114    
115                    return fileName;
116            }
117    
118            public String[] getFileNames(String name) {
119                    String[] fileNames = _uploadServletRequest.getFileNames(
120                            _namespace.concat(name));
121    
122                    if (fileNames == null) {
123                            fileNames = _uploadServletRequest.getFileNames(name);
124                    }
125    
126                    return fileNames;
127            }
128    
129            public File[] getFiles(String name) {
130                    File[] files = _uploadServletRequest.getFiles(_namespace.concat(name));
131    
132                    if (files == null) {
133                            files = _uploadServletRequest.getFiles(name);
134                    }
135    
136                    return files;
137            }
138    
139            public InputStream[] getFilesAsStream(String name) throws IOException {
140                    return getFilesAsStream(name, true);
141            }
142    
143            public InputStream[] getFilesAsStream(String name, boolean deleteOnClose)
144                    throws IOException {
145    
146                    InputStream[] inputStreams = _uploadServletRequest.getFilesAsStream(
147                            _namespace.concat(name), deleteOnClose);
148    
149                    if (inputStreams == null) {
150                            inputStreams = _uploadServletRequest.getFilesAsStream(
151                                    name, deleteOnClose);
152                    }
153    
154                    return inputStreams;
155            }
156    
157            public String getFullFileName(String name) {
158                    String fullFileName = _uploadServletRequest.getFullFileName(
159                            _namespace.concat(name));
160    
161                    if (fullFileName == null) {
162                            fullFileName = _uploadServletRequest.getFullFileName(name);
163                    }
164    
165                    return fullFileName;
166            }
167    
168            public Map<String, FileItem[]> getMultipartParameterMap() {
169                    if (!(_uploadServletRequest instanceof UploadServletRequestImpl)) {
170                            return Collections.emptyMap();
171                    }
172    
173                    Map<String, FileItem[]> map = new HashMap<String, FileItem[]>();
174    
175                    UploadServletRequestImpl uploadServletRequestImpl =
176                            (UploadServletRequestImpl)_uploadServletRequest;
177    
178                    Map<String, FileItem[]> multipartParameterMap =
179                            uploadServletRequestImpl.getMultipartParameterMap();
180    
181                    for (String name : multipartParameterMap.keySet()) {
182                            if (name.startsWith(_namespace)) {
183                                    map.put(
184                                            name.substring(_namespace.length(), name.length()),
185                                            multipartParameterMap.get(name));
186                            }
187                            else {
188                                    map.put(name, multipartParameterMap.get(name));
189                            }
190                    }
191    
192                    return map;
193            }
194    
195            @Override
196            public String getParameter(String name) {
197                    String parameter = _uploadServletRequest.getParameter(
198                            _namespace.concat(name));
199    
200                    if (parameter == null) {
201                            parameter = _uploadServletRequest.getParameter(name);
202                    }
203    
204                    return parameter;
205            }
206    
207            @Override
208            public Map<String, String[]> getParameterMap() {
209                    Map<String, String[]> map = new HashMap<String, String[]>();
210    
211                    Enumeration<String> enu = getParameterNames();
212    
213                    while (enu.hasMoreElements()) {
214                            String name = enu.nextElement();
215    
216                            map.put(name, getParameterValues(name));
217                    }
218    
219                    return map;
220            }
221    
222            @Override
223            public Enumeration<String> getParameterNames() {
224                    List<String> parameterNames = new ArrayList<String>();
225    
226                    Enumeration<String> enu = _uploadServletRequest.getParameterNames();
227    
228                    while (enu.hasMoreElements()) {
229                            String name = enu.nextElement();
230    
231                            if (name.startsWith(_namespace)) {
232                                    parameterNames.add(name.substring(_namespace.length()));
233                            }
234                            else {
235                                    parameterNames.add(name);
236                            }
237                    }
238    
239                    return Collections.enumeration(parameterNames);
240            }
241    
242            @Override
243            public String[] getParameterValues(String name) {
244                    String[] parameterValues = _uploadServletRequest.getParameterValues(
245                            _namespace.concat(name));
246    
247                    if (parameterValues == null) {
248                            parameterValues = _uploadServletRequest.getParameterValues(name);
249                    }
250    
251                    return parameterValues;
252            }
253    
254            public Map<String, List<String>> getRegularParameterMap() {
255                    if (!(_uploadServletRequest instanceof UploadServletRequestImpl)) {
256                            return Collections.emptyMap();
257                    }
258    
259                    Map<String, List<String>> map = new HashMap<String, List<String>>();
260    
261                    UploadServletRequestImpl uploadServletRequestImpl =
262                            (UploadServletRequestImpl)_uploadServletRequest;
263    
264                    Map<String, List<String>> regularParameterMap =
265                            uploadServletRequestImpl.getRegularParameterMap();
266    
267                    for (String name : regularParameterMap.keySet()) {
268                            if (name.startsWith(_namespace)) {
269                                    map.put(
270                                            name.substring(_namespace.length(), name.length()),
271                                            regularParameterMap.get(name));
272                            }
273                            else {
274                                    map.put(name, regularParameterMap.get(name));
275                            }
276                    }
277    
278                    return map;
279            }
280    
281            public Long getSize(String name) {
282                    Long size = _uploadServletRequest.getSize(_namespace.concat(name));
283    
284                    if (size == null) {
285                            size = _uploadServletRequest.getSize(name);
286                    }
287    
288                    if (size == null) {
289                            return Long.valueOf(0);
290                    }
291    
292                    return size;
293            }
294    
295            public Boolean isFormField(String name) {
296                    Boolean formField = _uploadServletRequest.isFormField(
297                            _namespace.concat(name));
298    
299                    if (formField == null) {
300                            formField = _uploadServletRequest.isFormField(name);
301                    }
302    
303                    if (formField == null) {
304                            return true;
305                    }
306                    else {
307                            return formField.booleanValue();
308                    }
309            }
310    
311            private String _namespace;
312            private UploadServletRequest _uploadServletRequest;
313    
314    }