com.seajas.search.utilities.web.WebFeeds.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.utilities.web.WebFeeds.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.utilities.web;

import java.net.URI;
import java.net.URISyntaxException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.commons.lang.StringEscapeUtils;

import org.springframework.util.StringUtils;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.FeedException;

/**
 * Internet feed routines.
 * 
 * @author Pascal S. de Kloe <pascal@quies.net>
 */
public final class WebFeeds {

    private static final Logger logger = LoggerFactory.getLogger(WebFeeds.class);

    /**
     * Default constructor.
     */
    private WebFeeds() {
    }

    /**
     * Ensures a usable {@code feed}.
     *
     * @param feed the subject.
     * @param link the content location.
     * @throws FeedException if all correction attempts fail.
     */
    public static void validate(SyndFeed feed, URI link) throws FeedException {
        if (feed.getUri() == null)
            feed.setUri(link.toString());
        if (feed.getLink() == null)
            feed.setLink(link.toString());

        if (feed.getFeedType() == null) {
            logger.debug("Defaulting to RSS 2.0 spec.");
            feed.setFeedType("rss_2.0");
        }

        if (feed.getTitleEx() == null)
            feed.setTitle("Feed");
        if (feed.getDescriptionEx() == null)
            feed.setDescription("Feed content");

        for (Object entry : feed.getEntries())
            validateEntry((SyndEntry) entry, feed);
    }

    /**
     * Ensures an usable {@code entry}.
     * The verification includes, but is not limited to:
     * <ul>
     * <li>absolute {@link SyndEntry#getLink() link}.</li>
     * </ul>
     * 
     * @param entry the subject.
     * @param feed the parent.
     * @throws FeedException if all correction attempts fail.
     */
    public static void validateEntry(SyndEntry entry, SyndFeed feed) throws FeedException {
        ensureResourceIdentification(entry, feed);

        SyndContent description = entry.getDescription();
        if (description != null) {
            String type = description.getType();
            if (type == null || "text/plain".equals(type)) {
                String html = "<?xml version='1.0' encoding='UTF-8'?><html><head /><body>";
                html += StringEscapeUtils.escapeXml(description.getValue());
                html += "</body></html>";
                description.setValue(html);
                description.setType("text/html");
            }
        }
    }

    private static void ensureResourceIdentification(SyndEntry entry, SyndFeed feed) throws FeedException {
        try {
            String uri = entry.getLink();
            if (!StringUtils.hasText(uri))
                uri = entry.getUri();

            boolean generated = false;
            if (!StringUtils.hasText(uri)) {
                logger.debug("Entry has no resource identification");
                int position = feed.getEntries().indexOf(entry);
                if (position < 0)
                    uri = "#hash-" + entry.hashCode();
                else
                    uri = "#entry-" + position;
                generated = true;
            }

            URI parsed = WebResourceLocators.parseURI(uri, feed.getLink(), feed.getUri());
            if (!parsed.isAbsolute()) {
                if (generated)
                    throw new FeedException("No resource identification available");
                else
                    throw new FeedException("Relative resource: " + parsed);
            }
            uri = parsed.toASCIIString();

            entry.setLink(uri);
            if (!StringUtils.hasText(entry.getUri()))
                entry.setUri(uri);
        } catch (URISyntaxException e) {
            throw new FeedException("Can't resolve resource", e);
        }
    }

}