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.StatusConstants;
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                  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      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                  response, null, getRSS(request), ContentTypes.TEXT_XML_UTF8);
82  
83              setForward(actionRequest, ActionConstants.COMMON_NULL);
84          }
85          catch (Exception e) {
86              PortalUtil.sendError(e, actionRequest, actionResponse);
87          }
88      }
89  
90      protected byte[] getRSS(HttpServletRequest request) throws Exception {
91          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
92              WebKeys.THEME_DISPLAY);
93  
94          Layout layout = themeDisplay.getLayout();
95  
96          long plid = ParamUtil.getLong(request, "p_l_id");
97          long companyId = ParamUtil.getLong(request, "companyId");
98          long groupId = ParamUtil.getLong(request, "groupId");
99          long organizationId = ParamUtil.getLong(request, "organizationId");
100         int status = StatusConstants.APPROVED;
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, status, max, type, version, displayStyle, feedURL,
123                 entryURL, themeDisplay);
124         }
125         else if (groupId > 0) {
126             feedURL += "p_l_id=" + plid;
127 
128             entryURL = feedURL;
129 
130             rss = BlogsEntryServiceUtil.getGroupEntriesRSS(
131                 groupId, status, max, type, version, displayStyle, feedURL,
132                 entryURL, themeDisplay);
133         }
134         else if (organizationId > 0) {
135             feedURL = StringPool.BLANK;
136 
137             rss = BlogsEntryServiceUtil.getOrganizationEntriesRSS(
138                 organizationId, status, max, type, version, displayStyle,
139                 feedURL, 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, status, max, type, version, displayStyle, feedURL,
157                 entryURL, 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 }