-
Notifications
You must be signed in to change notification settings - Fork 28
/
RomeNotificationService.java
278 lines (224 loc) · 9.99 KB
/
RomeNotificationService.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Licensed to Apereo under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Apereo licenses this file to you 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 the following location:
*
* 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 org.jasig.portlet.notice.service.rome;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.util.*;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.servlet.http.HttpServletRequest;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;
import org.apache.commons.lang3.StringUtils;
import org.jasig.portlet.notice.NotificationAttribute;
import org.jasig.portlet.notice.NotificationCategory;
import org.jasig.portlet.notice.NotificationError;
import org.jasig.portlet.notice.NotificationResponse;
import org.jasig.portlet.notice.service.AbstractNotificationService;
import org.jasig.portlet.notice.util.UsernameFinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.rometools.rome.feed.synd.SyndContent;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;
import org.springframework.beans.factory.annotation.Value;
public final class RomeNotificationService extends AbstractNotificationService {
private static final String URLS_PREFERENCE = "RomeNotificationService.urls";
private static final Object NO_FURTHER_INFORMATION = "No further information is available.";
private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT);
/**
* Service URLs defined in an external file, in the post-Portlet API style.
* Comma-separated list.
*/
@Value("${RomeNotificationService.feedUrls:}")
private String feedUrlsProperty;
private List<String> feedUrlsList = Collections.emptyList();
private Cache cache;
@Autowired
private UsernameFinder usernameFinder;
private final Logger logger = LoggerFactory.getLogger(getClass());
@Resource(name="RomeNotificationService.feedCache")
public void setCache(Cache cache) {
this.cache = cache;
}
@PostConstruct
public void init() {
if (!StringUtils.isEmpty(feedUrlsProperty)) {
feedUrlsList =
Collections.unmodifiableList(Arrays.asList(feedUrlsProperty.split(",")));
}
}
@Override
public void invoke(final ActionRequest req, final ActionResponse res, final boolean refresh) {
if (logger.isDebugEnabled()) {
logger.debug("Performing RomeNotificationService.invoke() for user='"
+ usernameFinder.findUsername(req)
+ "' refresh=" + refresh);
}
if (refresh) {
final PortletPreferences prefs = req.getPreferences();
final String[] urls = prefs.getValues(URLS_PREFERENCE, new String[0]);
for (String item : urls) {
cache.remove(item);
}
}
}
@Override
public NotificationResponse fetch(final PortletRequest req) {
final PortletPreferences prefs = req.getPreferences();
final String[] feedUrls = prefs.getValues(URLS_PREFERENCE, new String[0]);
final String username = usernameFinder.findUsername(req);
return doFetch(Arrays.asList(feedUrls), username);
}
@Override
public NotificationResponse fetch(HttpServletRequest req) {
final String username = usernameFinder.findUsername(req);
return doFetch(feedUrlsList, username);
}
/*
* Implementation
*/
private NotificationResponse doFetch(List<String> feedUrls, String username) {
final List<NotificationCategory> categories = new ArrayList<>();
final List<NotificationError> errors = new ArrayList<>();
for (String item : feedUrls) {
// It's okay to pull a response from cache, if we have one, since refresh happens in invoke()
final Element m = cache.get(item);
if (m != null) {
// ## CACHE HIT ##
if (logger.isDebugEnabled()) {
logger.debug("Feed cache HIT for url: " + item);
}
final NotificationCategory category = (NotificationCategory) m.getObjectValue();
categories.add(category);
} else {
// ## CACHE MISS ##
if (logger.isDebugEnabled()) {
logger.debug("Feed cache MISS for url: " + item);
logger.debug("Checking the following feed URL for notifications for user '"
+ username + "' -- " + item);
}
final NotificationCategory category = fetchFromSourceUrl(item);
if (category != null) {
cache.put(new Element(item, category));
categories.add(category);
} else {
final NotificationError error = new NotificationError();
error.setError("Service Unavailable");
error.setSource(getName());
errors.add(error);
}
}
}
final NotificationResponse rslt = new NotificationResponse();
rslt.setCategories(categories);
rslt.setErrors(errors);
return rslt;
}
private NotificationCategory fetchFromSourceUrl(final String url) {
NotificationCategory rslt = null; // default
XmlReader reader = null;
try {
final URL u = new URL(url);
reader = new XmlReader(u);
final SyndFeedInput input = new SyndFeedInput();
final SyndFeed feed = input.build(reader);
rslt = new NotificationCategory();
rslt.setTitle(feed.getTitle());
final List<TimestampNotificationEntry> entries = new ArrayList<>();
@SuppressWarnings("unchecked")
final List<SyndEntry> list = feed.getEntries();
for (SyndEntry y : list) {
if (logger.isTraceEnabled()) {
logger.trace("Processing SyndEntry: \n" + y.toString());
}
final long timestamp = y.getPublishedDate().getTime();
final TimestampNotificationEntry entry = new TimestampNotificationEntry(timestamp);
// Strongly-typed members
entry.setSource(feed.getAuthor());
entry.setTitle(y.getTitle());
entry.setUrl(y.getLink());
// No priority
// No due date
// No image
// Body
final StringBuilder body = new StringBuilder();
final SyndContent desc = y.getDescription();
if (desc != null) {
// Prefer description
body.append(desc.getValue());
}
if (body.length() == 0) {
// Fall back to contents
@SuppressWarnings("unchecked")
final List<SyndContent> contents = y.getContents();
for (SyndContent c : contents) {
body.append(c.getValue());
}
}
if (body.length() == 0) {
// Last resort...
body.append(NO_FURTHER_INFORMATION);
}
entry.setBody(body.toString());
// Attributes -- TODO: These labels should be internationalized in messages.properties
final List<NotificationAttribute> attributes = new ArrayList<>();
final String author = y.getAuthor();
if (StringUtils.isNotBlank(author)) {
attributes.add(new NotificationAttribute("Author", author));
}
final Date publishedDate = y.getPublishedDate();
if (publishedDate != null) {
attributes.add(new NotificationAttribute("Published date", DATE_FORMAT.format(publishedDate)));
}
final Date updatededDate = y.getUpdatedDate();
if (updatededDate != null) {
attributes.add(new NotificationAttribute("Updated date", DATE_FORMAT.format(updatededDate)));
}
entry.setAttributes(attributes);
entries.add(entry);
}
// Items should be in reverse chronological order
Collections.sort(entries);
Collections.reverse(entries);
rslt.setEntries(new ArrayList<>(entries));
} catch (Exception e) {
final String msg = "Unable to read the specified feed: " + url;
logger.error(msg, e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ioe) {
final String msg = "Unable to close the XmlReader";
logger.error(msg, ioe);
}
}
}
return rslt;
}
}