1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.documentlibrary.service.impl;
21  
22  import com.liferay.documentlibrary.FileNameException;
23  import com.liferay.documentlibrary.FileSizeException;
24  import com.liferay.documentlibrary.SourceFileNameException;
25  import com.liferay.documentlibrary.service.DLLocalService;
26  import com.liferay.documentlibrary.util.Hook;
27  import com.liferay.documentlibrary.util.HookFactory;
28  import com.liferay.portal.PortalException;
29  import com.liferay.portal.SystemException;
30  import com.liferay.portal.kernel.annotation.BeanReference;
31  import com.liferay.portal.kernel.search.BooleanClauseOccur;
32  import com.liferay.portal.kernel.search.BooleanQuery;
33  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
34  import com.liferay.portal.kernel.search.Field;
35  import com.liferay.portal.kernel.search.Hits;
36  import com.liferay.portal.kernel.search.SearchEngineUtil;
37  import com.liferay.portal.kernel.search.TermQuery;
38  import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
39  import com.liferay.portal.kernel.util.FileUtil;
40  import com.liferay.portal.kernel.util.StringPool;
41  import com.liferay.portal.kernel.util.StringUtil;
42  import com.liferay.portal.kernel.util.Validator;
43  import com.liferay.portal.util.PrefsPropsUtil;
44  import com.liferay.portal.util.PropsKeys;
45  import com.liferay.portal.util.PropsValues;
46  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
47  import com.liferay.portlet.documentlibrary.service.DLFolderService;
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, long fileEntryId, String properties,
66              Date modifiedDate, String[] tagsCategories, String[] tagsEntries,
67              InputStream is)
68          throws PortalException, SystemException {
69  
70          validate(fileName, is);
71  
72          Hook hook = HookFactory.getInstance();
73  
74          hook.addFile(
75              companyId, portletId, groupId, repositoryId, fileName, fileEntryId,
76              properties, modifiedDate, tagsCategories, tagsEntries, is);
77      }
78  
79      public void checkRoot(long companyId) throws SystemException {
80          Hook hook = HookFactory.getInstance();
81  
82          hook.checkRoot(companyId);
83      }
84  
85      public InputStream getFileAsStream(
86              long companyId, long repositoryId, String fileName)
87          throws PortalException, SystemException {
88  
89          Hook hook = HookFactory.getInstance();
90  
91          return hook.getFileAsStream(companyId, repositoryId, fileName);
92      }
93  
94      public InputStream getFileAsStream(
95              long companyId, long repositoryId, String fileName,
96              double versionNumber)
97          throws PortalException, SystemException {
98  
99          Hook hook = HookFactory.getInstance();
100 
101         return hook.getFileAsStream(
102             companyId, repositoryId, fileName, versionNumber);
103     }
104 
105     public boolean hasFile(
106             long companyId, long repositoryId, String fileName,
107             double versionNumber)
108         throws PortalException, SystemException {
109 
110         Hook hook = HookFactory.getInstance();
111 
112         return hook.hasFile(companyId, repositoryId, fileName, versionNumber);
113     }
114 
115     public void move(String srcDir, String destDir) throws SystemException {
116         Hook hook = HookFactory.getInstance();
117 
118         hook.move(srcDir, destDir);
119     }
120 
121     public Hits search(
122             long companyId, String portletId, long groupId,
123             long userId, long[] repositoryIds, String keywords, int start,
124             int end)
125         throws SystemException {
126 
127         try {
128             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
129 
130             contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
131 
132             if (groupId > 0) {
133                 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
134             }
135 
136             if ((repositoryIds != null) && (repositoryIds.length > 0)) {
137                 BooleanQuery repositoryIdsQuery =
138                     BooleanQueryFactoryUtil.create();
139 
140                 for (long repositoryId : repositoryIds) {
141                     try {
142                         if (userId > 0) {
143                             try {
144                                 dlFolderService.getFolder(repositoryId);
145                             }
146                             catch (Exception e) {
147                                 continue;
148                             }
149                         }
150 
151                         TermQuery termQuery = TermQueryFactoryUtil.create(
152                             "repositoryId", repositoryId);
153 
154                         repositoryIdsQuery.add(
155                             termQuery, BooleanClauseOccur.SHOULD);
156                     }
157                     catch (Exception e) {
158                     }
159                 }
160 
161                 contextQuery.add(repositoryIdsQuery, BooleanClauseOccur.MUST);
162             }
163 
164             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
165 
166             if (Validator.isNotNull(keywords)) {
167                 searchQuery.addTerm(Field.CONTENT, keywords);
168                 searchQuery.addTerm(Field.PROPERTIES, keywords);
169                 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords);
170             }
171 
172             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
173 
174             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
175 
176             if (searchQuery.clauses().size() > 0) {
177                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
178             }
179 
180             return SearchEngineUtil.search(
181                 companyId, groupId, userId, DLFileEntry.class.getName(),
182                 fullQuery, start, end);
183         }
184         catch (Exception e) {
185             throw new SystemException(e);
186         }
187     }
188 
189     public void updateFile(
190             long companyId, String portletId, long groupId, long repositoryId,
191             String fileName, double versionNumber, String sourceFileName,
192             long fileEntryId, String properties, Date modifiedDate,
193             String[] tagsCategories, String[] tagsEntries, InputStream is)
194         throws PortalException, SystemException {
195 
196         validate(fileName, sourceFileName, is);
197 
198         Hook hook = HookFactory.getInstance();
199 
200         hook.updateFile(
201             companyId, portletId, groupId, repositoryId, fileName,
202             versionNumber, sourceFileName, fileEntryId, properties,
203             modifiedDate, tagsCategories, tagsEntries, is);
204     }
205 
206     public void validate(String fileName, File file)
207         throws PortalException, SystemException {
208 
209         validate(fileName);
210 
211         if (((PropsValues.WEBDAV_LITMUS) ||
212              (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
213             ((file == null) ||
214              (file.length() >
215                 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
216 
217             throw new FileSizeException(fileName);
218         }
219     }
220 
221     public void validate(String fileName, byte[] bytes)
222         throws PortalException, SystemException {
223 
224         validate(fileName);
225 
226         if (((PropsValues.WEBDAV_LITMUS) ||
227             (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
228             ((bytes == null) ||
229             (bytes.length >
230                  PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
231 
232             throw new FileSizeException(fileName);
233         }
234     }
235 
236     public void validate(String fileName, InputStream is)
237         throws PortalException, SystemException {
238 
239         validate(fileName);
240 
241         // LEP-4851
242 
243         try {
244             if (((PropsValues.WEBDAV_LITMUS) ||
245                 (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
246                 ((is == null) ||
247                 (is.available() >
248                      PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
249 
250                 throw new FileSizeException(fileName);
251             }
252         }
253         catch (IOException ioe) {
254             throw new FileSizeException(ioe.getMessage());
255         }
256     }
257 
258     public void validate(String fileName)
259         throws PortalException, SystemException {
260 
261         if ((fileName.indexOf("\\\\") != -1) ||
262             (fileName.indexOf("//") != -1) ||
263             (fileName.indexOf(":") != -1) ||
264             (fileName.indexOf("*") != -1) ||
265             (fileName.indexOf("?") != -1) ||
266             (fileName.indexOf("\"") != -1) ||
267             (fileName.indexOf("<") != -1) ||
268             (fileName.indexOf(">") != -1) ||
269             (fileName.indexOf("|") != -1) ||
270             (fileName.indexOf("&") != -1) ||
271             (fileName.indexOf("[") != -1) ||
272             (fileName.indexOf("]") != -1) ||
273             (fileName.indexOf("'") != -1)) {
274 
275             throw new FileNameException(fileName);
276         }
277 
278         boolean validFileExtension = false;
279 
280         String[] fileExtensions = PrefsPropsUtil.getStringArray(
281             PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
282 
283         if (!PropsValues.WEBDAV_LITMUS) {
284             for (int i = 0; i < fileExtensions.length; i++) {
285                 if (StringPool.STAR.equals(fileExtensions[i]) ||
286                     StringUtil.endsWith(fileName, fileExtensions[i])) {
287 
288                     validFileExtension = true;
289 
290                     break;
291                 }
292             }
293 
294             if (!validFileExtension) {
295                 throw new FileNameException(fileName);
296             }
297         }
298     }
299 
300     public void validate(String fileName, String sourceFileName, InputStream is)
301         throws PortalException {
302 
303         String fileNameExtension = FileUtil.getExtension(fileName);
304         String sourceFileNameExtension = FileUtil.getExtension(sourceFileName);
305 
306         if (!PropsValues.WEBDAV_LITMUS) {
307             if (Validator.isNull(fileNameExtension) ||
308                 !fileNameExtension.equalsIgnoreCase(sourceFileNameExtension)) {
309 
310                 throw new SourceFileNameException(sourceFileName);
311             }
312         }
313 
314         if (is == null) {
315             throw new FileSizeException(fileName);
316         }
317     }
318 
319     @BeanReference(name = _DL_FOLDER_SERVICE)
320     protected DLFolderService dlFolderService;
321 
322     private static final String _DL_FOLDER_SERVICE =
323         "com.liferay.portlet.documentlibrary.service.DLFolderService.impl";
324 
325 }