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.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  public class DLLocalServiceImpl implements DLLocalService {
61  
62      public void addFile(
63              long companyId, String portletId, long groupId, long repositoryId,
64              String fileName, String properties, Date modifiedDate,
65              String[] tagsEntries, 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              modifiedDate, tagsEntries, 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, int start, int end)
122         throws SystemException {
123 
124         try {
125             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
126 
127             contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
128 
129             if (groupId > 0) {
130                 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
131             }
132 
133             if ((repositoryIds != null) && (repositoryIds.length > 0)) {
134                 BooleanQuery repositoryIdsQuery =
135                     BooleanQueryFactoryUtil.create();
136 
137                 for (long repositoryId : repositoryIds) {
138                     TermQuery termQuery = TermQueryFactoryUtil.create(
139                         "repositoryId", repositoryId);
140 
141                     repositoryIdsQuery.add(
142                         termQuery, BooleanClauseOccur.SHOULD);
143                 }
144 
145                 contextQuery.add(repositoryIdsQuery, BooleanClauseOccur.MUST);
146             }
147 
148             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
149 
150             if (Validator.isNotNull(keywords)) {
151                 searchQuery.addTerm(Field.CONTENT, keywords);
152                 searchQuery.addTerm(Field.PROPERTIES, keywords);
153                 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords, true);
154             }
155 
156             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
157 
158             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
159 
160             if (searchQuery.clauses().size() > 0) {
161                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
162             }
163 
164             return SearchEngineUtil.search(companyId, fullQuery, start, end);
165         }
166         catch (Exception e) {
167             throw new SystemException(e);
168         }
169     }
170 
171     public void updateFile(
172             long companyId, String portletId, long groupId, long repositoryId,
173             String fileName, double versionNumber, String sourceFileName,
174             String properties, Date modifiedDate, String[] tagsEntries,
175             InputStream is)
176         throws PortalException, SystemException {
177 
178         validate(fileName, sourceFileName, is);
179 
180         Hook hook = HookFactory.getInstance();
181 
182         hook.updateFile(
183             companyId, portletId, groupId, repositoryId, fileName,
184             versionNumber, sourceFileName, properties, modifiedDate,
185             tagsEntries, is);
186     }
187 
188     public void validate(String fileName, File file)
189         throws PortalException, SystemException {
190 
191         validate(fileName);
192 
193         if (((PropsValues.WEBDAV_LITMUS) ||
194              (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
195             ((file == null) ||
196              (file.length() >
197                 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
198 
199             throw new FileSizeException(fileName);
200         }
201     }
202 
203     public void validate(String fileName, byte[] bytes)
204         throws PortalException, SystemException {
205 
206         validate(fileName);
207 
208         if (((PropsValues.WEBDAV_LITMUS) ||
209             (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
210             ((bytes == null) ||
211             (bytes.length >
212                  PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
213 
214             throw new FileSizeException(fileName);
215         }
216     }
217 
218     public void validate(String fileName, InputStream is)
219         throws PortalException, SystemException {
220 
221         validate(fileName);
222 
223         // LEP-4851
224 
225         try {
226             if (((PropsValues.WEBDAV_LITMUS) ||
227                 (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
228                 ((is == null) ||
229                 (is.available() >
230                      PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
231 
232                 throw new FileSizeException(fileName);
233             }
234         }
235         catch (IOException ioe) {
236             throw new FileSizeException(ioe.getMessage());
237         }
238     }
239 
240     public void validate(String fileName)
241         throws PortalException, SystemException {
242 
243         if ((fileName.indexOf("\\\\") != -1) ||
244             (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 
257             throw new FileNameException(fileName);
258         }
259 
260         boolean validFileExtension = false;
261 
262         String[] fileExtensions = PrefsPropsUtil.getStringArray(
263             PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
264 
265         if (!PropsValues.WEBDAV_LITMUS) {
266             for (int i = 0; i < fileExtensions.length; i++) {
267                 if (StringPool.STAR.equals(fileExtensions[i]) ||
268                     StringUtil.endsWith(fileName, fileExtensions[i])) {
269 
270                     validFileExtension = true;
271 
272                     break;
273                 }
274             }
275 
276             if (!validFileExtension) {
277                 throw new FileNameException(fileName);
278             }
279         }
280     }
281 
282     public void validate(String fileName, String sourceFileName, InputStream is)
283         throws PortalException {
284 
285         String fileNameExtension = FileUtil.getExtension(fileName);
286         String sourceFileNameExtension = FileUtil.getExtension(sourceFileName);
287 
288         if (!PropsValues.WEBDAV_LITMUS) {
289             if (Validator.isNull(fileNameExtension) ||
290                 !fileNameExtension.equalsIgnoreCase(sourceFileNameExtension)) {
291 
292                 throw new SourceFileNameException(sourceFileName);
293             }
294         }
295 
296         if (is == null) {
297             throw new FileSizeException(fileName);
298         }
299     }
300 
301 }