From 504736f9404ece087e7b694638bab982ea84ce4e Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Thu, 20 May 2021 08:12:51 +0200 Subject: [PATCH] replaced deprecated rome-fetcher with java 11 HttpClient. --- app/pom.xml | 2 +- .../business/fetcher/RomeFeedFetcher.java | 138 +++++------------- .../business/WebloggerRomeFeedFetcher.java | 4 +- .../util/rome/DiskFeedInfoCacheTest.java | 56 ------- 4 files changed, 38 insertions(+), 162 deletions(-) delete mode 100644 app/src/test/java/org/apache/roller/planet/util/rome/DiskFeedInfoCacheTest.java diff --git a/app/pom.xml b/app/pom.xml index eeaab680d1..538ee45f46 100644 --- a/app/pom.xml +++ b/app/pom.xml @@ -506,7 +506,7 @@ limitations under the License. com.rometools - rome-fetcher + rome ${rome.version} compile diff --git a/app/src/main/java/org/apache/roller/planet/business/fetcher/RomeFeedFetcher.java b/app/src/main/java/org/apache/roller/planet/business/fetcher/RomeFeedFetcher.java index e0e8328cfb..83777c1707 100644 --- a/app/src/main/java/org/apache/roller/planet/business/fetcher/RomeFeedFetcher.java +++ b/app/src/main/java/org/apache/roller/planet/business/fetcher/RomeFeedFetcher.java @@ -23,16 +23,16 @@ import com.rometools.rome.feed.synd.SyndContent; import com.rometools.rome.feed.synd.SyndEntry; import com.rometools.rome.feed.synd.SyndFeed; -import com.rometools.fetcher.FeedFetcher; -import com.rometools.fetcher.impl.FeedFetcherCache; -import com.rometools.fetcher.impl.HttpURLFeedFetcher; -import com.rometools.fetcher.impl.SyndFeedInfo; -import com.rometools.fetcher.impl.DiskFeedInfoCache; +import com.rometools.rome.io.FeedException; +import com.rometools.rome.io.SyndFeedInput; +import com.rometools.rome.io.XmlReader; -import java.io.File; -import java.net.MalformedURLException; -import java.net.URL; +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; import java.sql.Timestamp; +import java.time.Duration; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; @@ -43,28 +43,35 @@ import org.apache.commons.logging.LogFactory; import org.apache.roller.planet.pojos.SubscriptionEntry; import org.apache.roller.planet.pojos.Subscription; -import org.apache.roller.weblogger.config.WebloggerConfig; + +import static java.net.http.HttpResponse.BodyHandlers.ofInputStream; /** - * A FeedFetcher based on the ROME RSS/Atom feed parser (http://rome.dev.java.net). + * A FeedFetcher based on Apache ROME and {@link java.net.http.HttpClient}. */ -public class RomeFeedFetcher implements org.apache.roller.planet.business.fetcher.FeedFetcher { +public class RomeFeedFetcher implements FeedFetcher { + + private static final Log log = LogFactory.getLog(RomeFeedFetcher.class); - private static Log log = LogFactory.getLog(RomeFeedFetcher.class); + // mutable, copy() first + private static final HttpRequest.Builder requestBuilder = HttpRequest.newBuilder() + .timeout(Duration.ofSeconds(3)) + .header("User-Agent", "RollerPlanetAggregator"); + private final HttpClient client; public RomeFeedFetcher() { - // no-op + // immutable + thread safe, prefers HTTP/2, no redirects + this.client = HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(3)).build(); } - /** * @inheritDoc */ @Override - public Subscription fetchSubscription(String feedURL) - throws FetcherException { + public Subscription fetchSubscription(String feedURL) throws FetcherException { return fetchSubscription(feedURL, null); } @@ -73,22 +80,18 @@ public Subscription fetchSubscription(String feedURL) * @inheritDoc */ @Override - public Subscription fetchSubscription(String feedURL, Date lastModified) - throws FetcherException { - + public Subscription fetchSubscription(String feedURL, Date lastModified) throws FetcherException { + if(feedURL == null) { throw new IllegalArgumentException("feed url cannot be null"); } - // setup Rome feed fetcher - FeedFetcher feedFetcher = getRomeFetcher(); - // fetch the feed log.debug("Fetching feed: "+feedURL); SyndFeed feed; try { - feed = feedFetcher.retrieveFeed(new URL(feedURL)); - } catch (Exception ex) { + feed = fetchFeed(feedURL); + } catch (FeedException | IOException | InterruptedException ex) { throw new FetcherException("Error fetching subscription - "+feedURL, ex); } @@ -112,21 +115,6 @@ public Subscription fetchSubscription(String feedURL, Date lastModified) // set the author to the title newSub.setAuthor(newSub.getTitle()); } - if(newSub.getLastUpdated() == null) { - // no update time specified in feed, so try consulting feed info cache - FeedFetcherCache feedCache = getRomeFetcherCache(); - try { - SyndFeedInfo feedInfo = feedCache.getFeedInfo(new URL(newSub.getFeedURL())); - if(feedInfo.getLastModified() != null) { - long lastUpdatedLong = (Long) feedInfo.getLastModified(); - if (lastUpdatedLong != 0) { - newSub.setLastUpdated(new Date(lastUpdatedLong)); - } - } - } catch (MalformedURLException ex) { - // should never happen since we check this above - } - } // check if feed is unchanged and bail now if so if(lastModified != null && newSub.getLastUpdated() != null && @@ -162,9 +150,7 @@ public Subscription fetchSubscription(String feedURL, Date lastModified) cal.add(Calendar.DATE, -1); } - if(newEntry != null) { - newSub.addEntry(newEntry); - } + newSub.addEntry(newEntry); } log.debug(feedEntries.size()+" entries included"); @@ -230,8 +216,8 @@ private SubscriptionEntry buildEntry(SyndEntry romeEntry) { // copy categories if (!romeEntry.getCategories().isEmpty()) { List list = new ArrayList<>(); - for (Object cat : romeEntry.getCategories()) { - list.add(((SyndCategory) cat).getName()); + for (SyndCategory cat : romeEntry.getCategories()) { + list.add(cat.getName()); } newEntry.setCategoriesString(list); } @@ -239,68 +225,14 @@ private SubscriptionEntry buildEntry(SyndEntry romeEntry) { return newEntry; } - - // get a feed fetcher cache, if possible - private FeedFetcherCache getRomeFetcherCache() { - - String cacheDirPath = WebloggerConfig.getProperty("cache.dir"); - - // can't continue without cache dir - if (cacheDirPath == null) { - log.warn("Planet cache directory not set, feeds cannot be cached."); - return null; - } - - // allow ${user.home} in cache dir property - String cacheDirName = cacheDirPath.replaceFirst( - "\\$\\{user.home}",System.getProperty("user.home")); - - // allow ${catalina.home} in cache dir property - if (System.getProperty("catalina.home") != null) { - cacheDirName = cacheDirName.replaceFirst( - "\\$\\{catalina.home}",System.getProperty("catalina.home")); - } - - // create cache dir if it does not exist - File cacheDir = null; - try { - cacheDir = new File(cacheDirName); - if (!cacheDir.exists()) { - cacheDir.mkdirs(); - } - } catch (Exception e) { - log.error("Unable to create planet cache directory: " + - ((cacheDir != null) ? cacheDir.getPath() : null), e); - return null; - } - - // abort if cache dir is not writable - if (!cacheDir.canWrite()) { - log.error("Planet cache directory is not writable: " + cacheDir.getPath()); - return null; - } - - return new DiskFeedInfoCache(cacheDirName); - } - - - // get a feed fetcher - private FeedFetcher getRomeFetcher() { + private SyndFeed fetchFeed(String url) throws IOException, InterruptedException, FeedException { - FeedFetcherCache feedCache = getRomeFetcherCache(); + HttpRequest request = requestBuilder.copy().uri(URI.create(url)).build(); - FeedFetcher feedFetcher; - if(feedCache != null) { - feedFetcher = new HttpURLFeedFetcher(feedCache); - } else { - feedFetcher = new HttpURLFeedFetcher(); + try(XmlReader reader = new XmlReader(client.send(request, ofInputStream()).body())) { + return new SyndFeedInput().build(reader); } - - // set options - feedFetcher.setUsingDeltaEncoding(false); - feedFetcher.setUserAgent("RollerPlanetAggregator"); - - return feedFetcher; + } } diff --git a/app/src/main/java/org/apache/roller/weblogger/planet/business/WebloggerRomeFeedFetcher.java b/app/src/main/java/org/apache/roller/weblogger/planet/business/WebloggerRomeFeedFetcher.java index 88f4810c5f..b69e6b8bed 100644 --- a/app/src/main/java/org/apache/roller/weblogger/planet/business/WebloggerRomeFeedFetcher.java +++ b/app/src/main/java/org/apache/roller/weblogger/planet/business/WebloggerRomeFeedFetcher.java @@ -47,7 +47,7 @@ */ public class WebloggerRomeFeedFetcher extends RomeFeedFetcher { - private static Log log = LogFactory.getLog(WebloggerRomeFeedFetcher.class); + private static final Log log = LogFactory.getLog(WebloggerRomeFeedFetcher.class); /** @@ -135,7 +135,7 @@ public Subscription fetchSubscription(String feedURL, Date lastModified) Map pagePlugins = ppmgr.getWeblogEntryPlugins(localWeblog); for ( WeblogEntry rollerEntry : entries ) { SubscriptionEntry entry = new SubscriptionEntry(); - String content = ""; + String content; if (!StringUtils.isEmpty(rollerEntry.getText())) { content = rollerEntry.getText(); } else { diff --git a/app/src/test/java/org/apache/roller/planet/util/rome/DiskFeedInfoCacheTest.java b/app/src/test/java/org/apache/roller/planet/util/rome/DiskFeedInfoCacheTest.java deleted file mode 100644 index 6a7b62e894..0000000000 --- a/app/src/test/java/org/apache/roller/planet/util/rome/DiskFeedInfoCacheTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2005 Sun Microsystems, Inc. - * - * 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.apache.roller.planet.util.rome; - -import com.rometools.fetcher.impl.DiskFeedInfoCache; -import com.rometools.fetcher.impl.SyndFeedInfo; -import org.apache.roller.weblogger.config.WebloggerConfig; -import org.junit.jupiter.api.Test; - -import java.io.File; -import java.net.URL; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * @author David M Johnson - */ -public class DiskFeedInfoCacheTest { - - @Test - public void testCache() throws Exception { - URL url = new URL("http://cnn.com"); - SyndFeedInfo info = new SyndFeedInfo(); - info.setUrl(url); - - String testPlanetCache = WebloggerConfig.getProperty("cache.dir"); - assertNotNull("testPlanetCache not null", testPlanetCache); - assertTrue( testPlanetCache.trim().length() > 0, "testPlanetCache not zero length"); - - File cacheDir = new File(testPlanetCache); - if (!cacheDir.exists()) cacheDir.mkdirs(); - - DiskFeedInfoCache cache = - new DiskFeedInfoCache(WebloggerConfig.getProperty("cache.dir")); - cache.setFeedInfo(info.getUrl(), info); - - SyndFeedInfo info2 = cache.getFeedInfo(url); - assertNotNull(info2); - assertEquals(url, info2.getUrl()); - } - -}