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.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 mapping, ActionForm form, PortletConfig portletConfig,
054                            ActionRequest actionRequest, ActionResponse actionResponse)
055                    throws Exception {
056    
057                    try {
058                            long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
059                            String title = ParamUtil.getString(actionRequest, "title");
060                            String fileName = ParamUtil.getString(actionRequest, "fileName");
061    
062                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
063                                    actionRequest);
064                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
065                                    actionResponse);
066    
067                            getFile(nodeId, title, fileName, request, response);
068    
069                            setForward(actionRequest, ActionConstants.COMMON_NULL);
070                    }
071                    catch (Exception e) {
072                            PortalUtil.sendError(e, actionRequest, actionResponse);
073                    }
074            }
075    
076            @Override
077            public ActionForward strutsExecute(
078                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
079                            HttpServletResponse response)
080                    throws Exception {
081    
082                    try {
083                            long nodeId = ParamUtil.getLong(request, "nodeId");
084                            String title = ParamUtil.getString(request, "title");
085                            String fileName = ParamUtil.getString(request, "fileName");
086    
087                            getFile(nodeId, title, fileName, request, response);
088    
089                            return null;
090                    }
091                    catch (Exception e) {
092                            if ((e instanceof NoSuchPageException) ||
093                                    (e instanceof NoSuchFileException)) {
094    
095                                    if (_log.isWarnEnabled()) {
096                                            _log.warn(e);
097                                    }
098                            }
099                            else {
100                                    PortalUtil.sendError(e, request, response);
101                            }
102    
103                            return null;
104                    }
105            }
106    
107            protected void getFile(
108                            long nodeId, String title, String fileName,
109                            HttpServletRequest request, HttpServletResponse response)
110                    throws Exception {
111    
112                    int pos = fileName.indexOf(CharPool.SLASH);
113    
114                    if (pos != -1) {
115                            title = fileName.substring(0, pos);
116                            fileName = fileName.substring(pos + 1);
117                    }
118    
119                    WikiPage page = WikiPageServiceUtil.getPage(nodeId, title);
120    
121                    String path = page.getAttachmentsDir() + "/" + fileName;
122    
123                    InputStream is = DLStoreUtil.getFileAsStream(
124                            page.getCompanyId(), CompanyConstants.SYSTEM, path);
125                    long contentLength = DLStoreUtil.getFileSize(
126                            page.getCompanyId(), CompanyConstants.SYSTEM, path);
127                    String contentType = MimeTypesUtil.getContentType(fileName);
128    
129                    ServletResponseUtil.sendFile(
130                            request, response, fileName, is, contentLength, contentType);
131            }
132    
133            @Override
134            protected boolean isCheckMethodOnProcessAction() {
135                    return _CHECK_METHOD_ON_PROCESS_ACTION;
136            }
137    
138            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
139    
140            private static Log _log = LogFactoryUtil.getLog(
141                    GetPageAttachmentAction.class);
142    
143    }