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.blogs.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.model.Layout;
23  import com.liferay.portal.struts.ActionConstants;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.Portal;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
30  import com.liferay.util.RSSUtil;
31  import com.liferay.util.servlet.ServletResponseUtil;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
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 PortletAction {
50  
51      public ActionForward strutsExecute(
52              ActionMapping mapping, ActionForm form, HttpServletRequest request,
53              HttpServletResponse response)
54          throws Exception {
55  
56          try {
57              ServletResponseUtil.sendFile(
58                  request, response, null, getRSS(request),
59                  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      public void processAction(
71              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72              ActionRequest actionRequest, ActionResponse actionResponse)
73          throws Exception {
74  
75          try {
76              HttpServletRequest request = PortalUtil.getHttpServletRequest(
77                  actionRequest);
78              HttpServletResponse response = PortalUtil.getHttpServletResponse(
79                  actionResponse);
80  
81              ServletResponseUtil.sendFile(
82                  request, response, null, getRSS(request),
83                  ContentTypes.TEXT_XML_UTF8);
84  
85              setForward(actionRequest, ActionConstants.COMMON_NULL);
86          }
87          catch (Exception e) {
88              PortalUtil.sendError(e, actionRequest, actionResponse);
89          }
90      }
91  
92      protected byte[] getRSS(HttpServletRequest request) throws Exception {
93          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
94              WebKeys.THEME_DISPLAY);
95  
96          Layout layout = themeDisplay.getLayout();
97  
98          long plid = ParamUtil.getLong(request, "p_l_id");
99          long companyId = ParamUtil.getLong(request, "companyId");
100         long groupId = ParamUtil.getLong(request, "groupId");
101         long organizationId = ParamUtil.getLong(request, "organizationId");
102         int status = WorkflowConstants.STATUS_APPROVED;
103         int max = ParamUtil.getInteger(
104             request, "max", SearchContainer.DEFAULT_DELTA);
105         String type = ParamUtil.getString(
106             request, "type", RSSUtil.DEFAULT_TYPE);
107         double version = ParamUtil.getDouble(
108             request, "version", RSSUtil.DEFAULT_VERSION);
109         String displayStyle = ParamUtil.getString(
110             request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
111 
112         String feedURL =
113             themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
114                 "/blogs/find_entry?";
115 
116         String entryURL = feedURL;
117 
118         String rss = StringPool.BLANK;
119 
120         if (companyId > 0) {
121             feedURL = StringPool.BLANK;
122 
123             rss = BlogsEntryServiceUtil.getCompanyEntriesRSS(
124                 companyId, status, max, type, version, displayStyle, feedURL,
125                 entryURL, themeDisplay);
126         }
127         else if (groupId > 0) {
128             feedURL += "p_l_id=" + plid;
129 
130             entryURL = feedURL;
131 
132             rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
133                 groupId, status, max, type, version, displayStyle, feedURL,
134                 entryURL, themeDisplay);
135         }
136         else if (organizationId > 0) {
137             feedURL = StringPool.BLANK;
138 
139             rss = BlogsEntryServiceUtil.getOrganizationEntriesRSS(
140                 organizationId, status, max, type, version, displayStyle,
141                 feedURL, entryURL, themeDisplay);
142         }
143         else if (layout != null) {
144             if (layout.hasScopeGroup()) {
145                 groupId = layout.getScopeGroup().getGroupId();
146             }
147             else {
148                 groupId = layout.getGroupId();
149             }
150 
151             feedURL =
152                 PortalUtil.getLayoutFullURL(themeDisplay) +
153                     Portal.FRIENDLY_URL_SEPARATOR + "blogs/rss";
154 
155             entryURL = feedURL;
156 
157             rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
158                 groupId, status, max, type, version, displayStyle, feedURL,
159                 entryURL, themeDisplay);
160         }
161 
162         return rss.getBytes(StringPool.UTF8);
163     }
164 
165     protected boolean isCheckMethodOnProcessAction() {
166         return _CHECK_METHOD_ON_PROCESS_ACTION;
167     }
168 
169     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
170 
171 }