View Javadoc

1   /**
2    * This file Copyright (c) 2003-2013 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 info.magnolia.module.rssaggregator.RSSAggregator.*;
37  import static java.lang.String.format;
38  
39  import info.magnolia.cms.beans.config.ServerConfiguration;
40  import info.magnolia.jcr.util.PropertyUtil;
41  import info.magnolia.jcr.util.SessionUtil;
42  import info.magnolia.module.data.DataConsts;
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.List;
49  
50  import javax.jcr.Node;
51  import javax.jcr.RepositoryException;
52  
53  import org.apache.commons.lang.StringUtils;
54  import org.slf4j.Logger;
55  import org.slf4j.LoggerFactory;
56  
57  import com.sun.syndication.feed.synd.SyndContent;
58  import com.sun.syndication.feed.synd.SyndContentImpl;
59  import com.sun.syndication.feed.synd.SyndEntry;
60  import com.sun.syndication.feed.synd.SyndEntryImpl;
61  import com.sun.syndication.feed.synd.SyndFeed;
62  
63  /**
64   * Generates a {@link com.sun.syndication.feed.synd.SyndFeed} based on aggregate planet feeds defined via the RSS Aggregator Module.
65   *
66   * @author lfischer
67   */
68  public class PlanetFeedGenerator extends AbstractSyndFeedGenerator implements Cloneable {
69  
70      private static final ContentMapper<SyndEntry> MAPPER = new FeedEntryMapper();
71  
72      private static final Logger log = LoggerFactory.getLogger(PlanetFeedGenerator.class);
73  
74      private static final String FEED_TYPE_RSS = "rss_2.0";
75      private static final String FEED_TYPE_ATOM = "atom_1.0";
76  
77      private MagnoliaTemplate magnoliaTemplate;
78  
79      private String feedPath;
80  
81      public PlanetFeedGenerator() {
82          this.magnoliaTemplate = new MagnoliaTemplate();
83      }
84  
85      public void setFeedPath(String feedPath) {
86          this.feedPath = feedPath;
87      }
88  
89      @Override
90      public Feed generate() throws FeedGenerationException {
91          String feedType = getFeedType();
92  
93          try {
94              SyndFeed syndFeed = newSyndFeed();
95              syndFeed.setFeedType(feedType);
96              syndFeed.setEntries(loadFeedEntries());
97              setFeedInfo(syndFeed);
98  
99              String xml = syndFeedToXml(syndFeed);
100             return new Feed(xml, DEFAULT_CONTENT_TYPE, DEFAULT_ENCODING);
101         } catch (Exception e) {
102             String message = format("Failed to generate Feed using generator '%s'", getClass().getName());
103             log.error(message, e);
104             throw new FeedGenerationException(message, e);
105         }
106     }
107 
108     @Override
109     public List<SyndEntry> loadFeedEntries() {
110         String entriesQuery = format("/jcr:root%s/planetData[1]/*/* order by @pubDate descending", feedPath);
111         return magnoliaTemplate.xpathQueryForList(DataModule.getRepository(), entriesQuery, DataConsts.MODULE_DATA_CONTENT_NODE_TYPE, MAPPER);
112     }
113 
114     @Override
115     public void setFeedInfo(SyndFeed feed) {
116         // Ideally retrieve this from the aggregate node definition. However the RSS Aggregate creation dialog does not
117         // provide for this information (yet).
118         String link = Components.getComponent(ServerConfiguration.class).getDefaultBaseUrl();
119         Node feedDescr = SessionUtil.getNode(DataModule.getRepository(), feedPath);
120         feed.setTitle(StringUtils.defaultIfEmpty(PropertyUtil.getString(feedDescr, "title"), ""));
121         feed.setLink(link);
122         feed.setDescription(StringUtils.defaultIfEmpty(PropertyUtil.getString(feedDescr, "description"), ""));
123     }
124 
125     @Override
126     public Object clone() throws CloneNotSupportedException {
127         return super.clone();
128     }
129 
130     protected String getFeedType() {
131         String feedType = "";
132 
133         if (StringUtils.endsWith(this.feedPath, "/rss")) {
134             this.feedPath = StringUtils.substringBeforeLast(this.feedPath, "/rss");
135             feedType = FEED_TYPE_RSS;
136         } else {
137             if (StringUtils.endsWith(this.feedPath, "/atom")) {
138                 this.feedPath = StringUtils.substringBeforeLast(this.feedPath, "/atom");
139                 feedType = FEED_TYPE_ATOM;
140             }
141         }
142 
143         if (StringUtils.isBlank(feedType)) {
144             feedType = DEFAULT_FEEDTYPE;
145         }
146 
147         return feedType;
148     }
149 
150     private static class FeedEntryMapper implements ContentMapper<SyndEntry> {
151 
152         @Override
153         public SyndEntry map(Node content) throws RepositoryException {
154 
155             SyndEntry entry = new SyndEntryImpl();
156             entry.setAuthor(PropertyUtil.getString(content, "author"));
157             entry.setTitle(PropertyUtil.getString(content, "title"));
158             entry.setLink(PropertyUtil.getString(content, "link"));
159             if (content.hasProperty("pubDate")) {
160                 entry.setPublishedDate(PropertyUtil.getDate(content, "pubDate").getTime());
161             }
162             SyndContent description = new SyndContentImpl();
163             //description.setType("text/html");
164             description.setValue(PropertyUtil.getString(content, "description"));
165             entry.setDescription(description);
166 
167             // if categories are needed, they should be integrated here
168 
169             return entry;
170         }
171     }
172 }