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("<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             int pages = articleDisplay.getNumberOfPages();
136 
137             for (int i = 2; i <= pages; i++) {
138                 articleDisplay = JournalContentUtil.getDisplay(
139                     groupId, articleId, "export", languageId, themeDisplay, i);
140 
141                 sb.append(articleDisplay.getContent());
142             }
143 
144             sb.append("</body>");
145             sb.append("</html>");
146 
147             is = new ByteArrayInputStream(
148                 sb.toString().getBytes(StringPool.UTF8));
149 
150             String title = articleDisplay.getTitle();
151             String sourceExtension = "html";
152 
153             sb = new StringBuilder();
154 
155             sb.append(title);
156             sb.append(StringPool.PERIOD);
157             sb.append(sourceExtension);
158 
159             String fileName = sb.toString();
160 
161             if (Validator.isNotNull(targetExtension) &&
162                 ArrayUtil.contains(allowedExtensions, targetExtension)) {
163 
164                 String id = DocumentConversionUtil.getTempFileId(
165                     articleDisplay.getId(), articleDisplay.getVersion());
166 
167                 InputStream convertedIS = DocumentConversionUtil.convert(
168                     id, is, sourceExtension, targetExtension);
169 
170                 if ((convertedIS != null) && (convertedIS != is)) {
171                     sb = new StringBuilder();
172 
173                     sb.append(title);
174                     sb.append(StringPool.PERIOD);
175                     sb.append(targetExtension);
176 
177                     fileName = sb.toString();
178 
179                     is = convertedIS;
180                 }
181             }
182 
183             String contentType = MimeTypesUtil.getContentType(fileName);
184 
185             ServletResponseUtil.sendFile(response, fileName, is, contentType);
186         }
187         catch (Exception e) {
188             _log.error(e, e);
189         }
190         finally {
191             ServletResponseUtil.cleanUp(is);
192         }
193     }
194 
195     protected boolean isCheckMethodOnProcessAction() {
196         return _CHECK_METHOD_ON_PROCESS_ACTION;
197     }
198 
199     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
200 
201     private static Log _log = LogFactoryUtil.getLog(ExportArticleAction.class);
202 
203 }