[Git][java-team/jsonld-java][upstream] New upstream version 0.13.1

Andrius Merkys gitlab at salsa.debian.org
Mon Sep 14 07:29:58 BST 2020



Andrius Merkys pushed to branch upstream at Debian Java Maintainers / jsonld-java


Commits:
c3c3d532 by Andrius Merkys at 2020-09-14T01:48:18-04:00
New upstream version 0.13.1
- - - - -


9 changed files:

- core/pom.xml
- core/src/main/java/com/github/jsonldjava/utils/JsonLdUrl.java
- core/src/main/java/com/github/jsonldjava/utils/JsonUtils.java
- core/src/test/java/com/github/jsonldjava/core/LocalBaseTest.java
- core/src/test/java/com/github/jsonldjava/core/MinimalSchemaOrgRegressionTest.java
- core/src/test/java/com/github/jsonldjava/utils/JsonUtilsTest.java
- + core/src/test/resources/custom/base-0003-in.jsonld
- + core/src/test/resources/custom/base-0003-out.jsonld
- pom.xml


Changes:

=====================================
core/pom.xml
=====================================
@@ -4,7 +4,7 @@
 	<parent>
 		<artifactId>jsonld-java-parent</artifactId>
 		<groupId>com.github.jsonld-java</groupId>
-		<version>0.13.0</version>
+		<version>0.13.1</version>
 	</parent>
 	<modelVersion>4.0.0</modelVersion>
 	<artifactId>jsonld-java</artifactId>
@@ -74,6 +74,7 @@
 					<artifactSet>
 						<includes>
 							<include>com.google.guava:guava</include>
+							<include>com.google.guava:failureaccess</include>
 						</includes>
 					</artifactSet>
 					<relocations>
@@ -82,7 +83,6 @@
 							<shadedPattern>com.github.jsonldjava.shaded.com.google.common</shadedPattern>
 						</relocation>
 					</relocations>
-					<minimizeJar>true</minimizeJar>
 					<filters>
 						<filter>
 							<artifact>com.google.guava:guava</artifact>
@@ -90,6 +90,12 @@
 								<exclude>META-INF/maven/**</exclude>
 							</excludes>
 						</filter>
+						<filter>
+							<artifact>com.google.guava:failureaccess</artifact>
+							<excludes>
+								<exclude>META-INF/maven/**</exclude>
+							</excludes>
+						</filter>
 					</filters>
 				</configuration>
 				<executions>


=====================================
core/src/main/java/com/github/jsonldjava/utils/JsonLdUrl.java
=====================================
@@ -280,6 +280,10 @@ public class JsonLdUrl {
                 // add fragment to the end manually (as URI#resolve does it wrong)
                 return uri.toString() + pathToResolve;
             }
+            // ensure a slash between the authority and the path of a URL
+            if (uri.getSchemeSpecificPart().startsWith("//") && !uri.getSchemeSpecificPart().matches("//.*/.*")) {
+                uri = new URI(uri + "/");
+            }
             uri = uri.resolve(pathToResolve);
             // java doesn't discard unnecessary dot segments
             String path = uri.getPath();


=====================================
core/src/main/java/com/github/jsonldjava/utils/JsonUtils.java
=====================================
@@ -9,14 +9,28 @@ import java.io.StringReader;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Map;
 
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.core.JsonGenerationException;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonToken;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.github.jsonldjava.core.DocumentLoader;
+import com.github.jsonldjava.core.JsonLdApi;
+import com.github.jsonldjava.core.JsonLdProcessor;
+
 import org.apache.commons.io.ByteOrderMark;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.input.BOMInputStream;
+import org.apache.http.Header;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpUriRequest;
@@ -28,17 +42,8 @@ import org.apache.http.impl.client.HttpClientBuilder;
 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 com.fasterxml.jackson.core.JsonFactory;
-import com.fasterxml.jackson.core.JsonGenerationException;
-import com.fasterxml.jackson.core.JsonGenerator;
-import com.fasterxml.jackson.core.JsonParseException;
-import com.fasterxml.jackson.core.JsonParser;
-import com.fasterxml.jackson.core.JsonToken;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.github.jsonldjava.core.DocumentLoader;
-import com.github.jsonldjava.core.JsonLdApi;
-import com.github.jsonldjava.core.JsonLdProcessor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Functions used to make loading, parsing, and serializing JSON easy using
@@ -66,6 +71,8 @@ public class JsonUtils {
     private static final JsonFactory JSON_FACTORY = new JsonFactory(JSON_MAPPER);
 
     private static volatile CloseableHttpClient DEFAULT_HTTP_CLIENT;
+    // Avoid possible endless loop when following alternate locations
+    private static final int MAX_LINKS_FOLLOW = 20;
 
     static {
         // Disable default Jackson behaviour to close
@@ -109,6 +116,10 @@ public class JsonUtils {
                 }
             }
             return fromInputStream(bOMInputStream, charset);
+        } finally {
+            if (input != null) {
+                input.close();
+            }
         }
     }
 
@@ -335,40 +346,69 @@ public class JsonUtils {
         final String protocol = url.getProtocol();
         // We can only use the Apache HTTPClient for HTTP/HTTPS, so use the
         // native java client for the others
-        CloseableHttpResponse response = null;
-        InputStream in = null;
-        try {
-            if (!protocol.equalsIgnoreCase("http") && !protocol.equalsIgnoreCase("https")) {
-                // Can't use the HTTP client for those!
-                // Fallback to Java's built-in JsonLdUrl handler. No need for
-                // Accept headers as it's likely to be file: or jar:
-                in = url.openStream();
-            } else {
-                final HttpUriRequest request = new HttpGet(url.toExternalForm());
-                // We prefer application/ld+json, but fallback to
-                // application/json
-                // or whatever is available
-                request.addHeader("Accept", ACCEPT_HEADER);
-
-                response = httpClient.execute(request);
-                final int status = response.getStatusLine().getStatusCode();
-                if (status != 200 && status != 203) {
-                    throw new IOException("Can't retrieve " + url + ", status code: " + status);
-                }
-                in = response.getEntity().getContent();
+        if (!protocol.equalsIgnoreCase("http") && !protocol.equalsIgnoreCase("https")) {
+            // Can't use the HTTP client for those!
+            // Fallback to Java's built-in JsonLdUrl handler. No need for
+            // Accept headers as it's likely to be file: or jar:
+            return fromInputStream(url.openStream());
+        } else {
+            return fromJsonLdViaHttpUri(url, httpClient, 0);
+        }
+    }
+
+    private static Object fromJsonLdViaHttpUri(final URL url, final CloseableHttpClient httpClient, int linksFollowed)
+            throws IOException {
+        final HttpUriRequest request = new HttpGet(url.toExternalForm());
+        // We prefer application/ld+json, but fallback to application/json
+        // or whatever is available
+        request.addHeader("Accept", ACCEPT_HEADER);
+        try (CloseableHttpResponse response = httpClient.execute(request)) {
+            final int status = response.getStatusLine().getStatusCode();
+            if (status != 200 && status != 203) {
+                throw new IOException("Can't retrieve " + url + ", status code: " + status);
             }
-            return fromInputStream(in);
-        } finally {
-            try {
-                if (in != null) {
-                    in.close();
+            // follow alternate document location
+            // https://www.w3.org/TR/json-ld11/#alternate-document-location
+            URL alternateLink = alternateLink(url, response);
+            if (alternateLink != null) {
+                linksFollowed++;
+                if (linksFollowed > MAX_LINKS_FOLLOW) {
+                    throw new IOException("Too many alternate links followed. This may indicate a cycle. Aborting.");
                 }
-            } finally {
-                if (response != null) {
-                    response.close();
+                return fromJsonLdViaHttpUri(alternateLink, httpClient, linksFollowed);
+            }
+            return fromInputStream(response.getEntity().getContent());
+        }
+    }
+
+    private static URL alternateLink(URL url, CloseableHttpResponse response)
+            throws MalformedURLException {
+        if (response.getEntity().getContentType() != null
+                && !response.getEntity().getContentType().getValue().equals("application/ld+json")) {
+            for (Header header : response.getAllHeaders()) {
+                if (header.getName().equalsIgnoreCase("link")) {
+                    String alternateLink = "";
+                    boolean relAlternate = false;
+                    boolean jsonld = false;
+                    for (String value : header.getValue().split(";")) {
+                        value=value.trim();
+                        if (value.startsWith("<") && value.endsWith(">")) {
+                            alternateLink = value.substring(1, value.length() - 1);
+                        }
+                        if (value.startsWith("type=\"application/ld+json\"")) {
+                            jsonld = true;
+                        }
+                        if (value.startsWith("rel=\"alternate\"")) {
+                            relAlternate = true;
+                        }
+                    }
+                    if (jsonld && relAlternate && !alternateLink.isEmpty()) {
+                        return new URL(url.getProtocol() + "://" + url.getAuthority() + alternateLink);
+                    }
                 }
             }
         }
+        return null;
     }
 
     /**
@@ -384,7 +424,7 @@ public class JsonUtils {
      * @throws IOException
      *             If there was an IO error during parsing.
      */
-    public static Object fromURLJavaNet(java.net.URL url) throws JsonParseException, IOException {
+    public static Object fromURLJavaNet(URL url) throws JsonParseException, IOException {
         final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
         urlConn.addRequestProperty("Accept", ACCEPT_HEADER);
 


=====================================
core/src/test/java/com/github/jsonldjava/core/LocalBaseTest.java
=====================================
@@ -1,12 +1,14 @@
 package com.github.jsonldjava.core;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.nio.charset.Charset;
+import java.util.List;
 
 import org.junit.Test;
 
@@ -55,4 +57,25 @@ public class LocalBaseTest {
         assertEquals(expanded, output);
     }
 
+    @Test
+    public void testUriResolveWhenExpandingBase() throws Exception {
+
+        final Reader reader = new BufferedReader(new InputStreamReader(
+                this.getClass().getResourceAsStream("/custom/base-0003-in.jsonld"),
+                Charset.forName("UTF-8")));
+        final Object input = JsonUtils.fromReader(reader);
+        assertNotNull(input);
+
+        final JsonLdOptions options = new JsonLdOptions();
+        final List<Object> expanded = JsonLdProcessor.expand(input, options);
+        assertFalse("expanded form must not be empty", expanded.isEmpty());
+
+        final Reader outReader = new BufferedReader(new InputStreamReader(
+                this.getClass().getResourceAsStream("/custom/base-0003-out.jsonld"),
+                Charset.forName("UTF-8")));
+        final Object expected = JsonLdProcessor.expand(JsonUtils.fromReader(outReader), options);
+        assertNotNull(expected);
+        assertEquals(expected, expanded);
+    }
+
 }


=====================================
core/src/test/java/com/github/jsonldjava/core/MinimalSchemaOrgRegressionTest.java
=====================================
@@ -1,20 +1,13 @@
 package com.github.jsonldjava.core;
 
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.StringWriter;
-import java.net.HttpURLConnection;
 import java.net.URL;
-import java.nio.charset.StandardCharsets;
 
-import org.apache.commons.io.IOUtils;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpUriRequest;
+import com.github.jsonldjava.utils.JarCacheStorage;
+import com.github.jsonldjava.utils.JsonUtils;
+
 import org.apache.http.client.protocol.RequestAcceptEncoding;
 import org.apache.http.client.protocol.ResponseContentEncoding;
 import org.apache.http.impl.client.CloseableHttpClient;
@@ -22,98 +15,47 @@ 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.junit.Ignore;
 import org.junit.Test;
 
-import com.github.jsonldjava.utils.JarCacheStorage;
-
 public class MinimalSchemaOrgRegressionTest {
 
-    private static final String ACCEPT_HEADER = "application/ld+json, application/json;q=0.9, application/javascript;q=0.5, text/javascript;q=0.5, text/plain;q=0.2, */*;q=0.1";
-
-    @Ignore("Java API does not have any way of redirecting automatically from HTTP to HTTPS, which breaks schema.org usage with it")
+    /**
+     * Tests getting JSON from schema.org with the HTTP Accept header set to
+     * {@value com.github.jsonldjava.utils.JsonUtils#ACCEPT_HEADER}? .
+     */
     @Test
-    public void testHttpURLConnection() throws Exception {
+    public void testApacheHttpClient() throws Exception {
         final URL url = new URL("http://schema.org/");
-        final boolean followRedirectsSetting = HttpURLConnection.getFollowRedirects();
-        try {
-            HttpURLConnection.setFollowRedirects(true);
-            final HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
-            urlConn.setInstanceFollowRedirects(true);
-            urlConn.addRequestProperty("Accept", ACCEPT_HEADER);
-
-            final InputStream directStream = urlConn.getInputStream();
-            verifyInputStream(directStream);
-        } finally {
-            HttpURLConnection.setFollowRedirects(followRedirectsSetting);
-        }
+        // Common CacheConfig for both the JarCacheStorage and the underlying
+        // BasicHttpCacheStorage
+        final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000)
+        .setMaxObjectSize(1024 * 128).build();
+        
+        final CloseableHttpClient httpClient = CachingHttpClientBuilder.create()
+        // allow caching
+        .setCacheConfig(cacheConfig)
+        // Wrap the local JarCacheStorage around a BasicHttpCacheStorage
+        .setHttpCacheStorage(new JarCacheStorage(null, cacheConfig,
+        new BasicHttpCacheStorage(cacheConfig)))
+        // Support compressed data
+        // http://hc.apache.org/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();
+        
+        Object content = JsonUtils.fromURL(url, httpClient);
+        checkBasicConditions(content.toString());
     }
 
-    private void verifyInputStream(InputStream directStream) throws IOException {
-        assertNotNull("InputStream was null", directStream);
-        final StringWriter output = new StringWriter();
-        try {
-            IOUtils.copy(directStream, output, StandardCharsets.UTF_8);
-        } finally {
-            directStream.close();
-            output.flush();
-        }
-        final String outputString = output.toString();
-        // System.out.println(outputString);
+    private void checkBasicConditions(final String outputString) {
         // Test for some basic conditions without including the JSON/JSON-LD
         // parsing code here
-        // assertTrue(outputString, outputString.endsWith("}"));
+        assertTrue(outputString, outputString.endsWith("}"));
         assertFalse("Output string should not be empty: " + outputString.length(),
                 outputString.isEmpty());
         assertTrue("Unexpected length: " + outputString.length(), outputString.length() > 100000);
     }
-
-    @Test
-    public void testApacheHttpClient() throws Exception {
-        final URL url = new URL("http://schema.org/");
-        // Common CacheConfig for both the JarCacheStorage and the underlying
-        // BasicHttpCacheStorage
-        final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000)
-                .setMaxObjectSize(1024 * 128).build();
-
-        final CloseableHttpClient httpClient = CachingHttpClientBuilder.create()
-                // allow caching
-                .setCacheConfig(cacheConfig)
-                // Wrap the local JarCacheStorage around a BasicHttpCacheStorage
-                .setHttpCacheStorage(new JarCacheStorage(null, cacheConfig,
-                        new BasicHttpCacheStorage(cacheConfig)))
-                // Support compressed data
-                // http://hc.apache.org/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();
-
-        try {
-            final HttpUriRequest request = new HttpGet(url.toExternalForm());
-            // We prefer application/ld+json, but fallback to application/json
-            // or whatever is available
-            request.addHeader("Accept", ACCEPT_HEADER);
-
-            final CloseableHttpResponse response = httpClient.execute(request);
-            try {
-                final int status = response.getStatusLine().getStatusCode();
-                if (status != 200 && status != 203) {
-                    throw new IOException("Can't retrieve " + url + ", status code: " + status);
-                }
-                final InputStream content = response.getEntity().getContent();
-                verifyInputStream(content);
-            } finally {
-                if (response != null) {
-                    response.close();
-                }
-            }
-        } finally {
-            if (httpClient != null) {
-                httpClient.close();
-            }
-        }
-    }
-
+    
 }


=====================================
core/src/test/java/com/github/jsonldjava/utils/JsonUtilsTest.java
=====================================
@@ -1,5 +1,7 @@
 package com.github.jsonldjava.utils;
 
+
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
@@ -15,6 +17,19 @@ import com.fasterxml.jackson.core.JsonParser;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
 public class JsonUtilsTest {
+    @Test
+    public void resolveTest() {
+        final String baseUri = "http://mysite.net";
+        final String pathToResolve = "picture.jpg";
+        String resolve = "";
+
+        try {
+            resolve = JsonLdUrl.resolve(baseUri, pathToResolve);
+            assertEquals(baseUri + "/" + pathToResolve, resolve);
+        } catch (final Exception e) {
+            assertTrue(false);
+        }
+    }
 
     @SuppressWarnings("unchecked")
     @Test
@@ -25,13 +40,12 @@ public class JsonUtilsTest {
 
         try {
             obj = JsonUtils.fromString(testString);
+            assertTrue(((Map<String, Object>) obj).containsKey("seq"));
+            assertTrue(((Map<String, Object>) obj).get("seq") instanceof Number);
         } catch (final Exception e) {
             assertTrue(false);
         }
 
-        assertTrue(((Map<String, Object>) obj).containsKey("seq"));
-        assertTrue(((Map<String, Object>) obj).get("seq") instanceof Number);
-
         try {
             obj = JsonUtils.fromString(testFailure);
             assertTrue(false);


=====================================
core/src/test/resources/custom/base-0003-in.jsonld
=====================================
@@ -0,0 +1,17 @@
+{
+    "@context": {
+        "@base": "http://mysite.net",
+        "DataSet": "http://schema.org/DataSet",
+        "CreativeWork": "http://schema.org/CreativeWork"
+    },
+    "@graph": [
+        {
+            "@type": "CreativeWork",
+            "@id": "picture.jpg"
+        },
+        {
+            "@id": "./",
+            "@type": "DataSet"
+        }
+    ]
+}


=====================================
core/src/test/resources/custom/base-0003-out.jsonld
=====================================
@@ -0,0 +1,7 @@
+[ {
+  "@id" : "http://mysite.net/picture.jpg",
+  "@type" : [ "http://schema.org/CreativeWork" ]
+}, {
+  "@id" : "http://mysite.net/",
+  "@type" : [ "http://schema.org/DataSet" ]
+} ]


=====================================
pom.xml
=====================================
@@ -4,7 +4,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>com.github.jsonld-java</groupId>
 	<artifactId>jsonld-java-parent</artifactId>
-	<version>0.13.0</version>
+	<version>0.13.1</version>
 	<name>JSONLD Java :: Parent</name>
 	<description>Json-LD Java Parent POM</description>
 	<packaging>pom</packaging>
@@ -39,11 +39,11 @@
 		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 
-		<httpclient.version>4.5.10</httpclient.version>
-		<httpcore.version>4.4.12</httpcore.version>
-		<jackson.version>2.10.1</jackson.version>
-		<junit.version>4.12</junit.version>
-		<slf4j.version>1.7.29</slf4j.version>
+		<httpclient.version>4.5.12</httpclient.version>
+		<httpcore.version>4.4.13</httpcore.version>
+		<jackson.version>2.11.2</jackson.version>
+		<junit.version>4.13</junit.version>
+		<slf4j.version>1.7.30</slf4j.version>
 
 		<last-compare-version>0.11.0</last-compare-version>
 	</properties>
@@ -192,7 +192,7 @@
 			<dependency>
 				<groupId>commons-codec</groupId>
 				<artifactId>commons-codec</artifactId>
-				<version>1.13</version>
+				<version>1.15</version>
 			</dependency>
 			<dependency>
 				<groupId>org.mockito</groupId>
@@ -202,14 +202,14 @@
 			<dependency>
 				<groupId>commons-io</groupId>
 				<artifactId>commons-io</artifactId>
-				<version>2.6</version>
+				<version>2.7</version>
 			</dependency>
 			<!-- Reuse Guava, but shade it to avoid transitive classpath issues for 
 				users given the high major release rate -->
 			<dependency>
 				<groupId>com.google.guava</groupId>
 				<artifactId>guava</artifactId>
-				<version>28.1-jre</version>
+				<version>29.0-jre</version>
 			</dependency>
 		</dependencies>
 	</dependencyManagement>
@@ -267,7 +267,7 @@
 						<dependency>
 							<groupId>org.codehaus.mojo</groupId>
 							<artifactId>extra-enforcer-rules</artifactId>
-							<version>1.2</version>
+							<version>1.3</version>
 						</dependency>
 					</dependencies>
 				</plugin>



View it on GitLab: https://salsa.debian.org/java-team/jsonld-java/-/commit/c3c3d5320043967e8288fd7c5c2b327a2a929c6c

-- 
View it on GitLab: https://salsa.debian.org/java-team/jsonld-java/-/commit/c3c3d5320043967e8288fd7c5c2b327a2a929c6c
You're receiving this email because of your account on salsa.debian.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-java-commits/attachments/20200914/b3981633/attachment.html>


More information about the pkg-java-commits mailing list