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.assetpublisher.action;
16  
17  import com.liferay.portal.kernel.util.ContentTypes;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.theme.PortletDisplay;
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.asset.service.AssetEntryServiceUtil;
29  import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
30  import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
31  import com.liferay.util.RSSUtil;
32  
33  import java.io.OutputStream;
34  
35  import javax.portlet.PortletConfig;
36  import javax.portlet.PortletPreferences;
37  import javax.portlet.PortletRequest;
38  import javax.portlet.ResourceRequest;
39  import javax.portlet.ResourceResponse;
40  
41  import org.apache.struts.action.ActionForm;
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   * @author Julio Camarero
49   */
50  public class RSSAction extends PortletAction {
51  
52      public void serveResource(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              ResourceRequest resourceRequest, ResourceResponse resourceResponse)
55          throws Exception {
56  
57          resourceResponse.setContentType(ContentTypes.TEXT_XML_UTF8);
58  
59          OutputStream os = resourceResponse.getPortletOutputStream();
60  
61          try {
62              os.write(getRSS(resourceRequest));
63          }
64          finally {
65              os.close();
66          }
67      }
68  
69      protected String getEntryURL(PortletRequest portletRequest)
70          throws Exception {
71  
72          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
73              WebKeys.THEME_DISPLAY);
74  
75          Layout layout = themeDisplay.getLayout();
76  
77          PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
78  
79          StringBundler sb = new StringBundler(5);
80  
81          sb.append(PortalUtil.getLayoutURL(layout, themeDisplay));
82          sb.append(Portal.FRIENDLY_URL_SEPARATOR);
83          sb.append("asset_publisher/");
84          sb.append(portletDisplay.getInstanceId());
85          sb.append(StringPool.SLASH);
86  
87          return sb.toString();
88      }
89  
90      protected String getFeedURL(PortletRequest portletRequest)
91          throws Exception {
92  
93          String feedURL = getEntryURL(portletRequest);
94  
95          return feedURL.concat("rss");
96      }
97  
98      protected byte[] getRSS(PortletRequest portletRequest) throws Exception {
99          PortletPreferences preferences = portletRequest.getPreferences();
100 
101         String selectionStyle = preferences.getValue(
102             "selection-style", "dynamic");
103 
104         if (!selectionStyle.equals("dynamic")) {
105             return new byte[0];
106         }
107 
108         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
109             WebKeys.THEME_DISPLAY);
110 
111         long[] groupIds = AssetPublisherUtil.getGroupIds(
112             preferences, themeDisplay.getScopeGroupId(),
113             themeDisplay.getLayout());
114 
115         boolean excludeZeroViewCount = GetterUtil.getBoolean(
116             preferences.getValue("exclude-zero-view-count", "0"));
117 
118         int rssDelta = GetterUtil.getInteger(
119             preferences.getValue("rss-delta", "20"));
120         String rssDisplayStyle = preferences.getValue(
121             "rss-display-style", RSSUtil.DISPLAY_STYLE_ABSTRACT);
122         String rssFormat = preferences.getValue("rss-format", "atom10");
123         String rssName = preferences.getValue("rss-name", null);
124 
125         String rssFormatType = RSSUtil.getFormatType(rssFormat);
126         double rssFormatVersion = RSSUtil.getFormatVersion(rssFormat);
127 
128         AssetEntryQuery assetEntryQuery =
129             AssetPublisherUtil.getAssetEntryQuery(
130                 preferences, themeDisplay.getScopeGroupId());
131 
132         assetEntryQuery.setEnd(rssDelta);
133         assetEntryQuery.setExcludeZeroViewCount(excludeZeroViewCount);
134         assetEntryQuery.setGroupIds(groupIds);
135         assetEntryQuery.setStart(0);
136 
137         String rss = AssetEntryServiceUtil.getEntriesRSS(
138             assetEntryQuery, rssName, rssFormatType, rssFormatVersion,
139             rssDisplayStyle, getFeedURL(portletRequest),
140             getEntryURL(portletRequest));
141 
142         return rss.getBytes(StringPool.UTF8);
143     }
144 
145 }