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.blogs.action;
24  
25  import com.liferay.portal.kernel.dao.search.SearchContainer;
26  import com.liferay.portal.kernel.util.ContentTypes;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.struts.ActionConstants;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
36  import com.liferay.util.RSSUtil;
37  import com.liferay.util.servlet.ServletResponseUtil;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class RSSAction extends PortletAction {
57  
58      public ActionForward strutsExecute(
59              ActionMapping mapping, ActionForm form, HttpServletRequest request,
60              HttpServletResponse response)
61          throws Exception {
62  
63          try {
64              ServletResponseUtil.sendFile(
65                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
66  
67              return null;
68          }
69          catch (Exception e) {
70              PortalUtil.sendError(e, request, response);
71  
72              return null;
73          }
74      }
75  
76      public void processAction(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              ActionRequest actionRequest, ActionResponse actionResponse)
79          throws Exception {
80  
81          try {
82              HttpServletRequest request = PortalUtil.getHttpServletRequest(
83                  actionRequest);
84              HttpServletResponse response = PortalUtil.getHttpServletResponse(
85                  actionResponse);
86  
87              ServletResponseUtil.sendFile(
88                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
89  
90              setForward(actionRequest, ActionConstants.COMMON_NULL);
91          }
92          catch (Exception e) {
93              PortalUtil.sendError(e, actionRequest, actionResponse);
94          }
95      }
96  
97      protected byte[] getRSS(HttpServletRequest request) throws Exception {
98          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
99              WebKeys.THEME_DISPLAY);
100 
101         Layout layout = themeDisplay.getLayout();
102 
103         long plid = ParamUtil.getLong(request, "p_l_id");
104         long companyId = ParamUtil.getLong(request, "companyId");
105         long groupId = ParamUtil.getLong(request, "groupId");
106         long organizationId = ParamUtil.getLong(request, "organizationId");
107         int max = ParamUtil.getInteger(
108             request, "max", SearchContainer.DEFAULT_DELTA);
109         String type = ParamUtil.getString(
110             request, "type", RSSUtil.DEFAULT_TYPE);
111         double version = ParamUtil.getDouble(
112             request, "version", RSSUtil.DEFAULT_VERSION);
113         String displayStyle = ParamUtil.getString(
114             request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
115 
116         String feedURL =
117             themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
118                 "/blogs/find_entry?";
119 
120         String entryURL = feedURL;
121 
122         String rss = StringPool.BLANK;
123 
124         if (companyId > 0) {
125             feedURL = StringPool.BLANK;
126 
127             rss = BlogsEntryServiceUtil.getCompanyEntriesRSS(
128                 companyId, max, type, version, displayStyle, feedURL, entryURL,
129                 themeDisplay);
130         }
131         else if (groupId > 0) {
132             feedURL += "p_l_id=" + plid;
133 
134             entryURL = feedURL;
135 
136             rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
137                 groupId, max, type, version, displayStyle, feedURL, entryURL,
138                 themeDisplay);
139         }
140         else if (organizationId > 0) {
141             feedURL = StringPool.BLANK;
142 
143             rss = BlogsEntryServiceUtil.getOrganizationEntriesRSS(
144                 organizationId, max, type, version, displayStyle, feedURL,
145                 entryURL, themeDisplay);
146         }
147         else if (layout != null) {
148             groupId = layout.getGroupId();
149 
150             feedURL =
151                 themeDisplay.getURLPortal() +
152                     PortalUtil.getLayoutURL(themeDisplay) + "/-/blogs/rss";
153 
154             entryURL = feedURL;
155 
156             rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
157                 groupId, max, type, version, displayStyle, feedURL, entryURL,
158                 themeDisplay);
159         }
160 
161         return rss.getBytes(StringPool.UTF8);
162     }
163 
164     protected boolean isCheckMethodOnProcessAction() {
165         return _CHECK_METHOD_ON_PROCESS_ACTION;
166     }
167 
168     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
169 
170 }