1
14
15 package com.liferay.portlet.journalcontent.action;
16
17 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
18 import com.liferay.portal.kernel.language.LanguageUtil;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.kernel.util.ArrayUtil;
22 import com.liferay.portal.kernel.util.ContentTypes;
23 import com.liferay.portal.kernel.util.MimeTypesUtil;
24 import com.liferay.portal.kernel.util.ParamUtil;
25 import com.liferay.portal.kernel.util.StringBundler;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.struts.ActionConstants;
29 import com.liferay.portal.struts.PortletAction;
30 import com.liferay.portal.theme.ThemeDisplay;
31 import com.liferay.portal.util.PortalUtil;
32 import com.liferay.portal.util.WebKeys;
33 import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
34 import com.liferay.portlet.journal.model.JournalArticleDisplay;
35 import com.liferay.portlet.journalcontent.util.JournalContentUtil;
36 import com.liferay.util.servlet.ServletResponseUtil;
37
38 import java.io.InputStream;
39
40 import javax.portlet.ActionRequest;
41 import javax.portlet.ActionResponse;
42 import javax.portlet.PortletConfig;
43 import javax.portlet.PortletPreferences;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.apache.struts.action.ActionForm;
49 import org.apache.struts.action.ActionMapping;
50
51
56 public class ExportArticleAction extends PortletAction {
57
58 public void processAction(
59 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
60 ActionRequest actionRequest, ActionResponse actionResponse)
61 throws Exception {
62
63 try {
64 long groupId = ParamUtil.getLong(actionRequest, "groupId");
65 String articleId = ParamUtil.getString(actionRequest, "articleId");
66
67 String targetExtension = ParamUtil.getString(
68 actionRequest, "targetExtension");
69
70 PortletPreferences preferences = actionRequest.getPreferences();
71
72 String[] allowedExtensions = preferences.getValues(
73 "extensions", null);
74
75 String languageId = LanguageUtil.getLanguageId(actionRequest);
76
77 ThemeDisplay themeDisplay =
78 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
79
80 HttpServletRequest request = PortalUtil.getHttpServletRequest(
81 actionRequest);
82 HttpServletResponse response = PortalUtil.getHttpServletResponse(
83 actionResponse);
84
85 getFile(
86 groupId, articleId, targetExtension, allowedExtensions,
87 languageId, themeDisplay, request, response);
88
89 setForward(actionRequest, ActionConstants.COMMON_NULL);
90 }
91 catch (Exception e) {
92 PortalUtil.sendError(e, actionRequest, actionResponse);
93 }
94 }
95
96 protected void getFile(
97 long groupId, String articleId, String targetExtension,
98 String[] allowedExtensions, String languageId,
99 ThemeDisplay themeDisplay, HttpServletRequest request,
100 HttpServletResponse response)
101 throws Exception {
102
103 try {
104 JournalArticleDisplay articleDisplay =
105 JournalContentUtil.getDisplay(
106 groupId, articleId, null, languageId, themeDisplay);
107
108 int pages = articleDisplay.getNumberOfPages();
109
110 StringBundler sb = new StringBundler(pages + 12);
111
112 sb.append("<html>");
113
114 sb.append("<head>");
115 sb.append("<meta content=\"");
116 sb.append(ContentTypes.TEXT_HTML_UTF8);
117 sb.append("\" http-equiv=\"content-type\" />");
118 sb.append("<base href=\"");
119 sb.append(themeDisplay.getPortalURL());
120 sb.append("\" />");
121 sb.append("</head>");
122
123 sb.append("<body>");
124
125 sb.append(articleDisplay.getContent());
126
127 for (int i = 2; i <= pages; i++) {
128 articleDisplay = JournalContentUtil.getDisplay(
129 groupId, articleId, "export", languageId, themeDisplay, i);
130
131 sb.append(articleDisplay.getContent());
132 }
133
134 sb.append("</body>");
135 sb.append("</html>");
136
137 InputStream is = new UnsyncByteArrayInputStream(
138 sb.toString().getBytes(StringPool.UTF8));
139
140 String title = articleDisplay.getTitle();
141 String sourceExtension = "html";
142
143 String fileName = title.concat(StringPool.PERIOD).concat(
144 sourceExtension);
145
146 if (Validator.isNotNull(targetExtension) &&
147 ArrayUtil.contains(allowedExtensions, targetExtension)) {
148
149 String id = DocumentConversionUtil.getTempFileId(
150 articleDisplay.getId(),
151 String.valueOf(articleDisplay.getVersion()));
152
153 InputStream convertedIS = DocumentConversionUtil.convert(
154 id, is, sourceExtension, targetExtension);
155
156 if ((convertedIS != null) && (convertedIS != is)) {
157 fileName = title.concat(StringPool.PERIOD).concat(
158 targetExtension);
159
160 is = convertedIS;
161 }
162 }
163
164 String contentType = MimeTypesUtil.getContentType(fileName);
165
166 ServletResponseUtil.sendFile(response, fileName, is, contentType);
167 }
168 catch (Exception e) {
169 _log.error(e, e);
170 }
171 }
172
173 protected boolean isCheckMethodOnProcessAction() {
174 return _CHECK_METHOD_ON_PROCESS_ACTION;
175 }
176
177 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
178
179 private static Log _log = LogFactoryUtil.getLog(ExportArticleAction.class);
180
181 }