001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.servlet.ServletResponseUtil;
022    import com.liferay.portal.kernel.util.ArrayUtil;
023    import com.liferay.portal.kernel.util.ContentTypes;
024    import com.liferay.portal.kernel.util.MimeTypesUtil;
025    import com.liferay.portal.kernel.util.ParamUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.Validator;
029    import com.liferay.portal.struts.ActionConstants;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portal.util.WebKeys;
034    import com.liferay.portlet.documentlibrary.util.DLUtil;
035    import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
036    import com.liferay.portlet.journal.model.JournalArticleDisplay;
037    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
038    import com.liferay.util.portlet.PortletRequestUtil;
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    /**
056     * @author Bruno Farache
057     */
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    
082                            ThemeDisplay themeDisplay =
083                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
084    
085                            String xmlRequest = PortletRequestUtil.toXML(
086                                    actionRequest, actionResponse);
087    
088                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
089                                    actionRequest);
090                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
091                                    actionResponse);
092    
093                            getFile(
094                                    groupId, articleId, targetExtension, allowedExtensions,
095                                    languageId, themeDisplay, xmlRequest, request, response);
096    
097                            setForward(actionRequest, ActionConstants.COMMON_NULL);
098                    }
099                    catch (Exception e) {
100                            PortalUtil.sendError(e, actionRequest, actionResponse);
101                    }
102            }
103    
104            protected void getFile(
105                            long groupId, String articleId, String targetExtension,
106                            String[] allowedExtensions, String languageId,
107                            ThemeDisplay themeDisplay, String xmlRequest,
108                            HttpServletRequest request, HttpServletResponse response)
109                    throws Exception {
110    
111                    try {
112                            JournalArticleDisplay articleDisplay =
113                                    JournalContentUtil.getDisplay(
114                                            groupId, articleId, null, "export", languageId,
115                                            themeDisplay, 1, xmlRequest);
116    
117                            int pages = articleDisplay.getNumberOfPages();
118    
119                            StringBundler sb = new StringBundler(pages + 12);
120    
121                            sb.append("<html>");
122    
123                            sb.append("<head>");
124                            sb.append("<meta content=\"");
125                            sb.append(ContentTypes.TEXT_HTML_UTF8);
126                            sb.append("\" http-equiv=\"content-type\" />");
127                            sb.append("<base href=\"");
128                            sb.append(themeDisplay.getPortalURL());
129                            sb.append("\" />");
130                            sb.append("</head>");
131    
132                            sb.append("<body>");
133    
134                            sb.append(articleDisplay.getContent());
135    
136                            for (int i = 2; i <= pages; i++) {
137                                    articleDisplay = JournalContentUtil.getDisplay(
138                                            groupId, articleId, "export", languageId, themeDisplay, i);
139    
140                                    sb.append(articleDisplay.getContent());
141                            }
142    
143                            sb.append("</body>");
144                            sb.append("</html>");
145    
146                            InputStream is = new UnsyncByteArrayInputStream(
147                                    sb.toString().getBytes(StringPool.UTF8));
148    
149                            String title = articleDisplay.getTitle();
150                            String sourceExtension = "html";
151    
152                            String fileName = title.concat(StringPool.PERIOD).concat(
153                                    sourceExtension);
154    
155                            if (Validator.isNotNull(targetExtension) &&
156                                    ArrayUtil.contains(allowedExtensions, targetExtension)) {
157    
158                                    String id = DLUtil.getTempFileId(
159                                            articleDisplay.getId(),
160                                            String.valueOf(articleDisplay.getVersion()), languageId);
161    
162                                    File convertedFile = DocumentConversionUtil.convert(
163                                            id, is, sourceExtension, targetExtension);
164    
165                                    if (convertedFile != null) {
166                                            fileName = title.concat(StringPool.PERIOD).concat(
167                                                    targetExtension);
168    
169                                            is = new FileInputStream(convertedFile);
170                                    }
171                            }
172    
173                            String contentType = MimeTypesUtil.getContentType(fileName);
174    
175                            ServletResponseUtil.sendFile(
176                                    request, response, fileName, is, contentType);
177                    }
178                    catch (Exception e) {
179                            _log.error(e, e);
180                    }
181            }
182    
183            @Override
184            protected boolean isCheckMethodOnProcessAction() {
185                    return _CHECK_METHOD_ON_PROCESS_ACTION;
186            }
187    
188            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
189    
190            private static Log _log = LogFactoryUtil.getLog(ExportArticleAction.class);
191    
192    }