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.kernel.util.StringPool;
19  import com.liferay.portal.kernel.webdav.BaseResourceImpl;
20  import com.liferay.portal.kernel.webdav.WebDAVException;
21  import com.liferay.portal.kernel.webdav.WebDAVRequest;
22  import com.liferay.portal.util.PropsValues;
23  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
24  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
25  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
26  import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
27  import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
28  
29  import java.io.InputStream;
30  
31  /**
32   * <a href="DLFileEntryResourceImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class DLFileEntryResourceImpl extends BaseResourceImpl {
37  
38      public DLFileEntryResourceImpl(
39          WebDAVRequest webDavRequest, DLFileEntry fileEntry, String parentPath,
40          String name) {
41  
42          super(
43              parentPath, name, fileEntry.getTitle(), fileEntry.getCreateDate(),
44              fileEntry.getModifiedDate(), fileEntry.getSize());
45  
46          setModel(fileEntry);
47          setClassName(DLFileEntry.class.getName());
48          setPrimaryKey(fileEntry.getPrimaryKey());
49  
50          _webDavRequest = webDavRequest;
51          _fileEntry = fileEntry;
52      }
53  
54      public boolean isCollection() {
55          return false;
56      }
57  
58      public boolean isLocked() {
59          try {
60              return DLFileEntryServiceUtil.hasFileEntryLock(
61                  _fileEntry.getGroupId(), _fileEntry.getFolderId(),
62                  _fileEntry.getName());
63          }
64          catch (Exception e) {
65          }
66  
67          return false;
68      }
69  
70      public String getContentType() {
71          return MimeTypesUtil.getContentType(_fileEntry.getTitle());
72      }
73  
74      public InputStream getContentAsStream() throws WebDAVException {
75          try {
76              String version = StringPool.BLANK;
77  
78              if (PropsValues.DL_WEBDAV_HOLD_LOCK) {
79  
80                  // Get last version regardless of status
81  
82                  DLFileVersion fileVersion =
83                      DLFileVersionLocalServiceUtil.getLatestFileVersion(
84                          _fileEntry.getGroupId(), _fileEntry.getFolderId(),
85                          _fileEntry.getName());
86  
87                  version = fileVersion.getVersion();
88              }
89  
90              return DLFileEntryLocalServiceUtil.getFileAsStream(
91                  _webDavRequest.getCompanyId(), _webDavRequest.getUserId(),
92                  _fileEntry.getGroupId(), _fileEntry.getFolderId(),
93                  _fileEntry.getName(), version);
94          }
95          catch (Exception e) {
96              throw new WebDAVException(e);
97          }
98      }
99  
100     private WebDAVRequest _webDavRequest;
101     private DLFileEntry _fileEntry;
102 
103 }