1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journalcontent.action;
21  
22  import com.liferay.portal.kernel.language.LanguageUtil;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.ArrayUtil;
26  import com.liferay.portal.kernel.util.ContentTypes;
27  import com.liferay.portal.kernel.util.MimeTypesUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.struts.ActionConstants;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
37  import com.liferay.portlet.journal.model.JournalArticleDisplay;
38  import com.liferay.portlet.journalcontent.util.JournalContentUtil;
39  import com.liferay.util.servlet.ServletResponseUtil;
40  
41  import java.io.ByteArrayInputStream;
42  import java.io.InputStream;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletConfig;
47  import javax.portlet.PortletPreferences;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  import org.apache.struts.action.ActionForm;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="ExportArticleAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Bruno Farache
59   *
60   */
61  public class ExportArticleAction extends PortletAction {
62  
63      public void processAction(
64              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
65              ActionRequest actionRequest, ActionResponse actionResponse)
66          throws Exception {
67  
68          try {
69              long groupId = ParamUtil.getLong(actionRequest, "groupId");
70              String articleId = ParamUtil.getString(actionRequest, "articleId");
71  
72              String targetExtension = ParamUtil.getString(
73                  actionRequest, "targetExtension");
74  
75              PortletPreferences preferences = actionRequest.getPreferences();
76  
77              String[] allowedExtensions = preferences.getValues(
78                  "extensions", null);
79  
80              String languageId = LanguageUtil.getLanguageId(actionRequest);
81  
82              ThemeDisplay themeDisplay =
83                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
84  
85              HttpServletRequest request = PortalUtil.getHttpServletRequest(
86                  actionRequest);
87              HttpServletResponse response = PortalUtil.getHttpServletResponse(
88                  actionResponse);
89  
90              getFile(
91                  groupId, articleId, targetExtension, allowedExtensions,
92                  languageId, themeDisplay, request, response);
93  
94              setForward(actionRequest, ActionConstants.COMMON_NULL);
95          }
96          catch (Exception e) {
97              PortalUtil.sendError(e, actionRequest, actionResponse);
98          }
99      }
100 
101     protected void getFile(
102             long groupId, String articleId, String targetExtension,
103             String[] allowedExtensions, String languageId,
104             ThemeDisplay themeDisplay, HttpServletRequest request,
105             HttpServletResponse response)
106         throws Exception {
107 
108         InputStream is = null;
109 
110         try {
111             JournalArticleDisplay articleDisplay =
112                 JournalContentUtil.getDisplay(
113                     groupId, articleId, null, languageId, themeDisplay);
114 
115             StringBuilder sb = new StringBuilder();
116 
117             sb.append("<html>");
118 
119             sb.append("<head>");
120             sb.append("<meta content=\"");
121             sb.append(ContentTypes.TEXT_HTML_UTF8);
122             sb.append("\" http-equiv=\"content-type\" />");
123             sb.append("<base href=\"");
124             sb.append(themeDisplay.getPortalURL());
125             sb.append("\" />");
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 }