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.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.ContentTypes;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.MimeTypesUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.struts.ActionConstants;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.WebKeys;
032    import com.liferay.portlet.ActionRequestImpl;
033    import com.liferay.portlet.PortletURLImpl;
034    import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
035    import com.liferay.portlet.wiki.model.WikiPage;
036    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
037    import com.liferay.portlet.wiki.util.WikiUtil;
038    import com.liferay.util.servlet.ServletResponseUtil;
039    
040    import java.io.InputStream;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.PortletMode;
046    import javax.portlet.PortletRequest;
047    import javax.portlet.PortletURL;
048    import javax.portlet.WindowState;
049    
050    import javax.servlet.http.HttpServletRequest;
051    import javax.servlet.http.HttpServletResponse;
052    
053    import org.apache.struts.action.ActionForm;
054    import org.apache.struts.action.ActionMapping;
055    
056    /**
057     * @author Bruno Farache
058     */
059    public class ExportPageAction extends PortletAction {
060    
061            public void processAction(
062                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
063                            ActionRequest actionRequest, ActionResponse actionResponse)
064                    throws Exception {
065    
066                    try {
067                            long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
068                            String nodeName = ParamUtil.getString(actionRequest, "nodeName");
069                            String title = ParamUtil.getString(actionRequest, "title");
070                            double version = ParamUtil.getDouble(actionRequest, "version");
071    
072                            String targetExtension = ParamUtil.getString(
073                                    actionRequest, "targetExtension");
074    
075                            ThemeDisplay themeDisplay =
076                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
077    
078                            PortletURL viewPageURL = new PortletURLImpl(
079                                    (ActionRequestImpl)actionRequest,
080                                    portletConfig.getPortletName(), themeDisplay.getPlid(),
081                                    PortletRequest.RENDER_PHASE);
082    
083                            viewPageURL.setPortletMode(PortletMode.VIEW);
084                            viewPageURL.setWindowState(WindowState.MAXIMIZED);
085                            viewPageURL.setParameter("struts_action", "/wiki/view");
086                            viewPageURL.setParameter("nodeName", nodeName);
087                            viewPageURL.setParameter("title", title);
088    
089                            PortletURL editPageURL = new PortletURLImpl(
090                                    (ActionRequestImpl)actionRequest,
091                                    portletConfig.getPortletName(), themeDisplay.getPlid(),
092                                    PortletRequest.RENDER_PHASE);
093    
094                            editPageURL.setPortletMode(PortletMode.VIEW);
095                            editPageURL.setWindowState(WindowState.MAXIMIZED);
096                            editPageURL.setParameter("struts_action", "/wiki/edit_page");
097                            editPageURL.setParameter("nodeId", String.valueOf(nodeId));
098                            editPageURL.setParameter("title", title);
099    
100                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
101                                    actionRequest);
102                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
103                                    actionResponse);
104    
105                            getFile(
106                                    nodeId, title, version, targetExtension, viewPageURL,
107                                    editPageURL, themeDisplay, request, response);
108    
109                            setForward(actionRequest, ActionConstants.COMMON_NULL);
110                    }
111                    catch (Exception e) {
112                            PortalUtil.sendError(e, actionRequest, actionResponse);
113                    }
114            }
115    
116            protected void getFile(
117                            long nodeId, String title, double version, String targetExtension,
118                            PortletURL viewPageURL, PortletURL editPageURL,
119                            ThemeDisplay themeDisplay, HttpServletRequest request,
120                            HttpServletResponse response)
121                    throws Exception {
122    
123                    WikiPage page = WikiPageServiceUtil.getPage(nodeId, title, version);
124    
125                    String content = page.getContent();
126    
127                    String attachmentURLPrefix =
128                            themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
129                                    "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
130                                            "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
131    
132                    try {
133                            content = WikiUtil.convert(
134                                    page, viewPageURL, editPageURL, attachmentURLPrefix);
135                    }
136                    catch (Exception e) {
137                            _log.error(
138                                    "Error formatting the wiki page " + page.getPageId() +
139                                            " with the format " + page.getFormat(), e);
140                    }
141    
142                    StringBundler sb = new StringBundler(16);
143    
144                    sb.append("<html>");
145    
146                    sb.append("<head>");
147                    sb.append("<meta content=\"");
148                    sb.append(ContentTypes.TEXT_HTML_UTF8);
149                    sb.append("\" http-equiv=\"content-type\" />");
150                    sb.append("<base href=\"");
151                    sb.append(themeDisplay.getPortalURL());
152                    sb.append("\" />");
153                    sb.append("</head>");
154    
155                    sb.append("<body>");
156    
157                    sb.append("<h1>");
158                    sb.append(title);
159                    sb.append("</h1>");
160                    sb.append(content);
161    
162                    sb.append("</body>");
163                    sb.append("</html>");
164    
165                    InputStream is = new UnsyncByteArrayInputStream(
166                            sb.toString().getBytes(StringPool.UTF8));
167    
168                    String sourceExtension = "html";
169    
170                    String fileName = title.concat(StringPool.PERIOD).concat(
171                            sourceExtension);
172    
173                    if (Validator.isNotNull(targetExtension)) {
174                            String id = page.getUuid();
175    
176                            InputStream convertedIS = DocumentConversionUtil.convert(
177                                    id, is, sourceExtension, targetExtension);
178    
179                            if ((convertedIS != null) && (convertedIS != is)) {
180                                    fileName = title.concat(StringPool.PERIOD).concat(
181                                            targetExtension);
182    
183                                    is = convertedIS;
184                            }
185                    }
186    
187                    String contentType = MimeTypesUtil.getContentType(fileName);
188    
189                    ServletResponseUtil.sendFile(
190                            request, response, fileName, is, contentType);
191            }
192    
193            protected boolean isCheckMethodOnProcessAction() {
194                    return _CHECK_METHOD_ON_PROCESS_ACTION;
195            }
196    
197            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
198    
199            private static Log _log = LogFactoryUtil.getLog(ExportPageAction.class);
200    
201    }