Search in sources :

Example 1 with ParsingFeedException

use of com.rometools.rome.io.ParsingFeedException in project OpenOLAT by OpenOLAT.

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);
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) ParsingFeedException(com.rometools.rome.io.ParsingFeedException) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FileNotFoundException(java.io.FileNotFoundException) XmlReader(com.rometools.rome.io.XmlReader) URL(java.net.URL) FileNotFoundException(java.io.FileNotFoundException) ParsingFeedException(com.rometools.rome.io.ParsingFeedException)

Example 2 with ParsingFeedException

use of com.rometools.rome.io.ParsingFeedException 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);
}
Also used : SyndFeed(com.rometools.rome.feed.synd.SyndFeed) ParsingFeedException(com.rometools.rome.io.ParsingFeedException) SyndFeedInput(com.rometools.rome.io.SyndFeedInput) SyndEntry(com.rometools.rome.feed.synd.SyndEntry) FileNotFoundException(java.io.FileNotFoundException) XmlReader(com.rometools.rome.io.XmlReader) URL(java.net.URL) FileNotFoundException(java.io.FileNotFoundException) ParsingFeedException(com.rometools.rome.io.ParsingFeedException)

Aggregations

SyndEntry (com.rometools.rome.feed.synd.SyndEntry)2 SyndFeed (com.rometools.rome.feed.synd.SyndFeed)2 ParsingFeedException (com.rometools.rome.io.ParsingFeedException)2 SyndFeedInput (com.rometools.rome.io.SyndFeedInput)2 XmlReader (com.rometools.rome.io.XmlReader)2 FileNotFoundException (java.io.FileNotFoundException)2 URL (java.net.URL)2