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.WorkflowConstants;
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                  request, response, null, getRSS(request),
52                  ContentTypes.TEXT_XML_UTF8);
53  
54              return null;
55          }
56          catch (Exception e) {
57              PortalUtil.sendError(e, request, response);
58  
59              return null;
60          }
61      }
62  
63      protected byte[] getRSS(HttpServletRequest request) throws Exception {
64          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
65              WebKeys.THEME_DISPLAY);
66  
67          String plid = ParamUtil.getString(request, "p_l_id");
68          long companyId = ParamUtil.getLong(request, "companyId");
69          long groupId = ParamUtil.getLong(request, "groupId");
70          long userId = ParamUtil.getLong(request, "userId");
71          long categoryId = ParamUtil.getLong(request, "mbCategoryId");
72          long threadId = ParamUtil.getLong(request, "threadId");
73          int max = ParamUtil.getInteger(
74              request, "max", SearchContainer.DEFAULT_DELTA);
75          String type = ParamUtil.getString(
76              request, "type", RSSUtil.DEFAULT_TYPE);
77          double version = ParamUtil.getDouble(
78              request, "version", RSSUtil.DEFAULT_VERSION);
79          String displayStyle = ParamUtil.getString(
80              request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
81  
82          String entryURL =
83              themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
84                  "/message_boards/find_message?p_l_id=" + plid;
85  
86          String rss = StringPool.BLANK;
87  
88          if (companyId > 0) {
89              String feedURL = StringPool.BLANK;
90  
91              rss = MBMessageServiceUtil.getCompanyMessagesRSS(
92                  companyId, WorkflowConstants.STATUS_APPROVED, max, type,
93                  version, displayStyle, feedURL, entryURL, themeDisplay);
94          }
95          else if (groupId > 0) {
96              String feedURL =
97                  themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
98                      "/message_boards/find_recent_posts?p_l_id=" + plid;
99  
100             if (userId > 0) {
101                 rss = MBMessageServiceUtil.getGroupMessagesRSS(
102                     groupId, userId, WorkflowConstants.STATUS_APPROVED, max,
103                     type, version, displayStyle, feedURL, entryURL,
104                     themeDisplay);
105             }
106             else {
107                 rss = MBMessageServiceUtil.getGroupMessagesRSS(
108                     groupId, WorkflowConstants.STATUS_APPROVED, max, type,
109                     version, displayStyle, feedURL, entryURL, themeDisplay);
110             }
111         }
112         else if (categoryId > 0) {
113             String feedURL =
114                 themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
115                     "/message_boards/find_category?p_l_id=" + plid +
116                         "&mbCategoryId=" + categoryId;
117 
118             rss = MBMessageServiceUtil.getCategoryMessagesRSS(
119                 groupId, categoryId, WorkflowConstants.STATUS_APPROVED, max,
120                 type, version, displayStyle, feedURL, entryURL, themeDisplay);
121         }
122         else if (threadId > 0) {
123             String feedURL =
124                 themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
125                     "/message_boards/find_thread?p_l_id=" + plid +
126                         "&threadId=" + threadId;
127 
128             rss = MBMessageServiceUtil.getThreadMessagesRSS(
129                 threadId, WorkflowConstants.STATUS_APPROVED, max, type, version,
130                 displayStyle, feedURL, entryURL, themeDisplay);
131         }
132 
133         return rss.getBytes(StringPool.UTF8);
134     }
135 
136 }