use of com.rometools.rome.feed.synd.SyndFeed in project openolat by klemens.
the class RomeFeedFetcher method validateFeedUrl.
@Override
public ValidatedURL validateFeedUrl(String url, boolean enclosuresExpected) {
SyndFeedInput input = new SyndFeedInput();
boolean modifiedProtocol = false;
try {
if (url != null) {
url = url.trim();
}
if (url.startsWith("feed") || url.startsWith("itpc")) {
// accept feed(s) urls like generated in safari browser
url = "http" + url.substring(4);
modifiedProtocol = true;
}
URL realUrl = new URL(url);
SyndFeed feed = input.build(new XmlReader(realUrl));
if (!feed.getEntries().isEmpty()) {
if (enclosuresExpected) {
SyndEntry entry = feed.getEntries().get(0);
if (entry.getEnclosures().isEmpty()) {
return new ValidatedURL(url, ValidatedURL.State.NO_ENCLOSURE);
}
}
return new ValidatedURL(url, ValidatedURL.State.VALID);
}
// The feed was read successfully
return new ValidatedURL(url, ValidatedURL.State.VALID);
} catch (ParsingFeedException e) {
if (modifiedProtocol) {
// fallback for SWITCHcast itpc -> http -> https
url = "https" + url.substring(4);
return validateFeedUrl(url, enclosuresExpected);
}
String message = String.format("Validation of the feed url %s failed. %s: %s ", url, e.getClass(), e.getMessage());
log.debug(message);
return new ValidatedURL(url, ValidatedURL.State.NOT_FOUND);
} catch (FileNotFoundException e) {
String message = String.format("Validation of the feed url %s failed. %s: %s ", url, e.getClass(), e.getMessage());
log.debug(message);
return new ValidatedURL(url, ValidatedURL.State.NOT_FOUND);
} catch (Exception e) {
String message = String.format("Validation of the feed url %s failed. %s: %s ", url, e.getClass(), e.getMessage());
log.debug(message);
}
return new ValidatedURL(url, ValidatedURL.State.MALFORMED);
}
use of com.rometools.rome.feed.synd.SyndFeed in project coffeenet-frontpage-plugin-rss by coffeenet.
the class BlogParserTest method parseBlog.
@Test
public void parseBlog() throws FeedException, IOException {
SyndContentImpl description = new SyndContentImpl();
description.setValue("description");
SyndEntryImpl syndEntry = new SyndEntryImpl();
syndEntry.setDescription(description);
syndEntry.setLink("link");
syndEntry.setAuthor("author");
syndEntry.setTitle("title");
syndEntry.setPublishedDate(Date.from(Instant.parse("2014-12-03T10:15:30.00Z")));
SyndFeed result = new SyndFeedImpl();
result.setEntries(singletonList(syndEntry));
when(feedFactory.build(any(URL.class))).thenReturn(result);
List<BlogEntry> blogEntries = sut.parse("http://blog/feed/", 10, 150);
assertThat(blogEntries, hasSize(1));
assertThat(blogEntries.get(0).getDescription(), is("description"));
assertThat(blogEntries.get(0).getLink(), is("link"));
assertThat(blogEntries.get(0).getPublishDate(), is("3. December 2014"));
assertThat(blogEntries.get(0).getAuthor(), is("author"));
assertThat(blogEntries.get(0).getTitle(), is("title"));
}
use of com.rometools.rome.feed.synd.SyndFeed in project structr by structr.
the class DataFeed method updateFeed.
static void updateFeed(final DataFeed thisFeed, final boolean cleanUp) {
final String remoteUrl = thisFeed.getUrl();
if (StringUtils.isNotBlank(remoteUrl)) {
final SecurityContext securityContext = thisFeed.getSecurityContext();
final App app = StructrApp.getInstance(securityContext);
try {
final PropertyKey<Date> dateKey = StructrApp.key(FeedItem.class, "pubDate");
final PropertyKey<String> urlKey = StructrApp.key(FeedItem.class, "url");
final URL remote = new URL(remoteUrl);
final SyndFeedInput input = new SyndFeedInput();
try (final Reader reader = new XmlReader(remote)) {
final SyndFeed feed = input.build(reader);
final List<SyndEntry> entries = feed.getEntries();
thisFeed.setProperty(StructrApp.key(DataFeed.class, "feedType"), feed.getFeedType());
thisFeed.setProperty(StructrApp.key(DataFeed.class, "description"), feed.getDescription());
final List<FeedItem> newItems = Iterables.toList(thisFeed.getItems());
for (final SyndEntry entry : entries) {
final PropertyMap props = new PropertyMap();
final String link = entry.getLink();
// Check if item with this link already exists
if (app.nodeQuery(FeedItem.class).and(urlKey, link).getFirst() == null) {
props.put(urlKey, entry.getLink());
props.put(StructrApp.key(FeedItem.class, "name"), entry.getTitle());
props.put(StructrApp.key(FeedItem.class, "author"), entry.getAuthor());
props.put(StructrApp.key(FeedItem.class, "comments"), entry.getComments());
props.put(StructrApp.key(FeedItem.class, "description"), entry.getDescription().getValue());
final FeedItem item = app.create(FeedItem.class, props);
item.setProperty(dateKey, entry.getPublishedDate());
final List<FeedItemContent> itemContents = new LinkedList<>();
final List<FeedItemEnclosure> itemEnclosures = new LinkedList<>();
// Get and add all contents
final List<SyndContent> contents = entry.getContents();
for (final SyndContent content : contents) {
final FeedItemContent itemContent = app.create(FeedItemContent.class);
itemContent.setValue(content.getValue());
itemContents.add(itemContent);
}
// Get and add all enclosures
final List<SyndEnclosure> enclosures = entry.getEnclosures();
for (final SyndEnclosure enclosure : enclosures) {
final FeedItemEnclosure itemEnclosure = app.create(FeedItemEnclosure.class);
itemEnclosure.setProperty(StructrApp.key(FeedItemEnclosure.class, "url"), enclosure.getUrl());
itemEnclosure.setProperty(StructrApp.key(FeedItemEnclosure.class, "enclosureLength"), enclosure.getLength());
itemEnclosure.setProperty(StructrApp.key(FeedItemEnclosure.class, "enclosureType"), enclosure.getType());
itemEnclosures.add(itemEnclosure);
}
item.setProperty(StructrApp.key(FeedItem.class, "contents"), itemContents);
item.setProperty(StructrApp.key(FeedItem.class, "enclosures"), itemEnclosures);
newItems.add(item);
logger.debug("Created new item: {} ({}) ", item.getProperty(FeedItem.name), item.getProperty(dateKey));
}
}
thisFeed.setProperty(StructrApp.key(DataFeed.class, "items"), newItems);
thisFeed.setProperty(StructrApp.key(DataFeed.class, "lastUpdated"), new Date());
}
} catch (IllegalArgumentException | IOException | FeedException | FrameworkException ex) {
logger.error("Error while updating feed", ex);
}
}
if (cleanUp) {
thisFeed.cleanUp();
}
}
use of com.rometools.rome.feed.synd.SyndFeed in project openolat by klemens.
the class PersonalRSSServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try (Writer writer = response.getWriter()) {
String pathInfo = request.getPathInfo();
if ((pathInfo == null) || (pathInfo.equals(""))) {
// error
return;
}
SyndFeed feed = null;
// pathInfo is like /personal/username/tokenid.rss
if (pathInfo.indexOf(PersonalRSSUtil.RSS_PREFIX_PERSONAL) == 0) {
feed = getPersonalFeed(pathInfo);
if (feed == null) {
DispatcherModule.sendNotFound(pathInfo, response);
return;
}
} else {
DispatcherModule.sendNotFound(pathInfo, response);
return;
}
// OLAT-5400 and OLAT-5243 related: sending back the reply can take arbitrary long,
// considering slow end-user connections for example - or a sudden death of the connection
// on the client-side which remains unnoticed (network partitioning)
DBFactory.getInstance().intermediateCommit();
response.setBufferSize(response.getBufferSize());
String encoding = feed.getEncoding();
if (encoding == null) {
encoding = DEFAULT_ENCODING;
if (log.isDebug()) {
log.debug("Feed encoding::" + encoding);
}
log.warn("No encoding provided by feed::" + feed.getClass().getCanonicalName() + " Using utf-8 as default.");
}
response.setCharacterEncoding(encoding);
response.setContentType("application/rss+xml");
Date pubDate = feed.getPublishedDate();
if (pubDate != null) {
response.setDateHeader("Last-Modified", pubDate.getTime());
}
SyndFeedOutput output = new SyndFeedOutput();
output.output(feed, writer);
} catch (FeedException e) {
// throw olat exception for nice logging
log.warn("Error when generating RSS stream for path::" + request.getPathInfo(), e);
DispatcherModule.sendNotFound("none", response);
} catch (Exception e) {
log.warn("Unknown Exception in rssservlet", e);
DispatcherModule.sendNotFound("none", response);
} catch (Error e) {
log.warn("Unknown Error in rssservlet", e);
DispatcherModule.sendNotFound("none", response);
} finally {
DBFactory.getInstance().commitAndCloseSession();
}
}
use of com.rometools.rome.feed.synd.SyndFeed in project rssriver by dadoonet.
the class RomeTest method testGeoLoc.
@Test
public void testGeoLoc() throws Exception {
SyndFeedInput input = new SyndFeedInput();
input.setPreserveWireFeed(true);
SyndFeed feed = input.build(new XmlReader(getClass().getResource("/reuters/rss.xml")));
assertThat(feed, notNullValue());
assertThat(feed.getEntries().isEmpty(), equalTo(false));
for (Object o : feed.getEntries()) {
assertThat(o, instanceOf(SyndEntryImpl.class));
SyndEntryImpl entry = (SyndEntryImpl) o;
GeoRSSModule geoRSSModule = GeoRSSUtils.getGeoRSS(entry);
assertThat(geoRSSModule, notNullValue());
Position position = geoRSSModule.getPosition();
assertThat(position, notNullValue());
assertThat(position.getLatitude(), equalTo(41.8947384616695));
assertThat(position.getLongitude(), equalTo(12.4839019775391));
}
}
Aggregations