-
Notifications
You must be signed in to change notification settings - Fork 153
Follow alternate document location #292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2e1135d
Follow alternate document location
dr0i a2e6f8a
Remove unused imports
dr0i 2ef090b
Remove ignored test
dr0i a2f3c9f
Reorganize imports
dr0i 776e77d
Try-with on response in the submethod
dr0i 818b118
Avoid reading from closed stream
fsteeg 1d675d7
Abort if to many alternate links are followed.
dr0i 1229f73
Fix missing trim(); simplify value parsing
dr0i faddb48
Fix testing if object not null
dr0i 9100b13
Throw IOException instead of returning null
dr0i d5e2761
Close InputStream within finally block
dr0i File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 32 additions & 90 deletions
122
core/src/test/java/com/github/jsonldjava/core/MinimalSchemaOrgRegressionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,119 +1,61 @@ | ||
| 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; | ||
| 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(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.