001    /**
002     * Copyright (c) 2000-2011 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.UploadPortletRequest;
018    import com.liferay.portal.kernel.upload.UploadServletRequest;
019    import com.liferay.portal.kernel.util.ContentTypes;
020    import com.liferay.portal.kernel.util.MimeTypesUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.io.File;
024    import java.io.IOException;
025    import java.io.InputStream;
026    
027    import java.util.ArrayList;
028    import java.util.Collections;
029    import java.util.Enumeration;
030    import java.util.HashMap;
031    import java.util.List;
032    import java.util.Map;
033    
034    import javax.servlet.http.HttpServletRequestWrapper;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Harry Mark
039     */
040    public class UploadPortletRequestImpl
041            extends HttpServletRequestWrapper implements UploadPortletRequest {
042    
043            public UploadPortletRequestImpl(
044                    UploadServletRequest uploadServletRequest, String namespace) {
045    
046                    super(uploadServletRequest);
047    
048                    _uploadServletRequest = uploadServletRequest;
049                    _namespace = namespace;
050            }
051    
052            public void cleanUp() {
053                    _uploadServletRequest.cleanUp();
054            }
055    
056            public String getContentType(String name) {
057                    String contentType = _uploadServletRequest.getContentType(
058                            _namespace.concat(name));
059    
060                    if (contentType == null) {
061                            contentType = _uploadServletRequest.getContentType(name);
062                    }
063    
064                    if (Validator.isNull(contentType) ||
065                            contentType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {
066    
067                            contentType = MimeTypesUtil.getContentType(getFile(name));
068                    }
069    
070                    return contentType;
071            }
072    
073            public File getFile(String name) {
074                    return getFile(name, false);
075            }
076    
077            public File getFile(String name, boolean forceCreate) {
078                    File file = _uploadServletRequest.getFile(
079                            _namespace.concat(name), forceCreate);
080    
081                    if (file == null) {
082                            file = _uploadServletRequest.getFile(name, forceCreate);
083                    }
084    
085                    return file;
086            }
087    
088            public InputStream getFileAsStream(String name) throws IOException {
089                    return getFileAsStream(name, true);
090            }
091    
092            public InputStream getFileAsStream(String name, boolean deleteOnClose)
093                    throws IOException {
094    
095                    InputStream inputStream = _uploadServletRequest.getFileAsStream(
096                            _namespace.concat(name), deleteOnClose);
097    
098                    if (inputStream == null) {
099                            inputStream = _uploadServletRequest.getFileAsStream(
100                                    name, deleteOnClose);
101                    }
102    
103                    return inputStream;
104            }
105    
106            public String getFileName(String name) {
107                    String fileName = _uploadServletRequest.getFileName(
108                            _namespace.concat(name));
109    
110                    if (fileName == null) {
111                            fileName = _uploadServletRequest.getFileName(name);
112                    }
113    
114                    return fileName;
115            }
116    
117            public String[] getFileNames(String name) {
118                    String[] fileNames = _uploadServletRequest.getFileNames(
119                            _namespace.concat(name));
120    
121                    if (fileNames == null) {
122                            fileNames = _uploadServletRequest.getFileNames(name);
123                    }
124    
125                    return fileNames;
126            }
127    
128            public File[] getFiles(String name) {
129                    File[] files = _uploadServletRequest.getFiles(_namespace.concat(name));
130    
131                    if (files == null) {
132                            files = _uploadServletRequest.getFiles(name);
133                    }
134    
135                    return files;
136            }
137    
138            public InputStream[] getFilesAsStream(String name) throws IOException {
139                    return getFilesAsStream(name, true);
140            }
141    
142            public InputStream[] getFilesAsStream(String name, boolean deleteOnClose)
143                    throws IOException {
144    
145                    InputStream[] inputStreams = _uploadServletRequest.getFilesAsStream(
146                            _namespace.concat(name), deleteOnClose);
147    
148                    if (inputStreams == null) {
149                            inputStreams = _uploadServletRequest.getFilesAsStream(
150                                    name, deleteOnClose);
151                    }
152    
153                    return inputStreams;
154            }
155    
156            public String getFullFileName(String name) {
157                    String fullFileName = _uploadServletRequest.getFullFileName(
158                            _namespace.concat(name));
159    
160                    if (fullFileName == null) {
161                            fullFileName = _uploadServletRequest.getFullFileName(name);
162                    }
163    
164                    return fullFileName;
165            }
166    
167            @Override
168            public String getParameter(String name) {
169                    String parameter = _uploadServletRequest.getParameter(
170                            _namespace.concat(name));
171    
172                    if (parameter == null) {
173                            parameter = _uploadServletRequest.getParameter(name);
174                    }
175    
176                    return parameter;
177            }
178    
179            @Override
180            public Map<String, String[]> getParameterMap() {
181                    Map<String, String[]> map = new HashMap<String, String[]>();
182    
183                    Enumeration<String> enu = getParameterNames();
184    
185                    while (enu.hasMoreElements()) {
186                            String name = enu.nextElement();
187    
188                            map.put(name, getParameterValues(name));
189                    }
190    
191                    return map;
192            }
193    
194            @Override
195            public Enumeration<String> getParameterNames() {
196                    List<String> parameterNames = new ArrayList<String>();
197    
198                    Enumeration<String> enu = _uploadServletRequest.getParameterNames();
199    
200                    while (enu.hasMoreElements()) {
201                            String name = enu.nextElement();
202    
203                            if (name.startsWith(_namespace)) {
204                                    parameterNames.add(
205                                            name.substring(_namespace.length(), name.length()));
206                            }
207                            else {
208                                    parameterNames.add(name);
209                            }
210                    }
211    
212                    return Collections.enumeration(parameterNames);
213            }
214    
215            @Override
216            public String[] getParameterValues(String name) {
217                    String[] parameterValues = _uploadServletRequest.getParameterValues(
218                            _namespace.concat(name));
219    
220                    if (parameterValues == null) {
221                            parameterValues = _uploadServletRequest.getParameterValues(name);
222                    }
223    
224                    return parameterValues;
225            }
226    
227            public long getSize(String name) {
228                    Long size = _uploadServletRequest.getSize(_namespace.concat(name));
229    
230                    if (size == null) {
231                            size = _uploadServletRequest.getSize(name);
232                    }
233    
234                    if (size == null) {
235                            return 0;
236                    }
237    
238                    return size;
239            }
240    
241            public boolean isFormField(String name) {
242                    Boolean formField = _uploadServletRequest.isFormField(
243                            _namespace.concat(name));
244    
245                    if (formField == null) {
246                            formField = _uploadServletRequest.isFormField(name);
247                    }
248    
249                    if (formField == null) {
250                            return true;
251                    }
252                    else {
253                            return formField.booleanValue();
254                    }
255            }
256    
257            private String _namespace;
258            private UploadServletRequest _uploadServletRequest;
259    
260    }