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.service.impl;
16  
17  import com.liferay.documentlibrary.FileNameException;
18  import com.liferay.documentlibrary.FileSizeException;
19  import com.liferay.documentlibrary.SourceFileNameException;
20  import com.liferay.documentlibrary.service.DLLocalService;
21  import com.liferay.documentlibrary.util.Hook;
22  import com.liferay.portal.kernel.annotation.BeanReference;
23  import com.liferay.portal.kernel.exception.PortalException;
24  import com.liferay.portal.kernel.exception.SystemException;
25  import com.liferay.portal.kernel.search.BooleanClauseOccur;
26  import com.liferay.portal.kernel.search.BooleanQuery;
27  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
28  import com.liferay.portal.kernel.search.Field;
29  import com.liferay.portal.kernel.search.Hits;
30  import com.liferay.portal.kernel.search.SearchEngineUtil;
31  import com.liferay.portal.kernel.search.TermQuery;
32  import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
33  import com.liferay.portal.kernel.util.FileUtil;
34  import com.liferay.portal.kernel.util.PropsKeys;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.kernel.util.Validator;
38  import com.liferay.portal.model.Group;
39  import com.liferay.portal.security.permission.ActionKeys;
40  import com.liferay.portal.security.permission.PermissionChecker;
41  import com.liferay.portal.security.permission.PermissionThreadLocal;
42  import com.liferay.portal.service.GroupLocalService;
43  import com.liferay.portal.service.ServiceContext;
44  import com.liferay.portal.util.PrefsPropsUtil;
45  import com.liferay.portal.util.PropsValues;
46  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
47  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
48  import com.liferay.portlet.documentlibrary.service.DLFolderService;
49  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
50  
51  import java.io.File;
52  import java.io.IOException;
53  import java.io.InputStream;
54  
55  import java.util.Date;
56  
57  /**
58   * <a href="DLLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   */
62  public class DLLocalServiceImpl implements DLLocalService {
63  
64      public void addFile(
65              long companyId, String portletId, long groupId, long repositoryId,
66              String fileName, boolean validateFileExtension, long fileEntryId,
67              String properties, Date modifiedDate, ServiceContext serviceContext,
68              InputStream is)
69          throws PortalException, SystemException {
70  
71          validate(fileName, validateFileExtension, is);
72  
73          hook.addFile(
74              companyId, portletId, groupId, repositoryId, fileName, fileEntryId,
75              properties, modifiedDate, serviceContext, is);
76      }
77  
78      public void checkRoot(long companyId) throws SystemException {
79          hook.checkRoot(companyId);
80      }
81  
82      public InputStream getFileAsStream(
83              long companyId, long repositoryId, String fileName)
84          throws PortalException, SystemException {
85  
86          return hook.getFileAsStream(companyId, repositoryId, fileName);
87      }
88  
89      public InputStream getFileAsStream(
90              long companyId, long repositoryId, String fileName,
91              String versionNumber)
92          throws PortalException, SystemException {
93  
94          return hook.getFileAsStream(
95              companyId, repositoryId, fileName, versionNumber);
96      }
97  
98      public boolean hasFile(
99              long companyId, long repositoryId, String fileName,
100             String versionNumber)
101         throws PortalException, SystemException {
102 
103         return hook.hasFile(companyId, repositoryId, fileName, versionNumber);
104     }
105 
106     public void move(String srcDir, String destDir) throws SystemException {
107         hook.move(srcDir, destDir);
108     }
109 
110     public Hits search(
111             long companyId, String portletId, long groupId,
112             long userId, long[] repositoryIds, String keywords, int start,
113             int end)
114         throws SystemException {
115 
116         try {
117             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
118 
119             contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
120 
121             if (groupId > 0) {
122                 Group group = groupLocalService.getGroup(groupId);
123 
124                 if (group.isLayout()) {
125                     contextQuery.addRequiredTerm(Field.SCOPE_GROUP_ID, groupId);
126 
127                     groupId = group.getParentGroupId();
128                 }
129 
130                 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
131             }
132 
133             if ((repositoryIds != null) && (repositoryIds.length > 0)) {
134                 BooleanQuery repositoryIdsQuery =
135                     BooleanQueryFactoryUtil.create();
136 
137                 for (long repositoryId : repositoryIds) {
138                     try {
139                         if (userId > 0) {
140                             PermissionChecker permissionChecker =
141                                 PermissionThreadLocal.getPermissionChecker();
142 
143                             DLFolderPermission.check(
144                                 permissionChecker, groupId, repositoryId,
145                                 ActionKeys.VIEW);
146                         }
147 
148                         if (repositoryId ==
149                                 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
150 
151                             repositoryId = groupId;
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.ASSET_TAG_NAMES, keywords, true);
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, boolean validateFileExtension,
195             String versionNumber, String sourceFileName, long fileEntryId,
196             String properties, Date modifiedDate, ServiceContext serviceContext,
197             InputStream is)
198         throws PortalException, SystemException {
199 
200         if (validateFileExtension) {
201             validate(fileName, sourceFileName, is);
202         }
203 
204         hook.updateFile(
205             companyId, portletId, groupId, repositoryId, fileName,
206             versionNumber, sourceFileName, fileEntryId, properties,
207             modifiedDate, serviceContext, is);
208     }
209 
210     public void validate(String fileName, boolean validateFileExtension)
211         throws PortalException, SystemException {
212 
213         if ((fileName.indexOf("\\\\") != -1) ||
214             (fileName.indexOf("//") != -1) ||
215             (fileName.indexOf(":") != -1) ||
216             (fileName.indexOf("*") != -1) ||
217             (fileName.indexOf("?") != -1) ||
218             (fileName.indexOf("\"") != -1) ||
219             (fileName.indexOf("<") != -1) ||
220             (fileName.indexOf(">") != -1) ||
221             (fileName.indexOf("|") != -1) ||
222             (fileName.indexOf("[") != -1) ||
223             (fileName.indexOf("]") != -1) ||
224             (fileName.indexOf("'") != -1)) {
225 
226             throw new FileNameException(fileName);
227         }
228 
229         if (validateFileExtension) {
230             boolean validFileExtension = false;
231 
232             String[] fileExtensions = PrefsPropsUtil.getStringArray(
233                 PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
234 
235             if (!PropsValues.WEBDAV_LITMUS) {
236                 for (int i = 0; i < fileExtensions.length; i++) {
237                     if (StringPool.STAR.equals(fileExtensions[i]) ||
238                         StringUtil.endsWith(fileName, fileExtensions[i])) {
239 
240                         validFileExtension = true;
241 
242                         break;
243                     }
244                 }
245 
246                 if (!validFileExtension) {
247                     throw new FileNameException(fileName);
248                 }
249             }
250         }
251     }
252 
253     public void validate(
254             String fileName, boolean validateFileExtension, byte[] bytes)
255         throws PortalException, SystemException {
256 
257         validate(fileName, validateFileExtension);
258 
259         if (((PropsValues.WEBDAV_LITMUS) ||
260             (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
261             ((bytes == null) ||
262             (bytes.length >
263                  PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
264 
265             throw new FileSizeException(fileName);
266         }
267     }
268 
269     public void validate(
270             String fileName, boolean validateFileExtension, File file)
271         throws PortalException, SystemException {
272 
273         validate(fileName, validateFileExtension);
274 
275         if (((PropsValues.WEBDAV_LITMUS) ||
276              (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
277             ((file == null) ||
278              (file.length() >
279                 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
280 
281             throw new FileSizeException(fileName);
282         }
283     }
284 
285     public void validate(
286             String fileName, boolean validateFileExtension, InputStream is)
287         throws PortalException, SystemException {
288 
289         validate(fileName, validateFileExtension);
290 
291         // LEP-4851
292 
293         try {
294             if (((PropsValues.WEBDAV_LITMUS) ||
295                 (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
296                 ((is == null) ||
297                 (is.available() >
298                      PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
299 
300                 throw new FileSizeException(fileName);
301             }
302         }
303         catch (IOException ioe) {
304             throw new FileSizeException(ioe.getMessage());
305         }
306     }
307 
308     public void validate(String fileName, String sourceFileName, InputStream is)
309         throws PortalException, SystemException {
310 
311         String fileNameExtension = FileUtil.getExtension(fileName);
312         String sourceFileNameExtension = FileUtil.getExtension(sourceFileName);
313 
314         if (!PropsValues.WEBDAV_LITMUS) {
315             validate(fileName, true);
316 
317             if (!fileNameExtension.equalsIgnoreCase(sourceFileNameExtension)) {
318                 throw new SourceFileNameException(sourceFileName);
319             }
320         }
321 
322         try {
323             if (((PropsValues.WEBDAV_LITMUS) ||
324                  (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
325                 ((is == null) ||
326                  (is.available() >
327                     PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
328 
329                 throw new FileSizeException(fileName);
330             }
331         }
332         catch (IOException ioe) {
333             throw new FileSizeException(ioe.getMessage());
334         }
335     }
336 
337     @BeanReference(name = "com.liferay.portal.service.GroupLocalService")
338     protected GroupLocalService groupLocalService;
339 
340     @BeanReference(
341         name = "com.liferay.portlet.documentlibrary.service.DLFolderService")
342     protected DLFolderService dlFolderService;
343 
344     @BeanReference(name = "com.liferay.documentlibrary.util.HookProxyBean")
345     protected Hook hook;
346 
347 }