001    /**
002     * Copyright (c) 2000-2013 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.activities.action;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.Portal;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.social.model.SocialActivity;
031    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
032    import com.liferay.portlet.social.service.SocialActivityInterpreterLocalServiceUtil;
033    import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
034    import com.liferay.util.RSSUtil;
035    
036    import com.sun.syndication.feed.synd.SyndContent;
037    import com.sun.syndication.feed.synd.SyndContentImpl;
038    import com.sun.syndication.feed.synd.SyndEntry;
039    import com.sun.syndication.feed.synd.SyndEntryImpl;
040    import com.sun.syndication.feed.synd.SyndFeed;
041    import com.sun.syndication.feed.synd.SyndFeedImpl;
042    import com.sun.syndication.feed.synd.SyndLink;
043    import com.sun.syndication.feed.synd.SyndLinkImpl;
044    import com.sun.syndication.io.FeedException;
045    
046    import java.io.OutputStream;
047    
048    import java.util.ArrayList;
049    import java.util.Collections;
050    import java.util.Date;
051    import java.util.List;
052    
053    import javax.portlet.PortletConfig;
054    import javax.portlet.PortletRequest;
055    import javax.portlet.ResourceRequest;
056    import javax.portlet.ResourceResponse;
057    
058    import org.apache.struts.action.ActionForm;
059    import org.apache.struts.action.ActionMapping;
060    
061    /**
062     * @author Brian Wing Shun Chan
063     * @author Vilmos Papp
064     */
065    public class RSSAction extends PortletAction {
066    
067            @Override
068            public void serveResource(
069                            ActionMapping actionMapping, ActionForm actionForm,
070                            PortletConfig portletConfig, ResourceRequest resourceRequest,
071                            ResourceResponse resourceResponse)
072                    throws Exception {
073    
074                    resourceResponse.setContentType(ContentTypes.TEXT_XML_UTF8);
075    
076                    OutputStream outputStream = resourceResponse.getPortletOutputStream();
077    
078                    try {
079                            byte[] bytes = getRSS(resourceRequest);
080    
081                            outputStream.write(bytes);
082                    }
083                    finally {
084                            outputStream.close();
085                    }
086            }
087    
088            protected List<SocialActivity> getActivities(PortletRequest portletRequest)
089                    throws Exception {
090    
091                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
092                            WebKeys.THEME_DISPLAY);
093    
094                    Group group = GroupLocalServiceUtil.getGroup(
095                            themeDisplay.getScopeGroupId());
096    
097                    int start = 0;
098                    int end = 10;
099    
100                    if (group.isOrganization()) {
101                            return SocialActivityLocalServiceUtil.getOrganizationActivities(
102                                    group.getOrganizationId(), start, end);
103                    }
104                    else if (group.isRegularSite()) {
105                            return SocialActivityLocalServiceUtil.getGroupActivities(
106                                    group.getGroupId(), start, end);
107                    }
108                    else if (group.isUser()) {
109                            return SocialActivityLocalServiceUtil.getUserActivities(
110                                    group.getClassPK(), start, end);
111                    }
112    
113                    return Collections.emptyList();
114            }
115    
116            protected byte[] getRSS(PortletRequest portletRequest) throws Exception {
117                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
118                            WebKeys.THEME_DISPLAY);
119    
120                    SyndFeed syndFeed = new SyndFeedImpl();
121    
122                    String description = ParamUtil.getString(portletRequest, "feedTitle");
123    
124                    syndFeed.setDescription(description);
125    
126                    List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
127    
128                    syndFeed.setEntries(syndEntries);
129    
130                    List<SocialActivity> activities = getActivities(portletRequest);
131    
132                    for (SocialActivity activity : activities) {
133                            SocialActivityFeedEntry activityFeedEntry =
134                                    SocialActivityInterpreterLocalServiceUtil.interpret(
135                                            activity, themeDisplay);
136    
137                            if (activityFeedEntry == null) {
138                                    continue;
139                            }
140    
141                            SyndEntry syndEntry = new SyndEntryImpl();
142    
143                            SyndContent syndContent = new SyndContentImpl();
144    
145                            syndContent.setType(RSSUtil.FEED_TYPE_DEFAULT);
146                            syndContent.setValue(activityFeedEntry.getBody());
147    
148                            syndEntry.setDescription(syndContent);
149    
150                            if (Validator.isNotNull(activityFeedEntry.getLink())) {
151                                    syndEntry.setLink(activityFeedEntry.getLink());
152                            }
153    
154                            syndEntry.setPublishedDate(new Date(activity.getCreateDate()));
155                            syndEntry.setTitle(
156                                    HtmlUtil.extractText(activityFeedEntry.getTitle()));
157    
158                            syndEntries.add(syndEntry);
159                    }
160    
161                    syndFeed.setFeedType(RSSUtil.FEED_TYPE_DEFAULT);
162    
163                    List<SyndLink> syndLinks = new ArrayList<SyndLink>();
164    
165                    syndFeed.setLinks(syndLinks);
166    
167                    SyndLink selfSyndLink = new SyndLinkImpl();
168    
169                    syndLinks.add(selfSyndLink);
170    
171                    String link =
172                            PortalUtil.getLayoutFullURL(themeDisplay) +
173                                    Portal.FRIENDLY_URL_SEPARATOR + "activities/rss";
174    
175                    selfSyndLink.setHref(link);
176    
177                    selfSyndLink.setRel("self");
178    
179                    SyndLink alternateSyndLink = new SyndLinkImpl();
180    
181                    syndLinks.add(alternateSyndLink);
182    
183                    alternateSyndLink.setHref(PortalUtil.getLayoutFullURL(themeDisplay));
184                    alternateSyndLink.setRel("alternate");
185    
186                    syndFeed.setPublishedDate(new Date());
187                    syndFeed.setTitle(description);
188                    syndFeed.setUri(link);
189    
190                    String rss = StringPool.BLANK;
191    
192                    try {
193                            rss = RSSUtil.export(syndFeed);
194                    }
195                    catch (FeedException fe) {
196                            throw new SystemException(fe);
197                    }
198    
199                    return rss.getBytes(StringPool.UTF8);
200            }
201    
202            @Override
203            protected boolean isCheckMethodOnProcessAction() {
204                    return _CHECK_METHOD_ON_PROCESS_ACTION;
205            }
206    
207            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
208    
209    }