1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class RSSAction extends Action {
50  
51      public ActionForward execute(
52              ActionMapping mapping, ActionForm form, HttpServletRequest request,
53              HttpServletResponse response)
54          throws Exception {
55  
56          try {
57              ServletResponseUtil.sendFile(
58                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
59  
60              return null;
61          }
62          catch (Exception e) {
63              PortalUtil.sendError(e, request, response);
64  
65              return null;
66          }
67      }
68  
69      protected byte[] getRSS(HttpServletRequest request) throws Exception {
70          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
71              WebKeys.THEME_DISPLAY);
72  
73          String plid = ParamUtil.getString(request, "p_l_id");
74          long companyId = ParamUtil.getLong(request, "companyId");
75          long groupId = ParamUtil.getLong(request, "groupId");
76          long userId = ParamUtil.getLong(request, "userId");
77          long categoryId = ParamUtil.getLong(request, "categoryId");
78          long threadId = ParamUtil.getLong(request, "threadId");
79          int max = ParamUtil.getInteger(
80              request, "max", SearchContainer.DEFAULT_DELTA);
81          String type = ParamUtil.getString(
82              request, "type", RSSUtil.DEFAULT_TYPE);
83          double version = ParamUtil.getDouble(
84              request, "version", RSSUtil.DEFAULT_VERSION);
85          String displayStyle = ParamUtil.getString(
86              request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
87  
88          String entryURL =
89              themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
90                  "/message_boards/find_message?p_l_id=" + plid;
91  
92          String rss = StringPool.BLANK;
93  
94          if (companyId > 0) {
95              String feedURL = StringPool.BLANK;
96  
97              rss = MBMessageServiceUtil.getCompanyMessagesRSS(
98                  companyId, max, type, version, displayStyle, feedURL, entryURL,
99                  themeDisplay);
100         }
101         else if (groupId > 0) {
102             String feedURL =
103                 themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
104                     "/message_boards/find_recent_posts?p_l_id=" + plid;
105 
106             if (userId > 0) {
107                 rss = MBMessageServiceUtil.getGroupMessagesRSS(
108                     groupId, userId, max, type, version, displayStyle, feedURL,
109                     entryURL, themeDisplay);
110             }
111             else {
112                 rss = MBMessageServiceUtil.getGroupMessagesRSS(
113                     groupId, max, type, version, displayStyle, feedURL,
114                     entryURL, themeDisplay);
115             }
116         }
117         else if (categoryId > 0) {
118             String feedURL =
119                 themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
120                     "/message_boards/find_category?p_l_id=" + plid +
121                         "&categoryId=" + categoryId;
122 
123             rss = MBMessageServiceUtil.getCategoryMessagesRSS(
124                 categoryId, max, type, version, displayStyle, feedURL, entryURL,
125                 themeDisplay);
126         }
127         else if (threadId > 0) {
128             String feedURL =
129                 themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
130                     "/message_boards/find_thread?p_l_id=" + plid +
131                         "&threadId=" + threadId;
132 
133             rss = MBMessageServiceUtil.getThreadMessagesRSS(
134                 threadId, max, type, version, displayStyle, feedURL, entryURL,
135                 themeDisplay);
136         }
137 
138         return rss.getBytes(StringPool.UTF8);
139     }
140 
141 }