1
22
23 package com.liferay.portlet.documentlibrary.webdav;
24
25 import com.liferay.documentlibrary.DuplicateFileException;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.security.permission.ActionKeys;
29 import com.liferay.portal.kernel.util.StringPool;
30 import com.liferay.portal.kernel.util.StringUtil;
31 import com.liferay.portal.security.auth.PrincipalException;
32 import com.liferay.portal.service.LayoutLocalServiceUtil;
33 import com.liferay.portal.webdav.BaseResourceImpl;
34 import com.liferay.portal.webdav.BaseWebDAVStorageImpl;
35 import com.liferay.portal.webdav.Resource;
36 import com.liferay.portal.webdav.Status;
37 import com.liferay.portal.webdav.WebDAVException;
38 import com.liferay.portal.webdav.WebDAVRequest;
39 import com.liferay.portal.webdav.WebDAVUtil;
40 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
41 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
42 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
43 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
44 import com.liferay.portlet.documentlibrary.model.DLFolder;
45 import com.liferay.portlet.documentlibrary.model.impl.DLFolderImpl;
46 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
47 import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
48 import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
49 import com.liferay.portlet.documentlibrary.service.DLFolderServiceUtil;
50 import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
51 import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
52 import com.liferay.util.FileUtil;
53 import com.liferay.util.PwdGenerator;
54 import com.liferay.util.SystemProperties;
55 import com.liferay.util.Time;
56
57 import java.io.File;
58 import java.io.InputStream;
59
60 import java.util.ArrayList;
61 import java.util.Iterator;
62 import java.util.List;
63
64 import javax.servlet.http.HttpServletRequest;
65 import javax.servlet.http.HttpServletResponse;
66
67 import org.apache.commons.logging.Log;
68 import org.apache.commons.logging.LogFactory;
69
70
77 public class DLWebDAVStorageImpl extends BaseWebDAVStorageImpl {
78
79 public Status addCollection(WebDAVRequest webDavReq)
80 throws WebDAVException {
81
82 try {
83 HttpServletRequest req = webDavReq.getHttpServletRequest();
84
85 if (req.getContentLength() > 0) {
86 return new Status(
87 HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
88 }
89
90 String[] pathArray = webDavReq.getPathArray();
91 long parentFolderId = getParentFolderId(pathArray);
92 String name = WebDAVUtil.getEntryName(pathArray);
93
94 try {
95 DLFileEntryLocalServiceUtil.getFileEntryByTitle(
96 parentFolderId, name);
97
98 return new Status(HttpServletResponse.SC_CONFLICT);
99 }
100 catch (Exception e) {
101 }
102
103 long plid = getPlid(webDavReq.getGroupId());
104 String description = StringPool.BLANK;
105 boolean addCommunityPermissions = true;
106 boolean addGuestPermissions = true;
107
108 DLFolderServiceUtil.addFolder(
109 plid, parentFolderId, name, description,
110 addCommunityPermissions, addGuestPermissions);
111
112 String location = StringUtil.merge(pathArray, StringPool.SLASH);
113
114 return new Status(location, HttpServletResponse.SC_CREATED);
115 }
116 catch (DuplicateFolderNameException dfne) {
117 return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
118 }
119 catch (NoSuchFolderException nsfe) {
120 return new Status(HttpServletResponse.SC_CONFLICT);
121 }
122 catch (PrincipalException pe) {
123 return new Status(HttpServletResponse.SC_FORBIDDEN);
124 }
125 catch (Exception e) {
126 throw new WebDAVException(e);
127 }
128 }
129
130 public int copyCollectionResource(
131 WebDAVRequest webDavReq, Resource resource, String destination,
132 boolean overwrite, long depth)
133 throws WebDAVException {
134
135 try {
136 String[] destinationArray = WebDAVUtil.getPathArray(
137 destination, true);
138
139 long parentFolderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
140
141 try {
142 parentFolderId = getParentFolderId(destinationArray);
143 }
144 catch (NoSuchFolderException nsfe) {
145 return HttpServletResponse.SC_CONFLICT;
146 }
147
148 DLFolder folder = (DLFolder)resource.getModel();
149
150 long groupId = WebDAVUtil.getGroupId(destination);
151 long plid = getPlid(groupId);
152 String name = WebDAVUtil.getEntryName(destinationArray);
153 String description = folder.getDescription();
154 boolean addCommunityPermissions = true;
155 boolean addGuestPermissions = true;
156
157 int status = HttpServletResponse.SC_CREATED;
158
159 if (overwrite) {
160 try {
161 DLFolder destFolder = DLFolderServiceUtil.getFolder(
162 groupId, parentFolderId, name);
163
164 DLFolderServiceUtil.deleteFolder(destFolder.getFolderId());
165
166 status = HttpServletResponse.SC_NO_CONTENT;
167 }
168 catch (NoSuchFolderException nsfe) {
169 try {
170 DLFileEntryServiceUtil.deleteFileEntryByTitle(
171 parentFolderId, name);
172
173 status = HttpServletResponse.SC_NO_CONTENT;
174 }
175 catch (NoSuchFileEntryException nsfee) {
176 }
177 }
178 }
179
180 if (depth == 0) {
181 DLFolderServiceUtil.addFolder(
182 plid, parentFolderId, name, description,
183 addCommunityPermissions, addGuestPermissions);
184 }
185 else {
186 DLFolderServiceUtil.copyFolder(
187 plid, folder.getFolderId(), parentFolderId, name,
188 description, addCommunityPermissions, addGuestPermissions);
189 }
190
191 return status;
192 }
193 catch (DuplicateFolderNameException dfne) {
194 return HttpServletResponse.SC_PRECONDITION_FAILED;
195 }
196 catch (PrincipalException pe) {
197 return HttpServletResponse.SC_FORBIDDEN;
198 }
199 catch (Exception e) {
200 throw new WebDAVException(e);
201 }
202 }
203
204 public int copySimpleResource(
205 WebDAVRequest webDavReq, Resource resource, String destination,
206 boolean overwrite)
207 throws WebDAVException {
208
209 File file = null;
210
211 try {
212 String[] destinationArray = WebDAVUtil.getPathArray(
213 destination, true);
214
215 long parentFolderId;
216
217 try {
218 parentFolderId = getParentFolderId(destinationArray);
219 }
220 catch (NoSuchFolderException nsfe) {
221 return HttpServletResponse.SC_CONFLICT;
222 }
223
224 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
225
226 DLFileEntryPermission.check(
227 webDavReq.getPermissionChecker(), fileEntry,
228 ActionKeys.VIEW);
229
230 long userId = webDavReq.getUserId();
231 String name = StringPool.BLANK;
232 String sourceName = fileEntry.getName();
233 String title = WebDAVUtil.getEntryName(destinationArray);
234 String description = fileEntry.getDescription();
235 String[] tagsEntries = new String[0];
236 String extraSettings = fileEntry.getExtraSettings();
237 boolean addCommunityPermissions = true;
238 boolean addGuestPermissions = true;
239
240 String fileName =
241 SystemProperties.get(SystemProperties.TMP_DIR) +
242 StringPool.SLASH + Time.getTimestamp() +
243 PwdGenerator.getPassword(PwdGenerator.KEY2, 8);
244
245 file = new File(fileName);
246
247 InputStream is = DLFileEntryLocalServiceUtil.getFileAsStream(
248 fileEntry.getCompanyId(), userId, fileEntry.getFolderId(),
249 fileEntry.getName());
250
251 FileUtil.write(file, is);
252
253 if (_log.isDebugEnabled()) {
254 _log.debug("Writing request to file " + fileName);
255 }
256
257 DLFolderPermission.check(
258 webDavReq.getPermissionChecker(), parentFolderId,
259 ActionKeys.ADD_DOCUMENT);
260
261 int status = HttpServletResponse.SC_CREATED;
262
263 if (overwrite) {
264 try {
265 long groupId = webDavReq.getGroupId();
266
267 DLFolderServiceUtil.deleteFolder(
268 groupId, parentFolderId, title);
269
270 DLFileEntryLocalServiceUtil.addFileEntry(
271 userId, parentFolderId, name, title,
272 description, tagsEntries, extraSettings, file,
273 addCommunityPermissions, addGuestPermissions);
274
275 status = HttpServletResponse.SC_NO_CONTENT;
276 }
277 catch (NoSuchFolderException nsfe) {
278 DLFileEntry destFile =
279 DLFileEntryLocalServiceUtil.addOrOverwriteFileEntry(
280 userId, parentFolderId, name, sourceName, title,
281 description, tagsEntries, extraSettings, file,
282 addCommunityPermissions, addGuestPermissions);
283
284 if (destFile.getVersion() > 1.0) {
285 status = HttpServletResponse.SC_NO_CONTENT;
286 }
287 else {
288 status = HttpServletResponse.SC_CREATED;
289 }
290 }
291 }
292 else {
293 DLFileEntryLocalServiceUtil.addFileEntry(
294 userId, parentFolderId, name, title,
295 description, tagsEntries, extraSettings, file,
296 addCommunityPermissions, addGuestPermissions);
297
298 status = HttpServletResponse.SC_CREATED;
299 }
300
301 return status;
302 }
303 catch (DuplicateFolderNameException dfne) {
304 return HttpServletResponse.SC_PRECONDITION_FAILED;
305 }
306 catch (DuplicateFileException dfe) {
307 return HttpServletResponse.SC_PRECONDITION_FAILED;
308 }
309 catch (PrincipalException pe) {
310 return HttpServletResponse.SC_FORBIDDEN;
311 }
312 catch (Exception e) {
313 throw new WebDAVException(e);
314 }
315 finally {
316 if (file != null) {
317 file.delete();
318 }
319 }
320 }
321
322 public int deleteResource(WebDAVRequest webDavReq) throws WebDAVException {
323 try {
324 Resource resource = getResource(webDavReq);
325
326 if (resource == null) {
327 return HttpServletResponse.SC_NOT_FOUND;
328 }
329
330 Object model = resource.getModel();
331
332 if (model instanceof DLFolder) {
333 DLFolder folder = (DLFolder)model;
334
335 DLFolderServiceUtil.deleteFolder(folder.getFolderId());
336 }
337 else {
338 DLFileEntry fileEntry = (DLFileEntry)model;
339
340 DLFileEntryServiceUtil.deleteFileEntry(
341 fileEntry.getFolderId(), fileEntry.getName());
342 }
343
344 return HttpServletResponse.SC_NO_CONTENT;
345 }
346 catch (PrincipalException pe) {
347 return HttpServletResponse.SC_FORBIDDEN;
348 }
349 catch (Exception e) {
350 throw new WebDAVException(e);
351 }
352 }
353
354 public Resource getResource(WebDAVRequest webDavReq)
355 throws WebDAVException {
356
357 try {
358 String[] pathArray = webDavReq.getPathArray();
359
360 long parentFolderId = getParentFolderId(pathArray);
361 String name = WebDAVUtil.getEntryName(pathArray);
362
363 try {
364 DLFolder folder = DLFolderServiceUtil.getFolder(
365 webDavReq.getGroupId(), parentFolderId, name);
366
367 if ((folder.getParentFolderId() != parentFolderId) ||
368 (webDavReq.getGroupId() != folder.getGroupId())) {
369
370 throw new NoSuchFolderException();
371 }
372
373 return toResource(webDavReq, folder, false);
374 }
375 catch (NoSuchFolderException nsfe) {
376 try {
377 String titleWithExtension = name;
378
379 DLFileEntry fileEntry =
380 DLFileEntryLocalServiceUtil.getFileEntryByTitle(
381 parentFolderId, titleWithExtension);
382
383 DLFileEntryPermission.check(
384 webDavReq.getPermissionChecker(), fileEntry,
385 ActionKeys.VIEW);
386
387 return toResource(webDavReq, fileEntry, false);
388 }
389 catch (NoSuchFileEntryException nsfee) {
390 return null;
391 }
392 }
393 }
394 catch (Exception e) {
395 throw new WebDAVException(e);
396 }
397 }
398
399 public List getResources(WebDAVRequest webDavReq)
400 throws WebDAVException {
401
402 try {
403 long folderId = getFolderId(webDavReq.getPathArray());
404
405 List folders = getFolders(webDavReq, folderId);
406 List fileEntries = getFileEntries(webDavReq, folderId);
407
408 List resources = new ArrayList(folders.size() + fileEntries.size());
409
410 resources.addAll(folders);
411 resources.addAll(fileEntries);
412
413 return resources;
414 }
415 catch (Exception e) {
416 throw new WebDAVException(e);
417 }
418 }
419
420 public int moveCollectionResource(
421 WebDAVRequest webDavReq, Resource resource, String destination,
422 boolean overwrite)
423 throws WebDAVException {
424
425 try {
426 String[] destinationArray = WebDAVUtil.getPathArray(
427 destination, true);
428
429 DLFolder folder = (DLFolder)resource.getModel();
430
431 long groupId = webDavReq.getGroupId();
432 long folderId = folder.getFolderId();
433 long parentFolderId = getParentFolderId(destinationArray);
434 String name = WebDAVUtil.getEntryName(destinationArray);
435 String description = folder.getDescription();
436
437 if (parentFolderId != folder.getParentFolderId()) {
438 name = folder.getName();
439 }
440
441 int status = HttpServletResponse.SC_CREATED;
442
443 if (overwrite) {
444 try {
445 DLFolder destFolder = DLFolderServiceUtil.getFolder(
446 groupId, parentFolderId, name);
447
448 DLFolderServiceUtil.deleteFolder(
449 destFolder.getFolderId());
450
451 status = HttpServletResponse.SC_NO_CONTENT;
452 }
453 catch (NoSuchFolderException nsfe) {
454 try {
455 DLFileEntryServiceUtil.deleteFileEntryByTitle(
456 parentFolderId, name);
457
458 status = HttpServletResponse.SC_NO_CONTENT;
459 }
460 catch (NoSuchFileEntryException nsfee) {
461 }
462 }
463 }
464
465 DLFolderServiceUtil.updateFolder(
466 folderId, parentFolderId, name, description);
467
468 return status;
469 }
470 catch (PrincipalException pe) {
471 return HttpServletResponse.SC_FORBIDDEN;
472 }
473 catch (DuplicateFileException dfe) {
474 return HttpServletResponse.SC_PRECONDITION_FAILED;
475 }
476 catch (DuplicateFolderNameException dfne) {
477 return HttpServletResponse.SC_PRECONDITION_FAILED;
478 }
479 catch (Exception e) {
480 throw new WebDAVException(e);
481 }
482 }
483
484 public int moveSimpleResource(
485 WebDAVRequest webDavReq, Resource resource, String destination,
486 boolean overwrite)
487 throws WebDAVException {
488
489 try {
490 String[] destinationArray = WebDAVUtil.getPathArray(
491 destination, true);
492
493 DLFileEntry fileEntry = (DLFileEntry)resource.getModel();
494
495 long folderId = fileEntry.getFolderId();
496 long newFolderId = getParentFolderId(destinationArray);
497 String name = fileEntry.getName();
498 String sourceFileName = null;
499 String title = WebDAVUtil.getEntryName(destinationArray);
500 String description = fileEntry.getDescription();
501 String[] tagsEntries = new String[0];
502 String extraSettings = fileEntry.getExtraSettings();
503 byte[] byteArray = null;
504
505 if (newFolderId != folderId) {
506 title = fileEntry.getTitle();
507 }
508
509 int status = HttpServletResponse.SC_CREATED;
510
511 if (overwrite) {
512 try {
513 DLFileEntry destFile =
514 DLFileEntryServiceUtil.getFileEntryByTitle(
515 folderId, title);
516
517 DLFileEntryServiceUtil.deleteFileEntry(
518 destFile.getFolderId(), destFile.getName());
519
520 status = HttpServletResponse.SC_NO_CONTENT;
521 }
522 catch (NoSuchFileEntryException nsfee) {
523 try {
524 long groupId = webDavReq.getGroupId();
525
526 DLFolderServiceUtil.deleteFolder(
527 groupId, folderId, title);
528
529 status = HttpServletResponse.SC_NO_CONTENT;
530 }
531 catch (NoSuchFolderException nsfe) {
532 }
533 }
534 }
535
536 DLFileEntryServiceUtil.updateFileEntry(
537 folderId, newFolderId, name, sourceFileName, title,
538 description, tagsEntries, extraSettings, byteArray);
539
540 return status;
541 }
542 catch (PrincipalException pe) {
543 return HttpServletResponse.SC_FORBIDDEN;
544 }
545 catch (DuplicateFileException dfe) {
546 return HttpServletResponse.SC_PRECONDITION_FAILED;
547 }
548 catch (DuplicateFolderNameException dfne) {
549 return HttpServletResponse.SC_PRECONDITION_FAILED;
550 }
551 catch (Exception e) {
552 throw new WebDAVException(e);
553 }
554 }
555
556 public int putResource(WebDAVRequest webDavReq) throws WebDAVException {
557 File file = null;
558
559 try {
560 HttpServletRequest req = webDavReq.getHttpServletRequest();
561 String[] pathArray = webDavReq.getPathArray();
562 long userId = webDavReq.getUserId();
563
564 long parentFolderId = getParentFolderId(pathArray);
565 String name = WebDAVUtil.getEntryName(pathArray);
566 String title = WebDAVUtil.getEntryName(pathArray);
567 String description = StringPool.BLANK;
568 String[] tagsEntries = new String[0];
569 String extraSettings = StringPool.BLANK;
570 boolean addCommunityPermissions = true;
571 boolean addGuestPermissions = true;
572
573 String fileName =
574 SystemProperties.get(SystemProperties.TMP_DIR) +
575 StringPool.SLASH + Time.getTimestamp() +
576 PwdGenerator.getPassword(PwdGenerator.KEY2, 8);
577
578 file = new File(fileName);
579
580 FileUtil.write(file, req.getInputStream());
581
582 if (_log.isDebugEnabled()) {
583 _log.debug("Writing request to file " + fileName);
584 }
585
586 DLFolderPermission.check(
587 webDavReq.getPermissionChecker(), parentFolderId,
588 ActionKeys.ADD_DOCUMENT);
589
590 DLFileEntryLocalServiceUtil.addFileEntry(
591 userId, parentFolderId, name, title, description, tagsEntries,
592 extraSettings, file, addCommunityPermissions,
593 addGuestPermissions);
594
595 return HttpServletResponse.SC_CREATED;
596 }
597 catch (PrincipalException pe) {
598 return HttpServletResponse.SC_FORBIDDEN;
599 }
600 catch (PortalException pe) {
601 if (_log.isWarnEnabled()) {
602 _log.warn(pe, pe);
603 }
604
605 return HttpServletResponse.SC_CONFLICT;
606 }
607 catch (Exception e) {
608 throw new WebDAVException(e);
609 }
610 finally {
611 if (file != null) {
612 file.delete();
613 }
614 }
615 }
616
617 protected List getFileEntries(WebDAVRequest webDavReq, long parentFolderId)
618 throws Exception {
619
620 List fileEntries = new ArrayList();
621
622 long plid = getPlid(webDavReq.getGroupId());
623
624 DLFolderPermission.check(
625 webDavReq.getPermissionChecker(), plid, parentFolderId,
626 ActionKeys.VIEW);
627
628 Iterator itr = DLFileEntryLocalServiceUtil.getFileEntries(
629 parentFolderId).iterator();
630
631 while (itr.hasNext()) {
632 DLFileEntry fileEntry = (DLFileEntry)itr.next();
633
634 if (DLFileEntryPermission.contains(
635 webDavReq.getPermissionChecker(), fileEntry,
636 ActionKeys.VIEW)) {
637
638 Resource resource = toResource(webDavReq, fileEntry, true);
639
640 fileEntries.add(resource);
641 }
642 }
643
644 return fileEntries;
645 }
646
647 protected long getFolderId(String[] pathArray) throws Exception {
648 return getFolderId(pathArray, false);
649 }
650
651 protected long getFolderId(String[] pathArray, boolean parent)
652 throws Exception {
653
654 long folderId = DLFolderImpl.DEFAULT_PARENT_FOLDER_ID;
655
656 if (pathArray.length <= 2) {
657 return folderId;
658 }
659 else {
660 long groupId = WebDAVUtil.getGroupId(pathArray);
661
662 int x = pathArray.length;
663
664 if (parent) {
665 x--;
666 }
667
668 for (int i = 2; i < x; i++) {
669 String name = pathArray[i];
670
671 DLFolder folder = DLFolderServiceUtil.getFolder(
672 groupId, folderId, name);
673
674 if (groupId == folder.getGroupId()) {
675 folderId = folder.getFolderId();
676 }
677 }
678 }
679
680 return folderId;
681 }
682
683 protected List getFolders(WebDAVRequest webDavReq, long parentFolderId)
684 throws Exception {
685
686 List folders = new ArrayList();
687
688 long plid = getPlid(webDavReq.getGroupId());
689 long groupId = webDavReq.getGroupId();
690
691 DLFolderPermission.check(
692 webDavReq.getPermissionChecker(), plid, parentFolderId,
693 ActionKeys.VIEW);
694
695 Iterator itr = DLFolderLocalServiceUtil.getFolders(
696 groupId, parentFolderId).iterator();
697
698 while (itr.hasNext()) {
699 DLFolder folder = (DLFolder)itr.next();
700
701 if (DLFolderPermission.contains(
702 webDavReq.getPermissionChecker(), folder,
703 ActionKeys.VIEW)) {
704
705 Resource resource = toResource(webDavReq, folder, true);
706
707 folders.add(resource);
708 }
709 }
710
711 return folders;
712 }
713
714 protected long getParentFolderId(String[] pathArray) throws Exception {
715 return getFolderId(pathArray, true);
716 }
717
718 protected long getPlid(long groupId) throws SystemException {
719 return LayoutLocalServiceUtil.getDefaultPlid(groupId);
720 }
721
722 protected Resource toResource(
723 WebDAVRequest webDavReq, DLFileEntry fileEntry, boolean appendPath) {
724
725 String parentPath = getRootPath() + webDavReq.getPath();
726 String name = StringPool.BLANK;
727
728 if (appendPath) {
729 name = fileEntry.getTitleWithExtension();
730 }
731
732 return new DLFileEntryResourceImpl(
733 webDavReq, fileEntry, parentPath, name);
734 }
735
736 protected Resource toResource(
737 WebDAVRequest webDavReq, DLFolder folder, boolean appendPath) {
738
739 String parentPath = getRootPath() + webDavReq.getPath();
740 String name = StringPool.BLANK;
741
742 if (appendPath) {
743 name = folder.getName();
744 }
745
746 Resource resource = new BaseResourceImpl(
747 parentPath, name, folder.getName(), folder.getCreateDate(),
748 folder.getModifiedDate());
749
750 resource.setModel(folder);
751 resource.setClassName(DLFolder.class.getName());
752 resource.setPrimaryKey(folder.getPrimaryKey());
753
754 return resource;
755 }
756
757 private static Log _log = LogFactory.getLog(DLWebDAVStorageImpl.class);
758
759 }