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