1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.action;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.MimeTypesUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.security.permission.ActionKeys;
31  import com.liferay.portal.struts.ActionConstants;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
37  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
38  import com.liferay.portlet.documentlibrary.model.DLFileVersion;
39  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
40  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
41  import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
42  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
43  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
44  import com.liferay.util.servlet.ServletResponseUtil;
45  
46  import java.io.InputStream;
47  
48  import javax.portlet.ActionRequest;
49  import javax.portlet.ActionResponse;
50  import javax.portlet.PortletConfig;
51  
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  
55  import org.apache.struts.action.ActionForm;
56  import org.apache.struts.action.ActionForward;
57  import org.apache.struts.action.ActionMapping;
58  
59  /**
60   * <a href="GetFileAction.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Brian Wing Shun Chan
63   * @author Jorge Ferrer
64   * @author Charles May
65   * @author Bruno Farache
66   *
67   */
68  public class GetFileAction extends PortletAction {
69  
70      public ActionForward strutsExecute(
71              ActionMapping mapping, ActionForm form, HttpServletRequest request,
72              HttpServletResponse response)
73          throws Exception {
74  
75          try {
76              long folderId = ParamUtil.getLong(request, "folderId");
77              String name = ParamUtil.getString(request, "name");
78              double version = ParamUtil.getDouble(request, "version");
79  
80              long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
81  
82              String uuid = ParamUtil.getString(request, "uuid");
83              long groupId = ParamUtil.getLong(request, "groupId");
84  
85              String targetExtension = ParamUtil.getString(
86                  request, "targetExtension");
87  
88              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
89                  WebKeys.THEME_DISPLAY);
90  
91              getFile(
92                  folderId, name, version, fileShortcutId, uuid, groupId,
93                  targetExtension, themeDisplay, request, response);
94  
95              return null;
96          }
97          catch (Exception e) {
98              PortalUtil.sendError(e, request, response);
99  
100             return null;
101         }
102     }
103 
104     public void processAction(
105             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
106             ActionRequest actionRequest, ActionResponse actionResponse)
107         throws Exception {
108 
109         try {
110             long folderId = ParamUtil.getLong(actionRequest, "folderId");
111             String name = ParamUtil.getString(actionRequest, "name");
112             double version = ParamUtil.getDouble(actionRequest, "version");
113 
114             long fileShortcutId = ParamUtil.getLong(
115                 actionRequest, "fileShortcutId");
116 
117             String uuid = ParamUtil.getString(actionRequest, "uuid");
118             long groupId = ParamUtil.getLong(actionRequest, "groupId");
119 
120             String targetExtension = ParamUtil.getString(
121                 actionRequest, "targetExtension");
122 
123             ThemeDisplay themeDisplay =
124                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
125 
126             HttpServletRequest request = PortalUtil.getHttpServletRequest(
127                 actionRequest);
128             HttpServletResponse response = PortalUtil.getHttpServletResponse(
129                 actionResponse);
130 
131             getFile(
132                 folderId, name, version, fileShortcutId, uuid, groupId,
133                 targetExtension, themeDisplay, request, response);
134 
135             setForward(actionRequest, ActionConstants.COMMON_NULL);
136         }
137         catch (Exception e) {
138             PortalUtil.sendError(e, actionRequest, actionResponse);
139         }
140     }
141 
142     protected void getFile(
143             long folderId, String name, double version, long fileShortcutId,
144             String uuid, long groupId, String targetExtension,
145             ThemeDisplay themeDisplay, HttpServletRequest request,
146             HttpServletResponse response)
147         throws Exception {
148 
149         InputStream is = null;
150 
151         try {
152             long companyId = themeDisplay.getCompanyId();
153             long userId = themeDisplay.getUserId();
154 
155             DLFileEntry fileEntry = null;
156 
157             if (Validator.isNotNull(uuid) && (groupId > 0)) {
158                 try {
159                     fileEntry = DLFileEntryLocalServiceUtil.
160                         getFileEntryByUuidAndGroupId(
161                             uuid, groupId);
162 
163                     folderId = fileEntry.getFolderId();
164                     name = fileEntry.getName();
165                 }
166                 catch (Exception e) {
167                 }
168             }
169 
170             if (fileShortcutId <= 0) {
171                 DLFileEntryPermission.check(
172                     themeDisplay.getPermissionChecker(), folderId, name,
173                     ActionKeys.VIEW);
174             }
175             else {
176                 DLFileShortcut fileShortcut =
177                     DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
178 
179                 folderId = fileShortcut.getToFolderId();
180                 name = fileShortcut.getToName();
181             }
182 
183             if (fileEntry == null) {
184                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
185                     folderId, name);
186             }
187 
188             if (version == 0) {
189                 version = fileEntry.getVersion();
190             }
191 
192             is = DLFileEntryLocalServiceUtil.getFileAsStream(
193                 companyId, userId, folderId, name, version);
194 
195             String fileName = fileEntry.getTitleWithExtension();
196 
197             if (Validator.isNotNull(targetExtension)) {
198                 String id = DocumentConversionUtil.getTempFileId(
199                     fileEntry.getFileEntryId(), version);
200 
201                 String sourceExtension = FileUtil.getExtension(name);
202 
203                 InputStream convertedIS = DocumentConversionUtil.convert(
204                     id, is, sourceExtension, targetExtension);
205 
206                 if ((convertedIS != null) && (convertedIS != is)) {
207                     StringBuilder sb = new StringBuilder();
208 
209                     sb.append(fileEntry.getTitle());
210                     sb.append(StringPool.PERIOD);
211                     sb.append(targetExtension);
212 
213                     fileName = sb.toString();
214 
215                     is = convertedIS;
216                 }
217             }
218 
219             int contentLength = fileEntry.getSize();
220 
221             if (version < fileEntry.getVersion()) {
222                 DLFileVersion fileVersion =
223                     DLFileVersionLocalServiceUtil.getFileVersion(
224                         folderId, name, version);
225 
226                 contentLength = fileVersion.getSize();
227             }
228 
229             String contentType = MimeTypesUtil.getContentType(fileName);
230 
231             ServletResponseUtil.sendFile(
232                 response, fileName, is, contentLength, contentType);
233         }
234         finally {
235             ServletResponseUtil.cleanUp(is);
236         }
237     }
238 
239     protected boolean isCheckMethodOnProcessAction() {
240         return _CHECK_METHOD_ON_PROCESS_ACTION;
241     }
242 
243     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
244 
245 }