1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.documentlibrary.service.impl;
24  
25  import com.liferay.documentlibrary.FileNameException;
26  import com.liferay.documentlibrary.FileSizeException;
27  import com.liferay.documentlibrary.SourceFileNameException;
28  import com.liferay.documentlibrary.service.DLLocalService;
29  import com.liferay.documentlibrary.util.Hook;
30  import com.liferay.documentlibrary.util.HookFactory;
31  import com.liferay.portal.PortalException;
32  import com.liferay.portal.SystemException;
33  import com.liferay.portal.kernel.search.BooleanClauseOccur;
34  import com.liferay.portal.kernel.search.BooleanQuery;
35  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
36  import com.liferay.portal.kernel.search.Field;
37  import com.liferay.portal.kernel.search.Hits;
38  import com.liferay.portal.kernel.search.SearchEngineUtil;
39  import com.liferay.portal.kernel.search.TermQuery;
40  import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
41  import com.liferay.portal.kernel.util.FileUtil;
42  import com.liferay.portal.kernel.util.StringPool;
43  import com.liferay.portal.kernel.util.StringUtil;
44  import com.liferay.portal.kernel.util.Validator;
45  import com.liferay.portal.util.PrefsPropsUtil;
46  import com.liferay.portal.util.PropsKeys;
47  import com.liferay.portal.util.PropsValues;
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   */
61  public class DLLocalServiceImpl implements DLLocalService {
62  
63      public void addFile(
64              long companyId, String portletId, long groupId, long repositoryId,
65              String fileName, String properties, Date modifiedDate,
66              String[] tagsEntries, InputStream is)
67          throws PortalException, SystemException {
68  
69          validate(fileName, is);
70  
71          Hook hook = HookFactory.getInstance();
72  
73          hook.addFile(
74              companyId, portletId, groupId, repositoryId, fileName, properties,
75              modifiedDate, tagsEntries, is);
76      }
77  
78      public void checkRoot(long companyId) throws SystemException {
79          Hook hook = HookFactory.getInstance();
80  
81          hook.checkRoot(companyId);
82      }
83  
84      public InputStream getFileAsStream(
85              long companyId, long repositoryId, String fileName)
86          throws PortalException, SystemException {
87  
88          Hook hook = HookFactory.getInstance();
89  
90          return hook.getFileAsStream(companyId, repositoryId, fileName);
91      }
92  
93      public InputStream getFileAsStream(
94              long companyId, long repositoryId, String fileName,
95              double versionNumber)
96          throws PortalException, SystemException {
97  
98          Hook hook = HookFactory.getInstance();
99  
100         return hook.getFileAsStream(
101             companyId, repositoryId, fileName, versionNumber);
102     }
103 
104     public boolean hasFile(
105             long companyId, long repositoryId, String fileName,
106             double versionNumber)
107         throws PortalException, SystemException {
108 
109         Hook hook = HookFactory.getInstance();
110 
111         return hook.hasFile(companyId, repositoryId, fileName, versionNumber);
112     }
113 
114     public void move(String srcDir, String destDir) throws SystemException {
115         Hook hook = HookFactory.getInstance();
116 
117         hook.move(srcDir, destDir);
118     }
119 
120     public Hits search(
121             long companyId, String portletId, long groupId,
122             long[] repositoryIds, String keywords, int start, int end)
123         throws SystemException {
124 
125         try {
126             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
127 
128             contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
129 
130             if (groupId > 0) {
131                 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
132             }
133 
134             if ((repositoryIds != null) && (repositoryIds.length > 0)) {
135                 BooleanQuery repositoryIdsQuery =
136                     BooleanQueryFactoryUtil.create();
137 
138                 for (long repositoryId : repositoryIds) {
139                     TermQuery termQuery = TermQueryFactoryUtil.create(
140                         "repositoryId", repositoryId);
141 
142                     repositoryIdsQuery.add(
143                         termQuery, BooleanClauseOccur.SHOULD);
144                 }
145 
146                 contextQuery.add(repositoryIdsQuery, BooleanClauseOccur.MUST);
147             }
148 
149             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
150 
151             if (Validator.isNotNull(keywords)) {
152                 searchQuery.addTerm(Field.CONTENT, keywords);
153                 searchQuery.addTerm(Field.PROPERTIES, keywords);
154                 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords, true);
155             }
156 
157             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
158 
159             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
160 
161             if (searchQuery.clauses().size() > 0) {
162                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
163             }
164 
165             return SearchEngineUtil.search(companyId, fullQuery, start, end);
166         }
167         catch (Exception e) {
168             throw new SystemException(e);
169         }
170     }
171 
172     public void updateFile(
173             long companyId, String portletId, long groupId, long repositoryId,
174             String fileName, double versionNumber, String sourceFileName,
175             String properties, Date modifiedDate, String[] tagsEntries,
176             InputStream is)
177         throws PortalException, SystemException {
178 
179         validate(fileName, sourceFileName, is);
180 
181         Hook hook = HookFactory.getInstance();
182 
183         hook.updateFile(
184             companyId, portletId, groupId, repositoryId, fileName,
185             versionNumber, sourceFileName, properties, modifiedDate,
186             tagsEntries, is);
187     }
188 
189     public void validate(String fileName, File file)
190         throws PortalException, SystemException {
191 
192         validate(fileName);
193 
194         if (((PropsValues.WEBDAV_LITMUS) ||
195              (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
196             ((file == null) ||
197              (file.length() >
198                 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
199 
200             throw new FileSizeException(fileName);
201         }
202     }
203 
204     public void validate(String fileName, byte[] bytes)
205         throws PortalException, SystemException {
206 
207         validate(fileName);
208 
209         if (((PropsValues.WEBDAV_LITMUS) ||
210             (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
211             ((bytes == null) ||
212             (bytes.length >
213                  PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
214 
215             throw new FileSizeException(fileName);
216         }
217     }
218 
219     public void validate(String fileName, InputStream is)
220         throws PortalException, SystemException {
221 
222         validate(fileName);
223 
224         // LEP-4851
225 
226         try {
227             if (((PropsValues.WEBDAV_LITMUS) ||
228                 (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
229                 ((is == null) ||
230                 (is.available() >
231                      PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
232 
233                 throw new FileSizeException(fileName);
234             }
235         }
236         catch (IOException ioe) {
237             throw new FileSizeException(ioe.getMessage());
238         }
239     }
240 
241     public void validate(String fileName)
242         throws PortalException, SystemException {
243 
244         if ((fileName.indexOf("\\\\") != -1) ||
245             (fileName.indexOf("//") != -1) ||
246             (fileName.indexOf(":") != -1) ||
247             (fileName.indexOf("*") != -1) ||
248             (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 
258             throw new FileNameException(fileName);
259         }
260 
261         boolean validFileExtension = false;
262 
263         String[] fileExtensions = PrefsPropsUtil.getStringArray(
264             PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
265 
266         if (!PropsValues.WEBDAV_LITMUS) {
267             for (int i = 0; i < fileExtensions.length; i++) {
268                 if (StringPool.STAR.equals(fileExtensions[i]) ||
269                     StringUtil.endsWith(fileName, fileExtensions[i])) {
270 
271                     validFileExtension = true;
272 
273                     break;
274                 }
275             }
276 
277             if (!validFileExtension) {
278                 throw new FileNameException(fileName);
279             }
280         }
281     }
282 
283     public void validate(String fileName, String sourceFileName, InputStream is)
284         throws PortalException {
285 
286         String fileNameExtension = FileUtil.getExtension(fileName);
287         String sourceFileNameExtension = FileUtil.getExtension(sourceFileName);
288 
289         if (!PropsValues.WEBDAV_LITMUS) {
290             if (Validator.isNull(fileNameExtension) ||
291                 !fileNameExtension.equalsIgnoreCase(sourceFileNameExtension)) {
292 
293                 throw new SourceFileNameException(sourceFileName);
294             }
295         }
296 
297         if (is == null) {
298             throw new FileSizeException(fileName);
299         }
300     }
301 
302 }