1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journalcontent.action;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.kernel.util.ArrayUtil;
29  import com.liferay.portal.kernel.util.ContentTypes;
30  import com.liferay.portal.kernel.util.MimeTypesUtil;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.struts.ActionConstants;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
40  import com.liferay.portlet.journal.model.JournalArticleDisplay;
41  import com.liferay.portlet.journalcontent.util.JournalContentUtil;
42  import com.liferay.util.servlet.ServletResponseUtil;
43  
44  import java.io.ByteArrayInputStream;
45  import java.io.InputStream;
46  
47  import javax.portlet.ActionRequest;
48  import javax.portlet.ActionResponse;
49  import javax.portlet.PortletConfig;
50  import javax.portlet.PortletPreferences;
51  
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  
55  import org.apache.struts.action.ActionForm;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="ExportArticleAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Bruno Farache
62   *
63   */
64  public class ExportArticleAction extends PortletAction {
65  
66      public void processAction(
67              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
68              ActionRequest actionRequest, ActionResponse actionResponse)
69          throws Exception {
70  
71          try {
72              long groupId = ParamUtil.getLong(actionRequest, "groupId");
73              String articleId = ParamUtil.getString(actionRequest, "articleId");
74  
75              String targetExtension = ParamUtil.getString(
76                  actionRequest, "targetExtension");
77  
78              PortletPreferences preferences = actionRequest.getPreferences();
79  
80              String[] allowedExtensions = preferences.getValues(
81                  "extensions", null);
82  
83              String languageId = LanguageUtil.getLanguageId(actionRequest);
84  
85              ThemeDisplay themeDisplay =
86                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
87  
88              HttpServletRequest request = PortalUtil.getHttpServletRequest(
89                  actionRequest);
90              HttpServletResponse response = PortalUtil.getHttpServletResponse(
91                  actionResponse);
92  
93              getFile(
94                  groupId, articleId, targetExtension, allowedExtensions,
95                  languageId, themeDisplay, request, response);
96  
97              setForward(actionRequest, ActionConstants.COMMON_NULL);
98          }
99          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, HttpServletRequest request,
108             HttpServletResponse response)
109         throws Exception {
110 
111         InputStream is = null;
112 
113         try {
114             JournalArticleDisplay articleDisplay =
115                 JournalContentUtil.getDisplay(
116                     groupId, articleId, null, languageId, themeDisplay);
117 
118             StringBuilder sb = new StringBuilder();
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("</head>");
127 
128             sb.append("<body>");
129 
130             sb.append(articleDisplay.getContent());
131 
132             int pages = articleDisplay.getNumberOfPages();
133 
134             for (int i = 2; i <= pages; i++) {
135                 articleDisplay = JournalContentUtil.getDisplay(
136                     groupId, articleId, "export", languageId, themeDisplay, i);
137 
138                 sb.append(articleDisplay.getContent());
139             }
140 
141             sb.append("</body>");
142             sb.append("</html>");
143 
144             is = new ByteArrayInputStream(
145                 sb.toString().getBytes(StringPool.UTF8));
146 
147             String title = articleDisplay.getTitle();
148             String sourceExtension = "html";
149 
150             sb = new StringBuilder();
151 
152             sb.append(title);
153             sb.append(StringPool.PERIOD);
154             sb.append(sourceExtension);
155 
156             String fileName = sb.toString();
157 
158             if (Validator.isNotNull(targetExtension) &&
159                 ArrayUtil.contains(allowedExtensions, targetExtension)) {
160 
161                 String id = DocumentConversionUtil.getTempFileId(
162                     articleDisplay.getId(), articleDisplay.getVersion());
163 
164                 InputStream convertedIS = DocumentConversionUtil.convert(
165                     id, is, sourceExtension, targetExtension);
166 
167                 if ((convertedIS != null) && (convertedIS != is)) {
168                     sb = new StringBuilder();
169 
170                     sb.append(title);
171                     sb.append(StringPool.PERIOD);
172                     sb.append(targetExtension);
173 
174                     fileName = sb.toString();
175 
176                     is = convertedIS;
177                 }
178             }
179 
180             String contentType = MimeTypesUtil.getContentType(fileName);
181 
182             ServletResponseUtil.sendFile(response, fileName, is, contentType);
183         }
184         catch (Exception e) {
185             _log.error(e, e);
186         }
187         finally {
188             ServletResponseUtil.cleanUp(is);
189         }
190     }
191 
192     protected boolean isCheckMethodOnProcessAction() {
193         return _CHECK_METHOD_ON_PROCESS_ACTION;
194     }
195 
196     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
197 
198     private static Log _log = LogFactoryUtil.getLog(ExportArticleAction.class);
199 
200 }