1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
@link |
68 |
|
|
|
|
| 33.9% |
Uncovered Elements: 37 (56) |
Complexity: 12 |
Complexity Density: 0.3 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
1 |
public PlanetFeedGenerator() {... |
86 |
1 |
this.magnoliaTemplate = new MagnoliaTemplate(); |
87 |
|
} |
88 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
89 |
|
public void setFeedPath(String feedPath) {... |
90 |
|
this.feedPath = feedPath; |
91 |
|
} |
92 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
|
93 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
113 |
0 |
@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 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 3 |
Complexity Density: 0.2 |
|
119 |
0 |
@Override... |
120 |
|
public void setFeedInfo(SyndFeed feed) { |
121 |
|
|
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 |
|
|
128 |
0 |
if (FEED_TYPE_ATOM.equals(feedType)) { |
129 |
0 |
if (!StringUtils.endsWith(link, "/")) { |
130 |
0 |
link += "/"; |
131 |
|
} |
132 |
0 |
feed.setUri(link); |
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 |
|
|
142 |
0 |
feed.setLink(link); |
143 |
|
} |
144 |
|
} |
145 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
146 |
0 |
@Override... |
147 |
|
public Object clone() throws CloneNotSupportedException { |
148 |
0 |
return super.clone(); |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
@return |
155 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
156 |
3 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 3 |
Complexity Density: 0.21 |
|
174 |
|
private static class FeedEntryMapper implements ContentMapper<SyndEntry> { |
175 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 3 |
Complexity Density: 0.21 |
|
176 |
0 |
@Override... |
177 |
|
public SyndEntry map(Node content) throws RepositoryException { |
178 |
|
|
179 |
0 |
SyndEntry entry = new SyndEntryImpl(); |
180 |
|
|
181 |
|
|
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 |
|
|
197 |
0 |
if (FEED_TYPE_ATOM.equals(feedType)) { |
198 |
0 |
entry.setUri(PropertyUtil.getString(content, "link")); |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
0 |
return entry; |
203 |
|
} |
204 |
|
} |
205 |
|
} |