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.wiki.action;
016    
017    import com.liferay.portal.kernel.flash.FlashMagicBytesUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022    import com.liferay.portal.kernel.util.FileUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
026    import com.liferay.portal.struts.ActionConstants;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.documentlibrary.NoSuchFileException;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031    import com.liferay.portlet.trash.util.TrashUtil;
032    import com.liferay.portlet.wiki.NoSuchPageException;
033    import com.liferay.portlet.wiki.model.WikiPage;
034    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
035    
036    import java.io.InputStream;
037    
038    import javax.portlet.PortletConfig;
039    import javax.portlet.ResourceRequest;
040    import javax.portlet.ResourceResponse;
041    
042    import javax.servlet.http.HttpServletRequest;
043    import javax.servlet.http.HttpServletResponse;
044    
045    import org.apache.struts.action.ActionForm;
046    import org.apache.struts.action.ActionForward;
047    import org.apache.struts.action.ActionMapping;
048    
049    /**
050     * @author Jorge Ferrer
051     */
052    public class GetPageAttachmentAction extends PortletAction {
053    
054            @Override
055            public void serveResource(
056                            ActionMapping actionMapping, ActionForm actionForm,
057                            PortletConfig portletConfig, ResourceRequest resourceRequest,
058                            ResourceResponse resourceResponse)
059                    throws Exception {
060    
061                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
062                            resourceRequest);
063                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
064                            resourceResponse);
065    
066                    try {
067                            long nodeId = ParamUtil.getLong(resourceRequest, "nodeId");
068                            String title = ParamUtil.getString(resourceRequest, "title");
069                            String fileName = ParamUtil.getString(resourceRequest, "fileName");
070                            int status = ParamUtil.getInteger(
071                                    resourceRequest, "status", WorkflowConstants.STATUS_APPROVED);
072    
073                            getFile(nodeId, title, fileName, status, request, response);
074    
075                            setForward(resourceRequest, ActionConstants.COMMON_NULL);
076                    }
077                    catch (Exception e) {
078                            PortalUtil.sendError(e, request, response);
079                    }
080            }
081    
082            @Override
083            public ActionForward strutsExecute(
084                            ActionMapping actionMapping, ActionForm actionForm,
085                            HttpServletRequest request, HttpServletResponse response)
086                    throws Exception {
087    
088                    try {
089                            long nodeId = ParamUtil.getLong(request, "nodeId");
090                            String title = ParamUtil.getString(request, "title");
091                            String fileName = ParamUtil.getString(request, "fileName");
092                            int status = ParamUtil.getInteger(
093                                    request, "status", WorkflowConstants.STATUS_APPROVED);
094    
095                            getFile(nodeId, title, fileName, status, request, response);
096    
097                            return null;
098                    }
099                    catch (Exception e) {
100                            if ((e instanceof NoSuchPageException) ||
101                                    (e instanceof NoSuchFileException)) {
102    
103                                    if (_log.isWarnEnabled()) {
104                                            _log.warn(e);
105                                    }
106                            }
107                            else {
108                                    PortalUtil.sendError(e, request, response);
109                            }
110    
111                            return null;
112                    }
113            }
114    
115            protected void getFile(
116                            long nodeId, String title, String fileName, int status,
117                            HttpServletRequest request, HttpServletResponse response)
118                    throws Exception {
119    
120                    WikiPage wikiPage = WikiPageServiceUtil.getPage(nodeId, title);
121    
122                    FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
123                            wikiPage.getGroupId(), wikiPage.getAttachmentsFolderId(), fileName);
124    
125                    DLFileEntry dlFileEntry = (DLFileEntry)fileEntry.getModel();
126    
127                    if ((status != WorkflowConstants.STATUS_IN_TRASH) &&
128                            dlFileEntry.isInTrash()) {
129    
130                            return;
131                    }
132    
133                    if (dlFileEntry.isInTrash()) {
134                            fileName = TrashUtil.getOriginalTitle(dlFileEntry.getTitle());
135                    }
136    
137                    InputStream is = fileEntry.getContentStream();
138    
139                    FlashMagicBytesUtil.Result flashMagicBytesUtilResult =
140                            FlashMagicBytesUtil.check(is);
141    
142                    if (flashMagicBytesUtilResult.isFlash()) {
143                            fileName = FileUtil.stripExtension(fileName) + ".swf";
144                    }
145    
146                    is = flashMagicBytesUtilResult.getInputStream();
147    
148                    ServletResponseUtil.sendFile(
149                            request, response, fileName, is, fileEntry.getSize(),
150                            fileEntry.getMimeType());
151            }
152    
153            @Override
154            protected boolean isCheckMethodOnProcessAction() {
155                    return _CHECK_METHOD_ON_PROCESS_ACTION;
156            }
157    
158            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
159    
160            private static Log _log = LogFactoryUtil.getLog(
161                    GetPageAttachmentAction.class);
162    
163    }