1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.model.Layout;
22  import com.liferay.portal.struts.ActionConstants;
23  import com.liferay.portal.struts.PortletAction;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.Portal;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.portlet.blogs.service.BlogsEntryServiceUtil;
29  import com.liferay.util.RSSUtil;
30  import com.liferay.util.servlet.ServletResponseUtil;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.http.HttpServletResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="RSSAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class RSSAction extends PortletAction {
49  
50      public ActionForward strutsExecute(
51              ActionMapping mapping, ActionForm form, HttpServletRequest request,
52              HttpServletResponse response)
53          throws Exception {
54  
55          try {
56              ServletResponseUtil.sendFile(
57                  request, response, null, getRSS(request),
58                  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      public void processAction(
70              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
71              ActionRequest actionRequest, ActionResponse actionResponse)
72          throws Exception {
73  
74          try {
75              HttpServletRequest request = PortalUtil.getHttpServletRequest(
76                  actionRequest);
77              HttpServletResponse response = PortalUtil.getHttpServletResponse(
78                  actionResponse);
79  
80              ServletResponseUtil.sendFile(
81                  request, response, null, getRSS(request),
82                  ContentTypes.TEXT_XML_UTF8);
83  
84              setForward(actionRequest, ActionConstants.COMMON_NULL);
85          }
86          catch (Exception e) {
87              PortalUtil.sendError(e, actionRequest, actionResponse);
88          }
89      }
90  
91      protected byte[] getRSS(HttpServletRequest request) throws Exception {
92          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
93              WebKeys.THEME_DISPLAY);
94  
95          Layout layout = themeDisplay.getLayout();
96  
97          long plid = ParamUtil.getLong(request, "p_l_id");
98          long companyId = ParamUtil.getLong(request, "companyId");
99          long groupId = ParamUtil.getLong(request, "groupId");
100         long organizationId = ParamUtil.getLong(request, "organizationId");
101         int max = ParamUtil.getInteger(
102             request, "max", SearchContainer.DEFAULT_DELTA);
103         String type = ParamUtil.getString(
104             request, "type", RSSUtil.DEFAULT_TYPE);
105         double version = ParamUtil.getDouble(
106             request, "version", RSSUtil.DEFAULT_VERSION);
107         String displayStyle = ParamUtil.getString(
108             request, "displayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
109 
110         String feedURL =
111             themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
112                 "/blogs/find_entry?";
113 
114         String entryURL = feedURL;
115 
116         String rss = StringPool.BLANK;
117 
118         if (companyId > 0) {
119             feedURL = StringPool.BLANK;
120 
121             rss = BlogsEntryServiceUtil.getCompanyEntriesRSS(
122                 companyId, max, type, version, displayStyle, feedURL, entryURL,
123                 themeDisplay);
124         }
125         else if (groupId > 0) {
126             feedURL += "p_l_id=" + plid;
127 
128             entryURL = feedURL;
129 
130             rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
131                 groupId, max, type, version, displayStyle, feedURL, entryURL,
132                 themeDisplay);
133         }
134         else if (organizationId > 0) {
135             feedURL = StringPool.BLANK;
136 
137             rss = BlogsEntryServiceUtil.getOrganizationEntriesRSS(
138                 organizationId, max, type, version, displayStyle, feedURL,
139                 entryURL, themeDisplay);
140         }
141         else if (layout != null) {
142             if (layout.hasScopeGroup()) {
143                 groupId = layout.getScopeGroup().getGroupId();
144             }
145             else {
146                 groupId = layout.getGroupId();
147             }
148 
149             feedURL =
150                 PortalUtil.getLayoutFullURL(themeDisplay) +
151                     Portal.FRIENDLY_URL_SEPARATOR + "blogs/rss";
152 
153             entryURL = feedURL;
154 
155             rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
156                 groupId, max, type, version, displayStyle, feedURL, entryURL,
157                 themeDisplay);
158         }
159 
160         return rss.getBytes(StringPool.UTF8);
161     }
162 
163     protected boolean isCheckMethodOnProcessAction() {
164         return _CHECK_METHOD_ON_PROCESS_ACTION;
165     }
166 
167     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
168 
169 }