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