001    /**
002     * Copyright (c) 2000-2013 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.portlet.documentlibrary.store;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
021    
022    import java.io.File;
023    import java.io.InputStream;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     * @author Edward Han
028     */
029    public class SafeFileNameStoreWrapper implements Store {
030    
031            public SafeFileNameStoreWrapper(Store store) {
032                    _store = store;
033            }
034    
035            @Override
036            public void addDirectory(long companyId, long repositoryId, String dirName)
037                    throws PortalException, SystemException {
038    
039                    String safeDirName = FileUtil.encodeSafeFileName(dirName);
040    
041                    if (!safeDirName.equals(dirName)) {
042                            try {
043                                    _store.move(dirName, safeDirName);
044                            }
045                            catch (Exception e) {
046                            }
047                    }
048    
049                    _store.addDirectory(companyId, repositoryId, safeDirName);
050            }
051    
052            @Override
053            public void addFile(
054                            long companyId, long repositoryId, String fileName, byte[] bytes)
055                    throws PortalException, SystemException {
056    
057                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
058    
059                    renameUnsafeFile(companyId, repositoryId, fileName, safeFileName);
060    
061                    _store.addFile(companyId, repositoryId, safeFileName, bytes);
062            }
063    
064            @Override
065            public void addFile(
066                            long companyId, long repositoryId, String fileName, File file)
067                    throws PortalException, SystemException {
068    
069                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
070    
071                    renameUnsafeFile(companyId, repositoryId, fileName, safeFileName);
072    
073                    _store.addFile(companyId, repositoryId, safeFileName, file);
074            }
075    
076            @Override
077            public void addFile(
078                            long companyId, long repositoryId, String fileName, InputStream is)
079                    throws PortalException, SystemException {
080    
081                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
082    
083                    renameUnsafeFile(companyId, repositoryId, fileName, safeFileName);
084    
085                    _store.addFile(companyId, repositoryId, safeFileName, is);
086            }
087    
088            @Override
089            public void checkRoot(long companyId) throws SystemException {
090                    _store.checkRoot(companyId);
091            }
092    
093            @Override
094            public void copyFileVersion(
095                            long companyId, long repositoryId, String fileName,
096                            String fromVersionLabel, String toVersionLabel)
097                    throws PortalException, SystemException {
098    
099                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
100    
101                    renameUnsafeFile(companyId, repositoryId, fileName, safeFileName);
102    
103                    _store.copyFileVersion(
104                            companyId, repositoryId, safeFileName, fromVersionLabel,
105                            toVersionLabel);
106            }
107    
108            @Override
109            public void deleteDirectory(
110                            long companyId, long repositoryId, String dirName)
111                    throws PortalException, SystemException {
112    
113                    String safeDirName = FileUtil.encodeSafeFileName(dirName);
114    
115                    if (!safeDirName.equals(dirName)) {
116                            try {
117                                    _store.deleteDirectory(companyId, repositoryId, dirName);
118    
119                                    return;
120                            }
121                            catch (Exception e) {
122                            }
123                    }
124    
125                    _store.deleteDirectory(companyId, repositoryId, safeDirName);
126            }
127    
128            @Override
129            public void deleteFile(long companyId, long repositoryId, String fileName)
130                    throws PortalException, SystemException {
131    
132                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
133    
134                    if (!safeFileName.equals(fileName) &&
135                            _store.hasFile(companyId, repositoryId, fileName)) {
136    
137                            _store.deleteFile(companyId, repositoryId, fileName);
138    
139                            return;
140                    }
141    
142                    _store.deleteFile(companyId, repositoryId, safeFileName);
143            }
144    
145            @Override
146            public void deleteFile(
147                            long companyId, long repositoryId, String fileName,
148                            String versionLabel)
149                    throws PortalException, SystemException {
150    
151                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
152    
153                    if (!safeFileName.equals(fileName) &&
154                            _store.hasFile(companyId, repositoryId, fileName, versionLabel)) {
155    
156                            _store.deleteFile(companyId, repositoryId, fileName, versionLabel);
157    
158                            return;
159                    }
160    
161                    _store.deleteFile(companyId, repositoryId, safeFileName, versionLabel);
162            }
163    
164            @Override
165            public File getFile(long companyId, long repositoryId, String fileName)
166                    throws PortalException, SystemException {
167    
168                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
169    
170                    if (!safeFileName.equals(fileName) &&
171                            _store.hasFile(companyId, repositoryId, fileName)) {
172    
173                            return _store.getFile(companyId, repositoryId, fileName);
174                    }
175    
176                    return _store.getFile(companyId, repositoryId, safeFileName);
177            }
178    
179            @Override
180            public File getFile(
181                            long companyId, long repositoryId, String fileName,
182                            String versionLabel)
183                    throws PortalException, SystemException {
184    
185                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
186    
187                    if (!safeFileName.equals(fileName) &&
188                            _store.hasFile(companyId, repositoryId, fileName, versionLabel)) {
189    
190                            return _store.getFile(
191                                    companyId, repositoryId, fileName, versionLabel);
192                    }
193    
194                    return _store.getFile(
195                            companyId, repositoryId, safeFileName, versionLabel);
196            }
197    
198            @Override
199            public byte[] getFileAsBytes(
200                            long companyId, long repositoryId, String fileName)
201                    throws PortalException, SystemException {
202    
203                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
204    
205                    if (!safeFileName.equals(fileName) &&
206                            _store.hasFile(companyId, repositoryId, fileName)) {
207    
208                            return _store.getFileAsBytes(companyId, repositoryId, fileName);
209                    }
210    
211                    return _store.getFileAsBytes(companyId, repositoryId, safeFileName);
212            }
213    
214            @Override
215            public byte[] getFileAsBytes(
216                            long companyId, long repositoryId, String fileName,
217                            String versionLabel)
218                    throws PortalException, SystemException {
219    
220                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
221    
222                    if (!safeFileName.equals(fileName) &&
223                            _store.hasFile(companyId, repositoryId, fileName, versionLabel)) {
224    
225                            return _store.getFileAsBytes(
226                                    companyId, repositoryId, fileName, versionLabel);
227                    }
228    
229                    return _store.getFileAsBytes(
230                            companyId, repositoryId, safeFileName, versionLabel);
231            }
232    
233            @Override
234            public InputStream getFileAsStream(
235                            long companyId, long repositoryId, String fileName)
236                    throws PortalException, SystemException {
237    
238                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
239    
240                    if (!safeFileName.equals(fileName) &&
241                            _store.hasFile(companyId, repositoryId, fileName)) {
242    
243                            return _store.getFileAsStream(companyId, repositoryId, fileName);
244                    }
245    
246                    return _store.getFileAsStream(companyId, repositoryId, safeFileName);
247            }
248    
249            @Override
250            public InputStream getFileAsStream(
251                            long companyId, long repositoryId, String fileName,
252                            String versionLabel)
253                    throws PortalException, SystemException {
254    
255                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
256    
257                    if (!safeFileName.equals(fileName) &&
258                            _store.hasFile(companyId, repositoryId, fileName, versionLabel)) {
259    
260                            return _store.getFileAsStream(
261                                    companyId, repositoryId, fileName, versionLabel);
262                    }
263    
264                    return _store.getFileAsStream(
265                            companyId, repositoryId, safeFileName, versionLabel);
266            }
267    
268            @Override
269            public String[] getFileNames(long companyId, long repositoryId)
270                    throws SystemException {
271    
272                    String[] fileNames = _store.getFileNames(companyId, repositoryId);
273    
274                    String[] decodedFileNames = new String[fileNames.length];
275    
276                    for (int i = 0; i < fileNames.length; i++) {
277                            decodedFileNames[i] = FileUtil.decodeSafeFileName(fileNames[i]);
278                    }
279    
280                    return decodedFileNames;
281            }
282    
283            @Override
284            public String[] getFileNames(
285                            long companyId, long repositoryId, String dirName)
286                    throws PortalException, SystemException {
287    
288                    String safeDirName = FileUtil.encodeSafeFileName(dirName);
289    
290                    if (!safeDirName.equals(dirName)) {
291                            try {
292                                    _store.move(dirName, safeDirName);
293                            }
294                            catch (Exception e) {
295                            }
296                    }
297    
298                    String[] fileNames = _store.getFileNames(
299                            companyId, repositoryId, safeDirName);
300    
301                    String[] decodedFileNames = new String[fileNames.length];
302    
303                    for (int i = 0; i < fileNames.length; i++) {
304                            decodedFileNames[i] = FileUtil.decodeSafeFileName(fileNames[i]);
305                    }
306    
307                    return decodedFileNames;
308            }
309    
310            @Override
311            public long getFileSize(long companyId, long repositoryId, String fileName)
312                    throws PortalException, SystemException {
313    
314                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
315    
316                    if (!safeFileName.equals(fileName) &&
317                            _store.hasFile(companyId, repositoryId, fileName)) {
318    
319                            return _store.getFileSize(companyId, repositoryId, fileName);
320                    }
321    
322                    return _store.getFileSize(companyId, repositoryId, safeFileName);
323            }
324    
325            @Override
326            public boolean hasDirectory(
327                            long companyId, long repositoryId, String dirName)
328                    throws PortalException, SystemException {
329    
330                    String safeDirName = FileUtil.encodeSafeFileName(dirName);
331    
332                    return _store.hasDirectory(companyId, repositoryId, safeDirName);
333            }
334    
335            @Override
336            public boolean hasFile(long companyId, long repositoryId, String fileName)
337                    throws PortalException, SystemException {
338    
339                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
340    
341                    if (!safeFileName.equals(fileName) &&
342                            _store.hasFile(companyId, repositoryId, fileName)) {
343    
344                            return true;
345                    }
346    
347                    return _store.hasFile(companyId, repositoryId, safeFileName);
348            }
349    
350            @Override
351            public boolean hasFile(
352                            long companyId, long repositoryId, String fileName,
353                            String versionLabel)
354                    throws PortalException, SystemException {
355    
356                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
357    
358                    if (!safeFileName.equals(fileName) &&
359                            _store.hasFile(companyId, repositoryId, fileName, versionLabel)) {
360    
361                            return true;
362                    }
363    
364                    return _store.hasFile(
365                            companyId, repositoryId, safeFileName, versionLabel);
366            }
367    
368            @Override
369            public void move(String srcDir, String destDir) throws SystemException {
370                    _store.move(srcDir, destDir);
371            }
372    
373            @Override
374            public void updateFile(
375                            long companyId, long repositoryId, long newRepositoryId,
376                            String fileName)
377                    throws PortalException, SystemException {
378    
379                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
380    
381                    renameUnsafeFile(companyId, repositoryId, fileName, safeFileName);
382    
383                    _store.updateFile(
384                            companyId, repositoryId, newRepositoryId, safeFileName);
385            }
386    
387            @Override
388            public void updateFile(
389                            long companyId, long repositoryId, String fileName,
390                            String newFileName)
391                    throws PortalException, SystemException {
392    
393                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
394                    String safeNewFileName = FileUtil.encodeSafeFileName(newFileName);
395    
396                    if (!safeFileName.equals(fileName)) {
397                            if (_store.hasFile(
398                                            companyId, repositoryId, fileName,
399                                            DLFileEntryConstants.VERSION_DEFAULT)) {
400    
401                                    safeFileName = fileName;
402                            }
403                    }
404    
405                    _store.updateFile(
406                            companyId, repositoryId, safeFileName, safeNewFileName);
407            }
408    
409            @Override
410            public void updateFile(
411                            long companyId, long repositoryId, String fileName,
412                            String versionLabel, byte[] bytes)
413                    throws PortalException, SystemException {
414    
415                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
416    
417                    renameUnsafeFile(companyId, repositoryId, fileName, safeFileName);
418    
419                    _store.updateFile(
420                            companyId, repositoryId, safeFileName, versionLabel, bytes);
421            }
422    
423            @Override
424            public void updateFile(
425                            long companyId, long repositoryId, String fileName,
426                            String versionLabel, File file)
427                    throws PortalException, SystemException {
428    
429                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
430    
431                    renameUnsafeFile(companyId, repositoryId, fileName, safeFileName);
432    
433                    _store.updateFile(
434                            companyId, repositoryId, safeFileName, versionLabel, file);
435            }
436    
437            @Override
438            public void updateFile(
439                            long companyId, long repositoryId, String fileName,
440                            String versionLabel, InputStream is)
441                    throws PortalException, SystemException {
442    
443                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
444    
445                    renameUnsafeFile(companyId, repositoryId, fileName, safeFileName);
446    
447                    _store.updateFile(
448                            companyId, repositoryId, safeFileName, versionLabel, is);
449            }
450    
451            @Override
452            public void updateFileVersion(
453                            long companyId, long repositoryId, String fileName,
454                            String fromVersionLabel, String toVersionLabel)
455                    throws PortalException, SystemException {
456    
457                    String safeFileName = FileUtil.encodeSafeFileName(fileName);
458    
459                    renameUnsafeFile(companyId, repositoryId, fileName, safeFileName);
460    
461                    _store.updateFileVersion(
462                            companyId, repositoryId, safeFileName, fromVersionLabel,
463                            toVersionLabel);
464            }
465    
466            protected void renameUnsafeFile(
467                            long companyId, long repositoryId, String fileName,
468                            String safeFileName)
469                    throws PortalException, SystemException {
470    
471                    if (!safeFileName.equals(fileName)) {
472                            if (_store.hasFile(
473                                            companyId, repositoryId, fileName,
474                                            DLFileEntryConstants.VERSION_DEFAULT)) {
475    
476                                    _store.updateFile(
477                                            companyId, repositoryId, fileName, safeFileName);
478                            }
479                    }
480            }
481    
482            private Store _store;
483    
484    }