001    /**
002     * Copyright (c) 2000-present 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.journal.util;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.portlet.PortletRequestModel;
020    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
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.StringUtil;
028    import com.liferay.portal.kernel.util.Validator;
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.DLUtil;
034    import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
035    import com.liferay.portlet.journal.model.JournalArticleDisplay;
036    
037    import java.io.File;
038    import java.io.FileInputStream;
039    import java.io.InputStream;
040    
041    import javax.portlet.PortletPreferences;
042    import javax.portlet.PortletRequest;
043    import javax.portlet.PortletResponse;
044    
045    import javax.servlet.http.HttpServletRequest;
046    import javax.servlet.http.HttpServletResponse;
047    
048    /**
049     * @author Eudaldo Alonso
050     */
051    public class ExportArticleUtil extends PortletAction {
052    
053            public static void sendFile(
054                            PortletRequest portletRequest, PortletResponse portletResponse)
055                    throws Exception {
056    
057                    long groupId = ParamUtil.getLong(portletRequest, "groupId");
058                    String articleId = ParamUtil.getString(portletRequest, "articleId");
059    
060                    String targetExtension = ParamUtil.getString(
061                            portletRequest, "targetExtension");
062    
063                    PortletPreferences portletPreferences = portletRequest.getPreferences();
064    
065                    String[] allowedExtensions = StringUtil.split(
066                            portletPreferences.getValue("extensions", null));
067    
068                    String languageId = LanguageUtil.getLanguageId(portletRequest);
069                    PortletRequestModel portletRequestModel = new PortletRequestModel(
070                            portletRequest, portletResponse);
071                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
072                            WebKeys.THEME_DISPLAY);
073                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
074                            portletRequest);
075                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
076                            portletResponse);
077    
078                    JournalArticleDisplay articleDisplay =
079                            JournalContentUtil.getDisplay(
080                                    groupId, articleId, null, "export", languageId, 1,
081                                    portletRequestModel, themeDisplay);
082    
083                    int pages = articleDisplay.getNumberOfPages();
084    
085                    StringBundler sb = new StringBundler(pages + 12);
086    
087                    sb.append("<html>");
088    
089                    sb.append("<head>");
090                    sb.append("<meta content=\"");
091                    sb.append(ContentTypes.TEXT_HTML_UTF8);
092                    sb.append("\" http-equiv=\"content-type\" />");
093                    sb.append("<base href=\"");
094                    sb.append(themeDisplay.getPortalURL());
095                    sb.append("\" />");
096                    sb.append("</head>");
097    
098                    sb.append("<body>");
099    
100                    sb.append(articleDisplay.getContent());
101    
102                    for (int i = 2; i <= pages; i++) {
103                            articleDisplay = JournalContentUtil.getDisplay(
104                                    groupId, articleId, "export", languageId, i, themeDisplay);
105    
106                            sb.append(articleDisplay.getContent());
107                    }
108    
109                    sb.append("</body>");
110                    sb.append("</html>");
111    
112                    InputStream is = new UnsyncByteArrayInputStream(
113                            sb.toString().getBytes(StringPool.UTF8));
114    
115                    String title = articleDisplay.getTitle();
116                    String sourceExtension = "html";
117    
118                    String fileName = title.concat(StringPool.PERIOD).concat(
119                            sourceExtension);
120    
121                    String contentType = MimeTypesUtil.getContentType(fileName);
122    
123                    if (Validator.isNull(targetExtension) ||
124                            !ArrayUtil.contains(allowedExtensions, targetExtension)) {
125    
126                            ServletResponseUtil.sendFile(
127                                    request, response, fileName, is, contentType);
128    
129                            return;
130                    }
131    
132                    String id = DLUtil.getTempFileId(
133                            articleDisplay.getId(), String.valueOf(articleDisplay.getVersion()),
134                            languageId);
135    
136                    File convertedFile = DocumentConversionUtil.convert(
137                            id, is, sourceExtension, targetExtension);
138    
139                    if (convertedFile != null) {
140                            fileName = title.concat(StringPool.PERIOD).concat(targetExtension);
141    
142                            is = new FileInputStream(convertedFile);
143                    }
144    
145                    ServletResponseUtil.sendFile(
146                            request, response, fileName, is, contentType);
147            }
148    
149    }