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.search.BooleanClauseOccur;
31  import com.liferay.portal.kernel.search.BooleanQuery;
32  import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
33  import com.liferay.portal.kernel.search.Field;
34  import com.liferay.portal.kernel.search.Hits;
35  import com.liferay.portal.kernel.search.SearchEngineUtil;
36  import com.liferay.portal.kernel.search.TermQuery;
37  import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
38  import com.liferay.portal.kernel.util.FileUtil;
39  import com.liferay.portal.kernel.util.StringPool;
40  import com.liferay.portal.kernel.util.StringUtil;
41  import com.liferay.portal.kernel.util.Validator;
42  import com.liferay.portal.util.PrefsPropsUtil;
43  import com.liferay.portal.util.PropsKeys;
44  import com.liferay.portal.util.PropsValues;
45  
46  import java.io.File;
47  import java.io.IOException;
48  import java.io.InputStream;
49  
50  import java.util.Date;
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 void addFile(
61              long companyId, String portletId, long groupId, long repositoryId,
62              String fileName, String properties, Date modifiedDate,
63              String[] tagsEntries, InputStream is)
64          throws PortalException, SystemException {
65  
66          validate(fileName, is);
67  
68          Hook hook = HookFactory.getInstance();
69  
70          hook.addFile(
71              companyId, portletId, groupId, repositoryId, fileName, properties,
72              modifiedDate, tagsEntries, is);
73      }
74  
75      public void checkRoot(long companyId) throws SystemException {
76          Hook hook = HookFactory.getInstance();
77  
78          hook.checkRoot(companyId);
79      }
80  
81      public InputStream getFileAsStream(
82              long companyId, long repositoryId, String fileName)
83          throws PortalException, SystemException {
84  
85          Hook hook = HookFactory.getInstance();
86  
87          return hook.getFileAsStream(companyId, repositoryId, fileName);
88      }
89  
90      public InputStream getFileAsStream(
91              long companyId, long repositoryId, String fileName,
92              double versionNumber)
93          throws PortalException, SystemException {
94  
95          Hook hook = HookFactory.getInstance();
96  
97          return hook.getFileAsStream(
98              companyId, repositoryId, fileName, versionNumber);
99      }
100 
101     public boolean hasFile(
102             long companyId, long repositoryId, String fileName,
103             double versionNumber)
104         throws PortalException, SystemException {
105 
106         Hook hook = HookFactory.getInstance();
107 
108         return hook.hasFile(companyId, repositoryId, fileName, versionNumber);
109     }
110 
111     public void move(String srcDir, String destDir) throws SystemException {
112         Hook hook = HookFactory.getInstance();
113 
114         hook.move(srcDir, destDir);
115     }
116 
117     public Hits search(
118             long companyId, String portletId, long groupId,
119             long[] repositoryIds, String keywords, int start, int end)
120         throws SystemException {
121 
122         try {
123             BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
124 
125             contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
126 
127             if (groupId > 0) {
128                 contextQuery.addRequiredTerm(Field.GROUP_ID, groupId);
129             }
130 
131             if ((repositoryIds != null) && (repositoryIds.length > 0)) {
132                 BooleanQuery repositoryIdsQuery =
133                     BooleanQueryFactoryUtil.create();
134 
135                 for (long repositoryId : repositoryIds) {
136                     TermQuery termQuery = TermQueryFactoryUtil.create(
137                         "repositoryId", repositoryId);
138 
139                     repositoryIdsQuery.add(
140                         termQuery, BooleanClauseOccur.SHOULD);
141                 }
142 
143                 contextQuery.add(repositoryIdsQuery, BooleanClauseOccur.MUST);
144             }
145 
146             BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
147 
148             if (Validator.isNotNull(keywords)) {
149                 searchQuery.addTerm(Field.CONTENT, keywords);
150                 searchQuery.addTerm(Field.PROPERTIES, keywords);
151                 searchQuery.addTerm(Field.TAGS_ENTRIES, keywords);
152             }
153 
154             BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
155 
156             fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
157 
158             if (searchQuery.clauses().size() > 0) {
159                 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
160             }
161 
162             return SearchEngineUtil.search(companyId, fullQuery, start, end);
163         }
164         catch (Exception e) {
165             throw new SystemException(e);
166         }
167     }
168 
169     public void updateFile(
170             long companyId, String portletId, long groupId, long repositoryId,
171             String fileName, double versionNumber, String sourceFileName,
172             String properties, Date modifiedDate, String[] tagsEntries,
173             InputStream is)
174         throws PortalException, SystemException {
175 
176         validate(fileName, sourceFileName, is);
177 
178         Hook hook = HookFactory.getInstance();
179 
180         hook.updateFile(
181             companyId, portletId, groupId, repositoryId, fileName,
182             versionNumber, sourceFileName, properties, modifiedDate,
183             tagsEntries, is);
184     }
185 
186     public void validate(String fileName, File file)
187         throws PortalException, SystemException {
188 
189         validate(fileName);
190 
191         if (((PropsValues.WEBDAV_LITMUS) ||
192              (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
193             ((file == null) ||
194              (file.length() >
195                 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
196 
197             throw new FileSizeException(fileName);
198         }
199     }
200 
201     public void validate(String fileName, byte[] bytes)
202         throws PortalException, SystemException {
203 
204         validate(fileName);
205 
206         if (((PropsValues.WEBDAV_LITMUS) ||
207             (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
208             ((bytes == null) ||
209             (bytes.length >
210                  PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
211 
212             throw new FileSizeException(fileName);
213         }
214     }
215 
216     public void validate(String fileName, InputStream is)
217         throws PortalException, SystemException {
218 
219         validate(fileName);
220 
221         // LEP-4851
222 
223         try {
224             if (((PropsValues.WEBDAV_LITMUS) ||
225                 (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0)) &&
226                 ((is == null) ||
227                 (is.available() >
228                      PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
229 
230                 throw new FileSizeException(fileName);
231             }
232         }
233         catch (IOException ioe) {
234             throw new FileSizeException(ioe.getMessage());
235         }
236     }
237 
238     public void validate(String fileName)
239         throws PortalException, SystemException {
240 
241         if ((fileName.indexOf("\\\\") != -1) ||
242             (fileName.indexOf("//") != -1) ||
243             (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 
255             throw new FileNameException(fileName);
256         }
257 
258         boolean validFileExtension = false;
259 
260         String[] fileExtensions = PrefsPropsUtil.getStringArray(
261             PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
262 
263         if (!PropsValues.WEBDAV_LITMUS) {
264             for (int i = 0; i < fileExtensions.length; i++) {
265                 if (StringPool.STAR.equals(fileExtensions[i]) ||
266                     StringUtil.endsWith(fileName, fileExtensions[i])) {
267 
268                     validFileExtension = true;
269 
270                     break;
271                 }
272             }
273 
274             if (!validFileExtension) {
275                 throw new FileNameException(fileName);
276             }
277         }
278     }
279 
280     public void validate(String fileName, String sourceFileName, InputStream is)
281         throws PortalException {
282 
283         String fileNameExtension = FileUtil.getExtension(fileName);
284         String sourceFileNameExtension = FileUtil.getExtension(sourceFileName);
285 
286         if (!PropsValues.WEBDAV_LITMUS) {
287             if (Validator.isNull(fileNameExtension) ||
288                 !fileNameExtension.equalsIgnoreCase(sourceFileNameExtension)) {
289 
290                 throw new SourceFileNameException(sourceFileName);
291             }
292         }
293 
294         if (is == null) {
295             throw new FileSizeException(fileName);
296         }
297     }
298 
299 }