Skip to content

Instantly share code, notes, and snippets.

@ryanschuhler
Created April 14, 2015 20:38
Show Gist options
  • Select an option

  • Save ryanschuhler/9136a409d4c030f19159 to your computer and use it in GitHub Desktop.

Select an option

Save ryanschuhler/9136a409d4c030f19159 to your computer and use it in GitHub Desktop.
/**
* Copyright (c) 2000-present 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.osbui.blogsstream.portlet;
import com.liferay.compat.portal.kernel.portlet.PortletResponseUtil;
import com.liferay.compat.portal.kernel.util.StringUtil;
import com.liferay.compat.portal.util.PortalUtil;
import com.liferay.compat.util.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.ContentTypes;
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.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.PortletDisplay;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.Portal;
import com.liferay.portlet.asset.model.AssetCategory;
import com.liferay.portlet.asset.model.AssetEntry;
import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
import com.liferay.portlet.asset.service.AssetEntryServiceUtil;
import com.liferay.portlet.asset.service.persistence.AssetEntryQuery;
import com.liferay.portlet.blogs.model.BlogsEntry;
import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
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 com.sun.syndication.io.FeedException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
/**
* @author Amos Fong
* @author Ryan Schuhler
*/
public class BlogsStreamPortlet extends MVCPortlet {
@Override
public void serveResource(
ResourceRequest resourceRequest, ResourceResponse resourceResponse)
throws IOException, PortletException {
try {
String resourceID = resourceRequest.getResourceID();
if (resourceID.equals("rss")) {
serveRSS(resourceRequest, resourceResponse);
}
else if (resourceID.equals("getBlogContent")) {
long entryId = ParamUtil.getLong(resourceRequest, "entryId");
AssetEntry assetEntry = AssetEntryLocalServiceUtil.fetchAssetEntry(
entryId);
BlogsEntry blogsEntry =
BlogsEntryLocalServiceUtil.getBlogsEntryByUuidAndGroupId(
assetEntry.getClassUuid(), assetEntry.getGroupId());
JSONObject entryJSONObject = JSONFactoryUtil.createJSONObject();
entryJSONObject.put(
"title", blogsEntry.getTitle());
entryJSONObject.put(
"blogContent", blogsEntry.getContent());
writeJSON(resourceRequest, resourceResponse, entryJSONObject);
}
else if (resourceID.equals("getBlogEntries")) {
long categoryId = ParamUtil.getLong(
resourceRequest, "categoryId");
JSONObject entriesJSONObject =
JSONFactoryUtil.createJSONObject();
List<AssetEntry> assetEntries =
AssetEntryLocalServiceUtil.getAssetCategoryAssetEntries(
categoryId);
for (AssetEntry assetEntry : assetEntries) {
JSONObject entryJSONObject =
JSONFactoryUtil.createJSONObject();
entryJSONObject.put("author", assetEntry.getUserName());
entryJSONObject.put("date", assetEntry.getPublishDate());
entryJSONObject.put("title", assetEntry.getTitle());
entryJSONObject.put("url", assetEntry.getUrl());
entriesJSONObject.put(String.valueOf(
assetEntry.getEntryId()), entryJSONObject);
}
writeJSON(resourceRequest, resourceResponse, entriesJSONObject);
}
}
catch (Exception e) {
throw new PortletException(e);
}
}
protected String exportToRSS(
String name, String description, String type, double version,
String feedURL, List<AssetEntry> assetEntries,
ThemeDisplay themeDisplay)
throws SystemException {
SyndFeed syndFeed = new SyndFeedImpl();
syndFeed.setDescription(description);
List<SyndEntry> syndEntries = new ArrayList<SyndEntry>();
syndFeed.setEntries(syndEntries);
for (AssetEntry assetEntry : assetEntries) {
SyndEntry syndEntry = new SyndEntryImpl();
String author = PortalUtil.getUserName(assetEntry);
syndEntry.setAuthor(author);
SyndContent syndContent = new SyndContentImpl();
syndContent.setType(RSSUtil.ENTRY_TYPE_DEFAULT);
String summary = assetEntry.getSummary();
String value = StringUtil.shorten(
HtmlUtil.extractText(summary), 200, StringPool.BLANK);
syndContent.setValue(value);
syndEntry.setDescription(syndContent);
StringBundler sb = new StringBundler(4);
sb.append(themeDisplay.getPortalURL());
sb.append(themeDisplay.getPathMain());
sb.append("/blogs/find_entry?entryId=");
sb.append(assetEntry.getClassPK());
String link = sb.toString();
syndEntry.setLink(link);
syndEntry.setPublishedDate(assetEntry.getPublishDate());
syndEntry.setTitle(assetEntry.getTitle());
syndEntry.setUpdatedDate(assetEntry.getModifiedDate());
syndEntry.setUri(link);
syndEntries.add(syndEntry);
}
syndFeed.setFeedType(RSSUtil.getFeedType(type, version));
List<SyndLink> syndLinks = new ArrayList<SyndLink>();
syndFeed.setLinks(syndLinks);
SyndLink selfSyndLink = new SyndLinkImpl();
syndLinks.add(selfSyndLink);
selfSyndLink.setHref(feedURL);
selfSyndLink.setRel("self");
syndFeed.setPublishedDate(new Date());
syndFeed.setTitle(name);
syndFeed.setUri(feedURL);
try {
return RSSUtil.export(syndFeed);
}
catch (FeedException fe) {
throw new SystemException(fe);
}
}
protected void serveRSS(
ResourceRequest resourceRequest, ResourceResponse resourceResponse)
throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(
WebKeys.THEME_DISPLAY);
PortletPreferences preferences = resourceRequest.getPreferences();
long[] assetCategoryIds = GetterUtil.getLongValues(
preferences.getValues("assetCategoryIds", null));
StringBundler sb = new StringBundler(assetCategoryIds.length * 2);
int count = 0;
for (long assetCategoryId : assetCategoryIds) {
AssetCategory assetCategory =
AssetCategoryLocalServiceUtil.getAssetCategory(assetCategoryId);
sb.append(assetCategory.getTitle(themeDisplay.getLocale()));
sb.append(StringPool.COMMA_AND_SPACE);
count++;
}
sb.setIndex(sb.index() - 1);
String title = "Liferay Blogs - " + sb.toString();
AssetEntryQuery assetEntryQuery = new AssetEntryQuery();
assetEntryQuery.setAnyCategoryIds(assetCategoryIds);
assetEntryQuery.setClassName(BlogsEntry.class.getName());
assetEntryQuery.setEnablePermissions(true);
assetEntryQuery.setEnd(20);
assetEntryQuery.setOrderByCol1("publishDate");
assetEntryQuery.setOrderByType1("DESC");
assetEntryQuery.setStart(0);
assetEntryQuery.setVisible(true);
List<AssetEntry> assetEntries = AssetEntryServiceUtil.getEntries(
assetEntryQuery);
PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
String feedURL =
PortalUtil.getLayoutFullURL(themeDisplay) +
Portal.FRIENDLY_URL_SEPARATOR + "blogs_stream/" +
portletDisplay.getInstanceId() + "/rss";
String rss = exportToRSS(
title, title, RSSUtil.TYPE_DEFAULT, RSSUtil.VERSION_DEFAULT,
feedURL, assetEntries, themeDisplay);
PortletResponseUtil.sendFile(
resourceRequest, resourceResponse, null,
rss.getBytes(StringPool.UTF8), ContentTypes.TEXT_XML_UTF8);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment