001
014
015 package com.liferay.documentlibrary.service.impl;
016
017 import com.liferay.documentlibrary.FileNameException;
018 import com.liferay.documentlibrary.FileSizeException;
019 import com.liferay.documentlibrary.SourceFileNameException;
020 import com.liferay.documentlibrary.service.DLLocalService;
021 import com.liferay.documentlibrary.util.Hook;
022 import com.liferay.portal.kernel.annotation.BeanReference;
023 import com.liferay.portal.kernel.exception.PortalException;
024 import com.liferay.portal.kernel.exception.SystemException;
025 import com.liferay.portal.kernel.search.BooleanClauseOccur;
026 import com.liferay.portal.kernel.search.BooleanQuery;
027 import com.liferay.portal.kernel.search.BooleanQueryFactoryUtil;
028 import com.liferay.portal.kernel.search.Field;
029 import com.liferay.portal.kernel.search.Hits;
030 import com.liferay.portal.kernel.search.SearchEngineUtil;
031 import com.liferay.portal.kernel.search.TermQuery;
032 import com.liferay.portal.kernel.search.TermQueryFactoryUtil;
033 import com.liferay.portal.kernel.util.FileUtil;
034 import com.liferay.portal.kernel.util.PropsKeys;
035 import com.liferay.portal.kernel.util.StringPool;
036 import com.liferay.portal.kernel.util.StringUtil;
037 import com.liferay.portal.kernel.util.Validator;
038 import com.liferay.portal.model.Group;
039 import com.liferay.portal.security.permission.ActionKeys;
040 import com.liferay.portal.security.permission.PermissionChecker;
041 import com.liferay.portal.security.permission.PermissionThreadLocal;
042 import com.liferay.portal.service.GroupLocalService;
043 import com.liferay.portal.service.ServiceContext;
044 import com.liferay.portal.util.PrefsPropsUtil;
045 import com.liferay.portal.util.PropsValues;
046 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
047 import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
048 import com.liferay.portlet.documentlibrary.service.DLFolderService;
049 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
050
051 import java.io.File;
052 import java.io.IOException;
053 import java.io.InputStream;
054
055 import java.util.Date;
056
057
060 public class DLLocalServiceImpl implements DLLocalService {
061
062 public void addFile(
063 long companyId, String portletId, long groupId, long repositoryId,
064 String fileName, boolean validateFileExtension, long fileEntryId,
065 String properties, Date modifiedDate, ServiceContext serviceContext,
066 InputStream is)
067 throws PortalException, SystemException {
068
069 validate(fileName, validateFileExtension, is);
070
071 hook.addFile(
072 companyId, portletId, groupId, repositoryId, fileName, fileEntryId,
073 properties, modifiedDate, serviceContext, is);
074 }
075
076 public void checkRoot(long companyId) throws SystemException {
077 hook.checkRoot(companyId);
078 }
079
080 public InputStream getFileAsStream(
081 long companyId, long repositoryId, String fileName)
082 throws PortalException, SystemException {
083
084 return hook.getFileAsStream(companyId, repositoryId, fileName);
085 }
086
087 public InputStream getFileAsStream(
088 long companyId, long repositoryId, String fileName,
089 String versionNumber)
090 throws PortalException, SystemException {
091
092 return hook.getFileAsStream(
093 companyId, repositoryId, fileName, versionNumber);
094 }
095
096 public boolean hasFile(
097 long companyId, long repositoryId, String fileName,
098 String versionNumber)
099 throws PortalException, SystemException {
100
101 return hook.hasFile(companyId, repositoryId, fileName, versionNumber);
102 }
103
104 public void move(String srcDir, String destDir) throws SystemException {
105 hook.move(srcDir, destDir);
106 }
107
108 public Hits search(
109 long companyId, String portletId, long groupId,
110 long userId, long[] repositoryIds, String keywords, int start,
111 int end)
112 throws SystemException {
113
114 try {
115 BooleanQuery contextQuery = BooleanQueryFactoryUtil.create();
116
117 contextQuery.addRequiredTerm(Field.PORTLET_ID, portletId);
118
119 if (groupId > 0) {
120 Group group = groupLocalService.getGroup(groupId);
121
122 if (group.isLayout()) {
123 contextQuery.addRequiredTerm(Field.SCOPE_GROUP_ID, groupId);
124
125 groupId = group.getParentGroupId();
126 }
127
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 try {
137 if (userId > 0) {
138 PermissionChecker permissionChecker =
139 PermissionThreadLocal.getPermissionChecker();
140
141 DLFolderPermission.check(
142 permissionChecker, groupId, repositoryId,
143 ActionKeys.VIEW);
144 }
145
146 if (repositoryId ==
147 DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
148
149 repositoryId = groupId;
150 }
151
152 TermQuery termQuery = TermQueryFactoryUtil.create(
153 "repositoryId", repositoryId);
154
155 repositoryIdsQuery.add(
156 termQuery, BooleanClauseOccur.SHOULD);
157 }
158 catch (Exception e) {
159 }
160 }
161
162 contextQuery.add(repositoryIdsQuery, BooleanClauseOccur.MUST);
163 }
164
165 BooleanQuery searchQuery = BooleanQueryFactoryUtil.create();
166
167 searchQuery.addTerms(_KEYWORDS_FIELDS, keywords);
168
169 BooleanQuery fullQuery = BooleanQueryFactoryUtil.create();
170
171 fullQuery.add(contextQuery, BooleanClauseOccur.MUST);
172
173 if (searchQuery.clauses().size() > 0) {
174 fullQuery.add(searchQuery, BooleanClauseOccur.MUST);
175 }
176
177 return SearchEngineUtil.search(
178 companyId, new long[] {groupId}, userId,
179 DLFileEntry.class.getName(), fullQuery, start, end);
180 }
181 catch (Exception e) {
182 throw new SystemException(e);
183 }
184 }
185
186 public void updateFile(
187 long companyId, String portletId, long groupId, long repositoryId,
188 String fileName, String fileExtension,
189 boolean validateFileExtension, String versionNumber,
190 String sourceFileName, long fileEntryId, String properties,
191 Date modifiedDate, ServiceContext serviceContext, InputStream is)
192 throws PortalException, SystemException {
193
194 validate(
195 fileName, fileExtension, sourceFileName, validateFileExtension, is);
196
197 hook.updateFile(
198 companyId, portletId, groupId, repositoryId, fileName,
199 versionNumber, sourceFileName, fileEntryId, properties,
200 modifiedDate, serviceContext, is);
201 }
202
203 public void validate(String fileName, boolean validateFileExtension)
204 throws PortalException, SystemException {
205
206 if ((fileName.indexOf("\\\\") != -1) ||
207 (fileName.indexOf("
208 (fileName.indexOf(":") != -1) ||
209 (fileName.indexOf("*") != -1) ||
210 (fileName.indexOf("?") != -1) ||
211 (fileName.indexOf("\"") != -1) ||
212 (fileName.indexOf("<") != -1) ||
213 (fileName.indexOf(">") != -1) ||
214 (fileName.indexOf("|") != -1) ||
215 (fileName.indexOf("[") != -1) ||
216 (fileName.indexOf("]") != -1) ||
217 (fileName.indexOf("'") != -1) ||
218 (fileName.indexOf("..\\") != -1) ||
219 (fileName.indexOf("../") != -1) ||
220 (fileName.indexOf("\\..") != -1) ||
221 (fileName.indexOf("/..") != -1)) {
222
223 throw new FileNameException(fileName);
224 }
225
226 if (validateFileExtension) {
227 boolean validFileExtension = false;
228
229 String[] fileExtensions = PrefsPropsUtil.getStringArray(
230 PropsKeys.DL_FILE_EXTENSIONS, StringPool.COMMA);
231
232 for (int i = 0; i < fileExtensions.length; i++) {
233 if (StringPool.STAR.equals(fileExtensions[i]) ||
234 StringUtil.endsWith(fileName, fileExtensions[i])) {
235
236 validFileExtension = true;
237
238 break;
239 }
240 }
241
242 if (!validFileExtension) {
243 throw new FileNameException(fileName);
244 }
245 }
246 }
247
248 public void validate(
249 String fileName, boolean validateFileExtension, byte[] bytes)
250 throws PortalException, SystemException {
251
252 validate(fileName, validateFileExtension);
253
254 if ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
255 ((bytes == null) ||
256 (bytes.length >
257 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
258
259 throw new FileSizeException(fileName);
260 }
261 }
262
263 public void validate(
264 String fileName, boolean validateFileExtension, File file)
265 throws PortalException, SystemException {
266
267 validate(fileName, validateFileExtension);
268
269 if ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
270 ((file == null) ||
271 (file.length() >
272 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
273
274 throw new FileSizeException(fileName);
275 }
276 }
277
278 public void validate(
279 String fileName, boolean validateFileExtension, InputStream is)
280 throws PortalException, SystemException {
281
282 validate(fileName, validateFileExtension);
283
284
285
286 try {
287 if ((PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
288 ((is == null) ||
289 (is.available() >
290 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE)))) {
291
292 throw new FileSizeException(fileName);
293 }
294 }
295 catch (IOException ioe) {
296 throw new FileSizeException(ioe.getMessage());
297 }
298 }
299
300 public void validate(
301 String fileName, String fileExtension, String sourceFileName,
302 boolean validateFileExtension, InputStream is)
303 throws PortalException, SystemException {
304
305 String sourceFileExtension = FileUtil.getExtension(sourceFileName);
306
307 if (Validator.isNotNull(sourceFileName) &&
308 PropsValues.DL_FILE_EXTENSIONS_STRICT_CHECK &&
309 !fileExtension.equals(sourceFileExtension)) {
310
311 throw new SourceFileNameException(sourceFileExtension);
312 }
313
314 validate(fileName, validateFileExtension);
315
316 try {
317 if ((is != null) &&
318 (PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE) > 0) &&
319 (is.available() >
320 PrefsPropsUtil.getLong(PropsKeys.DL_FILE_MAX_SIZE))) {
321
322 throw new FileSizeException(fileName);
323 }
324 }
325 catch (IOException ioe) {
326 throw new FileSizeException(ioe.getMessage());
327 }
328 }
329
330 @BeanReference(type = GroupLocalService.class)
331 protected GroupLocalService groupLocalService;
332
333 @BeanReference(type = DLFolderService.class)
334 protected DLFolderService dlFolderService;
335
336 @BeanReference(type = Hook.class)
337 protected Hook hook;
338
339 private static final String[] _KEYWORDS_FIELDS = {
340 Field.ASSET_TAG_NAMES, Field.CONTENT, Field.PROPERTIES
341 };
342
343 }