1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.documentlibrary.webdav;
16  
17  import com.liferay.portal.kernel.util.MimeTypesUtil;
18  import com.liferay.portal.model.Lock;
19  import com.liferay.portal.webdav.BaseResourceImpl;
20  import com.liferay.portal.webdav.WebDAVException;
21  import com.liferay.portal.webdav.WebDAVRequest;
22  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
23  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
24  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
25  
26  import java.io.InputStream;
27  
28  /**
29   * <a href="DLFileEntryResourceImpl.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class DLFileEntryResourceImpl extends BaseResourceImpl {
34  
35      public DLFileEntryResourceImpl(
36          WebDAVRequest webDavRequest, DLFileEntry fileEntry, String parentPath,
37          String name) {
38  
39          super(
40              parentPath, name, fileEntry.getTitleWithExtension(),
41              fileEntry.getCreateDate(), fileEntry.getModifiedDate(),
42              fileEntry.getSize());
43  
44          setModel(fileEntry);
45          setClassName(DLFileEntry.class.getName());
46          setPrimaryKey(fileEntry.getPrimaryKey());
47  
48          _webDavRequest = webDavRequest;
49          _fileEntry = fileEntry;
50      }
51  
52      public boolean isCollection() {
53          return false;
54      }
55  
56      public Lock getLock() {
57          try {
58              return DLFileEntryServiceUtil.getFileEntryLock(
59                  _fileEntry.getFolderId(), _fileEntry.getName());
60          }
61          catch (Exception e) {
62          }
63  
64          return null;
65      }
66  
67      public boolean isLocked() {
68          try {
69              return DLFileEntryServiceUtil.hasFileEntryLock(
70                  _fileEntry.getFolderId(), _fileEntry.getName());
71          }
72          catch (Exception e) {
73          }
74  
75          return false;
76      }
77  
78      public String getContentType() {
79          return MimeTypesUtil.getContentType(_fileEntry.getName());
80      }
81  
82      public InputStream getContentAsStream() throws WebDAVException {
83          try {
84              return DLFileEntryLocalServiceUtil.getFileAsStream(
85                  _webDavRequest.getCompanyId(), _webDavRequest.getUserId(),
86                  _fileEntry.getFolderId(), _fileEntry.getName());
87          }
88          catch (Exception e) {
89              throw new WebDAVException(e);
90          }
91      }
92  
93      private WebDAVRequest _webDavRequest;
94      private DLFileEntry _fileEntry;
95  
96  }