001
014
015 package com.liferay.portlet.wiki.action;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
021 import com.liferay.portal.kernel.util.ContentTypes;
022 import com.liferay.portal.kernel.util.MimeTypesUtil;
023 import com.liferay.portal.kernel.util.ParamUtil;
024 import com.liferay.portal.kernel.util.PropsKeys;
025 import com.liferay.portal.kernel.util.StringBundler;
026 import com.liferay.portal.kernel.util.StringPool;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.struts.ActionConstants;
029 import com.liferay.portal.struts.PortletAction;
030 import com.liferay.portal.theme.ThemeDisplay;
031 import com.liferay.portal.util.PortalUtil;
032 import com.liferay.portal.util.PrefsPropsUtil;
033 import com.liferay.portal.util.WebKeys;
034 import com.liferay.portlet.PortletURLImpl;
035 import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
036 import com.liferay.portlet.wiki.model.WikiPage;
037 import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
038 import com.liferay.portlet.wiki.util.WikiUtil;
039
040 import java.io.File;
041 import java.io.FileInputStream;
042 import java.io.InputStream;
043
044 import javax.portlet.ActionRequest;
045 import javax.portlet.ActionResponse;
046 import javax.portlet.PortletConfig;
047 import javax.portlet.PortletMode;
048 import javax.portlet.PortletRequest;
049 import javax.portlet.PortletURL;
050 import javax.portlet.WindowState;
051
052 import javax.servlet.http.HttpServletRequest;
053 import javax.servlet.http.HttpServletResponse;
054
055 import org.apache.struts.action.ActionForm;
056 import org.apache.struts.action.ActionMapping;
057
058
061 public class ExportPageAction extends PortletAction {
062
063 @Override
064 public void processAction(
065 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
066 ActionRequest actionRequest, ActionResponse actionResponse)
067 throws Exception {
068
069 try {
070 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
071 String nodeName = ParamUtil.getString(actionRequest, "nodeName");
072 String title = ParamUtil.getString(actionRequest, "title");
073 double version = ParamUtil.getDouble(actionRequest, "version");
074
075 String targetExtension = ParamUtil.getString(
076 actionRequest, "targetExtension");
077
078 ThemeDisplay themeDisplay =
079 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
080
081 PortletURL viewPageURL = new PortletURLImpl(
082 actionRequest, portletConfig.getPortletName(),
083 themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
084
085 viewPageURL.setParameter("struts_action", "/wiki/view");
086 viewPageURL.setParameter("nodeName", nodeName);
087 viewPageURL.setParameter("title", title);
088 viewPageURL.setPortletMode(PortletMode.VIEW);
089 viewPageURL.setWindowState(WindowState.MAXIMIZED);
090
091 PortletURL editPageURL = new PortletURLImpl(
092 actionRequest, portletConfig.getPortletName(),
093 themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
094
095 editPageURL.setParameter("struts_action", "/wiki/edit_page");
096 editPageURL.setParameter("nodeId", String.valueOf(nodeId));
097 editPageURL.setParameter("title", title);
098 editPageURL.setPortletMode(PortletMode.VIEW);
099 editPageURL.setWindowState(WindowState.MAXIMIZED);
100
101 HttpServletRequest request = PortalUtil.getHttpServletRequest(
102 actionRequest);
103 HttpServletResponse response = PortalUtil.getHttpServletResponse(
104 actionResponse);
105
106 getFile(
107 nodeId, title, version, targetExtension, viewPageURL,
108 editPageURL, themeDisplay, request, response);
109
110 setForward(actionRequest, ActionConstants.COMMON_NULL);
111 }
112 catch (Exception e) {
113 String host = PrefsPropsUtil.getString(
114 PropsKeys.OPENOFFICE_SERVER_HOST);
115
116 if (Validator.isNotNull(host) && !host.equals(_LOCALHOST_IP) &&
117 !host.startsWith(_LOCALHOST)) {
118
119 StringBundler sb = new StringBundler(3);
120
121 sb.append("Conversion using a remote OpenOffice instance is ");
122 sb.append("not fully supported. Please use a local instance ");
123 sb.append("to prevent any limitations and problems.");
124
125 _log.error(sb.toString());
126 }
127
128 PortalUtil.sendError(e, actionRequest, actionResponse);
129 }
130 }
131
132 protected void getFile(
133 long nodeId, String title, double version, String targetExtension,
134 PortletURL viewPageURL, PortletURL editPageURL,
135 ThemeDisplay themeDisplay, HttpServletRequest request,
136 HttpServletResponse response)
137 throws Exception {
138
139 WikiPage page = WikiPageServiceUtil.getPage(nodeId, title, version);
140
141 String content = page.getContent();
142
143 String attachmentURLPrefix = WikiUtil.getAttachmentURLPrefix(
144 themeDisplay.getPathMain(), themeDisplay.getPlid(), nodeId, title);
145
146 try {
147 content = WikiUtil.convert(
148 page, viewPageURL, editPageURL, attachmentURLPrefix);
149 }
150 catch (Exception e) {
151 _log.error(
152 "Error formatting the wiki page " + page.getPageId() +
153 " with the format " + page.getFormat(), e);
154 }
155
156 StringBundler sb = new StringBundler(17);
157
158 sb.append("<!DOCTYPE html>");
159
160 sb.append("<html>");
161
162 sb.append("<head>");
163 sb.append("<meta content=\"");
164 sb.append(ContentTypes.TEXT_HTML_UTF8);
165 sb.append("\" http-equiv=\"content-type\" />");
166 sb.append("<base href=\"");
167 sb.append(themeDisplay.getPortalURL());
168 sb.append("\" />");
169 sb.append("</head>");
170
171 sb.append("<body>");
172
173 sb.append("<h1>");
174 sb.append(title);
175 sb.append("</h1>");
176 sb.append(content);
177
178 sb.append("</body>");
179 sb.append("</html>");
180
181 InputStream is = new UnsyncByteArrayInputStream(
182 sb.toString().getBytes(StringPool.UTF8));
183
184 String sourceExtension = "html";
185
186 String fileName = title.concat(StringPool.PERIOD).concat(
187 sourceExtension);
188
189 if (Validator.isNotNull(targetExtension)) {
190 String id = page.getUuid();
191
192 File convertedFile = DocumentConversionUtil.convert(
193 id, is, sourceExtension, targetExtension);
194
195 if (convertedFile != null) {
196 fileName = title.concat(StringPool.PERIOD).concat(
197 targetExtension);
198
199 is = new FileInputStream(convertedFile);
200 }
201 }
202
203 String contentType = MimeTypesUtil.getContentType(fileName);
204
205 ServletResponseUtil.sendFile(
206 request, response, fileName, is, contentType);
207 }
208
209 @Override
210 protected boolean isCheckMethodOnProcessAction() {
211 return _CHECK_METHOD_ON_PROCESS_ACTION;
212 }
213
214 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
215
216 private static final String _LOCALHOST = "localhost";
217
218 private static final String _LOCALHOST_IP = "127.0.0.1";
219
220 private static Log _log = LogFactoryUtil.getLog(ExportPageAction.class);
221
222 }