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.messageboards.action;
022:
023: import com.liferay.documentlibrary.service.DLLocalServiceUtil;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.model.impl.CompanyImpl;
026: import com.liferay.portal.struts.ActionConstants;
027: import com.liferay.portal.struts.PortletAction;
028: import com.liferay.portal.util.MimeTypesUtil;
029: import com.liferay.portlet.ActionResponseImpl;
030: import com.liferay.portlet.messageboards.model.MBMessage;
031: import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
032: import com.liferay.util.servlet.ServletResponseUtil;
033:
034: import java.io.InputStream;
035:
036: import javax.portlet.ActionRequest;
037: import javax.portlet.ActionResponse;
038: import javax.portlet.PortletConfig;
039:
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import javax.servlet.jsp.PageContext;
043:
044: import org.apache.struts.action.ActionForm;
045: import org.apache.struts.action.ActionForward;
046: import org.apache.struts.action.ActionMapping;
047:
048: /**
049: * <a href="GetMessageAttachmentAction.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Brian Wing Shun Chan
052: *
053: */
054: public class GetMessageAttachmentAction extends PortletAction {
055:
056: public ActionForward strutsExecute(ActionMapping mapping,
057: ActionForm form, HttpServletRequest req,
058: HttpServletResponse res) throws Exception {
059:
060: try {
061: long messageId = ParamUtil.getLong(req, "messageId");
062: String fileName = ParamUtil.getString(req, "attachment");
063:
064: getFile(messageId, fileName, res);
065:
066: return null;
067: } catch (Exception e) {
068: req.setAttribute(PageContext.EXCEPTION, e);
069:
070: return mapping.findForward(ActionConstants.COMMON_ERROR);
071: }
072: }
073:
074: public void processAction(ActionMapping mapping, ActionForm form,
075: PortletConfig config, ActionRequest req, ActionResponse res)
076: throws Exception {
077:
078: long messageId = ParamUtil.getLong(req, "messageId");
079: String fileName = ParamUtil.getString(req, "attachment");
080:
081: HttpServletResponse httpRes = ((ActionResponseImpl) res)
082: .getHttpServletResponse();
083:
084: getFile(messageId, fileName, httpRes);
085:
086: setForward(req, ActionConstants.COMMON_NULL);
087: }
088:
089: protected void getFile(long messageId, String fileName,
090: HttpServletResponse res) throws Exception {
091:
092: InputStream is = null;
093:
094: try {
095: MBMessage message = MBMessageServiceUtil
096: .getMessage(messageId);
097:
098: is = DLLocalServiceUtil.getFileAsStream(message
099: .getCompanyId(), CompanyImpl.SYSTEM, message
100: .getAttachmentsDir()
101: + "/" + fileName);
102:
103: String contentType = MimeTypesUtil.getContentType(fileName);
104:
105: ServletResponseUtil
106: .sendFile(res, fileName, is, contentType);
107: } finally {
108: ServletResponseUtil.cleanUp(is);
109: }
110: }
111:
112: protected boolean isCheckMethodOnProcessAction() {
113: return _CHECK_METHOD_ON_PROCESS_ACTION;
114: }
115:
116: private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
117:
118: }
|