View Javadoc
1   /**
2    * This file Copyright (c) 2009-2016 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 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   * Model class providing plumbing required for generating a {@link Feed} from a {@link com.rometools.rome.feed.synd.SyndFeed}.
66   *
67   * <p>{@link info.magnolia.module.rssaggregator.generator.FeedGeneratorResolver} will try to populate this class.
68   * Required parameters are:<ul>
69   * <li>{@link info.magnolia.module.rssaggregator.generator.FeedGeneratorResolver#REQUIRED_PARAMETER}, which would
70   * correspond to <code>generatorName=templateContent</code></li>
71   * <li><code>templateType</code>, e.g. <code>templateType=content</code> (see:
72   * {@link info.magnolia.rendering.template.type.DefaultTemplateTypes})</li>
73   * <li><code>templateSubtype</code>, e.g. <code>templateType=news</code> (optional, defaults to <code>null</code>)</li>
74   * <li><code>siteRoot</code>, e.g. <code>siteRoot=/some-path-to-a-site-root</code> (optional, defaults to <code>/</code>)</li></ul></p>
75   *
76   * @see info.magnolia.module.rssaggregator.servlet.FeedSyndicationServlet
77   * @see info.magnolia.module.rssaggregator.generator.FeedGeneratorResolver
78   * @see info.magnolia.rendering.template.type.DefaultTemplateTypes
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      * Mapper that maps a {@link javax.jcr.Node} to a {@link com.rometools.rome.feed.synd.SyndEntry}.
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 }