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