001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.wiki.action;
016    
017    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.MimeTypesUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.model.CompanyConstants;
022    import com.liferay.portal.struts.ActionConstants;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.documentlibrary.store.DLStoreUtil;
026    import com.liferay.portlet.wiki.model.WikiPage;
027    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
028    
029    import java.io.InputStream;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
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 Jorge Ferrer
044     */
045    public class GetPageAttachmentAction extends PortletAction {
046    
047            @Override
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            @Override
070            public void processAction(
071                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
072                            ActionRequest actionRequest, ActionResponse actionResponse)
073                    throws Exception {
074    
075                    try {
076                            long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
077                            String title = ParamUtil.getString(actionRequest, "title");
078                            String fileName = ParamUtil.getString(actionRequest, "fileName");
079    
080                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
081                                    actionRequest);
082                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
083                                    actionResponse);
084    
085                            getFile(nodeId, title, fileName, request, response);
086    
087                            setForward(actionRequest, ActionConstants.COMMON_NULL);
088                    }
089                    catch (Exception e) {
090                            PortalUtil.sendError(e, actionRequest, actionResponse);
091                    }
092            }
093    
094            protected void getFile(
095                            long nodeId, String title, String fileName,
096                            HttpServletRequest request, HttpServletResponse response)
097                    throws Exception {
098    
099                    int pos = fileName.indexOf(CharPool.SLASH);
100    
101                    if (pos != -1) {
102                            title = fileName.substring(0, pos);
103                            fileName = fileName.substring(pos + 1);
104                    }
105    
106                    WikiPage page = WikiPageServiceUtil.getPage(nodeId, title);
107    
108                    String path = page.getAttachmentsDir() + "/" + fileName;
109    
110                    InputStream is = DLStoreUtil.getFileAsStream(
111                            page.getCompanyId(), CompanyConstants.SYSTEM, path);
112                    long contentLength = DLStoreUtil.getFileSize(
113                            page.getCompanyId(), CompanyConstants.SYSTEM, path);
114                    String contentType = MimeTypesUtil.getContentType(fileName);
115    
116                    ServletResponseUtil.sendFile(
117                            request, response, fileName, is, contentLength, contentType);
118            }
119    
120            @Override
121            protected boolean isCheckMethodOnProcessAction() {
122                    return _CHECK_METHOD_ON_PROCESS_ACTION;
123            }
124    
125            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
126    
127    }