1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.documentlibrary.util;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.search.SearchException;
20  import com.liferay.portal.kernel.util.FileUtil;
21  import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
22  
23  import java.io.File;
24  import java.io.InputStream;
25  
26  import java.util.Date;
27  
28  /**
29   * <a href="SafeFileNameHookWrapper.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class SafeFileNameHookWrapper implements Hook {
34  
35      public SafeFileNameHookWrapper(Hook hook) {
36          _hook = hook;
37      }
38  
39      public void addDirectory(long companyId, long repositoryId, String dirName)
40          throws PortalException, SystemException {
41  
42          String safeDirName = FileUtil.encodeSafeFileName(dirName);
43  
44          if (!safeDirName.equals(dirName)) {
45              try {
46                  _hook.move(dirName, safeDirName);
47              }
48              catch (Exception e) {
49              }
50          }
51  
52          _hook.addDirectory(companyId, repositoryId, safeDirName);
53      }
54  
55      public void addFile(
56              long companyId, String portletId, long groupId, long repositoryId,
57              String fileName, long fileEntryId, String properties,
58              Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
59              byte[] bytes)
60          throws PortalException, SystemException {
61  
62          String safeFileName = FileUtil.encodeSafeFileName(fileName);
63  
64          renameUnsafeFile(
65              companyId, portletId, groupId, repositoryId, fileName,
66              safeFileName);
67  
68          _hook.addFile(
69              companyId, portletId, groupId, repositoryId, safeFileName,
70              fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries,
71              bytes);
72      }
73  
74      public void addFile(
75              long companyId, String portletId, long groupId, long repositoryId,
76              String fileName, long fileEntryId, String properties,
77              Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
78              File file)
79          throws PortalException, SystemException {
80  
81          String safeFileName = FileUtil.encodeSafeFileName(fileName);
82  
83          renameUnsafeFile(
84              companyId, portletId, groupId, repositoryId, fileName,
85              safeFileName);
86  
87          _hook.addFile(
88              companyId, portletId, groupId, repositoryId, safeFileName,
89              fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries,
90              file);
91      }
92  
93      public void addFile(
94              long companyId, String portletId, long groupId, long repositoryId,
95              String fileName, long fileEntryId, String properties,
96              Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
97              InputStream is)
98          throws PortalException, SystemException {
99  
100         String safeFileName = FileUtil.encodeSafeFileName(fileName);
101 
102         renameUnsafeFile(
103             companyId, portletId, groupId, repositoryId, fileName,
104             safeFileName);
105 
106         _hook.addFile(
107             companyId, portletId, groupId, repositoryId, safeFileName,
108             fileEntryId, properties, modifiedDate, tagsCategories, tagsEntries,
109             is);
110     }
111 
112     public void checkRoot(long companyId) throws SystemException {
113         _hook.checkRoot(companyId);
114     }
115 
116     public void deleteDirectory(
117             long companyId, String portletId, long repositoryId, String dirName)
118         throws PortalException, SystemException {
119 
120         String safeDirName = FileUtil.encodeSafeFileName(dirName);
121 
122         if (!safeDirName.equals(dirName)) {
123             try {
124                 _hook.deleteDirectory(
125                     companyId, portletId, repositoryId, dirName);
126 
127                 return;
128             }
129             catch (Exception e) {
130             }
131         }
132 
133         _hook.deleteDirectory(companyId, portletId, repositoryId, safeDirName);
134     }
135 
136     public void deleteFile(
137             long companyId, String portletId, long repositoryId,
138             String fileName)
139         throws PortalException, SystemException {
140 
141         String safeFileName = FileUtil.encodeSafeFileName(fileName);
142 
143         if (!safeFileName.equals(fileName) &&
144             _hook.hasFile(
145                 companyId, repositoryId, fileName,
146                 DLFileEntryConstants.DEFAULT_VERSION)) {
147 
148             _hook.deleteFile(companyId, portletId, repositoryId, fileName);
149 
150             return;
151         }
152 
153         _hook.deleteFile(companyId, portletId, repositoryId, safeFileName);
154     }
155 
156     public void deleteFile(
157             long companyId, String portletId, long repositoryId,
158             String fileName, double versionNumber)
159         throws PortalException, SystemException {
160 
161         String safeFileName = FileUtil.encodeSafeFileName(fileName);
162 
163         if (!safeFileName.equals(fileName) &&
164             _hook.hasFile(
165                 companyId, repositoryId, fileName, versionNumber)) {
166 
167             _hook.deleteFile(
168                 companyId, portletId, repositoryId, fileName, versionNumber);
169 
170             return;
171         }
172 
173         _hook.deleteFile(
174             companyId, portletId, repositoryId, safeFileName, versionNumber);
175     }
176 
177     public byte[] getFile(long companyId, long repositoryId, String fileName)
178         throws PortalException, SystemException {
179 
180         String safeFileName = FileUtil.encodeSafeFileName(fileName);
181 
182         if (!safeFileName.equals(fileName) &&
183             _hook.hasFile(
184                 companyId, repositoryId, fileName,
185                 DLFileEntryConstants.DEFAULT_VERSION)) {
186 
187             return _hook.getFile(companyId, repositoryId, fileName);
188         }
189 
190         return _hook.getFile(companyId, repositoryId, safeFileName);
191     }
192 
193     public byte[] getFile(
194             long companyId, long repositoryId, String fileName,
195             double versionNumber)
196         throws PortalException, SystemException {
197 
198         String safeFileName = FileUtil.encodeSafeFileName(fileName);
199 
200         if (!safeFileName.equals(fileName) &&
201             _hook.hasFile(companyId, repositoryId, fileName, versionNumber)) {
202 
203             return _hook.getFile(
204                 companyId, repositoryId, fileName, versionNumber);
205         }
206 
207         return _hook.getFile(
208             companyId, repositoryId, safeFileName, versionNumber);
209     }
210 
211     public InputStream getFileAsStream(
212             long companyId, long repositoryId, String fileName)
213         throws PortalException, SystemException {
214 
215         String safeFileName = FileUtil.encodeSafeFileName(fileName);
216 
217         if (!safeFileName.equals(fileName) &&
218             _hook.hasFile(
219                 companyId, repositoryId, fileName,
220                 DLFileEntryConstants.DEFAULT_VERSION)) {
221 
222             return _hook.getFileAsStream(companyId, repositoryId, fileName);
223         }
224 
225         return _hook.getFileAsStream(companyId, repositoryId, safeFileName);
226     }
227 
228     public InputStream getFileAsStream(
229             long companyId, long repositoryId, String fileName,
230             double versionNumber)
231         throws PortalException, SystemException {
232 
233         String safeFileName = FileUtil.encodeSafeFileName(fileName);
234 
235         if (!safeFileName.equals(fileName) &&
236             _hook.hasFile(
237                 companyId, repositoryId, fileName, versionNumber)) {
238 
239             return _hook.getFileAsStream(
240                 companyId, repositoryId, fileName, versionNumber);
241         }
242 
243         return _hook.getFileAsStream(
244             companyId, repositoryId, safeFileName, versionNumber);
245     }
246 
247     public String[] getFileNames(
248             long companyId, long repositoryId, String dirName)
249         throws PortalException, SystemException {
250 
251         String safeDirName = FileUtil.encodeSafeFileName(dirName);
252 
253         if (!safeDirName.equals(dirName)) {
254             try {
255                 _hook.move(dirName, safeDirName);
256             }
257             catch (Exception e) {
258             }
259         }
260 
261         String[] fileNames = _hook.getFileNames(
262             companyId, repositoryId, safeDirName);
263 
264         String[] decodedFileNames = new String[fileNames.length];
265 
266         for (int i = 0; i < fileNames.length; i++) {
267             decodedFileNames[i] = FileUtil.decodeSafeFileName(fileNames[i]);
268         }
269 
270         return decodedFileNames;
271     }
272 
273     public long getFileSize(
274             long companyId, long repositoryId, String fileName)
275         throws PortalException, SystemException {
276 
277         String safeFileName = FileUtil.encodeSafeFileName(fileName);
278 
279         if (!safeFileName.equals(fileName) &&
280             _hook.hasFile(
281                 companyId, repositoryId, fileName,
282                 DLFileEntryConstants.DEFAULT_VERSION)) {
283 
284             return _hook.getFileSize(companyId, repositoryId, fileName);
285         }
286 
287         return _hook.getFileSize(companyId, repositoryId, safeFileName);
288     }
289 
290     public boolean hasFile(
291             long companyId, long repositoryId, String fileName,
292             double versionNumber)
293         throws PortalException, SystemException {
294 
295         String safeFileName = FileUtil.encodeSafeFileName(fileName);
296 
297         if (!safeFileName.equals(fileName) &&
298             _hook.hasFile(companyId, repositoryId, fileName, versionNumber)) {
299 
300             return true;
301         }
302 
303         return _hook.hasFile(
304             companyId, repositoryId, safeFileName, versionNumber);
305     }
306 
307     public void move(String srcDir, String destDir) throws SystemException {
308         _hook.move(srcDir, destDir);
309     }
310 
311     public void reIndex(String[] ids) throws SearchException {
312         _hook.reIndex(ids);
313     }
314 
315     public void updateFile(
316             long companyId, String portletId, long groupId, long repositoryId,
317             long newRepositoryId, String fileName, long fileEntryId)
318         throws PortalException, SystemException {
319 
320         String safeFileName = FileUtil.encodeSafeFileName(fileName);
321 
322         renameUnsafeFile(
323             companyId, portletId, groupId, repositoryId, fileName,
324             safeFileName);
325 
326         _hook.updateFile(
327             companyId, portletId, groupId, repositoryId, newRepositoryId,
328             safeFileName, fileEntryId);
329     }
330 
331     public void updateFile(
332             long companyId, String portletId, long groupId, long repositoryId,
333             String fileName, double versionNumber, String sourceFileName,
334             long fileEntryId, String properties, Date modifiedDate,
335             String[] tagsCategories, String[] tagsEntries, byte[] bytes)
336         throws PortalException, SystemException {
337 
338         String safeFileName = FileUtil.encodeSafeFileName(fileName);
339         String safeSourceFileName = FileUtil.encodeSafeFileName(sourceFileName);
340 
341         renameUnsafeFile(
342             companyId, portletId, groupId, repositoryId, fileName,
343             safeFileName);
344         renameUnsafeFile(
345             companyId, portletId, groupId, repositoryId, sourceFileName,
346             safeSourceFileName);
347 
348         _hook.updateFile(
349             companyId, portletId, groupId, repositoryId, safeFileName,
350             versionNumber, safeSourceFileName, fileEntryId, properties,
351             modifiedDate, tagsCategories, tagsEntries, bytes);
352     }
353 
354     public void updateFile(
355             long companyId, String portletId, long groupId, long repositoryId,
356             String fileName, double versionNumber, String sourceFileName,
357             long fileEntryId, String properties, Date modifiedDate,
358             String[] tagsCategories, String[] tagsEntries, File file)
359         throws PortalException, SystemException {
360 
361         String safeFileName = FileUtil.encodeSafeFileName(fileName);
362         String safeSourceFileName = FileUtil.encodeSafeFileName(sourceFileName);
363 
364         renameUnsafeFile(
365             companyId, portletId, groupId, repositoryId, fileName,
366             safeFileName);
367         renameUnsafeFile(
368             companyId, portletId, groupId, repositoryId, sourceFileName,
369             safeSourceFileName);
370 
371         _hook.updateFile(
372             companyId, portletId, groupId, repositoryId, safeFileName,
373             versionNumber, safeSourceFileName, fileEntryId, properties,
374             modifiedDate, tagsCategories, tagsEntries, file);
375     }
376 
377     public void updateFile(
378             long companyId, String portletId, long groupId, long repositoryId,
379             String fileName, double versionNumber, String sourceFileName,
380             long fileEntryId, String properties, Date modifiedDate,
381             String[] tagsCategories, String[] tagsEntries, InputStream is)
382         throws PortalException, SystemException {
383 
384         String safeFileName = FileUtil.encodeSafeFileName(fileName);
385         String safeSourceFileName = FileUtil.encodeSafeFileName(sourceFileName);
386 
387         renameUnsafeFile(
388             companyId, portletId, groupId, repositoryId, fileName,
389             safeFileName);
390         renameUnsafeFile(
391             companyId, portletId, groupId, repositoryId, sourceFileName,
392             safeSourceFileName);
393 
394         _hook.updateFile(
395             companyId, portletId, groupId, repositoryId, safeFileName,
396             versionNumber, safeSourceFileName, fileEntryId, properties,
397             modifiedDate, tagsCategories, tagsEntries, is);
398     }
399 
400     public void updateFile(
401             long companyId, String portletId, long groupId, long repositoryId,
402             String fileName, String newFileName, boolean reIndex)
403         throws PortalException, SystemException {
404 
405         String safeFileName = FileUtil.encodeSafeFileName(fileName);
406         String safeNewFileName = FileUtil.encodeSafeFileName(newFileName);
407 
408         if (!safeFileName.equals(fileName)) {
409             if (_hook.hasFile(
410                     companyId, repositoryId, fileName,
411                     DLFileEntryConstants.DEFAULT_VERSION)) {
412 
413                 safeFileName = fileName;
414             }
415         }
416 
417         _hook.updateFile(
418             companyId, portletId, groupId, repositoryId, safeFileName,
419             safeNewFileName, reIndex);
420     }
421 
422     protected void renameUnsafeFile(
423             long companyId, String portletId, long groupId, long repositoryId,
424             String fileName, String safeFileName)
425         throws PortalException, SystemException {
426 
427         if (!safeFileName.equals(fileName)) {
428             if (_hook.hasFile(
429                     companyId, repositoryId, fileName,
430                     DLFileEntryConstants.DEFAULT_VERSION)) {
431 
432                 _hook.updateFile(
433                     companyId, portletId, groupId, repositoryId, fileName,
434                     safeFileName, true);
435             }
436         }
437     }
438 
439     private Hook _hook;
440 
441 }