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