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.repository.model.FileEntry;
018    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
022    import com.liferay.portal.struts.ActionConstants;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
026    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
027    import com.liferay.portlet.messageboards.model.MBMessage;
028    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
029    import com.liferay.portlet.trash.util.TrashUtil;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    
035    import javax.servlet.http.HttpServletRequest;
036    import javax.servlet.http.HttpServletResponse;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class GetMessageAttachmentAction extends PortletAction {
046    
047            @Override
048            public void processAction(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            ActionRequest actionRequest, ActionResponse actionResponse)
051                    throws Exception {
052    
053                    try {
054                            long messageId = ParamUtil.getLong(actionRequest, "messageId");
055                            String fileName = ParamUtil.getString(actionRequest, "attachment");
056                            int status = ParamUtil.getInteger(
057                                    actionRequest, "status", WorkflowConstants.STATUS_APPROVED);
058    
059                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
060                                    actionRequest);
061                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
062                                    actionResponse);
063    
064                            getFile(messageId, fileName, status, request, response);
065    
066                            setForward(actionRequest, ActionConstants.COMMON_NULL);
067                    }
068                    catch (Exception e) {
069                            PortalUtil.sendError(e, actionRequest, actionResponse);
070                    }
071            }
072    
073            @Override
074            public ActionForward strutsExecute(
075                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
076                            HttpServletResponse response)
077                    throws Exception {
078    
079                    try {
080                            long messageId = ParamUtil.getLong(request, "messageId");
081                            String fileName = ParamUtil.getString(request, "attachment");
082                            int status = ParamUtil.getInteger(
083                                    request, "status", WorkflowConstants.STATUS_APPROVED);
084    
085                            getFile(messageId, fileName, status, request, response);
086    
087                            return null;
088                    }
089                    catch (Exception e) {
090                            PortalUtil.sendError(e, request, response);
091    
092                            return null;
093                    }
094            }
095    
096            protected void getFile(
097                            long messageId, String fileName, int status,
098                            HttpServletRequest request, HttpServletResponse response)
099                    throws Exception {
100    
101                    MBMessage message = MBMessageServiceUtil.getMessage(messageId);
102    
103                    FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
104                            message.getGroupId(), message.getAttachmentsFolderId(), fileName);
105    
106                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
107    
108                    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
109    
110                    if ((status != WorkflowConstants.STATUS_IN_TRASH) &&
111                            (dlFileVersion.isInTrash() || dlFileEntry.isInTrashContainer())) {
112    
113                            return;
114                    }
115    
116                    if (dlFileVersion.isInTrash()) {
117                            fileName = TrashUtil.getOriginalTitle(dlFileEntry.getTitle());
118                    }
119    
120                    ServletResponseUtil.sendFile(
121                            request, response, fileName, fileEntry.getContentStream(),
122                            fileEntry.getSize(), fileEntry.getMimeType());
123            }
124    
125            @Override
126            protected boolean isCheckMethodOnProcessAction() {
127                    return _CHECK_METHOD_ON_PROCESS_ACTION;
128            }
129    
130            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
131    
132    }