1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.documentlibrary.service.impl;
24  
25  import com.liferay.documentlibrary.FileNameException;
26  import com.liferay.documentlibrary.FileSizeException;
27  import com.liferay.documentlibrary.SourceFileNameException;
28  import com.liferay.documentlibrary.service.DLLocalService;
29  import com.liferay.documentlibrary.util.Hook;
30  import com.liferay.documentlibrary.util.HookFactory;
31  import com.liferay.portal.PortalException;
32  import com.liferay.portal.SystemException;
33  import com.liferay.portal.kernel.annotation.BeanReference;
34  import com.liferay.portal.kernel.search.BooleanClauseOccur;
35  import com.liferay.portal.kernel.search.BooleanQuery;
36  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
37  import com.liferay.portal.kernel.search.Field;
38  import com.liferay.portal.kernel.search.Hits;
39  import com.liferay.portal.kernel.search.SearchEngineUtil;
40  import com.liferay.portal.kernel.search.TermQuery;
41  import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
42  import com.liferay.portal.kernel.util.FileUtil;
43  import com.liferay.portal.kernel.util.StringPool;
44  import com.liferay.portal.kernel.util.StringUtil;
45  import com.liferay.portal.kernel.util.Validator;
46  import com.liferay.portal.util.PrefsPropsUtil;
47  import com.liferay.portal.util.PropsKeys;
48  import com.liferay.portal.util.PropsValues;
49  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
50  import com.liferay.portlet.documentlibrary.service.DLFolderService;
51  
52  import java.io.File;
53  import java.io.IOException;
54  import java.io.InputStream;
55  
56  import java.util.Date;
57  
58  /**
59   * <a href="DLLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Brian Wing Shun Chan
62   *
63   */
64  public class DLLocalServiceImpl implements DLLocalService {
65  
66      public void addFile(
67              long companyId, String portletId, long groupId, long repositoryId,
68              String fileName, long fileEntryId, String properties,
69              Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
70              InputStream is)
71          throws PortalException, SystemException {
72  
73          validate(fileName, is);
74  
75          Hook hook = HookFactory.getInstance();
76  
77          hook.addFile(
78              companyId, portletId, groupId, repositoryId, fileName, fileEntryId,
79              properties, modifiedDate, tagsCategories, tagsEntries, is);
80      }
81  
82      public void checkRoot(long companyId) throws SystemException {
83          Hook hook = HookFactory.getInstance();
84  
85          hook.checkRoot(companyId);
86      }
87  
88      public InputStream getFileAsStream(
89              long companyId, long repositoryId, String fileName)
90          throws PortalException, SystemException {
91  
92          Hook hook = HookFactory.getInstance();
93  
94          return hook.getFileAsStream(companyId, repositoryId, fileName);
95      }
96  
97      public InputStream getFileAsStream(
98              long companyId, long repositoryId, String fileName,
99              double versionNumber)
100         throws PortalException, SystemException {
101 
102         Hook hook = HookFactory.getInstance();
103 
104         return hook.getFileAsStream(
105             companyId, repositoryId, fileName, versionNumber);
106     }
107 
108     public boolean hasFile(
109             long companyId, long repositoryId, String fileName,
110             double versionNumber)
111         throws PortalException, SystemException {
112 
113         Hook hook = HookFactory.getInstance();
114 
115         return hook.hasFile(companyId, repositoryId, fileName, versionNumber);
116     }
117 
118     public void move(String srcDir, String destDir) throws SystemException {
119         Hook hook = HookFactory.getInstance();
120 
121         hook.move(srcDir, destDir);
122     }
123 
124     public Hits search(
125             long companyId, String portletId, long groupId,
126             long userId, long[] repositoryIds, String keywords, int start,
127             int end)
128         throws SystemException {
129 
130         try {
131             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
132 
133             contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
134 
135             if (groupId > 0) {
136                 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
137             }
138 
139             if ((repositoryIds != null) && (repositoryIds.length > 0)) {
140                 BooleanQuery repositoryIdsQuery =
141                     BooleanQueryFactoryUtil.create();
142 
143                 for (long repositoryId : repositoryIds) {
144                     try {
145                         if (userId > 0) {
146                             try {
147                                 dlFolderService.getFolder(repositoryId);
148                             }
149                             catch (Exception e) {
150                                 continue;
151                             }
152                         }
153 
154                         TermQuery termQuery = TermQueryFactoryUtil.create(
155                             "repositoryId", repositoryId);
156 
157                         repositoryIdsQuery.add(
158                             termQuery, BooleanClauseOccur.SHOULD);
159                     }
160                     catch (Exception e) {
161                     }
162                 }
163 
164                 contextQuery.add(repositoryIdsQuery, BooleanClauseOccur.MUST);
165             }
166 
167             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
168 
169             if (Validator.isNotNull(keywords)) {
170                 searchQuery.addTerm(Field.CONTENT, keywords);
171                 searchQuery.addTerm(Field.PROPERTIES, keywords);
172                 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords);
173             }
174 
175             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
176 
177             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
178 
179             if (searchQuery.clauses().size() > 0) {
180                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
181             }
182 
183             return SearchEngineUtil.search(
184                 companyId, groupId, userId, DLFileEntry.class.getName(),
185                 fullQuery, start, end);
186         }
187         catch (Exception e) {
188             throw new SystemException(e);
189         }
190     }
191 
192     public void updateFile(
193             long companyId, String portletId, long groupId, long repositoryId,
194             String fileName, double versionNumber, String sourceFileName,
195             long fileEntryId, String properties, Date modifiedDate,
196             String[] tagsCategories, String[] tagsEntries, InputStream is)
197         throws PortalException, SystemException {
198 
199         validate(fileName, sourceFileName, is);
200 
201         Hook hook = HookFactory.getInstance();
202 
203         hook.updateFile(
204             companyId, portletId, groupId, repositoryId, fileName,
205             versionNumber, sourceFileName, fileEntryId, properties,
206             modifiedDate, tagsCategories, tagsEntries, is);
207     }
208 
209     public void validate(String fileName, File file)
210         throws PortalException, SystemException {
211 
212         validate(fileName);
213 
214         if (((PropsValues.WEBDAV_LITMUS) ||
215              (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
216             ((file == null) ||
217              (file.length() >
218                 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
219 
220             throw new FileSizeException(fileName);
221         }
222     }
223 
224     public void validate(String fileName, byte[] bytes)
225         throws PortalException, SystemException {
226 
227         validate(fileName);
228 
229         if (((PropsValues.WEBDAV_LITMUS) ||
230             (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
231             ((bytes == null) ||
232             (bytes.length >
233                  PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
234 
235             throw new FileSizeException(fileName);
236         }
237     }
238 
239     public void validate(String fileName, InputStream is)
240         throws PortalException, SystemException {
241 
242         validate(fileName);
243 
244         // LEP-4851
245 
246         try {
247             if (((PropsValues.WEBDAV_LITMUS) ||
248                 (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
249                 ((is == null) ||
250                 (is.available() >
251                      PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
252 
253                 throw new FileSizeException(fileName);
254             }
255         }
256         catch (IOException ioe) {
257             throw new FileSizeException(ioe.getMessage());
258         }
259     }
260 
261     public void validate(String fileName)
262         throws PortalException, SystemException {
263 
264         if ((fileName.indexOf("\\\\") != -1) ||
265             (fileName.indexOf("//") != -1) ||
266             (fileName.indexOf(":") != -1) ||
267             (fileName.indexOf("*") != -1) ||
268             (fileName.indexOf("?") != -1) ||
269             (fileName.indexOf("\"") != -1) ||
270             (fileName.indexOf("<") != -1) ||
271             (fileName.indexOf(">") != -1) ||
272             (fileName.indexOf("|") != -1) ||
273             (fileName.indexOf("&") != -1) ||
274             (fileName.indexOf("[") != -1) ||
275             (fileName.indexOf("]") != -1) ||
276             (fileName.indexOf("'") != -1)) {
277 
278             throw new FileNameException(fileName);
279         }
280 
281         boolean validFileExtension = false;
282 
283         String[] fileExtensions = PrefsPropsUtil.getStringArray(
284             PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
285 
286         if (!PropsValues.WEBDAV_LITMUS) {
287             for (int i = 0; i < fileExtensions.length; i++) {
288                 if (StringPool.STAR.equals(fileExtensions[i]) ||
289                     StringUtil.endsWith(fileName, fileExtensions[i])) {
290 
291                     validFileExtension = true;
292 
293                     break;
294                 }
295             }
296 
297             if (!validFileExtension) {
298                 throw new FileNameException(fileName);
299             }
300         }
301     }
302 
303     public void validate(String fileName, String sourceFileName, InputStream is)
304         throws PortalException {
305 
306         String fileNameExtension = FileUtil.getExtension(fileName);
307         String sourceFileNameExtension = FileUtil.getExtension(sourceFileName);
308 
309         if (!PropsValues.WEBDAV_LITMUS) {
310             if (Validator.isNull(fileNameExtension) ||
311                 !fileNameExtension.equalsIgnoreCase(sourceFileNameExtension)) {
312 
313                 throw new SourceFileNameException(sourceFileName);
314             }
315         }
316 
317         if (is == null) {
318             throw new FileSizeException(fileName);
319         }
320     }
321 
322     @BeanReference(name = _DL_FOLDER_SERVICE)
323     protected DLFolderService dlFolderService;
324 
325     private static final String _DL_FOLDER_SERVICE =
326         "com.liferay.portlet.documentlibrary.service.DLFolderService.impl";
327 
328 }