Clover icon

magnolia-module-rssaggregator 2.6.2

  1. Project Clover database Wed Feb 13 2019 12:15:44 CET
  2. Package info.magnolia.module.rssaggregator.generator

File PlanetFeedGenerator.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart3.png
54% of files have more coverage

Code metrics

14
54
7
2
205
125
15
0.28
7.71
3.5
2.14
2.6% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
PlanetFeedGenerator 69 40 3.4% 12 37
0.339285733.9%
PlanetFeedGenerator.FeedEntryMapper 174 14 0% 3 19
0.00%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2013-2018 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.NodeTypes;
41    import info.magnolia.jcr.util.PropertyUtil;
42    import info.magnolia.jcr.util.SessionUtil;
43    import info.magnolia.module.rssaggregator.RSSAggregatorConstants;
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.Date;
49    import java.util.List;
50   
51    import javax.jcr.Node;
52    import javax.jcr.RepositoryException;
53   
54    import org.apache.commons.lang3.StringUtils;
55    import org.slf4j.Logger;
56    import org.slf4j.LoggerFactory;
57   
58    import com.rometools.rome.feed.synd.SyndContent;
59    import com.rometools.rome.feed.synd.SyndContentImpl;
60    import com.rometools.rome.feed.synd.SyndEntry;
61    import com.rometools.rome.feed.synd.SyndEntryImpl;
62    import com.rometools.rome.feed.synd.SyndFeed;
63    import com.rometools.rome.feed.synd.SyndLink;
64    import com.rometools.rome.feed.synd.SyndLinkImpl;
65   
66    /**
67    * Generates a {@link com.rometools.rome.feed.synd.SyndFeed} based on aggregate planet feeds defined via the RSS Aggregator Module.
68    */
 
69    public class PlanetFeedGenerator extends AbstractSyndFeedGenerator implements Cloneable {
70   
71    private static final ContentMapper<SyndEntry> MAPPER = new FeedEntryMapper();
72   
73    private static final Logger log = LoggerFactory.getLogger(PlanetFeedGenerator.class);
74   
75    private static final String FEED_TYPE_RSS = "rss_2.0";
76    private static final String FEED_TYPE_ATOM = "atom_1.0";
77   
78    private static final String DEFAULT_DESCRIPTION = "Magnolia Planet feed post content";
79   
80    private MagnoliaTemplate magnoliaTemplate;
81   
82    private String feedPath;
83    private static String feedType;
84   
 
85  1 toggle public PlanetFeedGenerator() {
86  1 this.magnoliaTemplate = new MagnoliaTemplate();
87    }
88   
 
89    toggle public void setFeedPath(String feedPath) {
90    this.feedPath = feedPath;
91    }
92   
 
93  0 toggle @Override
94    public Feed generate() throws FeedGenerationException {
95  0 feedType = getFeedType();
96   
97  0 try {
98  0 SyndFeed syndFeed = newSyndFeed();
99  0 syndFeed.setFeedType(feedType);
100  0 setFeedInfo(syndFeed);
101  0 syndFeed.setEntries(loadFeedEntries());
102   
103  0 String xml = syndFeedToXml(syndFeed);
104   
105  0 return new Feed(xml, DEFAULT_CONTENT_TYPE, DEFAULT_ENCODING);
106    } catch (Exception e) {
107  0 String message = format("Failed to generate Feed using generator '%s'", getClass().getName());
108  0 log.error(message, e);
109  0 throw new FeedGenerationException(message, e);
110    }
111    }
112   
 
113  0 toggle @Override
114    public List<SyndEntry> loadFeedEntries() {
115  0 String entriesQuery = format("/jcr:root%s/planetData[1]/*/* order by @pubDate descending", feedPath);
116  0 return magnoliaTemplate.xpathQueryForList(RSSAggregatorConstants.WORKSPACE, entriesQuery, NodeTypes.Content.NAME, MAPPER);
117    }
118   
 
119  0 toggle @Override
120    public void setFeedInfo(SyndFeed feed) {
121    // common attributes
122  0 Node feedDescr = SessionUtil.getNode(RSSAggregatorConstants.WORKSPACE, feedPath);
123  0 feed.setTitle(StringUtils.defaultIfEmpty(PropertyUtil.getString(feedDescr, "title"), ""));
124  0 feed.setDescription(StringUtils.defaultIfEmpty(PropertyUtil.getString(feedDescr, "description"), DEFAULT_DESCRIPTION));
125  0 String link = Components.getComponent(ServerConfiguration.class).getDefaultBaseUrl();
126   
127    // ATOM feed
128  0 if (FEED_TYPE_ATOM.equals(feedType)) {
129  0 if (!StringUtils.endsWith(link, "/")) {
130  0 link += "/";
131    }
132  0 feed.setUri(link); // generates the ID, should end with an "/"
133  0 feed.setPublishedDate(new Date());
134   
135  0 SyndLink selfLink = new SyndLinkImpl();
136  0 selfLink.setRel("self");
137  0 selfLink.setHref(link);
138  0 selfLink.setType("application/atom+xml");
139  0 feed.getLinks().add(selfLink);
140    } else {
141    // RSS feed
142  0 feed.setLink(link);
143    }
144    }
145   
 
146  0 toggle @Override
147    public Object clone() throws CloneNotSupportedException {
148  0 return super.clone();
149    }
150   
151    /**
152    * Determine the feed type.
153    *
154    * @return ATOM or RSS feed type
155    */
 
156  3 toggle protected String getFeedType() {
157  3 String feedType = "";
158   
159  3 if (StringUtils.endsWith(this.feedPath, "/rss")) {
160  1 this.feedPath = StringUtils.substringBeforeLast(this.feedPath, "/rss");
161  1 feedType = FEED_TYPE_RSS;
162    } else {
163  2 if (StringUtils.endsWith(this.feedPath, "/atom")) {
164  1 this.feedPath = StringUtils.substringBeforeLast(this.feedPath, "/atom");
165  1 feedType = FEED_TYPE_ATOM;
166    }
167    }
168  3 if (StringUtils.isBlank(feedType)) {
169  1 feedType = DEFAULT_FEEDTYPE;
170    }
171  3 return feedType;
172    }
173   
 
174    private static class FeedEntryMapper implements ContentMapper<SyndEntry> {
175   
 
176  0 toggle @Override
177    public SyndEntry map(Node content) throws RepositoryException {
178   
179  0 SyndEntry entry = new SyndEntryImpl();
180   
181    // common attributes
182  0 entry.setTitle(PropertyUtil.getString(content, "title"));
183  0 entry.setLink(PropertyUtil.getString(content, "link"));
184  0 entry.setAuthor(PropertyUtil.getString(content, "author"));
185  0 if (content.hasProperty("pubDate")) {
186  0 entry.setPublishedDate(PropertyUtil.getDate(content, "pubDate").getTime());
187    } else {
188  0 entry.setPublishedDate(new Date());
189    }
190  0 SyndContent description = new SyndContentImpl();
191   
192  0 description.setType("text/html");
193  0 description.setValue(PropertyUtil.getString(content, "description"));
194  0 entry.setDescription(description);
195   
196    // ATOM feed
197  0 if (FEED_TYPE_ATOM.equals(feedType)) {
198  0 entry.setUri(PropertyUtil.getString(content, "link")); // ID property of an entry
199    }
200   
201    // if categories are needed, they should be integrated here
202  0 return entry;
203    }
204    }
205    }