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