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