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.util.GetterUtil;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.journal.model.JournalArticleDisplay;
34  import com.liferay.portlet.journalcontent.util.JournalContentUtil;
35  import com.liferay.util.portlet.PortletRequestUtil;
36  
37  import java.io.OutputStream;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.PortletPreferences;
43  import javax.portlet.ResourceRequest;
44  import javax.portlet.ResourceResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="WebContentAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Raymond Augé
53   */
54  public class WebContentAction extends PortletAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          PortletPreferences preferences = actionRequest.getPreferences();
62  
63          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
64              WebKeys.THEME_DISPLAY);
65  
66          long groupId = ParamUtil.getLong(actionRequest, "groupId");
67  
68          if (groupId < 1) {
69              groupId = GetterUtil.getLong(
70                  preferences.getValue("group-id", StringPool.BLANK));
71          }
72  
73          String articleId = ParamUtil.getString(actionRequest, "articleId");
74          String templateId = ParamUtil.getString(actionRequest, "templateId");
75  
76          if (Validator.isNull(articleId)) {
77              articleId = GetterUtil.getString(
78                  preferences.getValue("article-id", StringPool.BLANK));
79              templateId = GetterUtil.getString(
80                  preferences.getValue("template-id", StringPool.BLANK));
81          }
82  
83          String viewMode = ParamUtil.getString(actionRequest, "viewMode");
84          String languageId = LanguageUtil.getLanguageId(actionRequest);
85          int page = ParamUtil.getInteger(actionRequest, "page", 1);
86  
87          String xmlRequest = PortletRequestUtil.toXML(
88              actionRequest, actionResponse);
89  
90          if ((groupId > 0) && Validator.isNotNull(articleId)) {
91              JournalContentUtil.getDisplay(
92                  groupId, articleId, templateId, viewMode, languageId,
93                  themeDisplay, page, xmlRequest);
94          }
95      }
96  
97      public void serveResource(
98              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
99              ResourceRequest resourceRequest, ResourceResponse resourceResponse)
100         throws Exception {
101 
102         String contentType = ParamUtil.getString(
103             resourceRequest, "contentType");
104 
105         if (Validator.isNotNull(contentType)) {
106             resourceResponse.setContentType(contentType);
107         }
108 
109         if (resourceRequest.getResourceID() != null) {
110             super.serveResource(
111                 mapping, form, portletConfig, resourceRequest,
112                 resourceResponse);
113         }
114         else {
115             PortletPreferences preferences = resourceRequest.getPreferences();
116 
117             ThemeDisplay themeDisplay =
118                 (ThemeDisplay)resourceRequest.getAttribute(
119                     WebKeys.THEME_DISPLAY);
120 
121             long groupId = ParamUtil.getLong(resourceRequest, "groupId");
122 
123             if (groupId < 1) {
124                 groupId = GetterUtil.getLong(
125                     preferences.getValue("group-id", StringPool.BLANK));
126             }
127 
128             String articleId = ParamUtil.getString(
129                 resourceRequest, "articleId");
130             String templateId = ParamUtil.getString(
131                 resourceRequest, "templateId");
132 
133             if (Validator.isNull(articleId)) {
134                 articleId = GetterUtil.getString(
135                     preferences.getValue("article-id", StringPool.BLANK));
136                 templateId = GetterUtil.getString(
137                     preferences.getValue("template-id", StringPool.BLANK));
138             }
139 
140             String viewMode = ParamUtil.getString(resourceRequest, "viewMode");
141             String languageId = LanguageUtil.getLanguageId(resourceRequest);
142             int page = ParamUtil.getInteger(resourceRequest, "page", 1);
143             String xmlRequest = PortletRequestUtil.toXML(
144                 resourceRequest, resourceResponse);
145 
146             JournalArticleDisplay articleDisplay = null;
147 
148             if ((groupId > 0) && Validator.isNotNull(articleId)) {
149                 articleDisplay = JournalContentUtil.getDisplay(
150                     groupId, articleId, templateId, viewMode, languageId,
151                     themeDisplay, page, xmlRequest);
152             }
153 
154             if (articleDisplay != null) {
155                 OutputStream os = resourceResponse.getPortletOutputStream();
156 
157                 try {
158                     String content = articleDisplay.getContent();
159 
160                     byte[] bytes = content.getBytes(StringPool.UTF8);
161 
162                     os.write(bytes);
163                 }
164                 finally {
165                     os.close();
166                 }
167             }
168         }
169     }
170 
171 }