ANY23-343 upgrade to jsonld-java version 0.12.0
authorHans <firedrake93@gmail.com>
Thu, 12 Apr 2018 20:58:44 +0000 (15:58 -0500)
committerHans <firedrake93@gmail.com>
Thu, 12 Apr 2018 20:58:44 +0000 (15:58 -0500)
core/src/main/java/org/apache/any23/extractor/rdf/JSONLDExtractor.java
core/src/test/java/org/apache/any23/extractor/rdf/JSONLDExtractorTest.java
pom.xml

index a073a21..402e267 100644 (file)
 
 package org.apache.any23.extractor.rdf;
 
-import com.github.jsonldjava.utils.JarCacheStorage;
 import com.github.jsonldjava.utils.JsonUtils;
 import org.apache.any23.extractor.ExtractionContext;
 import org.apache.any23.extractor.ExtractionResult;
 import org.apache.any23.extractor.ExtractorDescription;
-import org.apache.http.client.cache.HttpCacheEntry;
-import org.apache.http.client.cache.HttpCacheStorage;
-import org.apache.http.client.protocol.RequestAcceptEncoding;
-import org.apache.http.client.protocol.ResponseContentEncoding;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.DefaultRedirectStrategy;
-import org.apache.http.impl.client.cache.BasicHttpCacheStorage;
-import org.apache.http.impl.client.cache.CacheConfig;
-import org.apache.http.impl.client.cache.CachingHttpClientBuilder;
 import org.eclipse.rdf4j.rio.RDFParser;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.IOException;
-import java.lang.invoke.MethodHandles;
-import java.lang.reflect.Field;
 
 /**
  * Concrete implementation of {@link org.apache.any23.extractor.Extractor.ContentExtractor}
@@ -46,80 +30,20 @@ import java.lang.reflect.Field;
  */
 public class JSONLDExtractor extends BaseRDFExtractor {
 
-    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-
-    //TODO: the static members of this class can be removed once jsonldjava 0.11.2 is released
-    //See https://issues.apache.org/jira/browse/ANY23-336
-    static final boolean needsHttpClientSwap;
-
     static {
-        if (!(needsHttpClientSwap = isHttpClientSwapNeeded())) {
-            LOG.warn("The static members of this class are no longer needed.");
-        } else {
-            try {
-                Field field = JsonUtils.class.getDeclaredField("DEFAULT_HTTP_CLIENT");
-                field.setAccessible(true);
-                field.set(null, createDefaultHttpClient());
-            } catch (Throwable e) {
-                LOG.warn("failed to swap jsonldjava http client", e);
-            }
-        }
-    }
-
-    private static boolean isHttpClientSwapNeeded() {
+        //See https://issues.apache.org/jira/browse/ANY23-336
         try {
-            JsonUtils.class.getDeclaredField("JSONLD_JAVA_USER_AGENT");
-            return false;
-        } catch (Throwable th) {
-            return true;
-        }
-    }
-
-    private static CloseableHttpClient createDefaultHttpClient() {
-        // Common CacheConfig for both the JarCacheStorage and the underlying
-        // BasicHttpCacheStorage
-        final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(500)
-                .setMaxObjectSize(1024 * 256).setSharedCache(false)
-                .setHeuristicCachingEnabled(true).setHeuristicDefaultLifetime(86400).build();
-
-        final CloseableHttpClient result = CachingHttpClientBuilder.create()
-                // allow caching
-                .setCacheConfig(cacheConfig)
-                // Wrap the local JarCacheStorage around a BasicHttpCacheStorage
-                .setHttpCacheStorage(new JarCacheStorage0(null, cacheConfig,
-                        new BasicHttpCacheStorage(cacheConfig)))
-                // Support compressed data
-                // https://wayback.archive.org/web/20130901115452/http://hc.apache.org:80/httpcomponents-client-ga/tutorial/html/httpagent.html#d5e1238
-                .addInterceptorFirst(new RequestAcceptEncoding())
-                .addInterceptorFirst(new ResponseContentEncoding())
-                .setRedirectStrategy(DefaultRedirectStrategy.INSTANCE)
-                // use system defaults for proxy etc.
-                .useSystemProperties().build();
-
-        return result;
-    }
-
-    private static class JarCacheStorage0 extends JarCacheStorage {
-
-        private final HttpCacheStorage delegate;
-
-        public JarCacheStorage0(ClassLoader classLoader, CacheConfig cacheConfig,
-                               HttpCacheStorage delegate) {
-            super(classLoader, cacheConfig, delegate);
-            this.delegate = delegate;
-        }
-
-        @Override
-        public HttpCacheEntry getEntry(String key) throws IOException {
-            HttpCacheEntry entry = delegate.getEntry(key);
-            return entry != null ? entry : super.getEntry(key);
+            //This field was introduced in jsonld-java version 0.12.0
+            if ((Object)JsonUtils.JSONLD_JAVA_USER_AGENT instanceof Void) {
+                throw new Error("This error will never be thrown.");
+            }
+        } catch (NoSuchFieldError th) {
+            throw new AssertionError("You have an outdated version of jsonld-java on the classpath. " +
+                    "Upgrade to at least version 0.12.0. See: https://issues.apache.org/jira/browse/ANY23-336", th);
         }
     }
 
 
-
-
-
     public JSONLDExtractor(boolean verifyDataType, boolean stopAtFirstError) {
         super(verifyDataType, stopAtFirstError);
     }
index fcef3e4..f21e038 100644 (file)
@@ -59,13 +59,11 @@ public class JSONLDExtractorTest {
 
   @Test
   public void testRemoteContextCaching() throws Exception {
-    Assert.assertTrue("The static members of " + JSONLDExtractor.class + " can now be removed!",
-            JSONLDExtractor.needsHttpClientSwap);
     DocumentLoader documentLoader = new DocumentLoader();
     final String[] urls = {"http://schema.org/", "http://schema.org/docs/jsonldcontext.json"};
     for (String url : urls) {
       long start = System.currentTimeMillis();
-      for (int i = 1; i <= 10000; i++) {
+      for (int i = 1; i <= 1000; i++) {
         documentLoader.loadDocument(url);
 
         long seconds = (System.currentTimeMillis() - start) / 1000;
diff --git a/pom.xml b/pom.xml
index f0f809d..516ed64 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <dependency>
         <groupId>com.github.jsonld-java</groupId>
         <artifactId>jsonld-java</artifactId>
-        <version>0.11.1</version>
+        <version>0.12.0</version>
       </dependency>
       <dependency>
         <groupId>org.semarglproject</groupId>