001    /**
002     * Copyright (c) 2000-2013 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.PortletConfig;
032    import javax.portlet.ResourceRequest;
033    import javax.portlet.ResourceResponse;
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 serveResource(
049                            ActionMapping actionMapping, ActionForm actionForm,
050                            PortletConfig portletConfig, ResourceRequest resourceRequest,
051                            ResourceResponse resourceResponse)
052                    throws Exception {
053    
054                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
055                            resourceRequest);
056                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
057                            resourceResponse);
058    
059                    try {
060                            long messageId = ParamUtil.getLong(resourceRequest, "messageId");
061                            String fileName = ParamUtil.getString(
062                                    resourceRequest, "attachment");
063                            int status = ParamUtil.getInteger(
064                                    resourceRequest, "status", WorkflowConstants.STATUS_APPROVED);
065    
066                            getFile(messageId, fileName, status, request, response);
067    
068                            setForward(resourceRequest, ActionConstants.COMMON_NULL);
069                    }
070                    catch (Exception e) {
071                            PortalUtil.sendError(e, request, response);
072                    }
073            }
074    
075            @Override
076            public ActionForward strutsExecute(
077                            ActionMapping actionMapping, ActionForm actionForm,
078                            HttpServletRequest request, HttpServletResponse response)
079                    throws Exception {
080    
081                    try {
082                            long messageId = ParamUtil.getLong(request, "messageId");
083                            String fileName = ParamUtil.getString(request, "attachment");
084                            int status = ParamUtil.getInteger(
085                                    request, "status", WorkflowConstants.STATUS_APPROVED);
086    
087                            getFile(messageId, fileName, status, request, response);
088    
089                            return null;
090                    }
091                    catch (Exception e) {
092                            PortalUtil.sendError(e, request, response);
093    
094                            return null;
095                    }
096            }
097    
098            protected void getFile(
099                            long messageId, String fileName, int status,
100                            HttpServletRequest request, HttpServletResponse response)
101                    throws Exception {
102    
103                    MBMessage message = MBMessageServiceUtil.getMessage(messageId);
104    
105                    FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
106                            message.getGroupId(), message.getAttachmentsFolderId(), fileName);
107    
108                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
109    
110                    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
111    
112                    if ((status != WorkflowConstants.STATUS_IN_TRASH) &&
113                            (dlFileVersion.isInTrash() || dlFileEntry.isInTrashContainer())) {
114    
115                            return;
116                    }
117    
118                    if (dlFileVersion.isInTrash()) {
119                            fileName = TrashUtil.getOriginalTitle(dlFileEntry.getTitle());
120                    }
121    
122                    ServletResponseUtil.sendFile(
123                            request, response, fileName, fileEntry.getContentStream(),
124                            fileEntry.getSize(), fileEntry.getMimeType());
125            }
126    
127            @Override
128            protected boolean isCheckMethodOnProcessAction() {
129                    return _CHECK_METHOD_ON_PROCESS_ACTION;
130            }
131    
132            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
133    
134    }