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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
020    import com.liferay.portal.kernel.util.CharPool;
021    import com.liferay.portal.kernel.util.MimeTypesUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.model.CompanyConstants;
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.NoSuchFileException;
028    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
029    import com.liferay.portlet.wiki.NoSuchPageException;
030    import com.liferay.portlet.wiki.model.WikiPage;
031    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
032    
033    import java.io.InputStream;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    
039    import javax.servlet.http.HttpServletRequest;
040    import javax.servlet.http.HttpServletResponse;
041    
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionForward;
044    import org.apache.struts.action.ActionMapping;
045    
046    /**
047     * @author Jorge Ferrer
048     */
049    public class GetPageAttachmentAction extends PortletAction {
050    
051            @Override
052            public void processAction(
053                            ActionMapping actionMapping, ActionForm actionForm,
054                            PortletConfig portletConfig, ActionRequest actionRequest,
055                            ActionResponse actionResponse)
056                    throws Exception {
057    
058                    try {
059                            long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
060                            String title = ParamUtil.getString(actionRequest, "title");
061                            String fileName = ParamUtil.getString(actionRequest, "fileName");
062    
063                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
064                                    actionRequest);
065                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
066                                    actionResponse);
067    
068                            getFile(nodeId, title, fileName, request, response);
069    
070                            setForward(actionRequest, ActionConstants.COMMON_NULL);
071                    }
072                    catch (Exception e) {
073                            PortalUtil.sendError(e, actionRequest, actionResponse);
074                    }
075            }
076    
077            @Override
078            public ActionForward strutsExecute(
079                            ActionMapping actionMapping, ActionForm actionForm,
080                            HttpServletRequest request, HttpServletResponse response)
081                    throws Exception {
082    
083                    try {
084                            long nodeId = ParamUtil.getLong(request, "nodeId");
085                            String title = ParamUtil.getString(request, "title");
086                            String fileName = ParamUtil.getString(request, "fileName");
087    
088                            getFile(nodeId, title, fileName, request, response);
089    
090                            return null;
091                    }
092                    catch (Exception e) {
093                            if ((e instanceof NoSuchPageException) ||
094                                    (e instanceof NoSuchFileException)) {
095    
096                                    if (_log.isWarnEnabled()) {
097                                            _log.warn(e);
098                                    }
099                            }
100                            else {
101                                    PortalUtil.sendError(e, request, response);
102                            }
103    
104                            return null;
105                    }
106            }
107    
108            protected void getFile(
109                            long nodeId, String title, String fileName,
110                            HttpServletRequest request, HttpServletResponse response)
111                    throws Exception {
112    
113                    int pos = fileName.indexOf(CharPool.SLASH);
114    
115                    if (pos != -1) {
116                            title = fileName.substring(0, pos);
117                            fileName = fileName.substring(pos + 1);
118                    }
119    
120                    WikiPage page = WikiPageServiceUtil.getPage(nodeId, title);
121    
122                    String path = page.getAttachmentsDir() + "/" + fileName;
123    
124                    InputStream is = DLStoreUtil.getFileAsStream(
125                            page.getCompanyId(), CompanyConstants.SYSTEM, path);
126                    long contentLength = DLStoreUtil.getFileSize(
127                            page.getCompanyId(), CompanyConstants.SYSTEM, path);
128                    String contentType = MimeTypesUtil.getContentType(fileName);
129    
130                    ServletResponseUtil.sendFile(
131                            request, response, fileName, is, contentLength, contentType);
132            }
133    
134            @Override
135            protected boolean isCheckMethodOnProcessAction() {
136                    return _CHECK_METHOD_ON_PROCESS_ACTION;
137            }
138    
139            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
140    
141            private static Log _log = LogFactoryUtil.getLog(
142                    GetPageAttachmentAction.class);
143    
144    }