com.liferay.portlet.activities.action.RSSAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of portal-impl Show documentation
Show all versions of portal-impl Show documentation
Contains implementation for the portal services.
/**
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portlet.activities.action;
import com.liferay.portal.kernel.dao.search.SearchContainer;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.HtmlUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Group;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.Portal;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.WebKeys;
import com.liferay.portlet.social.model.SocialActivity;
import com.liferay.portlet.social.model.SocialActivityFeedEntry;
import com.liferay.portlet.social.service.SocialActivityInterpreterLocalServiceUtil;
import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
import com.liferay.util.RSSUtil;
import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;
import com.sun.syndication.feed.synd.SyndLink;
import com.sun.syndication.feed.synd.SyndLinkImpl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.portlet.PortletRequest;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
/**
* @author Brian Wing Shun Chan
* @author Vilmos Papp
* @author Eduardo Garcia
*/
public class RSSAction extends com.liferay.portal.struts.RSSAction {
protected String exportToRSS(
PortletRequest portletRequest, String title, String description,
String format, double version, String displayStyle,
List activities, ServiceContext serviceContext)
throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
SyndFeed syndFeed = new SyndFeedImpl();
syndFeed.setDescription(GetterUtil.getString(description, title));
List syndEntries = new ArrayList();
syndFeed.setEntries(syndEntries);
for (SocialActivity activity : activities) {
SocialActivityFeedEntry activityFeedEntry =
SocialActivityInterpreterLocalServiceUtil.interpret(
StringPool.BLANK, activity, serviceContext);
if (activityFeedEntry == null) {
continue;
}
SyndEntry syndEntry = new SyndEntryImpl();
SyndContent syndContent = new SyndContentImpl();
syndContent.setType(RSSUtil.ENTRY_TYPE_DEFAULT);
String value = null;
if (displayStyle.equals(RSSUtil.DISPLAY_STYLE_TITLE)) {
value = StringPool.BLANK;
}
else {
value = activityFeedEntry.getBody();
}
syndContent.setValue(value);
syndEntry.setDescription(syndContent);
if (Validator.isNotNull(activityFeedEntry.getLink())) {
syndEntry.setLink(activityFeedEntry.getLink());
}
syndEntry.setPublishedDate(new Date(activity.getCreateDate()));
syndEntry.setTitle(
HtmlUtil.extractText(activityFeedEntry.getTitle()));
syndEntry.setUri(syndEntry.getLink());
syndEntries.add(syndEntry);
}
syndFeed.setFeedType(RSSUtil.getFeedType(format, version));
List syndLinks = new ArrayList();
syndFeed.setLinks(syndLinks);
SyndLink selfSyndLink = new SyndLinkImpl();
syndLinks.add(selfSyndLink);
String link =
PortalUtil.getLayoutFullURL(themeDisplay) +
Portal.FRIENDLY_URL_SEPARATOR + "activities/rss";
selfSyndLink.setHref(link);
selfSyndLink.setRel("self");
SyndLink alternateSyndLink = new SyndLinkImpl();
syndLinks.add(alternateSyndLink);
alternateSyndLink.setHref(PortalUtil.getLayoutFullURL(themeDisplay));
alternateSyndLink.setRel("alternate");
syndFeed.setPublishedDate(new Date());
syndFeed.setTitle(title);
syndFeed.setUri(link);
return RSSUtil.export(syndFeed);
}
protected List getActivities(
PortletRequest portletRequest, int max)
throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
Group group = GroupLocalServiceUtil.getGroup(
themeDisplay.getScopeGroupId());
int start = 0;
if (group.isOrganization()) {
return SocialActivityLocalServiceUtil.getOrganizationActivities(
group.getOrganizationId(), start, max);
}
else if (group.isRegularSite()) {
return SocialActivityLocalServiceUtil.getGroupActivities(
group.getGroupId(), start, max);
}
else if (group.isUser()) {
return SocialActivityLocalServiceUtil.getUserActivities(
group.getClassPK(), start, max);
}
return Collections.emptyList();
}
@Override
protected byte[] getRSS(
ResourceRequest resourceRequest, ResourceResponse resourceResponse)
throws Exception {
String feedTitle = ParamUtil.getString(resourceRequest, "feedTitle");
String format = ParamUtil.getString(
resourceRequest, "type", RSSUtil.FORMAT_DEFAULT);
double version = ParamUtil.getDouble(
resourceRequest, "version", RSSUtil.VERSION_DEFAULT);
String displayStyle = ParamUtil.getString(
resourceRequest, "displayStyle", RSSUtil.DISPLAY_STYLE_DEFAULT);
int max = ParamUtil.getInteger(
resourceRequest, "max", SearchContainer.DEFAULT_DELTA);
List activities = getActivities(resourceRequest, max);
ServiceContext serviceContext = ServiceContextFactory.getInstance(
resourceRequest);
String rss = exportToRSS(
resourceRequest, feedTitle, null, format, version, displayStyle,
activities, serviceContext);
return rss.getBytes(StringPool.UTF8);
}
}