1
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.PropsValues;
46 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
47 import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
48
49 import java.io.File;
50 import java.io.IOException;
51 import java.io.InputStream;
52
53 import java.util.Date;
54
55
61 public class DLLocalServiceImpl implements DLLocalService {
62
63 public void addFile(
64 long companyId, String portletId, long groupId, long repositoryId,
65 String fileName, long fileEntryId, String properties,
66 Date modifiedDate, String[] tagsEntries, InputStream is)
67 throws PortalException, SystemException {
68
69 validate(fileName, is);
70
71 Hook hook = HookFactory.getInstance();
72
73 hook.addFile(
74 companyId, portletId, groupId, repositoryId, fileName, fileEntryId,
75 properties, modifiedDate, tagsEntries, is);
76 }
77
78 public void checkRoot(long companyId) throws SystemException {
79 Hook hook = HookFactory.getInstance();
80
81 hook.checkRoot(companyId);
82 }
83
84 public InputStream getFileAsStream(
85 long companyId, long repositoryId, String fileName)
86 throws PortalException, SystemException {
87
88 Hook hook = HookFactory.getInstance();
89
90 return hook.getFileAsStream(companyId, repositoryId, fileName);
91 }
92
93 public InputStream getFileAsStream(
94 long companyId, long repositoryId, String fileName,
95 double versionNumber)
96 throws PortalException, SystemException {
97
98 Hook hook = HookFactory.getInstance();
99
100 return hook.getFileAsStream(
101 companyId, repositoryId, fileName, versionNumber);
102 }
103
104 public boolean hasFile(
105 long companyId, long repositoryId, String fileName,
106 double versionNumber)
107 throws PortalException, SystemException {
108
109 Hook hook = HookFactory.getInstance();
110
111 return hook.hasFile(companyId, repositoryId, fileName, versionNumber);
112 }
113
114 public void move(String srcDir, String destDir) throws SystemException {
115 Hook hook = HookFactory.getInstance();
116
117 hook.move(srcDir, destDir);
118 }
119
120 public Hits search(
121 long companyId, String portletId, long groupId,
122 long userId, long[] repositoryIds, String keywords, int start,
123 int end)
124 throws SystemException {
125
126 try {
127 BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
128
129 contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
130
131 if (groupId > 0) {
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 DLFolderServiceUtil.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);
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[] tagsEntries, InputStream is)
193 throws PortalException, SystemException {
194
195 validate(fileName, sourceFileName, is);
196
197 Hook hook = HookFactory.getInstance();
198
199 hook.updateFile(
200 companyId, portletId, groupId, repositoryId, fileName,
201 versionNumber, sourceFileName, fileEntryId, properties,
202 modifiedDate, tagsEntries, is);
203 }
204
205 public void validate(String fileName, File file) throws PortalException {
206 validate(fileName);
207
208 if (((PropsValues.WEBDAV_LITMUS) ||
209 (PropsValues.DL_FILE_MAX_SIZE > 0)) &&
210 ((file == null) ||
211 (file.length() > PropsValues.DL_FILE_MAX_SIZE))) {
212
213 throw new FileSizeException(fileName);
214 }
215 }
216
217 public void validate(String fileName, byte[] bytes) throws PortalException {
218 validate(fileName);
219
220 if (((PropsValues.WEBDAV_LITMUS) ||
221 (PropsValues.DL_FILE_MAX_SIZE > 0)) &&
222 ((bytes == null) ||
223 (bytes.length > PropsValues.DL_FILE_MAX_SIZE))) {
224
225 throw new FileSizeException(fileName);
226 }
227 }
228
229 public void validate(String fileName, InputStream is)
230 throws PortalException {
231
232 validate(fileName);
233
234
236 try {
237 if (((PropsValues.WEBDAV_LITMUS) ||
238 (PropsValues.DL_FILE_MAX_SIZE > 0)) &&
239 ((is == null) ||
240 (is.available() > PropsValues.DL_FILE_MAX_SIZE))) {
241
242 throw new FileSizeException(fileName);
243 }
244 }
245 catch (IOException ioe) {
246 throw new FileSizeException(ioe.getMessage());
247 }
248 }
249
250 public void validate(String fileName) throws PortalException {
251 if ((fileName.indexOf("\\\\") != -1) ||
252 (fileName.indexOf("//") != -1) ||
253 (fileName.indexOf(":") != -1) ||
254 (fileName.indexOf("*") != -1) ||
255 (fileName.indexOf("?") != -1) ||
256 (fileName.indexOf("\"") != -1) ||
257 (fileName.indexOf("<") != -1) ||
258 (fileName.indexOf(">") != -1) ||
259 (fileName.indexOf("|") != -1) ||
260 (fileName.indexOf("&") != -1) ||
261 (fileName.indexOf("[") != -1) ||
262 (fileName.indexOf("]") != -1) ||
263 (fileName.indexOf("'") != -1)) {
264
265 throw new FileNameException(fileName);
266 }
267
268 boolean validFileExtension = false;
269
270 String[] fileExtensions = PropsValues.DL_FILE_EXTENSIONS;
271
272 if (!PropsValues.WEBDAV_LITMUS) {
273 for (int i = 0; i < fileExtensions.length; i++) {
274 if (StringPool.STAR.equals(fileExtensions[i]) ||
275 StringUtil.endsWith(fileName, fileExtensions[i])) {
276
277 validFileExtension = true;
278
279 break;
280 }
281 }
282
283 if (!validFileExtension) {
284 throw new FileNameException(fileName);
285 }
286 }
287 }
288
289 public void validate(String fileName, String sourceFileName, InputStream is)
290 throws PortalException {
291
292 String fileNameExtension = FileUtil.getExtension(fileName);
293 String sourceFileNameExtension = FileUtil.getExtension(sourceFileName);
294
295 if (!PropsValues.WEBDAV_LITMUS) {
296 if (Validator.isNull(fileNameExtension) ||
297 !fileNameExtension.equalsIgnoreCase(sourceFileNameExtension)) {
298
299 throw new SourceFileNameException(sourceFileName);
300 }
301 }
302
303 if (is == null) {
304 throw new FileSizeException(fileName);
305 }
306 }
307
308 }