1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.action;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ContentTypes;
28  import com.liferay.portal.kernel.util.HttpUtil;
29  import com.liferay.portal.kernel.util.MimeTypesUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.struts.ActionConstants;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.portlet.ActionRequestImpl;
39  import com.liferay.portlet.PortletURLImpl;
40  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
41  import com.liferay.portlet.wiki.model.WikiPage;
42  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
43  import com.liferay.portlet.wiki.util.WikiUtil;
44  import com.liferay.util.servlet.ServletResponseUtil;
45  
46  import java.io.ByteArrayInputStream;
47  import java.io.InputStream;
48  
49  import javax.portlet.ActionRequest;
50  import javax.portlet.ActionResponse;
51  import javax.portlet.PortletConfig;
52  import javax.portlet.PortletMode;
53  import javax.portlet.PortletRequest;
54  import javax.portlet.PortletURL;
55  import javax.portlet.WindowState;
56  
57  import javax.servlet.http.HttpServletRequest;
58  import javax.servlet.http.HttpServletResponse;
59  
60  import org.apache.struts.action.ActionForm;
61  import org.apache.struts.action.ActionMapping;
62  
63  /**
64   * <a href="ExportPageAction.java.html"><b><i>View Source</i></b></a>
65   *
66   * @author Bruno Farache
67   *
68   */
69  public class ExportPageAction extends PortletAction {
70  
71      public void processAction(
72              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
73              ActionRequest actionRequest, ActionResponse actionResponse)
74          throws Exception {
75  
76          try {
77              long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
78              String nodeName = ParamUtil.getString(actionRequest, "nodeName");
79              String title = ParamUtil.getString(actionRequest, "title");
80              double version = ParamUtil.getDouble(actionRequest, "version");
81  
82              String targetExtension = ParamUtil.getString(
83                  actionRequest, "targetExtension");
84  
85              ThemeDisplay themeDisplay =
86                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
87  
88              PortletURL viewPageURL = new PortletURLImpl(
89                  (ActionRequestImpl)actionRequest,
90                  portletConfig.getPortletName(), themeDisplay.getPlid(),
91                  PortletRequest.RENDER_PHASE);
92  
93              viewPageURL.setPortletMode(PortletMode.VIEW);
94              viewPageURL.setWindowState(WindowState.MAXIMIZED);
95              viewPageURL.setParameter("struts_action", "/wiki/view");
96              viewPageURL.setParameter("nodeName", nodeName);
97              viewPageURL.setParameter("title", title);
98  
99              PortletURL editPageURL = new PortletURLImpl(
100                 (ActionRequestImpl)actionRequest,
101                 portletConfig.getPortletName(), themeDisplay.getPlid(),
102                 PortletRequest.RENDER_PHASE);
103 
104             editPageURL.setPortletMode(PortletMode.VIEW);
105             editPageURL.setWindowState(WindowState.MAXIMIZED);
106             editPageURL.setParameter("struts_action", "/wiki/edit_page");
107             editPageURL.setParameter("nodeId", String.valueOf(nodeId));
108             editPageURL.setParameter("title", title);
109 
110             HttpServletRequest request = PortalUtil.getHttpServletRequest(
111                 actionRequest);
112             HttpServletResponse response = PortalUtil.getHttpServletResponse(
113                 actionResponse);
114 
115             getFile(
116                 nodeId, title, version, targetExtension, viewPageURL,
117                 editPageURL, themeDisplay, request, response);
118 
119             setForward(actionRequest, ActionConstants.COMMON_NULL);
120         }
121         catch (Exception e) {
122             PortalUtil.sendError(e, actionRequest, actionResponse);
123         }
124     }
125 
126     protected void getFile(
127             long nodeId, String title, double version, String targetExtension,
128             PortletURL viewPageURL, PortletURL editPageURL,
129             ThemeDisplay themeDisplay, HttpServletRequest request,
130             HttpServletResponse response)
131         throws Exception {
132 
133         InputStream is = null;
134 
135         try {
136             WikiPage page = WikiPageServiceUtil.getPage(nodeId, title, version);
137 
138             String content = page.getContent();
139 
140             String attachmentURLPrefix =
141                 themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
142                     "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
143                         "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
144 
145             try {
146                 content = WikiUtil.convert(
147                     page, viewPageURL, editPageURL, attachmentURLPrefix);
148             }
149             catch (Exception e) {
150                 _log.error(
151                     "Error formatting the wiki page " + page.getPageId() +
152                         " with the format " + page.getFormat(), e);
153             }
154 
155             StringBuilder sb = new StringBuilder();
156 
157             sb.append("<html>");
158 
159             sb.append("<head>");
160             sb.append("<meta content=\"");
161             sb.append(ContentTypes.TEXT_HTML_UTF8);
162             sb.append("\" http-equiv=\"content-type\" />");
163             sb.append("</head>");
164 
165             sb.append("<body>");
166 
167             sb.append("<h1>");
168             sb.append(title);
169             sb.append("</h1>");
170             sb.append(content);
171 
172             sb.append("</body>");
173             sb.append("</html>");
174 
175             is = new ByteArrayInputStream(
176                 sb.toString().getBytes(StringPool.UTF8));
177 
178             String sourceExtension = "html";
179 
180             sb = new StringBuilder();
181 
182             sb.append(title);
183             sb.append(StringPool.PERIOD);
184             sb.append(sourceExtension);
185 
186             String fileName = sb.toString();
187 
188             if (Validator.isNotNull(targetExtension)) {
189                 String id = page.getUuid();
190 
191                 InputStream convertedIS = DocumentConversionUtil.convert(
192                     id, is, sourceExtension, targetExtension);
193 
194                 if ((convertedIS != null) && (convertedIS != is)) {
195                     sb = new StringBuilder();
196 
197                     sb.append(title);
198                     sb.append(StringPool.PERIOD);
199                     sb.append(targetExtension);
200 
201                     fileName = sb.toString();
202 
203                     is = convertedIS;
204                 }
205             }
206 
207             String contentType = MimeTypesUtil.getContentType(fileName);
208 
209             ServletResponseUtil.sendFile(response, fileName, is, contentType);
210         }
211         catch (Exception e) {
212             _log.error(e, e);
213         }
214         finally {
215             ServletResponseUtil.cleanUp(is);
216         }
217     }
218 
219     protected boolean isCheckMethodOnProcessAction() {
220         return _CHECK_METHOD_ON_PROCESS_ACTION;
221     }
222 
223     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
224 
225     private static Log _log = LogFactoryUtil.getLog(ExportPageAction.class);
226 
227 }