/** * Generate a compact representation of the input stream * * @param input The input JSON document * @param contextUrl the location of a context URL * @return the compacted JSON Object */ public String compact(final InputStream input, final String contextUrl) { LOGGER.info("using context from: {}", contextUrl); try { return doCompact(input, fromURL(new URL(contextUrl))); } catch (final MalformedURLException ex) { throw new RuntimeException("Invalid URL", ex); } catch (final JsonParseException ex) { throw new RuntimeException("Error parsing JSON", ex); } catch (final JsonGenerationException ex) { throw new RuntimeException("Error generating JSON", ex); } catch (final IOException ex) { throw new RuntimeException("Error reading/writing JSON document", ex); } }
@Test(expected = IOException.class) public void jarCacheMissThreadCtx() throws Exception { final URLClassLoader findNothingCL = new URLClassLoader(new URL[] {}, null); Thread.currentThread().setContextClassLoader(findNothingCL); JsonUtils.fromURL(new URL("http://nonexisting.example.com/context"), documentLoader.getHttpClient()); }
private Object readSource() throws IOException { // Due to checked IOException we can't easily // do this with .map and .orElseGet() if (getSourceInputStream().isPresent()) { return JsonUtils.fromInputStream(getSourceInputStream().get()); } if (getSourceIri().isPresent()) { // TODO: propagate @base from content return JsonUtils.fromURL(asURL(getSourceIri().get()), JsonUtils.getDefaultHttpClient()); } if (getSourceFile().isPresent()) { try (InputStream inputStream = Files.newInputStream(getSourceFile().get())) { return JsonUtils.fromInputStream(inputStream); } } throw new IllegalStateException("No known source found"); }
return new RemoteDocument(url, JsonUtils.fromURL(new URL(url), getHttpClient())); } catch (final Exception e) { throw new JsonLdError(JsonLdError.Error.LOADING_REMOTE_CONTEXT_FAILED, url, e);
return new RemoteDocument(url, JsonUtils.fromURL(new URL(url), getHttpClient())); } catch (final Exception e) { throw new JsonLdError(JsonLdError.Error.LOADING_REMOTE_CONTEXT_FAILED, url, e);
@Test(expected = IOException.class) public void jarCacheMiss404() throws Exception { // Should fail-fast as nonexisting.example.com is not in DNS JsonUtils.fromURL(new URL("http://nonexisting.example.com/miss"), documentLoader.getHttpClient()); }
@Test public void fromURLredirectHTTPSToHTTP() throws Exception { final URL url = new URL("https://w3id.org/bundle/context"); final Object context = JsonUtils.fromURL(url, documentLoader.getHttpClient()); // Should not fail because of // http://stackoverflow.com/questions/1884230/java-doesnt-follow-redirect-in-urlconnection // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4620571 assertTrue(context instanceof Map); assertFalse(((Map<?, ?>) context).isEmpty()); }
@Test public void jarCacheHitThreadCtx() throws Exception { final URL url = new URL("http://nonexisting.example.com/nested/hello"); final URL nestedJar = getClass().getResource("/nested.jar"); try { JsonUtils.fromURL(url, documentLoader.getHttpClient()); fail("Should not be able to find nested/hello yet"); } catch (final IOException ex) { // expected } Thread.currentThread().setContextClassLoader(null); try { JsonUtils.fromURL(url, documentLoader.getHttpClient()); fail("Should not be able to find nested/hello yet"); } catch (final IOException ex) { // expected } final ClassLoader cl = new URLClassLoader(new URL[] { nestedJar }); Thread.currentThread().setContextClassLoader(cl); final Object hello = JsonUtils.fromURL(url, documentLoader.getHttpClient()); assertTrue(hello instanceof Map); assertEquals("World!", ((Map) hello).get("Hello")); }
@Test public void jarCacheHit() throws Exception { // If no cache, should fail-fast as nonexisting.example.com is not in // DNS final Object context = JsonUtils.fromURL(new URL("http://nonexisting.example.com/context"), documentLoader.getHttpClient()); assertTrue(context instanceof Map); assertTrue(((Map) context).containsKey("@context")); }
@Test public void fromURLBomTest0004() throws Exception { final URL contexttest = getClass().getResource("/custom/contexttest-0004.jsonld"); assertNotNull(contexttest); final Object context = JsonUtils.fromURL(contexttest, documentLoader.getHttpClient()); assertTrue(context instanceof Map); assertFalse(((Map<?, ?>) context).isEmpty()); }
@Test public void fromURLredirect() throws Exception { final URL url = new URL("http://purl.org/wf4ever/ro-bundle/context.json"); final Object context = JsonUtils.fromURL(url, documentLoader.getHttpClient()); assertTrue(context instanceof Map); assertFalse(((Map<?, ?>) context).isEmpty()); }
@Test public void fromURLTest0001() throws Exception { final URL contexttest = getClass().getResource("/custom/contexttest-0001.jsonld"); assertNotNull(contexttest); final Object context = JsonUtils.fromURL(contexttest, documentLoader.getHttpClient()); assertTrue(context instanceof Map); final Map<String, Object> contextMap = (Map<String, Object>) context; assertEquals(1, contextMap.size()); final Map<String, Object> cont = (Map<String, Object>) contextMap.get("@context"); assertEquals(3, cont.size()); assertEquals("http://example.org/", cont.get("ex")); final Map<String, Object> term1 = (Map<String, Object>) cont.get("term1"); assertEquals("ex:term1", term1.get("@id")); }
@Test public void fromURLTest0002() throws Exception { final URL contexttest = getClass().getResource("/custom/contexttest-0002.jsonld"); assertNotNull(contexttest); final Object context = JsonUtils.fromURL(contexttest, documentLoader.getHttpClient()); assertTrue(context instanceof List); final List<Map<String, Object>> contextList = (List<Map<String, Object>>) context; final Map<String, Object> contextMap1 = contextList.get(0); assertEquals(1, contextMap1.size()); final Map<String, Object> cont1 = (Map<String, Object>) contextMap1.get("@context"); assertEquals(2, cont1.size()); assertEquals("http://example.org/", cont1.get("ex")); final Map<String, Object> term1 = (Map<String, Object>) cont1.get("term1"); assertEquals("ex:term1", term1.get("@id")); final Map<String, Object> contextMap2 = contextList.get(1); assertEquals(1, contextMap2.size()); final Map<String, Object> cont2 = (Map<String, Object>) contextMap2.get("@context"); assertEquals(1, cont2.size()); final Map<String, Object> term2 = (Map<String, Object>) cont2.get("term2"); assertEquals("ex:term2", term2.get("@id")); }
@Test public void fromURLCustomHandler() throws Exception { final AtomicInteger requests = new AtomicInteger(); final URLStreamHandler handler = new URLStreamHandler() { @Override protected URLConnection openConnection(URL u) throws IOException { return new URLConnection(u) { @Override public void connect() throws IOException { return; } @Override public InputStream getInputStream() throws IOException { requests.incrementAndGet(); return getClass().getResourceAsStream("/custom/contexttest-0001.jsonld"); } }; } }; final URL url = new URL(null, "jsonldtest:context", handler); assertEquals(0, requests.get()); final Object context = JsonUtils.fromURL(url, documentLoader.getHttpClient()); assertEquals(1, requests.get()); assertTrue(context instanceof Map); assertFalse(((Map<?, ?>) context).isEmpty()); }
@Ignore("Caching failed without any apparent cause on the client side") @Test public void fromURLCache() throws Exception { final URL url = new URL("https://json-ld.org/contexts/person.jsonld"); JsonUtils.fromURL(url, documentLoader.getHttpClient()); // Now try to get it again and ensure it is // cached final HttpClient clientCached = documentLoader.getHttpClient(); final HttpUriRequest getCached = new HttpGet(url.toURI()); getCached.setHeader("Accept", JsonUtils.ACCEPT_HEADER); final HttpCacheContext localContextCached = HttpCacheContext.create(); final HttpResponse respoCached = clientCached.execute(getCached, localContextCached); EntityUtils.consume(respoCached.getEntity()); // Check cache status // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html final CacheResponseStatus responseStatusCached = localContextCached .getCacheResponseStatus(); assertNotEquals(CacheResponseStatus.CACHE_MISS, responseStatusCached); }
@Test public void toRdfWithNamespace() throws Exception { final URL contextUrl = getClass().getResource("/custom/contexttest-0003.jsonld"); assertNotNull(contextUrl); final Object context = JsonUtils.fromURL(contextUrl, JsonUtils.getDefaultHttpClient()); assertNotNull(context); final JsonLdOptions options = new JsonLdOptions(); options.useNamespaces = true; final RDFDataset rdf = (RDFDataset) JsonLdProcessor.toRDF(context, options); // System.out.println(rdf.getNamespaces()); assertEquals("http://vocab.getty.edu/aat/", rdf.getNamespace("aat")); assertEquals("http://vocab.getty.edu/aat/rev/", rdf.getNamespace("aat_rev")); }
documentLoader.setHttpClient(fakeHttpClient(httpRequest)); try { final Object context = JsonUtils.fromURL(url, documentLoader.getHttpClient()); assertTrue(context instanceof Map); } finally {