1
22
23 package com.liferay.portlet.wiki.action;
24
25 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
26 import com.liferay.portal.kernel.log.Log;
27 import com.liferay.portal.kernel.log.LogFactoryUtil;
28 import com.liferay.portal.kernel.util.ContentTypes;
29 import com.liferay.portal.kernel.util.HttpUtil;
30 import com.liferay.portal.kernel.util.MimeTypesUtil;
31 import com.liferay.portal.kernel.util.ParamUtil;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.kernel.util.Validator;
34 import com.liferay.portal.struts.ActionConstants;
35 import com.liferay.portal.struts.PortletAction;
36 import com.liferay.portal.theme.ThemeDisplay;
37 import com.liferay.portal.util.PortalUtil;
38 import com.liferay.portal.util.WebKeys;
39 import com.liferay.portlet.ActionRequestImpl;
40 import com.liferay.portlet.PortletURLImpl;
41 import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
42 import com.liferay.portlet.wiki.model.WikiPage;
43 import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
44 import com.liferay.portlet.wiki.util.WikiUtil;
45 import com.liferay.util.servlet.ServletResponseUtil;
46
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
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 WikiPage page = WikiPageServiceUtil.getPage(nodeId, title, version);
133
134 String content = page.getContent();
135
136 String attachmentURLPrefix =
137 themeDisplay.getPathMain() + "/wiki/get_page_attachment?" +
138 "p_l_id=" + themeDisplay.getPlid() + "&nodeId=" + nodeId +
139 "&title=" + HttpUtil.encodeURL(title) + "&fileName=";
140
141 try {
142 content = WikiUtil.convert(
143 page, viewPageURL, editPageURL, attachmentURLPrefix);
144 }
145 catch (Exception e) {
146 _log.error(
147 "Error formatting the wiki page " + page.getPageId() +
148 " with the format " + page.getFormat(), e);
149 }
150
151 StringBuilder sb = new StringBuilder();
152
153 sb.append("<html>");
154
155 sb.append("<head>");
156 sb.append("<meta content=\"");
157 sb.append(ContentTypes.TEXT_HTML_UTF8);
158 sb.append("\" http-equiv=\"content-type\" />");
159 sb.append("<base href=\"");
160 sb.append(themeDisplay.getPortalURL());
161 sb.append("\" />");
162 sb.append("</head>");
163
164 sb.append("<body>");
165
166 sb.append("<h1>");
167 sb.append(title);
168 sb.append("</h1>");
169 sb.append(content);
170
171 sb.append("</body>");
172 sb.append("</html>");
173
174 InputStream is = new UnsyncByteArrayInputStream(
175 sb.toString().getBytes(StringPool.UTF8));
176
177 String sourceExtension = "html";
178
179 sb = new StringBuilder();
180
181 sb.append(title);
182 sb.append(StringPool.PERIOD);
183 sb.append(sourceExtension);
184
185 String fileName = sb.toString();
186
187 if (Validator.isNotNull(targetExtension)) {
188 String id = page.getUuid();
189
190 InputStream convertedIS = DocumentConversionUtil.convert(
191 id, is, sourceExtension, targetExtension);
192
193 if ((convertedIS != null) && (convertedIS != is)) {
194 sb = new StringBuilder();
195
196 sb.append(title);
197 sb.append(StringPool.PERIOD);
198 sb.append(targetExtension);
199
200 fileName = sb.toString();
201
202 is = convertedIS;
203 }
204 }
205
206 String contentType = MimeTypesUtil.getContentType(fileName);
207
208 ServletResponseUtil.sendFile(response, fileName, is, contentType);
209 }
210
211 protected boolean isCheckMethodOnProcessAction() {
212 return _CHECK_METHOD_ON_PROCESS_ACTION;
213 }
214
215 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
216
217 private static Log _log = LogFactoryUtil.getLog(ExportPageAction.class);
218
219 }