1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.documentlibrary.util;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.search.SearchException;
20  import com.liferay.portal.kernel.util.FileUtil;
21  import com.liferay.portal.service.ServiceContext;
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          _hook.addDirectory(
43              companyId, repositoryId, FileUtil.encodeSafeFileName(dirName));
44      }
45  
46      public void addFile(
47              long companyId, String portletId, long groupId, long repositoryId,
48              String fileName, long fileEntryId, String properties,
49              Date modifiedDate, ServiceContext serviceContext, byte[] bytes)
50          throws PortalException, SystemException {
51  
52          _hook.addFile(
53              companyId, portletId, groupId, repositoryId,
54              FileUtil.encodeSafeFileName(fileName), fileEntryId, properties,
55              modifiedDate, serviceContext, bytes);
56      }
57  
58      public void addFile(
59              long companyId, String portletId, long groupId, long repositoryId,
60              String fileName, long fileEntryId, String properties,
61              Date modifiedDate, ServiceContext serviceContext, File file)
62          throws PortalException, SystemException {
63  
64          _hook.addFile(
65              companyId, portletId, groupId, repositoryId,
66              FileUtil.encodeSafeFileName(fileName), fileEntryId, properties,
67              modifiedDate, serviceContext, file);
68      }
69  
70      public void addFile(
71              long companyId, String portletId, long groupId, long repositoryId,
72              String fileName, long fileEntryId, String properties,
73              Date modifiedDate, ServiceContext serviceContext, InputStream is)
74          throws PortalException, SystemException {
75  
76          _hook.addFile(
77              companyId, portletId, groupId, repositoryId,
78              FileUtil.encodeSafeFileName(fileName), fileEntryId,
79              properties, modifiedDate, serviceContext, is);
80      }
81  
82      public void checkRoot(long companyId) throws SystemException {
83          _hook.checkRoot(companyId);
84      }
85  
86      public void deleteDirectory(
87              long companyId, String portletId, long repositoryId, String dirName)
88          throws PortalException, SystemException {
89  
90          _hook.deleteDirectory(
91              companyId, portletId, repositoryId,
92              FileUtil.encodeSafeFileName(dirName));
93      }
94  
95      public void deleteFile(
96              long companyId, String portletId, long repositoryId,
97              String fileName)
98          throws PortalException, SystemException {
99  
100         _hook.deleteFile(
101             companyId, portletId, repositoryId,
102             FileUtil.encodeSafeFileName(fileName));
103     }
104 
105     public void deleteFile(
106             long companyId, String portletId, long repositoryId,
107             String fileName, String versionNumber)
108         throws PortalException, SystemException {
109 
110         _hook.deleteFile(
111             companyId, portletId, repositoryId,
112             FileUtil.encodeSafeFileName(fileName), versionNumber);
113     }
114 
115     public byte[] getFile(long companyId, long repositoryId, String fileName)
116         throws PortalException, SystemException {
117 
118         return _hook.getFile(
119             companyId, repositoryId, FileUtil.encodeSafeFileName(fileName));
120     }
121 
122     public byte[] getFile(
123             long companyId, long repositoryId, String fileName,
124             String versionNumber)
125         throws PortalException, SystemException {
126 
127         return _hook.getFile(
128             companyId, repositoryId, FileUtil.encodeSafeFileName(fileName),
129             versionNumber);
130     }
131 
132     public InputStream getFileAsStream(
133             long companyId, long repositoryId, String fileName)
134         throws PortalException, SystemException {
135 
136         return _hook.getFileAsStream(
137             companyId, repositoryId, FileUtil.encodeSafeFileName(fileName));
138     }
139 
140     public InputStream getFileAsStream(
141             long companyId, long repositoryId, String fileName,
142             String versionNumber)
143         throws PortalException, SystemException {
144 
145         return _hook.getFileAsStream(
146             companyId, repositoryId, FileUtil.encodeSafeFileName(fileName),
147             versionNumber);
148     }
149 
150     public String[] getFileNames(
151             long companyId, long repositoryId, String dirName)
152         throws PortalException, SystemException {
153 
154         String[] fileNames = _hook.getFileNames(
155             companyId, repositoryId, FileUtil.encodeSafeFileName(dirName));
156 
157         String[] decodedFileNames = new String[fileNames.length];
158 
159         for (int i = 0; i < fileNames.length; i++) {
160             decodedFileNames[i] = FileUtil.decodeSafeFileName(fileNames[i]);
161         }
162 
163         return decodedFileNames;
164     }
165 
166     public long getFileSize(
167             long companyId, long repositoryId, String fileName)
168         throws PortalException, SystemException {
169 
170         return _hook.getFileSize(
171             companyId, repositoryId, FileUtil.encodeSafeFileName(fileName));
172     }
173 
174     public boolean hasFile(
175             long companyId, long repositoryId, String fileName,
176             String versionNumber)
177         throws PortalException, SystemException {
178 
179         return _hook.hasFile(
180             companyId, repositoryId, FileUtil.encodeSafeFileName(fileName),
181             versionNumber);
182     }
183 
184     public void move(String srcDir, String destDir) throws SystemException {
185         _hook.move(srcDir, destDir);
186     }
187 
188     public void reindex(String[] ids) throws SearchException {
189         _hook.reindex(ids);
190     }
191 
192     public void updateFile(
193             long companyId, String portletId, long groupId, long repositoryId,
194             long newRepositoryId, String fileName, long fileEntryId)
195         throws PortalException, SystemException {
196 
197         _hook.updateFile(
198             companyId, portletId, groupId, repositoryId, newRepositoryId,
199             FileUtil.encodeSafeFileName(fileName), fileEntryId);
200     }
201 
202     public void updateFile(
203             long companyId, String portletId, long groupId, long repositoryId,
204             String fileName, String versionNumber, String sourceFileName,
205             long fileEntryId, String properties, Date modifiedDate,
206             ServiceContext serviceContext, byte[] bytes)
207         throws PortalException, SystemException {
208 
209         _hook.updateFile(
210             companyId, portletId, groupId, repositoryId,
211             FileUtil.encodeSafeFileName(fileName), versionNumber,
212             FileUtil.encodeSafeFileName(sourceFileName), fileEntryId,
213             properties, modifiedDate, serviceContext, bytes);
214     }
215 
216     public void updateFile(
217             long companyId, String portletId, long groupId, long repositoryId,
218             String fileName, String versionNumber, String sourceFileName,
219             long fileEntryId, String properties, Date modifiedDate,
220             ServiceContext serviceContext, File file)
221         throws PortalException, SystemException {
222 
223         _hook.updateFile(
224             companyId, portletId, groupId, repositoryId,
225             FileUtil.encodeSafeFileName(fileName),
226             versionNumber, FileUtil.encodeSafeFileName(sourceFileName),
227             fileEntryId, properties, modifiedDate, serviceContext, file);
228     }
229 
230     public void updateFile(
231             long companyId, String portletId, long groupId, long repositoryId,
232             String fileName, String versionNumber, String sourceFileName,
233             long fileEntryId, String properties, Date modifiedDate,
234             ServiceContext serviceContext, InputStream is)
235         throws PortalException, SystemException {
236 
237         _hook.updateFile(
238             companyId, portletId, groupId, repositoryId,
239             FileUtil.encodeSafeFileName(fileName), versionNumber,
240             FileUtil.encodeSafeFileName(sourceFileName), fileEntryId,
241             properties, modifiedDate, serviceContext, is);
242     }
243 
244     public void updateFile(
245             long companyId, String portletId, long groupId, long repositoryId,
246             String fileName, String newFileName, boolean reindex)
247         throws PortalException, SystemException {
248 
249         _hook.updateFile(
250             companyId, portletId, groupId, repositoryId,
251             FileUtil.encodeSafeFileName(fileName),
252             FileUtil.encodeSafeFileName(newFileName), reindex);
253     }
254 
255     private Hook _hook;
256 
257 }