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