001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.documentlibrary.action;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.kernel.security.permission.ActionKeys;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.StringMaker;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.security.auth.PrincipalException;
030: import com.liferay.portal.struts.ActionConstants;
031: import com.liferay.portal.struts.PortletAction;
032: import com.liferay.portal.theme.ThemeDisplay;
033: import com.liferay.portal.util.MimeTypesUtil;
034: import com.liferay.portal.util.PortalUtil;
035: import com.liferay.portal.util.WebKeys;
036: import com.liferay.portlet.ActionRequestImpl;
037: import com.liferay.portlet.ActionResponseImpl;
038: import com.liferay.portlet.documentlibrary.model.DLFileEntry;
039: import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
040: import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
041: import com.liferay.portlet.documentlibrary.service.DLFileShortcutServiceUtil;
042: import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
043: import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
044: import com.liferay.util.FileUtil;
045: import com.liferay.util.servlet.ServletResponseUtil;
046:
047: import java.io.InputStream;
048:
049: import javax.portlet.ActionRequest;
050: import javax.portlet.ActionResponse;
051: import javax.portlet.PortletConfig;
052:
053: import javax.servlet.http.HttpServletRequest;
054: import javax.servlet.http.HttpServletResponse;
055: import javax.servlet.jsp.PageContext;
056:
057: import org.apache.struts.action.ActionForm;
058: import org.apache.struts.action.ActionForward;
059: import org.apache.struts.action.ActionMapping;
060:
061: /**
062: * <a href="GetFileAction.java.html"><b><i>View Source</i></b></a>
063: *
064: * @author Brian Wing Shun Chan
065: * @author Jorge Ferrer
066: * @author Charles May
067: * @author Bruno Farache
068: *
069: */
070: public class GetFileAction extends PortletAction {
071:
072: public ActionForward strutsExecute(ActionMapping mapping,
073: ActionForm form, HttpServletRequest req,
074: HttpServletResponse res) throws Exception {
075:
076: try {
077: long folderId = ParamUtil.getLong(req, "folderId");
078: String name = ParamUtil.getString(req, "name");
079: double version = ParamUtil.getDouble(req, "version");
080:
081: long fileShortcutId = ParamUtil.getLong(req,
082: "fileShortcutId");
083:
084: String uuid = ParamUtil.getString(req, "uuid");
085: long groupId = ParamUtil.getLong(req, "groupId");
086:
087: String targetExtension = ParamUtil.getString(req,
088: "targetExtension");
089:
090: ThemeDisplay themeDisplay = (ThemeDisplay) req
091: .getAttribute(WebKeys.THEME_DISPLAY);
092:
093: getFile(folderId, name, version, fileShortcutId, uuid,
094: groupId, targetExtension, themeDisplay, req, res);
095:
096: return null;
097: } catch (Exception e) {
098: req.setAttribute(PageContext.EXCEPTION, e);
099:
100: return mapping.findForward(ActionConstants.COMMON_ERROR);
101: }
102: }
103:
104: public void processAction(ActionMapping mapping, ActionForm form,
105: PortletConfig config, ActionRequest req, ActionResponse res)
106: throws Exception {
107:
108: long folderId = ParamUtil.getLong(req, "folderId");
109: String name = ParamUtil.getString(req, "name");
110: double version = ParamUtil.getDouble(req, "version");
111:
112: long fileShortcutId = ParamUtil.getLong(req, "fileShortcutId");
113:
114: String uuid = ParamUtil.getString(req, "uuid");
115: long groupId = ParamUtil.getLong(req, "groupId");
116:
117: String targetExtension = ParamUtil.getString(req,
118: "targetExtension");
119:
120: ThemeDisplay themeDisplay = (ThemeDisplay) req
121: .getAttribute(WebKeys.THEME_DISPLAY);
122:
123: HttpServletRequest httpReq = ((ActionRequestImpl) req)
124: .getHttpServletRequest();
125: HttpServletResponse httpRes = ((ActionResponseImpl) res)
126: .getHttpServletResponse();
127:
128: getFile(folderId, name, version, fileShortcutId, uuid, groupId,
129: targetExtension, themeDisplay, httpReq, httpRes);
130:
131: setForward(req, ActionConstants.COMMON_NULL);
132: }
133:
134: protected void getFile(long folderId, String name, double version,
135: long fileShortcutId, String uuid, long groupId,
136: String targetExtension, ThemeDisplay themeDisplay,
137: HttpServletRequest req, HttpServletResponse res)
138: throws Exception {
139:
140: InputStream is = null;
141:
142: try {
143: long companyId = themeDisplay.getCompanyId();
144: long userId = themeDisplay.getUserId();
145:
146: DLFileEntry fileEntry = null;
147:
148: if (Validator.isNotNull(uuid) && (groupId > 0)) {
149: try {
150: fileEntry = DLFileEntryLocalServiceUtil
151: .getFileEntryByUuidAndGroupId(uuid, groupId);
152:
153: folderId = fileEntry.getFolderId();
154: name = fileEntry.getName();
155: } catch (Exception e) {
156: }
157: }
158:
159: if (fileShortcutId <= 0) {
160: DLFileEntryPermission.check(themeDisplay
161: .getPermissionChecker(), folderId, name,
162: ActionKeys.VIEW);
163: } else {
164: DLFileShortcut fileShortcut = DLFileShortcutServiceUtil
165: .getFileShortcut(fileShortcutId);
166:
167: folderId = fileShortcut.getToFolderId();
168: name = fileShortcut.getToName();
169: }
170:
171: if (fileEntry == null) {
172: fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
173: folderId, name);
174: }
175:
176: if (version == 0) {
177: version = fileEntry.getVersion();
178: }
179:
180: is = DLFileEntryLocalServiceUtil.getFileAsStream(companyId,
181: userId, folderId, name, version);
182:
183: String fileName = fileEntry.getTitleWithExtension();
184:
185: if (Validator.isNotNull(targetExtension)) {
186: StringMaker sm = new StringMaker();
187:
188: sm.append(fileEntry.getFileEntryId());
189: sm.append(StringPool.PERIOD);
190: sm.append(version);
191:
192: String id = sm.toString();
193:
194: String sourceExtension = FileUtil.getExtension(name);
195:
196: InputStream convertedIS = DocumentConversionUtil
197: .convert(id, is, sourceExtension,
198: targetExtension);
199:
200: if ((convertedIS != null) && (convertedIS != is)) {
201: sm = new StringMaker();
202:
203: sm.append(fileEntry.getTitle());
204: sm.append(StringPool.PERIOD);
205: sm.append(targetExtension);
206:
207: fileName = sm.toString();
208:
209: is = convertedIS;
210: }
211: }
212:
213: String contentType = MimeTypesUtil.getContentType(fileName);
214:
215: ServletResponseUtil
216: .sendFile(res, fileName, is, contentType);
217: } catch (PortalException pe) {
218: if (pe instanceof PrincipalException) {
219: PortalUtil.sendError(HttpServletResponse.SC_FORBIDDEN,
220: new PrincipalException(), req, res);
221: } else {
222: PortalUtil.sendError(HttpServletResponse.SC_NOT_FOUND,
223: pe, req, res);
224: }
225: } finally {
226: ServletResponseUtil.cleanUp(is);
227: }
228: }
229:
230: protected boolean isCheckMethodOnProcessAction() {
231: return _CHECK_METHOD_ON_PROCESS_ACTION;
232: }
233:
234: private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
235:
236: }
|