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