/
IndexRssView.java
126 lines (114 loc) · 4.89 KB
/
IndexRssView.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright 2015 EuregJUG.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.euregjug.site.web;
import ac.simons.syndication.modules.atom.AtomContent;
import ac.simons.syndication.modules.atom.AtomModuleImpl;
import ac.simons.syndication.utils.SyndicationGuid;
import ac.simons.syndication.utils.SyndicationLink;
import com.rometools.rome.feed.rss.Channel;
import com.rometools.rome.feed.rss.Content;
import com.rometools.rome.feed.rss.Guid;
import com.rometools.rome.feed.rss.Item;
import eu.euregjug.site.posts.Post;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.feed.AbstractRssFeedView;
/**
* @author Michael J. Simons, 2015-12-30
*/
@Component("index.rss")
class IndexRssView extends AbstractRssFeedView {
private final MessageSource messageSource;
private final DateTimeFormatter permalinkDateFormatter;
@Autowired
public IndexRssView(MessageSource messageSource) {
this.messageSource = messageSource;
this.permalinkDateFormatter = DateTimeFormatter.ofPattern("/y/M/d", Locale.ENGLISH);
}
String getAbsoluteUrl(final HttpServletRequest request, final String relativeUrl) {
final int port = request.getServerPort();
final String hostWithPort = String.format("%s://%s%s",
(request.isSecure() ? "https" : "http"),
request.getServerName(),
(Arrays.asList(80, 443).contains(port) ? "" : String.format(":%d", port))
);
return String.format("%s%s%s", hostWithPort, request.getContextPath(), relativeUrl);
}
@Override
protected void buildFeedMetadata(Map<String, Object> model, Channel feed, HttpServletRequest request) {
final Page<Post> posts = (Page<Post>) model.get("posts");
final Locale locale = request.getLocale();
feed.setEncoding("UTF-8");
feed.setTitle(String.format("%s - %s", messageSource.getMessage("siteTitle", null, locale), messageSource.getMessage("siteSubTitle", null, locale)));
feed.setDescription(messageSource.getMessage("feedDescription", null, locale));
feed.setLink(getAbsoluteUrl(request, ""));
if (posts.hasContent()) {
final Date pubDate = Date.from(posts.getContent().get(0).getPublishedOn().atStartOfDay(ZoneId.systemDefault()).toInstant());
feed.setLastBuildDate(pubDate);
feed.setPubDate(pubDate);
}
feed.setGenerator("https://github.com/EuregJUG-Maas-Rhine/site");
final String self = getAbsoluteUrl(request, "/feed.rss");
final AtomContent atomContent = new AtomContent();
atomContent.addLink(new SyndicationLink().withRel("previous").withType(super.getContentType()).withHref(self).getLink());
if (posts.hasPrevious()) {
atomContent.addLink(new SyndicationLink()
.withRel("previous")
.withType(super.getContentType())
.withHref(String.format("%s?page=%d", self, posts.previousPageable().getPageNumber()))
.getLink()
);
}
if (posts.hasNext()) {
atomContent.addLink(new SyndicationLink()
.withRel("next")
.withType(super.getContentType())
.withHref(String.format("%s?page=%d", self, posts.nextPageable().getPageNumber()))
.getLink()
);
}
feed.getModules().add(new AtomModuleImpl(atomContent));
}
@Override
protected List<Item> buildFeedItems(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
final Page<Post> posts = (Page<Post>) model.get("posts");
return posts.map(post -> {
final Item rv = new Item();
final Content content = new Content();
content.setType(Content.HTML);
content.setValue(post.getContent());
final String link = getAbsoluteUrl(request, String.format("%s/%s", permalinkDateFormatter.format(post.getPublishedOn()), post.getSlug()));
rv.setAuthor("euregjug.eu");
rv.setContent(content);
rv.setGuid(new SyndicationGuid().withPermaLink(true).withValue(link).getGuid());
rv.setLink(link);
rv.setPubDate(Date.from(post.getPublishedOn().atStartOfDay(ZoneId.systemDefault()).toInstant()));
rv.setTitle(post.getTitle());
return rv;
}).getContent();
}
}