1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.action;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.util.ContentTypes;
21  import com.liferay.portal.kernel.util.HttpUtil;
22  import com.liferay.portal.kernel.util.MimeTypesUtil;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.StringBundler;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.struts.ActionConstants;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.ActionRequestImpl;
33  import com.liferay.portlet.PortletURLImpl;
34  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
35  import com.liferay.portlet.wiki.model.WikiPage;
36  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
37  import com.liferay.portlet.wiki.util.WikiUtil;
38  import com.liferay.util.servlet.ServletResponseUtil;
39  
40  import java.io.InputStream;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.PortletMode;
46  import javax.portlet.PortletRequest;
47  import javax.portlet.PortletURL;
48  import javax.portlet.WindowState;
49  
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  import org.apache.struts.action.ActionForm;
54  import org.apache.struts.action.ActionMapping;
55  
56  /**
57   * <a href="ExportPageAction.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Bruno Farache
60   */
61  public class ExportPageAction extends PortletAction {
62  
63      public void processAction(
64              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
65              ActionRequest actionRequest, ActionResponse actionResponse)
66          throws Exception {
67  
68          try {
69              long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
70              String nodeName = ParamUtil.getString(actionRequest, "nodeName");
71              String title = ParamUtil.getString(actionRequest, "title");
72              double version = ParamUtil.getDouble(actionRequest, "version");
73  
74              String targetExtension = ParamUtil.getString(
75                  actionRequest, "targetExtension");
76  
77              ThemeDisplay themeDisplay =
78                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
79  
80              PortletURL viewPageURL = new PortletURLImpl(
81                  (ActionRequestImpl)actionRequest,
82                  portletConfig.getPortletName(), themeDisplay.getPlid(),
83                  PortletRequest.RENDER_PHASE);
84  
85              viewPageURL.setPortletMode(PortletMode.VIEW);
86              viewPageURL.setWindowState(WindowState.MAXIMIZED);
87              viewPageURL.setParameter("struts_action", "/wiki/view");
88              viewPageURL.setParameter("nodeName", nodeName);
89              viewPageURL.setParameter("title", title);
90  
91              PortletURL editPageURL = new PortletURLImpl(
92                  (ActionRequestImpl)actionRequest,
93                  portletConfig.getPortletName(), themeDisplay.getPlid(),
94                  PortletRequest.RENDER_PHASE);
95  
96              editPageURL.setPortletMode(PortletMode.VIEW);
97              editPageURL.setWindowState(WindowState.MAXIMIZED);
98              editPageURL.setParameter("struts_action", "/wiki/edit_page");
99              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             PortalUtil.sendError(e, actionRequest, actionResponse);
115         }
116     }
117 
118     protected void getFile(
119             long nodeId, String title, double version, String targetExtension,
120             PortletURL viewPageURL, PortletURL editPageURL,
121             ThemeDisplay themeDisplay, HttpServletRequest request,
122             HttpServletResponse response)
123         throws Exception {
124 
125         WikiPage page = WikiPageServiceUtil.getPage(nodeId, title, version);
126 
127         String content = page.getContent();
128 
129         String attachmentURLPrefix =
130             themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
131                 "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
132                     "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
133 
134         try {
135             content = WikiUtil.convert(
136                 page, viewPageURL, editPageURL, attachmentURLPrefix);
137         }
138         catch (Exception e) {
139             _log.error(
140                 "Error formatting the wiki page " + page.getPageId() +
141                     " with the format " + page.getFormat(), e);
142         }
143 
144         StringBundler sb = new StringBundler(16);
145 
146         sb.append("<html>");
147 
148         sb.append("<head>");
149         sb.append("<meta content=\"");
150         sb.append(ContentTypes.TEXT_HTML_UTF8);
151         sb.append("\" http-equiv=\"content-type\" />");
152         sb.append("<base href=\"");
153         sb.append(themeDisplay.getPortalURL());
154         sb.append("\" />");
155         sb.append("</head>");
156 
157         sb.append("<body>");
158 
159         sb.append("<h1>");
160         sb.append(title);
161         sb.append("</h1>");
162         sb.append(content);
163 
164         sb.append("</body>");
165         sb.append("</html>");
166 
167         InputStream is = new UnsyncByteArrayInputStream(
168             sb.toString().getBytes(StringPool.UTF8));
169 
170         String sourceExtension = "html";
171 
172         String fileName = title.concat(StringPool.PERIOD).concat(
173             sourceExtension);
174 
175         if (Validator.isNotNull(targetExtension)) {
176             String id = page.getUuid();
177 
178             InputStream convertedIS = DocumentConversionUtil.convert(
179                 id, is, sourceExtension, targetExtension);
180 
181             if ((convertedIS != null) && (convertedIS != is)) {
182                 fileName = title.concat(StringPool.PERIOD).concat(
183                     targetExtension);
184 
185                 is = convertedIS;
186             }
187         }
188 
189         String contentType = MimeTypesUtil.getContentType(fileName);
190 
191         ServletResponseUtil.sendFile(response, fileName, is, contentType);
192     }
193 
194     protected boolean isCheckMethodOnProcessAction() {
195         return _CHECK_METHOD_ON_PROCESS_ACTION;
196     }
197 
198     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
199 
200     private static Log _log = LogFactoryUtil.getLog(ExportPageAction.class);
201 
202 }