Example usage for java.net URLConnection getLastModified

List of usage examples for java.net URLConnection getLastModified

Introduction

In this page you can find the example usage for java.net URLConnection getLastModified.

Prototype

public long getLastModified() 

Source Link

Document

Returns the value of the last-modified header field.

Usage

From source file:org.clapper.curn.FeedDownloadThread.java

/**
 * Unconditionally download and process a feed. Only called by
 * handleFeed()./*w ww .  j a  v  a 2s .co m*/
 *
 * @param feedInfo      the info about the feed
 * @param parser        the RSS parser to use, or null if parsing is to
 *                      be skipped
 * @param urlConn       open URLConnection for the feed
 * @param forceDownload <tt>true</tt> to force the download even if the
 *                      feed hasn't changed, <tt>false</tt> to observe
 *                      the normal rules
 *
 * @return the <tt>RSSChannel</tt> representing the parsed feed, if
 *         parsing was enabled; otherwise, null.
 *
 * @throws FeedException  feed download error
 * @throws CurnException  some other error (e.g., plug-in error)
 */
private RSSChannel downloadAndProcessFeed(final FeedInfo feedInfo, final RSSParser parser,
        final URLConnection urlConn, final boolean forceDownload) throws FeedException, CurnException {
    RSSChannel resultChannel = null;
    URL feedURL = feedInfo.getURL();

    try {
        // Don't download the channel if it hasn't been modified since
        // we last checked it. We set the If-Modified-Since header, to
        // tell the web server not to return the content if it's not
        // newer than what we saw before. However, as a double-check
        // (for web servers that ignore the header), we also check the
        // Last-Modified header, if any, that's returned; if it's not
        // newer, we don't bother to parse and process the returned
        // XML.

        if (!forceDownload)
            setIfModifiedSinceHeader(urlConn, feedInfo, cache);

        // If the feed has actually changed, or if downloading is force,
        // process it.

        if ((!forceDownload) && (!feedHasChanged(urlConn, feedInfo, cache))) {
            log.info("Feed has not changed. Skipping it.");
        }

        else {
            log.debug("Feed may have changed. " + "Downloading and processing it.");

            // Download the feed to a file. We'll parse the file.

            DownloadedTempFile tempFile = downloadFeed(urlConn, feedInfo);

            if (tempFile.bytesDownloaded == 0) {
                log.debug("Feed \"" + feedURL + "\" returned no data.");
            }

            else {
                metaPlugIn.runPostFeedDownloadPlugIn(feedInfo, tempFile.file, tempFile.encoding);

                if (parser == null) {
                    log.debug("No RSS parser. Skipping XML parse phase.");
                }

                else {
                    log.debug("Using RSS parser " + parser.getClass().getName() + " to parse \"" + feedURL
                            + "\"");

                    InputStream is = new FileInputStream(tempFile.file);
                    resultChannel = parser.parseRSSFeed(feedURL, is, tempFile.encoding);
                    is.close();

                    // Make sure the channel has a link.

                    Collection<RSSLink> links = resultChannel.getLinks();
                    if ((links == null) || (links.size() == 0)) {
                        RSSLink link = new RSSLink(feedURL, "text/xml", RSSLink.Type.SELF);
                        resultChannel.setLinks(Collections.singleton(link));
                    }

                    if (!metaPlugIn.runPostFeedParsePlugIn(feedInfo, cache, resultChannel)) {
                        resultChannel = null;
                    }

                    if (resultChannel != null) {
                        processChannelItems(resultChannel, feedInfo);
                        if (resultChannel.getItems().size() == 0)
                            resultChannel = null;
                    }

                    if (resultChannel != null) {
                        boolean ok = metaPlugIn.runPostFeedProcessPlugIn(feedInfo, cache, resultChannel);
                        if (!ok)
                            resultChannel = null;
                    }
                }
            }

            tempFile.file.delete();
            if (cache != null) {
                cache.addToCache(feedURL, new Date(urlConn.getLastModified()), feedInfo);
            }
        }
    }

    catch (IOException ex) {
        throw new FeedException(feedInfo, ex);
    }

    catch (RSSParserException ex) {
        throw new FeedException(feedInfo, ex);
    }

    log.debug("downloadAndProcessFeed(): Feed=" + feedInfo.getURL() + ", returning "
            + ((resultChannel == null) ? "null" : resultChannel.toString()));

    return resultChannel;
}