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.messageboards.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.struts.ActionConstants;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.PortletPreferencesFactoryUtil;
34  import com.liferay.portlet.messageboards.NoSuchCategoryException;
35  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
36  import com.liferay.util.RSSUtil;
37  import com.liferay.util.servlet.ServletResponseUtil;
38  
39  import javax.portlet.PortletPreferences;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  import javax.servlet.jsp.PageContext;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  import org.apache.struts.action.Action;
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class RSSAction extends Action {
59  
60      public ActionForward execute(
61              ActionMapping mapping, ActionForm form, HttpServletRequest req,
62              HttpServletResponse res)
63          throws Exception {
64  
65          try {
66              ServletResponseUtil.sendFile(
67                  res, null, getRSS(req), ContentTypes.TEXT_XML_UTF8);
68  
69              return null;
70          }
71          catch (Exception e) {
72              req.setAttribute(PageContext.EXCEPTION, e);
73  
74              return mapping.findForward(ActionConstants.COMMON_ERROR);
75          }
76      }
77  
78      protected byte[] getRSS(HttpServletRequest req) throws Exception {
79          ThemeDisplay themeDisplay =
80              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
81  
82          PortletPreferences prefs =
83              PortletPreferencesFactoryUtil.getPortletSetup(
84                  req, PortletKeys.MESSAGE_BOARDS, false, true);
85  
86          String plid = ParamUtil.getString(req, "p_l_id");
87          long companyId = ParamUtil.getLong(req, "companyId");
88          long groupId = ParamUtil.getLong(req, "groupId");
89          long categoryId = ParamUtil.getLong(req, "categoryId");
90          long threadId = ParamUtil.getLong(req, "threadId");
91          int max = ParamUtil.getInteger(
92              req, "max", SearchContainer.DEFAULT_DELTA);
93          String type = ParamUtil.getString(req, "type", RSSUtil.DEFAULT_TYPE);
94          double version = ParamUtil.getDouble(
95              req, "version", RSSUtil.DEFAULT_VERSION);
96  
97          String entryURL =
98              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
99                  "/message_boards/find_message?p_l_id=" + plid;
100 
101         String rss = StringPool.BLANK;
102 
103         if (companyId > 0) {
104             String feedURL = StringPool.BLANK;
105 
106             rss = MBMessageServiceUtil.getCompanyMessagesRSS(
107                 companyId, max, type, version, feedURL, entryURL, prefs);
108         }
109         else if (groupId > 0) {
110             String feedURL =
111                 themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
112                     "/message_boards/find_recent_posts?p_l_id=" + plid;
113 
114             rss = MBMessageServiceUtil.getGroupMessagesRSS(
115                 groupId, max, type, version, feedURL, entryURL, prefs);
116         }
117         else if (categoryId > 0) {
118             String feedURL =
119                 themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
120                     "/message_boards/find_category?p_l_id=" + plid +
121                         "&categoryId=" + categoryId;
122 
123             try {
124                 rss = MBMessageServiceUtil.getCategoryMessagesRSS(
125                     categoryId, max, type, version, feedURL, entryURL, prefs);
126             }
127             catch (NoSuchCategoryException nsce) {
128                 if (_log.isWarnEnabled()) {
129                     _log.warn(nsce);
130                 }
131             }
132         }
133         else if (threadId > 0) {
134             String feedURL =
135                 themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
136                     "/message_boards/find_thread?p_l_id=" + plid +
137                         "&threadId=" + threadId;
138 
139             rss = MBMessageServiceUtil.getThreadMessagesRSS(
140                 threadId, max, type, version, feedURL, entryURL, prefs);
141         }
142 
143         return rss.getBytes(StringPool.UTF8);
144     }
145 
146     private static Log _log = LogFactory.getLog(RSSAction.class);
147 
148 }