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.backgroundtask.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.model.BackgroundTask;
022    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
023    import com.liferay.portal.service.BackgroundTaskLocalServiceUtil;
024    import com.liferay.portal.struts.ActionConstants;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
028    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
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 Michael C. Han
044     */
045    public class GetBackgroundTaskAttachmentAction 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 backgroundTaskId = ParamUtil.getLong(
061                                    resourceRequest, "backgroundTaskId");
062                            String fileName = ParamUtil.getString(
063                                    resourceRequest, "attachment");
064                            int status = ParamUtil.getInteger(
065                                    resourceRequest, "status", WorkflowConstants.STATUS_APPROVED);
066    
067                            getFile(backgroundTaskId, fileName, status, request, response);
068    
069                            setForward(resourceRequest, ActionConstants.COMMON_NULL);
070                    }
071                    catch (Exception e) {
072                            PortalUtil.sendError(e, request, response);
073                    }
074            }
075    
076            @Override
077            public ActionForward strutsExecute(
078                            ActionMapping actionMapping, ActionForm actionForm,
079                            HttpServletRequest request, HttpServletResponse response)
080                    throws Exception {
081    
082                    try {
083                            long backgroundTaskId = ParamUtil.getLong(
084                                    request, "backgroundTaskId");
085                            String fileName = ParamUtil.getString(request, "attachment");
086                            int status = ParamUtil.getInteger(
087                                    request, "status", WorkflowConstants.STATUS_APPROVED);
088    
089                            getFile(backgroundTaskId, fileName, status, request, response);
090    
091                            return null;
092                    }
093                    catch (Exception e) {
094                            PortalUtil.sendError(e, request, response);
095    
096                            return null;
097                    }
098            }
099    
100            protected void getFile(
101                            long backgroundTaskId, String fileName, int status,
102                            HttpServletRequest request, HttpServletResponse response)
103                    throws Exception {
104    
105                    BackgroundTask backgroundTask =
106                            BackgroundTaskLocalServiceUtil.getBackgroundTask(backgroundTaskId);
107    
108                    FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
109                            backgroundTask.getGroupId(),
110                            backgroundTask.getAttachmentsFolderId(), fileName);
111    
112                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
113    
114                    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();
115    
116                    if ((status != WorkflowConstants.STATUS_IN_TRASH) &&
117                            (dlFileVersion.isInTrash() || dlFileEntry.isInTrashContainer())) {
118    
119                            return;
120                    }
121    
122                    if (dlFileVersion.isInTrash()) {
123                            fileName = TrashUtil.getOriginalTitle(dlFileEntry.getTitle());
124                    }
125    
126                    ServletResponseUtil.sendFile(
127                            request, response, fileName, fileEntry.getContentStream(),
128                            fileEntry.getSize(), fileEntry.getMimeType());
129            }
130    
131            @Override
132            protected boolean isCheckMethodOnProcessAction() {
133                    return _CHECK_METHOD_ON_PROCESS_ACTION;
134            }
135    
136            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
137    
138    }