1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class ExportArticleAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
67              ActionRequest actionRequest, ActionResponse actionResponse)
68          throws Exception {
69  
70          try {
71              long groupId = ParamUtil.getLong(actionRequest, "groupId");
72              String articleId = ParamUtil.getString(actionRequest, "articleId");
73  
74              String targetExtension = ParamUtil.getString(
75                  actionRequest, "targetExtension");
76  
77              PortletPreferences preferences = actionRequest.getPreferences();
78  
79              String[] allowedExtensions = preferences.getValues(
80                  "extensions", null);
81  
82              String languageId = LanguageUtil.getLanguageId(actionRequest);
83  
84              ThemeDisplay themeDisplay =
85                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
86  
87              HttpServletRequest request = PortalUtil.getHttpServletRequest(
88                  actionRequest);
89              HttpServletResponse response = PortalUtil.getHttpServletResponse(
90                  actionResponse);
91  
92              getFile(
93                  groupId, articleId, targetExtension, allowedExtensions,
94                  languageId, themeDisplay, request, response);
95  
96              setForward(actionRequest, ActionConstants.COMMON_NULL);
97          }
98          catch (Exception e) {
99              PortalUtil.sendError(e, actionRequest, actionResponse);
100         }
101     }
102 
103     protected void getFile(
104             long groupId, String articleId, String targetExtension,
105             String[] allowedExtensions, String languageId,
106             ThemeDisplay themeDisplay, HttpServletRequest request,
107             HttpServletResponse response)
108         throws Exception {
109 
110         InputStream is = null;
111 
112         try {
113             JournalArticleDisplay articleDisplay =
114                 JournalContentUtil.getDisplay(
115                     groupId, articleId, null, languageId, themeDisplay);
116 
117             StringBuilder sb = new StringBuilder();
118 
119             sb.append("<html>");
120 
121             sb.append("<head>");
122             sb.append("<meta content=\"");
123             sb.append(ContentTypes.TEXT_HTML_UTF8);
124             sb.append("\" http-equiv=\"content-type\" />");
125             sb.append("<base href=\"");
126             sb.append(themeDisplay.getPortalURL());
127             sb.append("\" />");
128             sb.append("</head>");
129 
130             sb.append("<body>");
131 
132             sb.append(articleDisplay.getContent());
133 
134             int pages = articleDisplay.getNumberOfPages();
135 
136             for (int i = 2; i <= pages; i++) {
137                 articleDisplay = JournalContentUtil.getDisplay(
138                     groupId, articleId, "export", languageId, themeDisplay, i);
139 
140                 sb.append(articleDisplay.getContent());
141             }
142 
143             sb.append("</body>");
144             sb.append("</html>");
145 
146             is = new ByteArrayInputStream(
147                 sb.toString().getBytes(StringPool.UTF8));
148 
149             String title = articleDisplay.getTitle();
150             String sourceExtension = "html";
151 
152             sb = new StringBuilder();
153 
154             sb.append(title);
155             sb.append(StringPool.PERIOD);
156             sb.append(sourceExtension);
157 
158             String fileName = sb.toString();
159 
160             if (Validator.isNotNull(targetExtension) &&
161                 ArrayUtil.contains(allowedExtensions, targetExtension)) {
162 
163                 String id = DocumentConversionUtil.getTempFileId(
164                     articleDisplay.getId(), articleDisplay.getVersion());
165 
166                 InputStream convertedIS = DocumentConversionUtil.convert(
167                     id, is, sourceExtension, targetExtension);
168 
169                 if ((convertedIS != null) && (convertedIS != is)) {
170                     sb = new StringBuilder();
171 
172                     sb.append(title);
173                     sb.append(StringPool.PERIOD);
174                     sb.append(targetExtension);
175 
176                     fileName = sb.toString();
177 
178                     is = convertedIS;
179                 }
180             }
181 
182             String contentType = MimeTypesUtil.getContentType(fileName);
183 
184             ServletResponseUtil.sendFile(response, fileName, is, contentType);
185         }
186         catch (Exception e) {
187             _log.error(e, e);
188         }
189         finally {
190             ServletResponseUtil.cleanUp(is);
191         }
192     }
193 
194     protected boolean isCheckMethodOnProcessAction() {
195         return _CHECK_METHOD_ON_PROCESS_ACTION;
196     }
197 
198     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
199 
200     private static Log _log = LogFactoryUtil.getLog(ExportArticleAction.class);
201 
202 }