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.io.unsync.UnsyncByteArrayInputStream;
26  import com.liferay.portal.kernel.language.LanguageUtil;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.ArrayUtil;
30  import com.liferay.portal.kernel.util.ContentTypes;
31  import com.liferay.portal.kernel.util.MimeTypesUtil;
32  import com.liferay.portal.kernel.util.ParamUtil;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.struts.ActionConstants;
36  import com.liferay.portal.struts.PortletAction;
37  import com.liferay.portal.theme.ThemeDisplay;
38  import com.liferay.portal.util.PortalUtil;
39  import com.liferay.portal.util.WebKeys;
40  import com.liferay.portlet.documentlibrary.util.DocumentConversionUtil;
41  import com.liferay.portlet.journal.model.JournalArticleDisplay;
42  import com.liferay.portlet.journalcontent.util.JournalContentUtil;
43  import com.liferay.util.servlet.ServletResponseUtil;
44  
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         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             InputStream is = new UnsyncByteArrayInputStream(
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     }
188 
189     protected boolean isCheckMethodOnProcessAction() {
190         return _CHECK_METHOD_ON_PROCESS_ACTION;
191     }
192 
193     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
194 
195     private static Log _log = LogFactoryUtil.getLog(ExportArticleAction.class);
196 
197 }