1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ExportPageAction extends PortletAction {
69  
70      public void processAction(
71              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72              ActionRequest actionRequest, ActionResponse actionResponse)
73          throws Exception {
74  
75          try {
76              long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
77              String nodeName = ParamUtil.getString(actionRequest, "nodeName");
78              String title = ParamUtil.getString(actionRequest, "title");
79              double version = ParamUtil.getDouble(actionRequest, "version");
80  
81              String targetExtension = ParamUtil.getString(
82                  actionRequest, "targetExtension");
83  
84              ThemeDisplay themeDisplay =
85                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
86  
87              PortletURL viewPageURL = new PortletURLImpl(
88                  (ActionRequestImpl)actionRequest,
89                  portletConfig.getPortletName(), themeDisplay.getPlid(),
90                  PortletRequest.RENDER_PHASE);
91  
92              viewPageURL.setPortletMode(PortletMode.VIEW);
93              viewPageURL.setWindowState(WindowState.MAXIMIZED);
94              viewPageURL.setParameter("struts_action", "/wiki/view");
95              viewPageURL.setParameter("nodeName", nodeName);
96              viewPageURL.setParameter("title", title);
97  
98              PortletURL editPageURL = new PortletURLImpl(
99                  (ActionRequestImpl)actionRequest,
100                 portletConfig.getPortletName(), themeDisplay.getPlid(),
101                 PortletRequest.RENDER_PHASE);
102 
103             editPageURL.setPortletMode(PortletMode.VIEW);
104             editPageURL.setWindowState(WindowState.MAXIMIZED);
105             editPageURL.setParameter("struts_action", "/wiki/edit_page");
106             editPageURL.setParameter("nodeId", String.valueOf(nodeId));
107             editPageURL.setParameter("title", title);
108 
109             HttpServletRequest request = PortalUtil.getHttpServletRequest(
110                 actionRequest);
111             HttpServletResponse response = PortalUtil.getHttpServletResponse(
112                 actionResponse);
113 
114             getFile(
115                 nodeId, title, version, targetExtension, viewPageURL,
116                 editPageURL, themeDisplay, request, response);
117 
118             setForward(actionRequest, ActionConstants.COMMON_NULL);
119         }
120         catch (Exception e) {
121             PortalUtil.sendError(e, actionRequest, actionResponse);
122         }
123     }
124 
125     protected void getFile(
126             long nodeId, String title, double version, String targetExtension,
127             PortletURL viewPageURL, PortletURL editPageURL,
128             ThemeDisplay themeDisplay, HttpServletRequest request,
129             HttpServletResponse response)
130         throws Exception {
131 
132         InputStream is = null;
133 
134         try {
135             WikiPage page = WikiPageServiceUtil.getPage(nodeId, title, version);
136 
137             String content = page.getContent();
138 
139             String attachmentURLPrefix =
140                 themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
141                     "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
142                         "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
143 
144             try {
145                 content = WikiUtil.convert(
146                     page, viewPageURL, editPageURL, attachmentURLPrefix);
147             }
148             catch (Exception e) {
149                 _log.error(
150                     "Error formatting the wiki page " + page.getPageId() +
151                         " with the format " + page.getFormat(), e);
152             }
153 
154             StringBuilder sb = new StringBuilder();
155 
156             sb.append("<html>");
157 
158             sb.append("<head>");
159             sb.append("<meta content=\"");
160             sb.append(ContentTypes.TEXT_HTML_UTF8);
161             sb.append("\" http-equiv=\"content-type\" />");
162             sb.append("<base href=\"");
163             sb.append(themeDisplay.getPortalURL());
164             sb.append("\" />");
165             sb.append("</head>");
166 
167             sb.append("<body>");
168 
169             sb.append("<h1>");
170             sb.append(title);
171             sb.append("</h1>");
172             sb.append(content);
173 
174             sb.append("</body>");
175             sb.append("</html>");
176 
177             is = new ByteArrayInputStream(
178                 sb.toString().getBytes(StringPool.UTF8));
179 
180             String sourceExtension = "html";
181 
182             sb = new StringBuilder();
183 
184             sb.append(title);
185             sb.append(StringPool.PERIOD);
186             sb.append(sourceExtension);
187 
188             String fileName = sb.toString();
189 
190             if (Validator.isNotNull(targetExtension)) {
191                 String id = page.getUuid();
192 
193                 InputStream convertedIS = DocumentConversionUtil.convert(
194                     id, is, sourceExtension, targetExtension);
195 
196                 if ((convertedIS != null) && (convertedIS != is)) {
197                     sb = new StringBuilder();
198 
199                     sb.append(title);
200                     sb.append(StringPool.PERIOD);
201                     sb.append(targetExtension);
202 
203                     fileName = sb.toString();
204 
205                     is = convertedIS;
206                 }
207             }
208 
209             String contentType = MimeTypesUtil.getContentType(fileName);
210 
211             ServletResponseUtil.sendFile(response, fileName, is, contentType);
212         }
213         catch (Exception e) {
214             _log.error(e, e);
215         }
216         finally {
217             ServletResponseUtil.cleanUp(is);
218         }
219     }
220 
221     protected boolean isCheckMethodOnProcessAction() {
222         return _CHECK_METHOD_ON_PROCESS_ACTION;
223     }
224 
225     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
226 
227     private static Log _log = LogFactoryUtil.getLog(ExportPageAction.class);
228 
229 }