001    /**
002     * Copyright (c) 2000-2013 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.MimeTypesUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.struts.ActionConstants;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.PrefsPropsUtil;
033    import com.liferay.portal.util.WebKeys;
034    import com.liferay.portlet.PortletURLImpl;
035    import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
036    import com.liferay.portlet.wiki.model.WikiPage;
037    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
038    import com.liferay.portlet.wiki.util.WikiUtil;
039    
040    import java.io.File;
041    import java.io.FileInputStream;
042    import java.io.InputStream;
043    
044    import javax.portlet.ActionRequest;
045    import javax.portlet.ActionResponse;
046    import javax.portlet.PortletConfig;
047    import javax.portlet.PortletMode;
048    import javax.portlet.PortletRequest;
049    import javax.portlet.PortletURL;
050    import javax.portlet.WindowState;
051    
052    import javax.servlet.http.HttpServletRequest;
053    import javax.servlet.http.HttpServletResponse;
054    
055    import org.apache.struts.action.ActionForm;
056    import org.apache.struts.action.ActionMapping;
057    
058    /**
059     * @author Bruno Farache
060     */
061    public class ExportPageAction extends PortletAction {
062    
063            @Override
064            public void processAction(
065                            ActionMapping actionMapping, ActionForm actionForm,
066                            PortletConfig portletConfig, ActionRequest actionRequest,
067                            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.setParameter("struts_action", "/wiki/view");
087                            viewPageURL.setParameter("nodeName", nodeName);
088                            viewPageURL.setParameter("title", title);
089                            viewPageURL.setPortletMode(PortletMode.VIEW);
090                            viewPageURL.setWindowState(WindowState.MAXIMIZED);
091    
092                            PortletURL editPageURL = new PortletURLImpl(
093                                    actionRequest, portletConfig.getPortletName(),
094                                    themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
095    
096                            editPageURL.setParameter("struts_action", "/wiki/edit_page");
097                            editPageURL.setParameter("nodeId", String.valueOf(nodeId));
098                            editPageURL.setParameter("title", title);
099                            editPageURL.setPortletMode(PortletMode.VIEW);
100                            editPageURL.setWindowState(WindowState.MAXIMIZED);
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 = WikiUtil.getAttachmentURLPrefix(
145                            themeDisplay.getPathMain(), themeDisplay.getPlid(), nodeId, title);
146    
147                    try {
148                            content = WikiUtil.convert(
149                                    page, viewPageURL, editPageURL, attachmentURLPrefix);
150                    }
151                    catch (Exception e) {
152                            _log.error(
153                                    "Error formatting the wiki page " + page.getPageId() +
154                                            " with the format " + page.getFormat(), e);
155                    }
156    
157                    StringBundler sb = new StringBundler(17);
158    
159                    sb.append("<!DOCTYPE html>");
160    
161                    sb.append("<html>");
162    
163                    sb.append("<head>");
164                    sb.append("<meta content=\"");
165                    sb.append(ContentTypes.TEXT_HTML_UTF8);
166                    sb.append("\" http-equiv=\"content-type\" />");
167                    sb.append("<base href=\"");
168                    sb.append(themeDisplay.getPortalURL());
169                    sb.append("\" />");
170                    sb.append("</head>");
171    
172                    sb.append("<body>");
173    
174                    sb.append("<h1>");
175                    sb.append(title);
176                    sb.append("</h1>");
177                    sb.append(content);
178    
179                    sb.append("</body>");
180                    sb.append("</html>");
181    
182                    InputStream is = new UnsyncByteArrayInputStream(
183                            sb.toString().getBytes(StringPool.UTF8));
184    
185                    String sourceExtension = "html";
186    
187                    String fileName = title.concat(StringPool.PERIOD).concat(
188                            sourceExtension);
189    
190                    if (Validator.isNotNull(targetExtension)) {
191                            String id = page.getUuid();
192    
193                            File convertedFile = DocumentConversionUtil.convert(
194                                    id, is, sourceExtension, targetExtension);
195    
196                            if (convertedFile != null) {
197                                    fileName = title.concat(StringPool.PERIOD).concat(
198                                            targetExtension);
199    
200                                    is = new FileInputStream(convertedFile);
201                            }
202                    }
203    
204                    String contentType = MimeTypesUtil.getContentType(fileName);
205    
206                    ServletResponseUtil.sendFile(
207                            request, response, fileName, is, contentType);
208            }
209    
210            @Override
211            protected boolean isCheckMethodOnProcessAction() {
212                    return _CHECK_METHOD_ON_PROCESS_ACTION;
213            }
214    
215            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
216    
217            private static final String _LOCALHOST = "localhost";
218    
219            private static final String _LOCALHOST_IP = "127.0.0.1";
220    
221            private static Log _log = LogFactoryUtil.getLog(ExportPageAction.class);
222    
223    }