1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journalcontent.action;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.portlet.journal.model.JournalArticleDisplay;
26  import com.liferay.portlet.journalcontent.util.JournalContentUtil;
27  import com.liferay.util.portlet.PortletRequestUtil;
28  
29  import java.io.OutputStream;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.PortletPreferences;
35  import javax.portlet.ResourceRequest;
36  import javax.portlet.ResourceResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="WebContentAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Raymond Augé
45   */
46  public class WebContentAction extends PortletAction {
47  
48      public void processAction(
49              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
50              ActionRequest actionRequest, ActionResponse actionResponse)
51          throws Exception {
52  
53          PortletPreferences preferences = actionRequest.getPreferences();
54  
55          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
56              WebKeys.THEME_DISPLAY);
57  
58          long groupId = ParamUtil.getLong(actionRequest, "groupId");
59  
60          if (groupId < 1) {
61              groupId = GetterUtil.getLong(
62                  preferences.getValue("group-id", StringPool.BLANK));
63          }
64  
65          String articleId = ParamUtil.getString(actionRequest, "articleId");
66          String templateId = ParamUtil.getString(actionRequest, "templateId");
67  
68          if (Validator.isNull(articleId)) {
69              articleId = GetterUtil.getString(
70                  preferences.getValue("article-id", StringPool.BLANK));
71              templateId = GetterUtil.getString(
72                  preferences.getValue("template-id", StringPool.BLANK));
73          }
74  
75          String viewMode = ParamUtil.getString(actionRequest, "viewMode");
76          String languageId = LanguageUtil.getLanguageId(actionRequest);
77          int page = ParamUtil.getInteger(actionRequest, "page", 1);
78  
79          String xmlRequest = PortletRequestUtil.toXML(
80              actionRequest, actionResponse);
81  
82          if ((groupId > 0) && Validator.isNotNull(articleId)) {
83              JournalContentUtil.getDisplay(
84                  groupId, articleId, templateId, viewMode, languageId,
85                  themeDisplay, page, xmlRequest);
86          }
87      }
88  
89      public void serveResource(
90              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
91              ResourceRequest resourceRequest, ResourceResponse resourceResponse)
92          throws Exception {
93  
94          String contentType = ParamUtil.getString(
95              resourceRequest, "contentType");
96  
97          if (Validator.isNotNull(contentType)) {
98              resourceResponse.setContentType(contentType);
99          }
100 
101         if (resourceRequest.getResourceID() != null) {
102             super.serveResource(
103                 mapping, form, portletConfig, resourceRequest,
104                 resourceResponse);
105         }
106         else {
107             PortletPreferences preferences = resourceRequest.getPreferences();
108 
109             ThemeDisplay themeDisplay =
110                 (ThemeDisplay)resourceRequest.getAttribute(
111                     WebKeys.THEME_DISPLAY);
112 
113             long groupId = ParamUtil.getLong(resourceRequest, "groupId");
114 
115             if (groupId < 1) {
116                 groupId = GetterUtil.getLong(
117                     preferences.getValue("group-id", StringPool.BLANK));
118             }
119 
120             String articleId = ParamUtil.getString(
121                 resourceRequest, "articleId");
122             String templateId = ParamUtil.getString(
123                 resourceRequest, "templateId");
124 
125             if (Validator.isNull(articleId)) {
126                 articleId = GetterUtil.getString(
127                     preferences.getValue("article-id", StringPool.BLANK));
128                 templateId = GetterUtil.getString(
129                     preferences.getValue("template-id", StringPool.BLANK));
130             }
131 
132             String viewMode = ParamUtil.getString(resourceRequest, "viewMode");
133             String languageId = LanguageUtil.getLanguageId(resourceRequest);
134             int page = ParamUtil.getInteger(resourceRequest, "page", 1);
135             String xmlRequest = PortletRequestUtil.toXML(
136                 resourceRequest, resourceResponse);
137 
138             JournalArticleDisplay articleDisplay = null;
139 
140             if ((groupId > 0) && Validator.isNotNull(articleId)) {
141                 articleDisplay = JournalContentUtil.getDisplay(
142                     groupId, articleId, templateId, viewMode, languageId,
143                     themeDisplay, page, xmlRequest);
144             }
145 
146             if (articleDisplay != null) {
147                 OutputStream os = resourceResponse.getPortletOutputStream();
148 
149                 try {
150                     String content = articleDisplay.getContent();
151 
152                     byte[] bytes = content.getBytes(StringPool.UTF8);
153 
154                     os.write(bytes);
155                 }
156                 finally {
157                     os.close();
158                 }
159             }
160         }
161     }
162 
163 }