001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.action;
016    
017    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
018    import com.liferay.portal.kernel.util.MimeTypesUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.model.CompanyConstants;
021    import com.liferay.portal.struts.ActionConstants;
022    import com.liferay.portal.struts.PortletAction;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
025    import com.liferay.portlet.messageboards.model.MBMessage;
026    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
027    
028    import java.io.InputStream;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class GetMessageAttachmentAction extends PortletAction {
045    
046            @Override
047            public void processAction(
048                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
049                            ActionRequest actionRequest, ActionResponse actionResponse)
050                    throws Exception {
051    
052                    try {
053                            long messageId = ParamUtil.getLong(actionRequest, "messageId");
054                            String fileName = ParamUtil.getString(actionRequest, "attachment");
055    
056                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
057                                    actionRequest);
058                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
059                                    actionResponse);
060    
061                            getFile(messageId, fileName, request, response);
062    
063                            setForward(actionRequest, ActionConstants.COMMON_NULL);
064                    }
065                    catch (Exception e) {
066                            PortalUtil.sendError(e, actionRequest, actionResponse);
067                    }
068            }
069    
070            @Override
071            public ActionForward strutsExecute(
072                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
073                            HttpServletResponse response)
074                    throws Exception {
075    
076                    try {
077                            long messageId = ParamUtil.getLong(request, "messageId");
078                            String fileName = ParamUtil.getString(request, "attachment");
079    
080                            getFile(messageId, fileName, request, response);
081    
082                            return null;
083                    }
084                    catch (Exception e) {
085                            PortalUtil.sendError(e, request, response);
086    
087                            return null;
088                    }
089            }
090    
091            protected void getFile(
092                            long messageId, String fileName, HttpServletRequest request,
093                            HttpServletResponse response)
094                    throws Exception {
095    
096                    MBMessage message = MBMessageServiceUtil.getMessage(messageId);
097    
098                    String path = message.getAttachmentsDir() + "/" + fileName;
099    
100                    InputStream is = DLStoreUtil.getFileAsStream(
101                            message.getCompanyId(), CompanyConstants.SYSTEM, path);
102                    long contentLength = DLStoreUtil.getFileSize(
103                            message.getCompanyId(), CompanyConstants.SYSTEM, path);
104                    String contentType = MimeTypesUtil.getContentType(fileName);
105    
106                    ServletResponseUtil.sendFile(
107                            request, response, fileName, is, contentLength, contentType);
108            }
109    
110            @Override
111            protected boolean isCheckMethodOnProcessAction() {
112                    return _CHECK_METHOD_ON_PROCESS_ACTION;
113            }
114    
115            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
116    
117    }