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