001
014
015 package com.liferay.portlet.journalcontent.action;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018 import com.liferay.portal.kernel.language.LanguageUtil;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.portlet.PortletRequestModel;
022 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
023 import com.liferay.portal.kernel.util.ArrayUtil;
024 import com.liferay.portal.kernel.util.ContentTypes;
025 import com.liferay.portal.kernel.util.MimeTypesUtil;
026 import com.liferay.portal.kernel.util.ParamUtil;
027 import com.liferay.portal.kernel.util.StringBundler;
028 import com.liferay.portal.kernel.util.StringPool;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.struts.ActionConstants;
031 import com.liferay.portal.struts.PortletAction;
032 import com.liferay.portal.theme.ThemeDisplay;
033 import com.liferay.portal.util.PortalUtil;
034 import com.liferay.portal.util.WebKeys;
035 import com.liferay.portlet.documentlibrary.util.DLUtil;
036 import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
037 import com.liferay.portlet.journal.model.JournalArticleDisplay;
038 import com.liferay.portlet.journalcontent.util.JournalContentUtil;
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.PortletPreferences;
048
049 import javax.servlet.http.HttpServletRequest;
050 import javax.servlet.http.HttpServletResponse;
051
052 import org.apache.struts.action.ActionForm;
053 import org.apache.struts.action.ActionMapping;
054
055
058 public class ExportArticleAction extends PortletAction {
059
060 @Override
061 public void processAction(
062 ActionMapping actionMapping, ActionForm actionForm,
063 PortletConfig portletConfig, ActionRequest actionRequest,
064 ActionResponse actionResponse)
065 throws Exception {
066
067 try {
068 long groupId = ParamUtil.getLong(actionRequest, "groupId");
069 String articleId = ParamUtil.getString(actionRequest, "articleId");
070
071 String targetExtension = ParamUtil.getString(
072 actionRequest, "targetExtension");
073
074 PortletPreferences portletPreferences =
075 actionRequest.getPreferences();
076
077 String[] allowedExtensions = portletPreferences.getValues(
078 "extensions", null);
079
080 String languageId = LanguageUtil.getLanguageId(actionRequest);
081 PortletRequestModel portletRequestModel = new PortletRequestModel(
082 actionRequest, actionResponse);
083 ThemeDisplay themeDisplay =
084 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
085 HttpServletRequest request = PortalUtil.getHttpServletRequest(
086 actionRequest);
087 HttpServletResponse response = PortalUtil.getHttpServletResponse(
088 actionResponse);
089
090 getFile(
091 groupId, articleId, targetExtension, allowedExtensions,
092 languageId, portletRequestModel, themeDisplay, request,
093 response);
094
095 setForward(actionRequest, ActionConstants.COMMON_NULL);
096 }
097 catch (Exception e) {
098 PortalUtil.sendError(e, actionRequest, actionResponse);
099 }
100 }
101
102 protected void getFile(
103 long groupId, String articleId, String targetExtension,
104 String[] allowedExtensions, String languageId,
105 PortletRequestModel portletRequestModel, ThemeDisplay themeDisplay,
106 HttpServletRequest request, HttpServletResponse response)
107 throws Exception {
108
109 try {
110 JournalArticleDisplay articleDisplay =
111 JournalContentUtil.getDisplay(
112 groupId, articleId, null, "export", languageId, 1,
113 portletRequestModel, themeDisplay);
114
115 int pages = articleDisplay.getNumberOfPages();
116
117 StringBundler sb = new StringBundler(pages + 12);
118
119 sb.append("<html>");
120
121 sb.append("<head>");
122 sb.append("<meta content=\"");
123 sb.append(ContentTypes.TEXT_HTML_UTF8);
124 sb.append("\" http-equiv=\"content-type\" />");
125 sb.append("<base href=\"");
126 sb.append(themeDisplay.getPortalURL());
127 sb.append("\" />");
128 sb.append("</head>");
129
130 sb.append("<body>");
131
132 sb.append(articleDisplay.getContent());
133
134 for (int i = 2; i <= pages; i++) {
135 articleDisplay = JournalContentUtil.getDisplay(
136 groupId, articleId, "export", languageId, i, themeDisplay);
137
138 sb.append(articleDisplay.getContent());
139 }
140
141 sb.append("</body>");
142 sb.append("</html>");
143
144 InputStream is = new UnsyncByteArrayInputStream(
145 sb.toString().getBytes(StringPool.UTF8));
146
147 String title = articleDisplay.getTitle();
148 String sourceExtension = "html";
149
150 String fileName = title.concat(StringPool.PERIOD).concat(
151 sourceExtension);
152
153 if (Validator.isNotNull(targetExtension) &&
154 ArrayUtil.contains(allowedExtensions, targetExtension)) {
155
156 String id = DLUtil.getTempFileId(
157 articleDisplay.getId(),
158 String.valueOf(articleDisplay.getVersion()), languageId);
159
160 File convertedFile = DocumentConversionUtil.convert(
161 id, is, sourceExtension, targetExtension);
162
163 if (convertedFile != null) {
164 fileName = title.concat(StringPool.PERIOD).concat(
165 targetExtension);
166
167 is = new FileInputStream(convertedFile);
168 }
169 }
170
171 String contentType = MimeTypesUtil.getContentType(fileName);
172
173 ServletResponseUtil.sendFile(
174 request, response, fileName, is, contentType);
175 }
176 catch (Exception e) {
177 _log.error(e, e);
178 }
179 }
180
181 @Override
182 protected boolean isCheckMethodOnProcessAction() {
183 return _CHECK_METHOD_ON_PROCESS_ACTION;
184 }
185
186 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
187
188 private static Log _log = LogFactoryUtil.getLog(ExportArticleAction.class);
189
190 }