1
22
23 package com.liferay.portlet.wiki.action;
24
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.commons.logging.Log;
58 import org.apache.commons.logging.LogFactory;
59 import org.apache.struts.action.ActionForm;
60 import org.apache.struts.action.ActionMapping;
61
62
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("<h1>");
157 sb.append(title);
158 sb.append("</h1>");
159 sb.append(content);
160
161 is = new ByteArrayInputStream(
162 sb.toString().getBytes(StringPool.UTF8));
163
164 String sourceExtension = "html";
165
166 sb = new StringBuilder();
167
168 sb.append(title);
169 sb.append(StringPool.PERIOD);
170 sb.append(sourceExtension);
171
172 String fileName = sb.toString();
173
174 if (Validator.isNotNull(targetExtension)) {
175 String id = page.getUuid();
176
177 InputStream convertedIS = DocumentConversionUtil.convert(
178 id, is, sourceExtension, targetExtension);
179
180 if ((convertedIS != null) && (convertedIS != is)) {
181 sb = new StringBuilder();
182
183 sb.append(title);
184 sb.append(StringPool.PERIOD);
185 sb.append(targetExtension);
186
187 fileName = sb.toString();
188
189 is = convertedIS;
190 }
191 }
192
193 String contentType = MimeTypesUtil.getContentType(fileName);
194
195 ServletResponseUtil.sendFile(response, fileName, is, contentType);
196 }
197 catch (Exception e) {
198 _log.error(e, e);
199 }
200 finally {
201 ServletResponseUtil.cleanUp(is);
202 }
203 }
204
205 protected boolean isCheckMethodOnProcessAction() {
206 return _CHECK_METHOD_ON_PROCESS_ACTION;
207 }
208
209 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
210
211 private static Log _log = LogFactory.getLog(ExportPageAction.class);
212
213 }