View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.rssaggregator.generator;
35  
36  import static java.lang.String.format;
37  
38  import info.magnolia.cms.beans.config.ServerConfiguration;
39  import info.magnolia.jcr.util.NodeTypes;
40  import info.magnolia.jcr.util.PropertyUtil;
41  import info.magnolia.jcr.util.SessionUtil;
42  import info.magnolia.jcr.wrapper.JCRMgnlPropertiesFilteringNodeWrapper;
43  import info.magnolia.module.data.DataModule;
44  import info.magnolia.module.rssaggregator.util.ContentMapper;
45  import info.magnolia.module.rssaggregator.util.MagnoliaTemplate;
46  import info.magnolia.objectfactory.Components;
47  
48  import java.util.ArrayList;
49  import java.util.List;
50  
51  import javax.jcr.Node;
52  import javax.jcr.Property;
53  import javax.jcr.PropertyIterator;
54  import javax.jcr.RepositoryException;
55  
56  import com.sun.syndication.feed.synd.SyndCategory;
57  import com.sun.syndication.feed.synd.SyndCategoryImpl;
58  import com.sun.syndication.feed.synd.SyndContent;
59  import com.sun.syndication.feed.synd.SyndContentImpl;
60  import com.sun.syndication.feed.synd.SyndEntry;
61  import com.sun.syndication.feed.synd.SyndEntryImpl;
62  import com.sun.syndication.feed.synd.SyndFeed;
63  
64  /**
65   * Generates a {@link SyndFeed} based on aggregate feeds defined via the RSS Aggregator Module.
66   *
67   * @author Rob van der Linden Vooren
68   */
69  public class RSSModuleFeedGenerator extends AbstractSyndFeedGenerator implements Cloneable {
70  
71      private static final ContentMapper<SyndEntry> MAPPER = new FeedEntryMapper();
72  
73      private MagnoliaTemplate magnoliaTemplate;
74  
75      private String feedPath;
76  
77      /**
78       * Construct a new RSSModuleSyndFeedGenerator that generates a SyndFeed for the aggregate feed defined via the RSS
79       * Aggregator Module at the given {@code feedPath}.
80       *
81       */
82      public RSSModuleFeedGenerator() {
83          this.magnoliaTemplate = new MagnoliaTemplate();
84      }
85  
86      /**
87       * @param feedPath the path of the aggregate feed in the RSS Aggregator module (eg. "/rssaggregator/blogsaggregate")
88       */
89      public void setFeedPath(String feedPath) {
90          this.feedPath = feedPath;
91      }
92  
93      @Override
94      public void setFeedInfo(SyndFeed feed) {
95          // Ideally retrieve this from the aggregate node definition. However the RSS Aggregate creation dialog does not
96          // provide for this information (yet).
97          String link = Components.getComponent(ServerConfiguration.class).getDefaultBaseUrl();
98          Node feedDescr = SessionUtil.getNode(NodeTypes.Content.NAME, feedPath);
99          if (feedDescr != null) {
100             feed.setTitle(PropertyUtil.getString(feedDescr, "title", ""));
101             feed.setDescription(PropertyUtil.getString(feedDescr, "description", ""));
102         } else {
103             feed.setTitle("");
104             feed.setDescription("");
105         }
106     }
107 
108     @Override
109     public List<SyndEntry> loadFeedEntries() {
110         String entriesQuery = format("/jcr:root%s/data[1]/*/* order by @pubDate descending", feedPath);
111         return magnoliaTemplate.xpathQueryForList(DataModule.getRepository(), entriesQuery, NodeTypes.Content.NAME, MAPPER);
112     }
113 
114     @Override
115     public Object clone() throws CloneNotSupportedException {
116         return super.clone();
117     }
118 
119     private static class FeedEntryMapper implements ContentMapper<SyndEntry> {
120 
121         @Override
122         public SyndEntry map(Node content) throws RepositoryException {
123             SyndEntry entry = new SyndEntryImpl();
124             entry.setAuthor(PropertyUtil.getString(content, "author"));
125             entry.setTitle(PropertyUtil.getString(content, "title"));
126             entry.setLink(PropertyUtil.getString(content, "link"));
127             if (content.hasProperty("pubDate")) {
128                 entry.setPublishedDate(PropertyUtil.getProperty(content, "pubDate").getDate().getTime());
129             }
130             SyndContent description = new SyndContentImpl();
131             description.setType("text/html");
132             description.setValue(PropertyUtil.getString(content, "description"));
133             entry.setDescription(description);
134             List<SyndCategory> categories = new ArrayList<SyndCategory>();
135             if (content.hasNode("categories")) {
136                 PropertyIterator categoriesNodeData = new JCRMgnlPropertiesFilteringNodeWrapper(content.getNode("categories")).getProperties();
137                 while (categoriesNodeData.hasNext()) {
138                     Property property = categoriesNodeData.nextProperty();
139                     SyndCategory category = new SyndCategoryImpl();
140                     category.setName(property.getString());
141                     categories.add(category);
142                 }
143             }
144             entry.setCategories(categories);
145             return entry;
146         }
147 
148     }
149 
150 }