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