You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by mb...@apache.org on 2021/07/04 22:14:13 UTC

[roller] branch master updated: replaced deprecated rome-fetcher with java 11 HttpClient.

This is an automated email from the ASF dual-hosted git repository.

mbien pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/roller.git


The following commit(s) were added to refs/heads/master by this push:
     new 043da93  replaced deprecated rome-fetcher with java 11 HttpClient.
043da93 is described below

commit 043da93db5af3e68007365855c26f3ff6c5a33de
Author: Michael Bien <mb...@gmail.com>
AuthorDate: Thu May 20 08:12:51 2021 +0200

    replaced deprecated rome-fetcher with java 11 HttpClient.
---
 app/pom.xml                                        |   2 +-
 .../planet/business/fetcher/RomeFeedFetcher.java   | 138 ++++++---------------
 .../planet/business/WebloggerRomeFeedFetcher.java  |   4 +-
 .../planet/util/rome/DiskFeedInfoCacheTest.java    |  56 ---------
 4 files changed, 38 insertions(+), 162 deletions(-)

diff --git a/app/pom.xml b/app/pom.xml
index 1ee511e..97603f1 100644
--- a/app/pom.xml
+++ b/app/pom.xml
@@ -505,7 +505,7 @@ limitations under the License.
 
         <dependency>
             <groupId>com.rometools</groupId>
-            <artifactId>rome-fetcher</artifactId>
+            <artifactId>rome</artifactId>
             <version>${rome.version}</version>
             <scope>compile</scope>
             <exclusions>
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 e0e8328..83777c1 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.SyndCategory;
 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.Log;
 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 class RomeFeedFetcher implements org.apache.roller.planet.business.fetche
      * @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 class RomeFeedFetcher implements org.apache.roller.planet.business.fetche
             // 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 class RomeFeedFetcher implements org.apache.roller.planet.business.fetche
                 cal.add(Calendar.DATE, -1);
             }
             
-            if(newEntry != null) {
-                newSub.addEntry(newEntry);
-            }
+            newSub.addEntry(newEntry);
         }
         
         log.debug(feedEntries.size()+" entries included");
@@ -230,8 +216,8 @@ public class RomeFeedFetcher implements org.apache.roller.planet.business.fetche
         // copy categories
         if (!romeEntry.getCategories().isEmpty()) {
             List<String> 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 @@ public class RomeFeedFetcher implements org.apache.roller.planet.business.fetche
         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 88f4810..b69e6b8 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 @@ import org.apache.roller.weblogger.pojos.WeblogEntrySearchCriteria;
  */
 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 class WebloggerRomeFeedFetcher extends RomeFeedFetcher {
             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 6a7b62e..0000000
--- 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());
-    }
-    
-}