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   */
55  public class WebContentAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          PortletPreferences preferences = actionRequest.getPreferences();
63  
64          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
65              WebKeys.THEME_DISPLAY);
66  
67          long groupId = ParamUtil.getLong(actionRequest, "groupId");
68  
69          if (groupId < 1) {
70              groupId = GetterUtil.getLong(
71                  preferences.getValue("group-id", StringPool.BLANK));
72          }
73  
74          String articleId = ParamUtil.getString(actionRequest, "articleId");
75          String templateId = ParamUtil.getString(actionRequest, "templateId");
76  
77          if (Validator.isNull(articleId)) {
78              articleId = GetterUtil.getString(
79                  preferences.getValue("article-id", StringPool.BLANK));
80              templateId = GetterUtil.getString(
81                  preferences.getValue("template-id", StringPool.BLANK));
82          }
83  
84          String viewMode = ParamUtil.getString(actionRequest, "viewMode");
85          String languageId = LanguageUtil.getLanguageId(actionRequest);
86          int page = ParamUtil.getInteger(actionRequest, "page", 1);
87  
88          String xmlRequest = PortletRequestUtil.toXML(
89              actionRequest, actionResponse);
90  
91          if ((groupId > 0) && Validator.isNotNull(articleId)) {
92              JournalContentUtil.getDisplay(
93                  groupId, articleId, templateId, viewMode, languageId,
94                  themeDisplay, page, xmlRequest);
95          }
96      }
97  
98      public void serveResource(
99              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
100             ResourceRequest resourceRequest, ResourceResponse resourceResponse)
101         throws Exception {
102 
103         String contentType = ParamUtil.getString(
104             resourceRequest, "contentType");
105 
106         if (Validator.isNotNull(contentType)) {
107             resourceResponse.setContentType(contentType);
108         }
109 
110         if (resourceRequest.getResourceID() != null) {
111             super.serveResource(
112                 mapping, form, portletConfig, resourceRequest,
113                 resourceResponse);
114         }
115         else {
116             PortletPreferences preferences = resourceRequest.getPreferences();
117 
118             ThemeDisplay themeDisplay =
119                 (ThemeDisplay)resourceRequest.getAttribute(
120                     WebKeys.THEME_DISPLAY);
121 
122             long groupId = ParamUtil.getLong(resourceRequest, "groupId");
123 
124             if (groupId < 1) {
125                 groupId = GetterUtil.getLong(
126                     preferences.getValue("group-id", StringPool.BLANK));
127             }
128 
129             String articleId = ParamUtil.getString(
130                 resourceRequest, "articleId");
131             String templateId = ParamUtil.getString(
132                 resourceRequest, "templateId");
133 
134             if (Validator.isNull(articleId)) {
135                 articleId = GetterUtil.getString(
136                     preferences.getValue("article-id", StringPool.BLANK));
137                 templateId = GetterUtil.getString(
138                     preferences.getValue("template-id", StringPool.BLANK));
139             }
140 
141             String viewMode = ParamUtil.getString(resourceRequest, "viewMode");
142             String languageId = LanguageUtil.getLanguageId(resourceRequest);
143             int page = ParamUtil.getInteger(resourceRequest, "page", 1);
144             String xmlRequest = PortletRequestUtil.toXML(
145                 resourceRequest, resourceResponse);
146 
147             JournalArticleDisplay articleDisplay = null;
148 
149             if ((groupId > 0) && Validator.isNotNull(articleId)) {
150                 articleDisplay = JournalContentUtil.getDisplay(
151                     groupId, articleId, templateId, viewMode, languageId,
152                     themeDisplay, page, xmlRequest);
153             }
154 
155             if (articleDisplay != null) {
156                 OutputStream os = resourceResponse.getPortletOutputStream();
157 
158                 try {
159                     String content = articleDisplay.getContent();
160 
161                     byte[] bytes = content.getBytes(StringPool.UTF8);
162 
163                     os.write(bytes);
164                 }
165                 finally {
166                     os.close();
167                 }
168             }
169         }
170     }
171 
172 }