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