diff --git a/spring-integration-feed/src/main/java/org/springframework/integration/feed/FileUrlFeedFetcher.java b/spring-integration-feed/src/main/java/org/springframework/integration/feed/FileUrlFeedFetcher.java index 1d3e2abcf2..b6423da376 100644 --- a/spring-integration-feed/src/main/java/org/springframework/integration/feed/FileUrlFeedFetcher.java +++ b/spring-integration-feed/src/main/java/org/springframework/integration/feed/FileUrlFeedFetcher.java @@ -1,18 +1,19 @@ /* -* Copyright 2010 the original author or authors -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ + * Copyright 2002-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.integration.feed; import java.io.BufferedInputStream; @@ -22,6 +23,8 @@ import java.net.URL; import java.net.URLConnection; import java.util.zip.GZIPInputStream; +import org.springframework.util.Assert; + import com.sun.syndication.feed.synd.SyndFeed; import com.sun.syndication.fetcher.FetcherEvent; import com.sun.syndication.fetcher.FetcherException; @@ -33,37 +36,30 @@ import com.sun.syndication.io.XmlReader; /** * @author Oleg Zhurakousky + * @author Mark Fisher * @since 2.0 */ public class FileUrlFeedFetcher extends AbstractFeedFetcher { - /* (non-Javadoc) + /* + * (non-Javadoc) * @see com.sun.syndication.fetcher.FeedFetcher#retrieveFeed(java.net.URL) */ - public SyndFeed retrieveFeed(URL feedUrl) throws IllegalArgumentException, - IOException, FeedException, FetcherException { - if (feedUrl == null) { - throw new IllegalArgumentException("null is not a valid URL"); - } - + public SyndFeed retrieveFeed(URL feedUrl) throws IOException, FeedException, FetcherException { + Assert.notNull(feedUrl, "feedUrl must not be null"); URLConnection connection = feedUrl.openConnection(); - SyndFeedInfo syndFeedInfo = new SyndFeedInfo(); - retrieveAndCacheFeed(feedUrl, syndFeedInfo, connection); + this.refreshFeedInfo(feedUrl, syndFeedInfo, connection); return syndFeedInfo.getSyndFeed(); } - protected void retrieveAndCacheFeed(URL feedUrl, SyndFeedInfo syndFeedInfo, URLConnection connection) throws IllegalArgumentException, FeedException, FetcherException, IOException { - resetFeedInfo(feedUrl, syndFeedInfo, connection); - } - - protected void resetFeedInfo(URL orignalUrl, SyndFeedInfo syndFeedInfo, URLConnection connection) throws IllegalArgumentException, IOException, FeedException { + private void refreshFeedInfo(URL feedUrl, SyndFeedInfo syndFeedInfo, URLConnection connection) throws IOException, FeedException { // need to always set the URL because this may have changed due to 3xx redirects syndFeedInfo.setUrl(connection.getURL()); - // the ID is a persistant value that should stay the same even if the URL for the - // feed changes (eg, by 3xx redirects) - syndFeedInfo.setId(orignalUrl.toString()); + // the ID is a persistent value that should stay the same + // even if the URL for the feed changes (eg, by 3xx redirects) + syndFeedInfo.setId(feedUrl.toString()); // This will be 0 if the server doesn't support or isn't setting the last modified header syndFeedInfo.setLastModified(new Long(connection.getLastModified())); @@ -72,38 +68,40 @@ public class FileUrlFeedFetcher extends AbstractFeedFetcher { InputStream inputStream = null; try { inputStream = connection.getInputStream(); - SyndFeed syndFeed = getSyndFeedFromStream(inputStream, connection); + SyndFeed syndFeed = this.readFeedFromStream(inputStream, connection); syndFeedInfo.setSyndFeed(syndFeed); - } finally { - if (inputStream != null) { + } + finally { + try { inputStream.close(); } + catch (Exception e) { + // ignore + } } } - private SyndFeed getSyndFeedFromStream(InputStream inputStream, URLConnection connection) throws IOException, IllegalArgumentException, FeedException { - SyndFeed feed = readSyndFeedFromStream(inputStream, connection); + + private SyndFeed readFeedFromStream(InputStream inputStream, URLConnection connection) throws IOException, FeedException { + BufferedInputStream bufferedInputStream; + if ("gzip".equalsIgnoreCase(connection.getContentEncoding())) { + // handle gzip encoded content + bufferedInputStream = new BufferedInputStream(new GZIPInputStream(inputStream)); + } + else { + bufferedInputStream = new BufferedInputStream(inputStream); + } + XmlReader reader = null; + if (connection.getHeaderField("Content-Type") != null) { + reader = new XmlReader(bufferedInputStream, connection.getHeaderField("Content-Type"), true); + } + else { + reader = new XmlReader(bufferedInputStream, true); + } + SyndFeedInput syndFeedInput = new SyndFeedInput(); + syndFeedInput.setPreserveWireFeed(isPreserveWireFeed()); + SyndFeed feed = syndFeedInput.build(reader); fireEvent(FetcherEvent.EVENT_TYPE_FEED_RETRIEVED, connection, feed); return feed; } - private SyndFeed readSyndFeedFromStream(InputStream inputStream, URLConnection connection) throws IOException, IllegalArgumentException, FeedException { - BufferedInputStream is; - if ("gzip".equalsIgnoreCase(connection.getContentEncoding())) { - // handle gzip encoded content - is = new BufferedInputStream(new GZIPInputStream(inputStream)); - } else { - is = new BufferedInputStream(inputStream); - } - XmlReader reader = null; - if (connection.getHeaderField("Content-Type") != null) { - reader = new XmlReader(is, connection.getHeaderField("Content-Type"), true); - } else { - reader = new XmlReader(is, true); - } - - SyndFeedInput syndFeedInput = new SyndFeedInput(); - syndFeedInput.setPreserveWireFeed(isPreserveWireFeed()); - - return syndFeedInput.build(reader); - } }