1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.webdav;
16  
17  import com.liferay.portal.kernel.util.MimeTypesUtil;
18  import com.liferay.portal.webdav.BaseResourceImpl;
19  import com.liferay.portal.webdav.WebDAVException;
20  import com.liferay.portal.webdav.WebDAVRequest;
21  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
22  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
23  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
24  
25  import java.io.InputStream;
26  
27  /**
28   * <a href="DLFileEntryResourceImpl.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class DLFileEntryResourceImpl extends BaseResourceImpl {
33  
34      public DLFileEntryResourceImpl(
35          WebDAVRequest webDavRequest, DLFileEntry fileEntry, String parentPath,
36          String name) {
37  
38          super(
39              parentPath, name, fileEntry.getTitle(), fileEntry.getCreateDate(),
40              fileEntry.getModifiedDate(), fileEntry.getSize());
41  
42          setModel(fileEntry);
43          setClassName(DLFileEntry.class.getName());
44          setPrimaryKey(fileEntry.getPrimaryKey());
45  
46          _webDavRequest = webDavRequest;
47          _fileEntry = fileEntry;
48      }
49  
50      public boolean isCollection() {
51          return false;
52      }
53  
54      public boolean isLocked() {
55          try {
56              return DLFileEntryServiceUtil.hasFileEntryLock(
57                  _fileEntry.getGroupId(), _fileEntry.getFolderId(),
58                  _fileEntry.getName());
59          }
60          catch (Exception e) {
61          }
62  
63          return false;
64      }
65  
66      public String getContentType() {
67          return MimeTypesUtil.getContentType(_fileEntry.getTitle());
68      }
69  
70      public InputStream getContentAsStream() throws WebDAVException {
71          try {
72              return DLFileEntryLocalServiceUtil.getFileAsStream(
73                  _webDavRequest.getCompanyId(), _webDavRequest.getUserId(),
74                  _fileEntry.getGroupId(), _fileEntry.getFolderId(),
75                  _fileEntry.getName());
76          }
77          catch (Exception e) {
78              throw new WebDAVException(e);
79          }
80      }
81  
82      private WebDAVRequest _webDavRequest;
83      private DLFileEntry _fileEntry;
84  
85  }