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.portlet.documentlibrary.model.DLFileEntry;
46  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
47  import com.liferay.portlet.documentlibrary.service.DLFolderService;
48  import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
49  
50  import java.io.File;
51  import java.io.IOException;
52  import java.io.InputStream;
53  
54  import java.util.Date;
55  
56  /**
57   * <a href="DLLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   */
61  public class DLLocalServiceImpl implements DLLocalService {
62  
63      public void addFile(
64              long companyId, String portletId, long groupId, long repositoryId,
65              String fileName, boolean validateFileExtension, long fileEntryId,
66              String properties, Date modifiedDate, ServiceContext serviceContext,
67              InputStream is)
68          throws PortalException, SystemException {
69  
70          validate(fileName, validateFileExtension, is);
71  
72          hook.addFile(
73              companyId, portletId, groupId, repositoryId, fileName, fileEntryId,
74              properties, modifiedDate, serviceContext, is);
75      }
76  
77      public void checkRoot(long companyId) throws SystemException {
78          hook.checkRoot(companyId);
79      }
80  
81      public InputStream getFileAsStream(
82              long companyId, long repositoryId, String fileName)
83          throws PortalException, SystemException {
84  
85          return hook.getFileAsStream(companyId, repositoryId, fileName);
86      }
87  
88      public InputStream getFileAsStream(
89              long companyId, long repositoryId, String fileName,
90              String versionNumber)
91          throws PortalException, SystemException {
92  
93          return hook.getFileAsStream(
94              companyId, repositoryId, fileName, versionNumber);
95      }
96  
97      public boolean hasFile(
98              long companyId, long repositoryId, String fileName,
99              String versionNumber)
100         throws PortalException, SystemException {
101 
102         return hook.hasFile(companyId, repositoryId, fileName, versionNumber);
103     }
104 
105     public void move(String srcDir, String destDir) throws SystemException {
106         hook.move(srcDir, destDir);
107     }
108 
109     public Hits search(
110             long companyId, String portletId, long groupId,
111             long userId, long[] repositoryIds, String keywords, int start,
112             int end)
113         throws SystemException {
114 
115         try {
116             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
117 
118             contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
119 
120             if (groupId > 0) {
121                 Group group = groupLocalService.getGroup(groupId);
122 
123                 if (group.isLayout()) {
124                     contextQuery.addRequiredTerm(Field.SCOPE_GROUP_ID, groupId);
125 
126                     groupId = group.getParentGroupId();
127                 }
128 
129                 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
130             }
131 
132             if ((repositoryIds != null) && (repositoryIds.length > 0)) {
133                 BooleanQuery repositoryIdsQuery =
134                     BooleanQueryFactoryUtil.create();
135 
136                 for (long repositoryId : repositoryIds) {
137                     try {
138                         if (userId > 0) {
139                             PermissionChecker permissionChecker =
140                                 PermissionThreadLocal.getPermissionChecker();
141 
142                             DLFolderPermission.check(
143                                 permissionChecker, groupId, repositoryId,
144                                 ActionKeys.VIEW);
145                         }
146 
147                         if (repositoryId ==
148                                 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
149 
150                             repositoryId = groupId;
151                         }
152 
153                         TermQuery termQuery = TermQueryFactoryUtil.create(
154                             "repositoryId", repositoryId);
155 
156                         repositoryIdsQuery.add(
157                             termQuery, BooleanClauseOccur.SHOULD);
158                     }
159                     catch (Exception e) {
160                     }
161                 }
162 
163                 contextQuery.add(repositoryIdsQuery, BooleanClauseOccur.MUST);
164             }
165 
166             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
167 
168             if (Validator.isNotNull(keywords)) {
169                 searchQuery.addTerm(Field.CONTENT, keywords);
170                 searchQuery.addTerm(Field.PROPERTIES, keywords);
171                 searchQuery.addTerm(Field.ASSET_TAG_NAMES, keywords, true);
172             }
173 
174             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
175 
176             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
177 
178             if (searchQuery.clauses().size() > 0) {
179                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
180             }
181 
182             return SearchEngineUtil.search(
183                 companyId, groupId, userId, DLFileEntry.class.getName(),
184                 fullQuery, start, end);
185         }
186         catch (Exception e) {
187             throw new SystemException(e);
188         }
189     }
190 
191     public void updateFile(
192             long companyId, String portletId, long groupId, long repositoryId,
193             String fileName, boolean validateFileExtension,
194             String versionNumber, String sourceFileName, long fileEntryId,
195             String properties, Date modifiedDate, ServiceContext serviceContext,
196             InputStream is)
197         throws PortalException, SystemException {
198 
199         if (validateFileExtension) {
200             validate(fileName, sourceFileName, is);
201         }
202 
203         hook.updateFile(
204             companyId, portletId, groupId, repositoryId, fileName,
205             versionNumber, sourceFileName, fileEntryId, properties,
206             modifiedDate, serviceContext, is);
207     }
208 
209     public void validate(String fileName, boolean validateFileExtension)
210         throws PortalException, SystemException {
211 
212         if ((fileName.indexOf("\\\\") != -1) ||
213             (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 
225             throw new FileNameException(fileName);
226         }
227 
228         if (validateFileExtension) {
229             boolean validFileExtension = false;
230 
231             String[] fileExtensions = PrefsPropsUtil.getStringArray(
232                 PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
233 
234             for (int i = 0; i < fileExtensions.length; i++) {
235                 if (StringPool.STAR.equals(fileExtensions[i]) ||
236                     StringUtil.endsWith(fileName, fileExtensions[i])) {
237 
238                     validFileExtension = true;
239 
240                     break;
241                 }
242             }
243 
244             if (!validFileExtension) {
245                 throw new FileNameException(fileName);
246             }
247         }
248     }
249 
250     public void validate(
251             String fileName, boolean validateFileExtension, byte[] bytes)
252         throws PortalException, SystemException {
253 
254         validate(fileName, validateFileExtension);
255 
256         if ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
257             ((bytes == null) ||
258             (bytes.length >
259                  PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
260 
261             throw new FileSizeException(fileName);
262         }
263     }
264 
265     public void validate(
266             String fileName, boolean validateFileExtension, File file)
267         throws PortalException, SystemException {
268 
269         validate(fileName, validateFileExtension);
270 
271         if ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
272             ((file == null) ||
273              (file.length() >
274                 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
275 
276             throw new FileSizeException(fileName);
277         }
278     }
279 
280     public void validate(
281             String fileName, boolean validateFileExtension, InputStream is)
282         throws PortalException, SystemException {
283 
284         validate(fileName, validateFileExtension);
285 
286         // LEP-4851
287 
288         try {
289             if ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
290                 ((is == null) ||
291                 (is.available() >
292                      PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
293 
294                 throw new FileSizeException(fileName);
295             }
296         }
297         catch (IOException ioe) {
298             throw new FileSizeException(ioe.getMessage());
299         }
300     }
301 
302     public void validate(String fileName, String sourceFileName, InputStream is)
303         throws PortalException, SystemException {
304 
305         String fileNameExtension = FileUtil.getExtension(fileName);
306         String sourceFileNameExtension = FileUtil.getExtension(sourceFileName);
307 
308         validate(fileName, true);
309 
310         if (!fileNameExtension.equalsIgnoreCase(sourceFileNameExtension)) {
311             throw new SourceFileNameException(sourceFileName);
312         }
313 
314         try {
315             if ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
316                 ((is == null) ||
317                  (is.available() >
318                     PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
319 
320                 throw new FileSizeException(fileName);
321             }
322         }
323         catch (IOException ioe) {
324             throw new FileSizeException(ioe.getMessage());
325         }
326     }
327 
328     @BeanReference(type = GroupLocalService.class)
329     protected GroupLocalService groupLocalService;
330 
331     @BeanReference(type = DLFolderService.class)
332     protected DLFolderService dlFolderService;
333 
334     @BeanReference(type = Hook.class)
335     protected Hook hook;
336 
337 }