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