001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.io.FeedException;
043    
044    import edu.emory.mathcs.backport.java.util.Collections;
045    
046    import java.io.OutputStream;
047    
048    import java.util.ArrayList;
049    import java.util.Date;
050    import java.util.List;
051    
052    import javax.portlet.PortletConfig;
053    import javax.portlet.PortletRequest;
054    import javax.portlet.ResourceRequest;
055    import javax.portlet.ResourceResponse;
056    
057    import org.apache.struts.action.ActionForm;
058    import org.apache.struts.action.ActionMapping;
059    
060    /**
061     * @author Brian Wing Shun Chan
062     * @author Vilmos Papp
063     */
064    public class RSSAction extends PortletAction {
065    
066            @Override
067            public void serveResource(
068                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
069                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
070                    throws Exception {
071    
072                    resourceResponse.setContentType(ContentTypes.TEXT_XML_UTF8);
073    
074                    OutputStream outputStream = resourceResponse.getPortletOutputStream();
075    
076                    try {
077                            byte[] bytes = getRSS(resourceRequest);
078    
079                            outputStream.write(bytes);
080                    }
081                    finally {
082                            outputStream.close();
083                    }
084            }
085    
086            protected List<SocialActivity> getActivities(
087                            PortletRequest portletRequest)
088                    throws Exception {
089    
090                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
091                            WebKeys.THEME_DISPLAY);
092    
093                    Group group = GroupLocalServiceUtil.getGroup(
094                            themeDisplay.getScopeGroupId());
095    
096                    int start = 0;
097                    int end = 10;
098    
099                    if (group.isOrganization()) {
100                            return SocialActivityLocalServiceUtil.getOrganizationActivities(
101                                    group.getOrganizationId(), start, end);
102                    }
103                    else if (group.isRegularSite()) {
104                            return SocialActivityLocalServiceUtil.getGroupActivities(
105                                    group.getGroupId(), start, end);
106                    }
107                    else if (group.isUser()) {
108                            return SocialActivityLocalServiceUtil.getUserActivities(
109                                    group.getClassPK(), start, end);
110                    }
111    
112                    return Collections.emptyList();
113            }
114    
115            protected byte[] getRSS(PortletRequest portletRequest) throws Exception {
116                    ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
117                            WebKeys.THEME_DISPLAY);
118    
119                    SyndFeed syndFeed = new SyndFeedImpl();
120    
121                    String feedTitle = ParamUtil.getString(portletRequest, "feedTitle");
122    
123                    syndFeed.setDescription(feedTitle);
124    
125                    syndFeed.setFeedType(RSSUtil.FEED_TYPE_DEFAULT);
126    
127                    String feedLink = PortalUtil.getLayoutFullURL(themeDisplay) +
128                            Portal.FRIENDLY_URL_SEPARATOR + "activities/rss";
129    
130                    syndFeed.setLink(feedLink);
131    
132                    syndFeed.setTitle(feedTitle);
133    
134                    List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
135    
136                    syndFeed.setEntries(syndEntries);
137    
138                    List<SocialActivity> activities = getActivities(portletRequest);
139    
140                    for (SocialActivity activity : activities) {
141                            SocialActivityFeedEntry activityFeedEntry =
142                                    SocialActivityInterpreterLocalServiceUtil.interpret(
143                                            activity, themeDisplay);
144    
145                            if (activityFeedEntry == null) {
146                                    continue;
147                            }
148    
149                            SyndEntry syndEntry = new SyndEntryImpl();
150    
151                            SyndContent syndContent = new SyndContentImpl();
152    
153                            syndContent.setType(RSSUtil.FEED_TYPE_DEFAULT);
154                            syndContent.setValue(activityFeedEntry.getBody());
155    
156                            syndEntry.setDescription(syndContent);
157    
158                            if (Validator.isNotNull(activityFeedEntry.getLink())) {
159                                    syndEntry.setLink(activityFeedEntry.getLink());
160                            }
161    
162                            syndEntry.setPublishedDate(new Date(activity.getCreateDate()));
163                            syndEntry.setTitle(
164                                    HtmlUtil.extractText(activityFeedEntry.getTitle()));
165    
166                            syndEntries.add(syndEntry);
167                    }
168    
169                    String rss = StringPool.BLANK;
170    
171                    try {
172                            rss = RSSUtil.export(syndFeed);
173                    }
174                    catch (FeedException fe) {
175                            throw new SystemException(fe);
176                    }
177    
178                    return rss.getBytes(StringPool.UTF8);
179            }
180    
181            @Override
182            protected boolean isCheckMethodOnProcessAction() {
183                    return _CHECK_METHOD_ON_PROCESS_ACTION;
184            }
185    
186            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
187    
188    }