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 preferences = actionRequest.getPreferences();
075    
076                            String[] allowedExtensions = preferences.getValues(
077                                    "extensions", null);
078    
079                            String languageId = LanguageUtil.getLanguageId(actionRequest);
080    
081                            ThemeDisplay themeDisplay =
082                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
083    
084                            String xmlRequest = PortletRequestUtil.toXML(
085                                    actionRequest, actionResponse);
086    
087                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
088                                    actionRequest);
089                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
090                                    actionResponse);
091    
092                            getFile(
093                                    groupId, articleId, targetExtension, allowedExtensions,
094                                    languageId, themeDisplay, xmlRequest, request, response);
095    
096                            setForward(actionRequest, ActionConstants.COMMON_NULL);
097                    }
098                    catch (Exception e) {
099                            PortalUtil.sendError(e, actionRequest, actionResponse);
100                    }
101            }
102    
103            protected void getFile(
104                            long groupId, String articleId, String targetExtension,
105                            String[] allowedExtensions, String languageId,
106                            ThemeDisplay themeDisplay, String xmlRequest,
107                            HttpServletRequest request, HttpServletResponse response)
108                    throws Exception {
109    
110                    try {
111                            JournalArticleDisplay articleDisplay =
112                                    JournalContentUtil.getDisplay(
113                                            groupId, articleId, null, "export", languageId,
114                                            themeDisplay, 1, xmlRequest);
115    
116                            int pages = articleDisplay.getNumberOfPages();
117    
118                            StringBundler sb = new StringBundler(pages + 12);
119    
120                            sb.append("<html>");
121    
122                            sb.append("<head>");
123                            sb.append("<meta content=\"");
124                            sb.append(ContentTypes.TEXT_HTML_UTF8);
125                            sb.append("\" http-equiv=\"content-type\" />");
126                            sb.append("<base href=\"");
127                            sb.append(themeDisplay.getPortalURL());
128                            sb.append("\" />");
129                            sb.append("</head>");
130    
131                            sb.append("<body>");
132    
133                            sb.append(articleDisplay.getContent());
134    
135                            for (int i = 2; i <= pages; i++) {
136                                    articleDisplay = JournalContentUtil.getDisplay(
137                                            groupId, articleId, "export", languageId, themeDisplay, i);
138    
139                                    sb.append(articleDisplay.getContent());
140                            }
141    
142                            sb.append("</body>");
143                            sb.append("</html>");
144    
145                            InputStream is = new UnsyncByteArrayInputStream(
146                                    sb.toString().getBytes(StringPool.UTF8));
147    
148                            String title = articleDisplay.getTitle();
149                            String sourceExtension = "html";
150    
151                            String fileName = title.concat(StringPool.PERIOD).concat(
152                                    sourceExtension);
153    
154                            if (Validator.isNotNull(targetExtension) &&
155                                    ArrayUtil.contains(allowedExtensions, targetExtension)) {
156    
157                                    String id = DLUtil.getTempFileId(
158                                            articleDisplay.getId(),
159                                            String.valueOf(articleDisplay.getVersion()), languageId);
160    
161                                    File convertedFile = DocumentConversionUtil.convert(
162                                            id, is, sourceExtension, targetExtension);
163    
164                                    if (convertedFile != null) {
165                                            fileName = title.concat(StringPool.PERIOD).concat(
166                                                    targetExtension);
167    
168                                            is = new FileInputStream(convertedFile);
169                                    }
170                            }
171    
172                            String contentType = MimeTypesUtil.getContentType(fileName);
173    
174                            ServletResponseUtil.sendFile(
175                                    request, response, fileName, is, contentType);
176                    }
177                    catch (Exception e) {
178                            _log.error(e, e);
179                    }
180            }
181    
182            @Override
183            protected boolean isCheckMethodOnProcessAction() {
184                    return _CHECK_METHOD_ON_PROCESS_ACTION;
185            }
186    
187            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
188    
189            private static Log _log = LogFactoryUtil.getLog(ExportArticleAction.class);
190    
191    }