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