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.action;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.MimeTypesUtil;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.security.permission.ActionKeys;
28  import com.liferay.portal.struts.ActionConstants;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
34  import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
35  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
36  import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
37  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
38  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
39  import com.liferay.util.servlet.ServletResponseUtil;
40  
41  import java.io.InputStream;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  
50  import org.apache.struts.action.ActionForm;
51  import org.apache.struts.action.ActionForward;
52  import org.apache.struts.action.ActionMapping;
53  
54  /**
55   * <a href="GetFileAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   * @author Jorge Ferrer
59   * @author Charles May
60   * @author Bruno Farache
61   *
62   */
63  public class GetFileAction extends PortletAction {
64  
65      public ActionForward strutsExecute(
66              ActionMapping mapping, ActionForm form, HttpServletRequest request,
67              HttpServletResponse response)
68          throws Exception {
69  
70          try {
71              long folderId = ParamUtil.getLong(request, "folderId");
72              String name = ParamUtil.getString(request, "name");
73              double version = ParamUtil.getDouble(request, "version");
74  
75              long fileShortcutId = ParamUtil.getLong(request, "fileShortcutId");
76  
77              String uuid = ParamUtil.getString(request, "uuid");
78              long groupId = ParamUtil.getLong(request, "groupId");
79  
80              String targetExtension = ParamUtil.getString(
81                  request, "targetExtension");
82  
83              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
84                  WebKeys.THEME_DISPLAY);
85  
86              getFile(
87                  folderId, name, version, fileShortcutId, uuid, groupId,
88                  targetExtension, themeDisplay, request, response);
89  
90              return null;
91          }
92          catch (Exception e) {
93              PortalUtil.sendError(e, request, response);
94  
95              return null;
96          }
97      }
98  
99      public void processAction(
100             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101             ActionRequest actionRequest, ActionResponse actionResponse)
102         throws Exception {
103 
104         try {
105             long folderId = ParamUtil.getLong(actionRequest, "folderId");
106             String name = ParamUtil.getString(actionRequest, "name");
107             double version = ParamUtil.getDouble(actionRequest, "version");
108 
109             long fileShortcutId = ParamUtil.getLong(
110                 actionRequest, "fileShortcutId");
111 
112             String uuid = ParamUtil.getString(actionRequest, "uuid");
113             long groupId = ParamUtil.getLong(actionRequest, "groupId");
114 
115             String targetExtension = ParamUtil.getString(
116                 actionRequest, "targetExtension");
117 
118             ThemeDisplay themeDisplay =
119                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
120 
121             HttpServletRequest request = PortalUtil.getHttpServletRequest(
122                 actionRequest);
123             HttpServletResponse response = PortalUtil.getHttpServletResponse(
124                 actionResponse);
125 
126             getFile(
127                 folderId, name, version, fileShortcutId, uuid, groupId,
128                 targetExtension, themeDisplay, request, response);
129 
130             setForward(actionRequest, ActionConstants.COMMON_NULL);
131         }
132         catch (Exception e) {
133             PortalUtil.sendError(e, actionRequest, actionResponse);
134         }
135     }
136 
137     protected void getFile(
138             long folderId, String name, double version, long fileShortcutId,
139             String uuid, long groupId, String targetExtension,
140             ThemeDisplay themeDisplay, HttpServletRequest request,
141             HttpServletResponse response)
142         throws Exception {
143 
144         InputStream is = null;
145 
146         try {
147             long companyId = themeDisplay.getCompanyId();
148             long userId = themeDisplay.getUserId();
149 
150             DLFileEntry fileEntry = null;
151 
152             if (Validator.isNotNull(uuid) && (groupId > 0)) {
153                 try {
154                     fileEntry = DLFileEntryLocalServiceUtil.
155                         getFileEntryByUuidAndGroupId(
156                             uuid, groupId);
157 
158                     folderId = fileEntry.getFolderId();
159                     name = fileEntry.getName();
160                 }
161                 catch (Exception e) {
162                 }
163             }
164 
165             if (fileShortcutId <= 0) {
166                 DLFileEntryPermission.check(
167                     themeDisplay.getPermissionChecker(), folderId, name,
168                     ActionKeys.VIEW);
169             }
170             else {
171                 DLFileShortcut fileShortcut =
172                     DLFileShortcutServiceUtil.getFileShortcut(fileShortcutId);
173 
174                 folderId = fileShortcut.getToFolderId();
175                 name = fileShortcut.getToName();
176             }
177 
178             if (fileEntry == null) {
179                 fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
180                     folderId, name);
181             }
182 
183             if (version == 0) {
184                 version = fileEntry.getVersion();
185             }
186 
187             is = DLFileEntryLocalServiceUtil.getFileAsStream(
188                 companyId, userId, folderId, name, version);
189 
190             String fileName = fileEntry.getTitleWithExtension();
191 
192             if (Validator.isNotNull(targetExtension)) {
193                 String id = DocumentConversionUtil.getTempFileId(
194                     fileEntry.getFileEntryId(), version);
195 
196                 String sourceExtension = FileUtil.getExtension(name);
197 
198                 InputStream convertedIS = DocumentConversionUtil.convert(
199                     id, is, sourceExtension, targetExtension);
200 
201                 if ((convertedIS != null) && (convertedIS != is)) {
202                     StringBuilder sb = new StringBuilder();
203 
204                     sb.append(fileEntry.getTitle());
205                     sb.append(StringPool.PERIOD);
206                     sb.append(targetExtension);
207 
208                     fileName = sb.toString();
209 
210                     is = convertedIS;
211                 }
212             }
213 
214             int contentLength = fileEntry.getSize();
215             String contentType = MimeTypesUtil.getContentType(fileName);
216 
217             ServletResponseUtil.sendFile(
218                 response, fileName, is, contentLength, contentType);
219         }
220         finally {
221             ServletResponseUtil.cleanUp(is);
222         }
223     }
224 
225     protected boolean isCheckMethodOnProcessAction() {
226         return _CHECK_METHOD_ON_PROCESS_ACTION;
227     }
228 
229     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
230 
231 }