001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.assetpublisher.action;
016    
017    import com.liferay.portal.kernel.util.ContentTypes;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.struts.PortletAction;
023    import com.liferay.portal.theme.PortletDisplay;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.Portal;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.asset.service.AssetEntryServiceUtil;
029    import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
030    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
031    import com.liferay.util.RSSUtil;
032    
033    import java.io.OutputStream;
034    
035    import javax.portlet.PortletConfig;
036    import javax.portlet.PortletPreferences;
037    import javax.portlet.PortletRequest;
038    import javax.portlet.ResourceRequest;
039    import javax.portlet.ResourceResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     * @author Julio Camarero
047     */
048    public class RSSAction extends PortletAction {
049    
050            public void serveResource(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
053                    throws Exception {
054    
055                    resourceResponse.setContentType(ContentTypes.TEXT_XML_UTF8);
056    
057                    OutputStream os = resourceResponse.getPortletOutputStream();
058    
059                    try {
060                            os.write(getRSS(resourceRequest));
061                    }
062                    finally {
063                            os.close();
064                    }
065            }
066    
067            protected String getEntryURL(PortletRequest portletRequest)
068                    throws Exception {
069    
070                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
071                            WebKeys.THEME_DISPLAY);
072    
073                    Layout layout = themeDisplay.getLayout();
074    
075                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
076    
077                    StringBundler sb = new StringBundler(5);
078    
079                    sb.append(PortalUtil.getLayoutURL(layout, themeDisplay));
080                    sb.append(Portal.FRIENDLY_URL_SEPARATOR);
081                    sb.append("asset_publisher/");
082                    sb.append(portletDisplay.getInstanceId());
083                    sb.append(StringPool.SLASH);
084    
085                    return sb.toString();
086            }
087    
088            protected String getFeedURL(PortletRequest portletRequest)
089                    throws Exception {
090    
091                    String feedURL = getEntryURL(portletRequest);
092    
093                    return feedURL.concat("rss");
094            }
095    
096            protected byte[] getRSS(PortletRequest portletRequest) throws Exception {
097                    PortletPreferences preferences = portletRequest.getPreferences();
098    
099                    String selectionStyle = preferences.getValue(
100                            "selection-style", "dynamic");
101    
102                    if (!selectionStyle.equals("dynamic")) {
103                            return new byte[0];
104                    }
105    
106                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
107                            WebKeys.THEME_DISPLAY);
108    
109                    long[] groupIds = AssetPublisherUtil.getGroupIds(
110                            preferences, themeDisplay.getScopeGroupId(),
111                            themeDisplay.getLayout());
112    
113                    boolean excludeZeroViewCount = GetterUtil.getBoolean(
114                            preferences.getValue("exclude-zero-view-count", "0"));
115    
116                    int rssDelta = GetterUtil.getInteger(
117                            preferences.getValue("rss-delta", "20"));
118                    String rssDisplayStyle = preferences.getValue(
119                            "rss-display-style", RSSUtil.DISPLAY_STYLE_ABSTRACT);
120                    String rssFormat = preferences.getValue("rss-format", "atom10");
121                    String rssName = preferences.getValue("rss-name", null);
122    
123                    String rssFormatType = RSSUtil.getFormatType(rssFormat);
124                    double rssFormatVersion = RSSUtil.getFormatVersion(rssFormat);
125    
126                    AssetEntryQuery assetEntryQuery =
127                            AssetPublisherUtil.getAssetEntryQuery(
128                                    preferences, themeDisplay.getScopeGroupId());
129    
130                    assetEntryQuery.setEnd(rssDelta);
131                    assetEntryQuery.setExcludeZeroViewCount(excludeZeroViewCount);
132                    assetEntryQuery.setGroupIds(groupIds);
133                    assetEntryQuery.setStart(0);
134    
135                    String rss = AssetEntryServiceUtil.getEntriesRSS(
136                            assetEntryQuery, rssName, rssFormatType, rssFormatVersion,
137                            rssDisplayStyle, getFeedURL(portletRequest),
138                            getEntryURL(portletRequest));
139    
140                    return rss.getBytes(StringPool.UTF8);
141            }
142    
143    }