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.util.PrefsPropsUtil;
39  import com.liferay.portal.util.PropsValues;
40  
41  import java.io.File;
42  import java.io.IOException;
43  import java.io.InputStream;
44  
45  import java.util.Date;
46  
47  /**
48   * <a href="DLLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   */
52  public class DLLocalServiceImpl implements DLLocalService {
53  
54      public void addFile(
55              long companyId, String portletId, long groupId, long repositoryId,
56              String fileName, String properties, Date modifiedDate,
57              String[] tagsEntries, InputStream is)
58          throws PortalException, SystemException {
59  
60          validate(fileName, is);
61  
62          hook.addFile(
63              companyId, portletId, groupId, repositoryId, fileName, properties,
64              modifiedDate, tagsEntries, is);
65      }
66  
67      public void checkRoot(long companyId) throws SystemException {
68          hook.checkRoot(companyId);
69      }
70  
71      public InputStream getFileAsStream(
72              long companyId, long repositoryId, String fileName)
73          throws PortalException, SystemException {
74  
75          return hook.getFileAsStream(companyId, repositoryId, fileName);
76      }
77  
78      public InputStream getFileAsStream(
79              long companyId, long repositoryId, String fileName,
80              double versionNumber)
81          throws PortalException, SystemException {
82  
83          return hook.getFileAsStream(
84              companyId, repositoryId, fileName, versionNumber);
85      }
86  
87      public boolean hasFile(
88              long companyId, long repositoryId, String fileName,
89              double versionNumber)
90          throws PortalException, SystemException {
91  
92          return hook.hasFile(companyId, repositoryId, fileName, versionNumber);
93      }
94  
95      public void move(String srcDir, String destDir) throws SystemException {
96          hook.move(srcDir, destDir);
97      }
98  
99      public Hits search(
100             long companyId, String portletId, long groupId,
101             long[] repositoryIds, String keywords, int start, int end)
102         throws SystemException {
103 
104         try {
105             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
106 
107             contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
108 
109             if (groupId > 0) {
110                 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
111             }
112 
113             if ((repositoryIds != null) && (repositoryIds.length > 0)) {
114                 BooleanQuery repositoryIdsQuery =
115                     BooleanQueryFactoryUtil.create();
116 
117                 for (long repositoryId : repositoryIds) {
118                     TermQuery termQuery = TermQueryFactoryUtil.create(
119                         "repositoryId", repositoryId);
120 
121                     repositoryIdsQuery.add(
122                         termQuery, BooleanClauseOccur.SHOULD);
123                 }
124 
125                 contextQuery.add(repositoryIdsQuery, BooleanClauseOccur.MUST);
126             }
127 
128             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
129 
130             if (Validator.isNotNull(keywords)) {
131                 searchQuery.addTerm(Field.CONTENT, keywords);
132                 searchQuery.addTerm(Field.PROPERTIES, keywords);
133                 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords, true);
134             }
135 
136             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
137 
138             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
139 
140             if (searchQuery.clauses().size() > 0) {
141                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
142             }
143 
144             return SearchEngineUtil.search(companyId, fullQuery, start, end);
145         }
146         catch (Exception e) {
147             throw new SystemException(e);
148         }
149     }
150 
151     public void updateFile(
152             long companyId, String portletId, long groupId, long repositoryId,
153             String fileName, double versionNumber, String sourceFileName,
154             String properties, Date modifiedDate, String[] tagsEntries,
155             InputStream is)
156         throws PortalException, SystemException {
157 
158         validate(fileName, sourceFileName, is);
159 
160         hook.updateFile(
161             companyId, portletId, groupId, repositoryId, fileName,
162             versionNumber, sourceFileName, properties, modifiedDate,
163             tagsEntries, is);
164     }
165 
166     public void validate(String fileName, File file)
167         throws PortalException, SystemException {
168 
169         validate(fileName);
170 
171         if (((PropsValues.WEBDAV_LITMUS) ||
172              (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
173             ((file == null) ||
174              (file.length() >
175                 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
176 
177             throw new FileSizeException(fileName);
178         }
179     }
180 
181     public void validate(String fileName, byte[] bytes)
182         throws PortalException, SystemException {
183 
184         validate(fileName);
185 
186         if (((PropsValues.WEBDAV_LITMUS) ||
187             (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
188             ((bytes == null) ||
189             (bytes.length >
190                  PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
191 
192             throw new FileSizeException(fileName);
193         }
194     }
195 
196     public void validate(String fileName, InputStream is)
197         throws PortalException, SystemException {
198 
199         validate(fileName);
200 
201         // LEP-4851
202 
203         try {
204             if (((PropsValues.WEBDAV_LITMUS) ||
205                 (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
206                 ((is == null) ||
207                 (is.available() >
208                      PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
209 
210                 throw new FileSizeException(fileName);
211             }
212         }
213         catch (IOException ioe) {
214             throw new FileSizeException(ioe.getMessage());
215         }
216     }
217 
218     public void validate(String fileName)
219         throws PortalException, SystemException {
220 
221         if ((fileName.indexOf("\\\\") != -1) ||
222             (fileName.indexOf("//") != -1) ||
223             (fileName.indexOf(":") != -1) ||
224             (fileName.indexOf("*") != -1) ||
225             (fileName.indexOf("?") != -1) ||
226             (fileName.indexOf("\"") != -1) ||
227             (fileName.indexOf("<") != -1) ||
228             (fileName.indexOf(">") != -1) ||
229             (fileName.indexOf("|") != -1) ||
230             (fileName.indexOf("[") != -1) ||
231             (fileName.indexOf("]") != -1) ||
232             (fileName.indexOf("'") != -1)) {
233 
234             throw new FileNameException(fileName);
235         }
236 
237         boolean validFileExtension = false;
238 
239         String[] fileExtensions = PrefsPropsUtil.getStringArray(
240             PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
241 
242         if (!PropsValues.WEBDAV_LITMUS) {
243             for (int i = 0; i < fileExtensions.length; i++) {
244                 if (StringPool.STAR.equals(fileExtensions[i]) ||
245                     StringUtil.endsWith(fileName, fileExtensions[i])) {
246 
247                     validFileExtension = true;
248 
249                     break;
250                 }
251             }
252 
253             if (!validFileExtension) {
254                 throw new FileNameException(fileName);
255             }
256         }
257     }
258 
259     public void validate(String fileName, String sourceFileName, InputStream is)
260         throws PortalException, SystemException {
261 
262         String fileNameExtension = FileUtil.getExtension(fileName);
263         String sourceFileNameExtension = FileUtil.getExtension(sourceFileName);
264 
265         if (!PropsValues.WEBDAV_LITMUS) {
266             if (Validator.isNull(fileNameExtension) ||
267                 !fileNameExtension.equalsIgnoreCase(sourceFileNameExtension)) {
268 
269                 throw new SourceFileNameException(sourceFileName);
270             }
271         }
272 
273         try {
274             if (((PropsValues.WEBDAV_LITMUS) ||
275                  (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
276                 ((is == null) ||
277                  (is.available() >
278                     PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
279 
280                 throw new FileSizeException(fileName);
281             }
282         }
283         catch (IOException ioe) {
284             throw new FileSizeException(ioe.getMessage());
285         }
286     }
287 
288     @BeanReference(name = "com.liferay.documentlibrary.util.HookProxyBean")
289     protected Hook hook;
290 
291 }