Skip to content

Package: DefaultRssFeedViewController

DefaultRssFeedViewController

nameinstructionbranchcomplexitylinemethod
DefaultRssFeedViewController(RssFeedView, SiteNode, SiteProvider, RequestLocaleManager)
M: 76 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
findVirtualSiteNodes()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$renderPosts$0(Content)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$renderPosts$1(Content)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$renderPosts$2(Content)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
prepareRendering(RenderContext)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
renderPost(Content, Optional)
M: 97 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 21 C: 0
0%
M: 1 C: 0
0%
renderPosts(List, List, List)
M: 36 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
renderTagCloud(Collection)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.frontend.ui.component.rssfeed;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.Date;
33: import java.util.List;
34: import java.util.Optional;
35: import com.sun.syndication.feed.rss.Channel;
36: import com.sun.syndication.feed.rss.Content;
37: import com.sun.syndication.feed.rss.Guid;
38: import com.sun.syndication.feed.rss.Item;
39: import com.sun.syndication.io.WireFeedOutput;
40: import it.tidalwave.util.Finder;
41: import it.tidalwave.util.Key;
42: import it.tidalwave.util.NotFoundException;
43: import it.tidalwave.northernwind.core.model.HttpStatusException;
44: import it.tidalwave.northernwind.core.model.RequestLocaleManager;
45: import it.tidalwave.northernwind.core.model.ResourceProperties;
46: import it.tidalwave.northernwind.core.model.SiteNode;
47: import it.tidalwave.northernwind.core.model.SiteProvider;
48: import it.tidalwave.northernwind.frontend.ui.RenderContext;
49: import it.tidalwave.northernwind.frontend.ui.component.blog.DefaultBlogViewController;
50: import it.tidalwave.northernwind.core.impl.model.DefaultSiteFinder;
51: import lombok.extern.slf4j.Slf4j;
52: import static java.util.Collections.emptyMap;
53: import static it.tidalwave.northernwind.core.model.Content.*;
54:
55: /***********************************************************************************************************************
56: *
57: * @author Fabrizio Giudici
58: *
59: **********************************************************************************************************************/
60: @Slf4j
61: public class DefaultRssFeedViewController extends DefaultBlogViewController implements RssFeedViewController
62: {
63: @Nonnull
64: private final SiteProvider siteProvider;
65:
66: @Nonnull
67: private final SiteNode siteNode;
68:
69: @Nonnull
70: private final RssFeedView view;
71:
72: private final List<Item> items = new ArrayList<>();
73:
74: private final String linkBase;
75:
76: private final Channel feed;
77:
78: private final ResourceProperties properties;
79:
80: /*******************************************************************************************************************
81: *
82: *
83: *
84: ******************************************************************************************************************/
85: public DefaultRssFeedViewController (@Nonnull final RssFeedView view,
86: @Nonnull final SiteNode siteNode,
87: @Nonnull final SiteProvider siteProvider,
88: @Nonnull final RequestLocaleManager requestLocaleManager)
89: {
90: super(siteNode, view, requestLocaleManager);
91: this.siteProvider = siteProvider;
92: this.siteNode = siteNode;
93: this.view = view;
94: feed = new Channel("rss_2.0");
95: properties = siteNode.getPropertyGroup(view.getId());
96: linkBase = properties.getProperty(P_LINK).orElse("");
97: feed.setTitle(properties.getProperty(P_TITLE).orElse(""));
98: feed.setDescription(properties.getProperty(P_DESCRIPTION).orElse(""));
99: feed.setLink(linkBase); // FIXME: why not site.createLink()?
100: feed.setCopyright(properties.getProperty(P_CREATOR).orElse(""));
101: }
102:
103: /*******************************************************************************************************************
104: *
105: * {@inheritDoc}
106: *
107: ******************************************************************************************************************/
108: @Override
109: public void prepareRendering (@Nonnull final RenderContext context)
110: throws HttpStatusException
111: {
112: // do not call super!
113: log.info("prepareRendering(RenderContext) for {}", siteNode);
114: prepareBlogPosts(context, getViewProperties());
115: }
116:
117: /*******************************************************************************************************************
118: *
119: * {@inheritDoc}
120: *
121: * RSS must not present their inner contents to the sitemap.
122: *
123: ******************************************************************************************************************/
124: @Override @Nonnull
125: public Finder<SiteNode> findVirtualSiteNodes()
126: {
127: return new DefaultSiteFinder<>("empty", emptyMap(), null); // TODO: HierarchicFinderSupport.emptyFinder();
128: }
129:
130: /*******************************************************************************************************************
131: *
132: * {@inheritDoc}
133: *
134: ******************************************************************************************************************/
135: @Override
136: protected void renderPosts (@Nonnull final List<? extends it.tidalwave.northernwind.core.model.Content> fullPosts,
137: @Nonnull final List<? extends it.tidalwave.northernwind.core.model.Content> leadinPosts,
138: @Nonnull final List<? extends it.tidalwave.northernwind.core.model.Content> linkedPosts)
139: throws Exception
140: {
141: fullPosts.forEach (post -> renderPost(post, Optional.of(P_FULL_TEXT)));
142: leadinPosts.forEach(post -> renderPost(post, Optional.of(P_LEADIN_TEXT)));
143: linkedPosts.forEach(post -> renderPost(post, Optional.empty()));
144:
145: feed.setGenerator("NorthernWind v" + siteProvider.getVersionString());
146: feed.setItems(items);
147:
148: // if (!StringUtils.hasText(feed.getEncoding()))
149: // {
150: // feed.setEncoding("UTF-8");
151: // }
152:
153: final var feedOutput = new WireFeedOutput();
154: view.setContent(feedOutput.outputString(feed));
155: }
156:
157: /*******************************************************************************************************************
158: *
159: * {@inheritDoc}
160: *
161: ******************************************************************************************************************/
162: protected void renderPost (@Nonnull final it.tidalwave.northernwind.core.model.Content post,
163: @Nonnull final Optional<? extends Key<String>> textProperty)
164: {
165: final var blogDateTime = post.getProperty(DATE_KEYS).orElse(TIME0);
166: // FIXME: compute the latest date, which is not necessarily the first
167:• if (feed.getLastBuildDate() == null)
168: {
169: feed.setLastBuildDate(Date.from(blogDateTime.toInstant()));
170: }
171:
172: final var postProperties = post.getProperties();
173: final var item = new Item();
174: final var content = new Content();
175: // FIXME: text/xhtml?
176: content.setType("text/html"); // FIXME: should use post.getResourceFile().getMimeType()?
177: textProperty.flatMap(postProperties::getProperty).ifPresent(content::setValue);
178: item.setTitle(postProperties.getProperty(P_TITLE).orElse(""));
179: // item.setAuthor("author " + i); TODO
180: item.setPubDate(Date.from(blogDateTime.toInstant()));
181: item.setContent(content);
182:
183: try
184: {
185: // FIXME: manipulate through site.createLink()
186: final var link = linkBase.replaceAll("/$", "") + post.getExposedUri().orElseThrow(NotFoundException::new).asString() + "/";
187: final var guid = new Guid();
188: guid.setPermaLink(true);
189: guid.setValue(link);
190: item.setGuid(guid);
191: item.setLink(link);
192: }
193: catch (NotFoundException e)
194: {
195: // ok. no link
196: }
197:
198: items.add(item);
199: }
200:
201: /*******************************************************************************************************************
202: *
203: * {@inheritDoc}
204: *
205: ******************************************************************************************************************/
206: @Override
207: protected void renderTagCloud (@Nonnull final Collection<? extends TagAndCount> tagsAndCount)
208: {
209: // not meaningful for RSS
210: }
211: }