1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.webdav;
21  
22  import com.liferay.portal.util.ContentTypeUtil;
23  import com.liferay.portal.webdav.BaseResourceImpl;
24  import com.liferay.portal.webdav.WebDAVException;
25  import com.liferay.portal.webdav.WebDAVRequest;
26  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
27  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
28  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
29  
30  import java.io.InputStream;
31  
32  /**
33   * <a href="DLFileEntryResourceImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class DLFileEntryResourceImpl extends BaseResourceImpl {
39  
40      public DLFileEntryResourceImpl(
41          WebDAVRequest webDavRequest, DLFileEntry fileEntry, String parentPath,
42          String name) {
43  
44          super(
45              parentPath, name, fileEntry.getTitleWithExtension(),
46              fileEntry.getCreateDate(), fileEntry.getModifiedDate(),
47              fileEntry.getSize());
48  
49          setModel(fileEntry);
50          setClassName(DLFileEntry.class.getName());
51          setPrimaryKey(fileEntry.getPrimaryKey());
52  
53          _webDavRequest = webDavRequest;
54          _fileEntry = fileEntry;
55      }
56  
57      public boolean isCollection() {
58          return false;
59      }
60  
61      public boolean isLocked() {
62          try {
63              DLFileEntryServiceUtil.getFileEntryLock(
64                  _fileEntry.getFolderId(), _fileEntry.getName());
65  
66              return true;
67          }
68          catch (Exception e) {
69          }
70  
71          return false;
72      }
73  
74      public String getContentType() {
75          return ContentTypeUtil.getContentType(_fileEntry.getName());
76      }
77  
78      public InputStream getContentAsStream() throws WebDAVException {
79          try {
80              return DLFileEntryLocalServiceUtil.getFileAsStream(
81                  _webDavRequest.getCompanyId(), _webDavRequest.getUserId(),
82                  _fileEntry.getFolderId(), _fileEntry.getName());
83          }
84          catch (Exception e) {
85              throw new WebDAVException(e);
86          }
87      }
88  
89      private WebDAVRequest _webDavRequest;
90      private DLFileEntry _fileEntry;
91  
92  }