1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.action;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.ContentTypes;
25  import com.liferay.portal.kernel.util.HttpUtil;
26  import com.liferay.portal.kernel.util.MimeTypesUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.struts.ActionConstants;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.ActionRequestImpl;
36  import com.liferay.portlet.PortletURLImpl;
37  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
38  import com.liferay.portlet.wiki.model.WikiPage;
39  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
40  import com.liferay.portlet.wiki.util.WikiUtil;
41  import com.liferay.util.servlet.ServletResponseUtil;
42  
43  import java.io.ByteArrayInputStream;
44  import java.io.InputStream;
45  
46  import javax.portlet.ActionRequest;
47  import javax.portlet.ActionResponse;
48  import javax.portlet.PortletConfig;
49  import javax.portlet.PortletMode;
50  import javax.portlet.PortletRequest;
51  import javax.portlet.PortletURL;
52  import javax.portlet.WindowState;
53  
54  import javax.servlet.http.HttpServletRequest;
55  import javax.servlet.http.HttpServletResponse;
56  
57  import org.apache.struts.action.ActionForm;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="ExportPageAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Bruno Farache
64   *
65   */
66  public class ExportPageAction extends PortletAction {
67  
68      public void processAction(
69              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
70              ActionRequest actionRequest, ActionResponse actionResponse)
71          throws Exception {
72  
73          try {
74              long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
75              String nodeName = ParamUtil.getString(actionRequest, "nodeName");
76              String title = ParamUtil.getString(actionRequest, "title");
77              double version = ParamUtil.getDouble(actionRequest, "version");
78  
79              String targetExtension = ParamUtil.getString(
80                  actionRequest, "targetExtension");
81  
82              ThemeDisplay themeDisplay =
83                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
84  
85              PortletURL viewPageURL = new PortletURLImpl(
86                  (ActionRequestImpl)actionRequest,
87                  portletConfig.getPortletName(), themeDisplay.getPlid(),
88                  PortletRequest.RENDER_PHASE);
89  
90              viewPageURL.setPortletMode(PortletMode.VIEW);
91              viewPageURL.setWindowState(WindowState.MAXIMIZED);
92              viewPageURL.setParameter("struts_action", "/wiki/view");
93              viewPageURL.setParameter("nodeName", nodeName);
94              viewPageURL.setParameter("title", title);
95  
96              PortletURL editPageURL = new PortletURLImpl(
97                  (ActionRequestImpl)actionRequest,
98                  portletConfig.getPortletName(), themeDisplay.getPlid(),
99                  PortletRequest.RENDER_PHASE);
100 
101             editPageURL.setPortletMode(PortletMode.VIEW);
102             editPageURL.setWindowState(WindowState.MAXIMIZED);
103             editPageURL.setParameter("struts_action", "/wiki/edit_page");
104             editPageURL.setParameter("nodeId", String.valueOf(nodeId));
105             editPageURL.setParameter("title", title);
106 
107             HttpServletRequest request = PortalUtil.getHttpServletRequest(
108                 actionRequest);
109             HttpServletResponse response = PortalUtil.getHttpServletResponse(
110                 actionResponse);
111 
112             getFile(
113                 nodeId, title, version, targetExtension, viewPageURL,
114                 editPageURL, themeDisplay, request, response);
115 
116             setForward(actionRequest, ActionConstants.COMMON_NULL);
117         }
118         catch (Exception e) {
119             PortalUtil.sendError(e, actionRequest, actionResponse);
120         }
121     }
122 
123     protected void getFile(
124             long nodeId, String title, double version, String targetExtension,
125             PortletURL viewPageURL, PortletURL editPageURL,
126             ThemeDisplay themeDisplay, HttpServletRequest request,
127             HttpServletResponse response)
128         throws Exception {
129 
130         InputStream is = null;
131 
132         try {
133             WikiPage page = WikiPageServiceUtil.getPage(nodeId, title, version);
134 
135             String content = page.getContent();
136 
137             String attachmentURLPrefix =
138                 themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
139                     "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
140                         "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
141 
142             try {
143                 content = WikiUtil.convert(
144                     page, viewPageURL, editPageURL, attachmentURLPrefix);
145             }
146             catch (Exception e) {
147                 _log.error(
148                     "Error formatting the wiki page " + page.getPageId() +
149                         " with the format " + page.getFormat(), e);
150             }
151 
152             StringBuilder sb = new StringBuilder();
153 
154             sb.append("<html>");
155 
156             sb.append("<head>");
157             sb.append("<meta content=\"");
158             sb.append(ContentTypes.TEXT_HTML_UTF8);
159             sb.append("\" http-equiv=\"content-type\" />");
160             sb.append("<base href=\"");
161             sb.append(themeDisplay.getPortalURL());
162             sb.append("\" />");
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 }