Search in sources :

Example 16 with FeedException

use of com.rometools.rome.io.FeedException in project MtgDesktopCompanion by nicho92.

the class RSSNewsProvider method listNews.

@Override
public List<MagicNewsContent> listNews(MagicNews rssBean) throws IOException {
    InputStream is = null;
    SyndFeed feed;
    List<MagicNewsContent> ret = new ArrayList<>();
    try {
        HttpURLConnection openConnection = (HttpURLConnection) new URL(rssBean.getUrl()).openConnection();
        logger.debug("reading " + rssBean.getUrl());
        openConnection.setRequestProperty("User-Agent", MTGConstants.USER_AGENT);
        openConnection.setInstanceFollowRedirects(true);
        is = openConnection.getInputStream();
        InputSource source = new InputSource(is);
        feed = input.build(source);
        String baseURI = feed.getLink();
        for (SyndEntry s : feed.getEntries()) {
            MagicNewsContent content = new MagicNewsContent();
            content.setTitle(s.getTitle());
            content.setAuthor(s.getAuthor());
            content.setDate(s.getPublishedDate());
            URL link;
            if (!s.getLink().startsWith(baseURI))
                link = new URL(baseURI + s.getLink());
            else
                link = new URL(s.getLink());
            content.setLink(link);
            ret.add(content);
        }
        return ret;
    } catch (IllegalArgumentException | FeedException e) {
        throw new IOException(e);
    } finally {
        if (is != null)
            is.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) MagicNewsContent(org.magic.api.beans.MagicNewsContent) FeedException(com.rometools.rome.io.FeedException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) HttpURLConnection(java.net.HttpURLConnection)

Example 17 with FeedException

use of com.rometools.rome.io.FeedException 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();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) XmlReader(com.rometools.rome.io.XmlReader) Reader(java.io.Reader) URL(java.net.URL) SyndFeed(com.rometools.rome.feed.synd.SyndFeed) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) SyndEnclosure(com.rometools.rome.feed.synd.SyndEnclosure) FrameworkException(org.structr.common.error.FrameworkException) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FeedException(com.rometools.rome.io.FeedException) XmlReader(com.rometools.rome.io.XmlReader) IOException(java.io.IOException) Date(java.util.Date) LinkedList(java.util.LinkedList) PropertyMap(org.structr.core.property.PropertyMap) SyndContent(com.rometools.rome.feed.synd.SyndContent) SecurityContext(org.structr.common.SecurityContext)

Example 18 with FeedException

use of com.rometools.rome.io.FeedException 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();
    }
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) FeedException(com.rometools.rome.io.FeedException) SyndFeedOutput(com.rometools.rome.io.SyndFeedOutput) Writer(java.io.Writer) Date(java.util.Date) FeedException(com.rometools.rome.io.FeedException)

Example 19 with FeedException

use of com.rometools.rome.io.FeedException in project rssriver by dadoonet.

the class RssRiver method getFeed.

private SyndFeed getFeed(String url) {
    try {
        URL feedUrl = new URL(url);
        URLConnection openConnection = feedUrl.openConnection();
        openConnection.addRequestProperty("User-Agent", "RSS River for Elasticsearch (https://github.com/dadoonet/rssriver)");
        SyndFeedInput input = new SyndFeedInput();
        input.setPreserveWireFeed(true);
        SyndFeed feed = input.build(new XmlReader(openConnection));
        return feed;
    } catch (MalformedURLException e) {
        logger.error("RSS Url is incorrect : [{}].", url);
    } catch (IllegalArgumentException e) {
        logger.error("Feed from [{}] is incorrect.", url);
    } catch (FeedException e) {
        logger.error("Can not parse feed from [{}].", url);
    } catch (IOException e) {
        logger.error("Can not read feed from [{}].", url);
    }
    return null;
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) MalformedURLException(java.net.MalformedURLException) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) FeedException(com.rometools.rome.io.FeedException) XmlReader(com.rometools.rome.io.XmlReader) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

FeedException (com.rometools.rome.io.FeedException)19 SyndFeed (com.rometools.rome.feed.synd.SyndFeed)13 SyndEntry (com.rometools.rome.feed.synd.SyndEntry)7 SyndFeedInput (com.rometools.rome.io.SyndFeedInput)7 IOException (java.io.IOException)6 URL (java.net.URL)6 SyndFeedOutput (com.rometools.rome.io.SyndFeedOutput)5 XmlReader (com.rometools.rome.io.XmlReader)4 WireFeedOutput (com.rometools.rome.io.WireFeedOutput)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 Writer (java.io.Writer)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 SyndContent (com.rometools.rome.feed.synd.SyndContent)2 Result (ddf.catalog.data.Result)2 SourceResponseImpl (ddf.catalog.operation.impl.SourceResponseImpl)2 Reader (java.io.Reader)2 Charset (java.nio.charset.Charset)2 Element (org.jdom2.Element)2