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 info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.jcr.util.NodeTypes;
38 import info.magnolia.jcr.util.PropertyUtil;
39 import info.magnolia.jcr.util.SessionUtil;
40 import info.magnolia.link.LinkUtil;
41 import info.magnolia.module.rssaggregator.util.ContentMapper;
42 import info.magnolia.repository.RepositoryConstants;
43 import info.magnolia.templating.functions.TemplatingFunctions;
44
45 import java.util.ArrayList;
46 import java.util.List;
47
48 import javax.inject.Inject;
49 import javax.jcr.Node;
50 import javax.jcr.RepositoryException;
51
52 import org.apache.commons.lang3.StringUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 import com.rometools.rome.feed.synd.SyndCategory;
57 import com.rometools.rome.feed.synd.SyndCategoryImpl;
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public class PageSyndicator extends AbstractSyndFeedGenerator implements Cloneable {
81
82 private static final Logger log = LoggerFactory.getLogger(PageSyndicator.class);
83
84 private String templateType;
85
86 private String templateSubtype;
87
88 private String siteRoot;
89
90 private final TemplatingFunctions templatingFunctions;
91 private final ServerConfiguration serverConfiguration;
92
93 private final ContentMapper<SyndEntry> MAPPER = new FeedEntryMapper(this);
94
95 @Inject
96 public PageSyndicator(TemplatingFunctions templatingFunctions, ServerConfiguration serverConfiguration) {
97 this.templatingFunctions = templatingFunctions;
98 this.serverConfiguration = serverConfiguration;
99 }
100
101 @Override
102 public List<SyndEntry> loadFeedEntries() {
103 try {
104 Node siteRoot = SessionUtil.getNode(RepositoryConstants.WEBSITE, getSiteRoot());
105 if (siteRoot == null) {
106 return new ArrayList<SyndEntry>();
107 }
108
109 List<Node> nodes = templatingFunctions.contentListByTemplateType(siteRoot, templateType, templateSubtype);
110 if (nodes.isEmpty()) {
111 return new ArrayList<SyndEntry>();
112 }
113 List<SyndEntry> result = new ArrayList<SyndEntry>(nodes.size());
114 for (Node node : nodes) {
115 result.add(MAPPER.map(node));
116 }
117 return result;
118 } catch (RepositoryException e) {
119 log.error("Failed to generate feed from content. Returning empty list instead.", e);
120 return new ArrayList<SyndEntry>();
121 }
122 }
123
124 @Override
125 public void setFeedInfo(SyndFeed feed) {
126 final String startPath = getSiteRoot();
127 final Node startNode = SessionUtil.getNode(RepositoryConstants.WEBSITE, startPath);
128
129 feed.setTitle(PropertyUtil.getString(startNode, PROPERTY_NAME_TITLE, ""));
130 feed.setLink(StringUtils.defaultIfEmpty(LinkUtil.createExternalLink(startNode), serverConfiguration.getDefaultBaseUrl()));
131 feed.setDescription(PropertyUtil.getString(startNode, PROPERTY_NAME_DESCRIPTION, ""));
132 }
133
134 @Override
135 public Object clone() throws CloneNotSupportedException {
136 return super.clone();
137 }
138
139
140
141
142 private static class FeedEntryMapper implements ContentMapper<SyndEntry> {
143
144 private PageSyndicator pageSyndicator;
145
146 public FeedEntryMapper(PageSyndicator pageSyndicator) {
147 this.pageSyndicator = pageSyndicator;
148 }
149
150 @Override
151 public SyndEntry map(Node content) throws RepositoryException {
152 SyndEntry entry = new SyndEntryImpl();
153 entry.setAuthor(PropertyUtil.getString(content, PROPERTY_NAME_AUTHOR));
154 entry.setTitle(PropertyUtil.getString(content, PROPERTY_NAME_TITLE, "-"));
155 entry.setLink(LinkUtil.createExternalLink(content));
156 if (content.hasProperty(PROPERTY_NAME_PUB_DATE)) {
157 entry.setPublishedDate(NodeTypes.Activatable.getLastActivated(content).getTime());
158 }
159 SyndContent description = new SyndContentImpl();
160 description.setType("text/html");
161 description.setValue(PropertyUtil.getString(content, PROPERTY_NAME_ABSTRACT));
162 entry.setDescription(description);
163
164 List<SyndCategory> categories = new ArrayList<SyndCategory>();
165
166 SyndCategory category = new SyndCategoryImpl();
167 category.setName(pageSyndicator.getTemplateSubtype());
168 categories.add(category);
169 entry.setCategories(categories);
170 return entry;
171 }
172
173 }
174
175 public void setTemplateSubtype(String templateSubtype) {
176 this.templateSubtype = templateSubtype;
177 }
178
179 public void setTemplateType(String templateType) {
180 this.templateType = templateType;
181 }
182
183 public void setSiteRoot(String siteRoot) {
184 this.siteRoot = siteRoot;
185 }
186
187 public String getTemplateSubtype() {
188 return templateSubtype;
189 }
190
191 public String getTemplateType() {
192 return templateType;
193 }
194
195 public String getSiteRoot() {
196 return StringUtils.isNotBlank(this.siteRoot) ? this.siteRoot : "/";
197 }
198
199 }