1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.action;
16  
17  import com.liferay.portal.kernel.dao.search.SearchContainer;
18  import com.liferay.portal.kernel.util.ContentTypes;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.workflow.StatusConstants;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.util.WebKeys;
25  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
26  import com.liferay.util.RSSUtil;
27  import com.liferay.util.servlet.ServletResponseUtil;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.struts.action.Action;
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class RSSAction extends Action {
43  
44      public ActionForward execute(
45              ActionMapping mapping, ActionForm form, HttpServletRequest request,
46              HttpServletResponse response)
47          throws Exception {
48  
49          try {
50              ServletResponseUtil.sendFile(
51                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
52  
53              return null;
54          }
55          catch (Exception e) {
56              PortalUtil.sendError(e, request, response);
57  
58              return null;
59          }
60      }
61  
62      protected byte[] getRSS(HttpServletRequest request) throws Exception {
63          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
64              WebKeys.THEME_DISPLAY);
65  
66          String plid = ParamUtil.getString(request, "p_l_id");
67          long companyId = ParamUtil.getLong(request, "companyId");
68          long groupId = ParamUtil.getLong(request, "groupId");
69          long userId = ParamUtil.getLong(request, "userId");
70          long categoryId = ParamUtil.getLong(request, "mbCategoryId");
71          long threadId = ParamUtil.getLong(request, "threadId");
72          int max = ParamUtil.getInteger(
73              request, "max", SearchContainer.DEFAULT_DELTA);
74          String type = ParamUtil.getString(
75              request, "type", RSSUtil.DEFAULT_TYPE);
76          double version = ParamUtil.getDouble(
77              request, "version", RSSUtil.DEFAULT_VERSION);
78          String displayStyle = ParamUtil.getString(
79              request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
80  
81          String entryURL =
82              themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
83                  "/message_boards/find_message?p_l_id=" + plid;
84  
85          String rss = StringPool.BLANK;
86  
87          if (companyId > 0) {
88              String feedURL = StringPool.BLANK;
89  
90              rss = MBMessageServiceUtil.getCompanyMessagesRSS(
91                  companyId, StatusConstants.APPROVED, max, type, version,
92                  displayStyle, feedURL, entryURL, themeDisplay);
93          }
94          else if (groupId > 0) {
95              String feedURL =
96                  themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
97                      "/message_boards/find_recent_posts?p_l_id=" + plid;
98  
99              if (userId > 0) {
100                 rss = MBMessageServiceUtil.getGroupMessagesRSS(
101                     groupId, userId, StatusConstants.APPROVED, max, type,
102                     version, displayStyle, feedURL, entryURL, themeDisplay);
103             }
104             else {
105                 rss = MBMessageServiceUtil.getGroupMessagesRSS(
106                     groupId, StatusConstants.APPROVED, max, type, version,
107                     displayStyle, feedURL, entryURL, themeDisplay);
108             }
109         }
110         else if (categoryId > 0) {
111             String feedURL =
112                 themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
113                     "/message_boards/find_category?p_l_id=" + plid +
114                         "&mbCategoryId=" + categoryId;
115 
116             rss = MBMessageServiceUtil.getCategoryMessagesRSS(
117                 groupId, categoryId, StatusConstants.APPROVED, max, type,
118                 version, displayStyle, feedURL, entryURL, themeDisplay);
119         }
120         else if (threadId > 0) {
121             String feedURL =
122                 themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
123                     "/message_boards/find_thread?p_l_id=" + plid +
124                         "&threadId=" + threadId;
125 
126             rss = MBMessageServiceUtil.getThreadMessagesRSS(
127                 threadId, StatusConstants.APPROVED, max, type, version,
128                 displayStyle, feedURL, entryURL, themeDisplay);
129         }
130 
131         return rss.getBytes(StringPool.UTF8);
132     }
133 
134 }