1   /**
2    * Copyright (c) 2000-2007 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.NoSuchCompanyException;
26  import com.liferay.portal.NoSuchGroupException;
27  import com.liferay.portal.NoSuchOrganizationException;
28  import com.liferay.portal.kernel.dao.search.SearchContainer;
29  import com.liferay.portal.kernel.util.ContentTypes;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.struts.ActionConstants;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.portlet.ActionRequestImpl;
39  import com.liferay.portlet.ActionResponseImpl;
40  import com.liferay.portlet.blogs.NoSuchCategoryException;
41  import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
42  import com.liferay.util.RSSUtil;
43  import com.liferay.util.servlet.ServletResponseUtil;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  import javax.servlet.jsp.PageContext;
52  
53  import org.apache.commons.logging.Log;
54  import org.apache.commons.logging.LogFactory;
55  import org.apache.struts.action.ActionForm;
56  import org.apache.struts.action.ActionForward;
57  import org.apache.struts.action.ActionMapping;
58  
59  /**
60   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Brian Wing Shun Chan
63   *
64   */
65  public class RSSAction extends PortletAction {
66  
67      public ActionForward strutsExecute(
68              ActionMapping mapping, ActionForm form, HttpServletRequest req,
69              HttpServletResponse res)
70          throws Exception {
71  
72          try {
73              ServletResponseUtil.sendFile(
74                  res, null, getRSS(req), ContentTypes.TEXT_XML_UTF8);
75  
76              return null;
77          }
78          catch (Exception e) {
79              req.setAttribute(PageContext.EXCEPTION, e);
80  
81              return mapping.findForward(ActionConstants.COMMON_ERROR);
82          }
83      }
84  
85      public void processAction(
86              ActionMapping mapping, ActionForm form, PortletConfig config,
87              ActionRequest req, ActionResponse res)
88          throws Exception {
89  
90          HttpServletRequest httpReq =
91              ((ActionRequestImpl)req).getHttpServletRequest();
92          HttpServletResponse httpRes =
93              ((ActionResponseImpl)res).getHttpServletResponse();
94  
95          ServletResponseUtil.sendFile(
96              httpRes, null, getRSS(httpReq), ContentTypes.TEXT_XML_UTF8);
97  
98          setForward(req, ActionConstants.COMMON_NULL);
99      }
100 
101     protected byte[] getRSS(HttpServletRequest req) throws Exception {
102         ThemeDisplay themeDisplay =
103             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
104 
105         Layout layout = themeDisplay.getLayout();
106 
107         long plid = ParamUtil.getLong(req, "p_l_id");
108         long companyId = ParamUtil.getLong(req, "companyId");
109         long groupId = ParamUtil.getLong(req, "groupId");
110         long organizationId = ParamUtil.getLong(req, "organizationId");
111         long categoryId = ParamUtil.getLong(req, "categoryId");
112         int max = ParamUtil.getInteger(
113             req, "max", SearchContainer.DEFAULT_DELTA);
114         String type = ParamUtil.getString(req, "type", RSSUtil.DEFAULT_TYPE);
115         double version = ParamUtil.getDouble(
116             req, "version", RSSUtil.DEFAULT_VERSION);
117 
118         String feedURL =
119             themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
120                 "/blogs/find_entry?";
121 
122         String entryURL = feedURL;
123 
124         String rss = StringPool.BLANK;
125 
126         if (companyId > 0) {
127             feedURL = StringPool.BLANK;
128 
129             try {
130                 rss = BlogsEntryServiceUtil.getCompanyEntriesRSS(
131                     companyId, max, type, version, feedURL, entryURL);
132             }
133             catch (NoSuchCompanyException nsce) {
134                 if (_log.isWarnEnabled()) {
135                     _log.warn(nsce);
136                 }
137             }
138         }
139         else if (groupId > 0) {
140             feedURL += "p_l_id=" + plid;
141 
142             entryURL = feedURL;
143 
144             try {
145                 rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
146                     groupId, max, type, version, feedURL, entryURL);
147             }
148             catch (NoSuchGroupException nsge) {
149                 if (_log.isWarnEnabled()) {
150                     _log.warn(nsge);
151                 }
152             }
153         }
154         else if (organizationId > 0) {
155             feedURL = StringPool.BLANK;
156 
157             try {
158                 rss = BlogsEntryServiceUtil.getOrganizationEntriesRSS(
159                     organizationId, max, type, version, feedURL, entryURL);
160             }
161             catch (NoSuchOrganizationException nsge) {
162                 if (_log.isWarnEnabled()) {
163                     _log.warn(nsge);
164                 }
165             }
166         }
167         else if (categoryId > 0) {
168             feedURL += "p_l_id=" + plid;
169 
170             entryURL = feedURL;
171 
172             try {
173                 rss = BlogsEntryServiceUtil.getCategoryBlogsRSS(
174                     categoryId, max, type, version, feedURL, entryURL);
175             }
176             catch (NoSuchCategoryException nsce) {
177                 if (_log.isWarnEnabled()) {
178                     _log.warn(nsce);
179                 }
180             }
181         }
182         else if (layout != null) {
183             groupId = layout.getGroupId();
184 
185             feedURL =
186                 themeDisplay.getURLPortal() +
187                     PortalUtil.getLayoutURL(themeDisplay) + "/blogs/rss";
188 
189             entryURL = feedURL;
190 
191             try {
192                 rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
193                     groupId, max, type, version, feedURL, entryURL);
194             }
195             catch (NoSuchGroupException nsge) {
196                 if (_log.isWarnEnabled()) {
197                     _log.warn(nsge);
198                 }
199             }
200         }
201 
202         return rss.getBytes(StringPool.UTF8);
203     }
204 
205     private static Log _log = LogFactory.getLog(RSSAction.class);
206 
207 }