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