@Test public void differentHttpClient() throws Exception { // Custom http client try { documentLoader.setHttpClient(JsonUtils.createDefaultHttpClient()); assertNotSame(documentLoader.getHttpClient(), new DocumentLoader().getHttpClient()); } finally { // Use default again documentLoader.setHttpClient(null); assertSame(documentLoader.getHttpClient(), new DocumentLoader().getHttpClient()); } }
/** * Injects a document at the specified URL for offline use. */ public Builder injectDocument(String url, CharSequence content) { documentLoader.addInjectedDoc(url, content.toString()); return this; }
/** * Parses a JSON-LD document, from the contents of the JSON resource * resolved from the JsonLdUrl, to an object that can be used as input for * the {@link JsonLdApi} and {@link JsonLdProcessor} methods. * * @param url * The JsonLdUrl to resolve * @return A JSON Object. * @throws JsonParseException * If there was a JSON related error during parsing. * @throws IOException * If there was an IO error during parsing. */ public static Object fromURL(java.net.URL url) throws JsonParseException, IOException { return DOCUMENT_LOADER.fromURL(url); }
@Test public void sharedHttpClient() throws Exception { // Should be the same instance unless explicitly set assertSame(documentLoader.getHttpClient(), new DocumentLoader().getHttpClient()); }
final HttpResponse response = getHttpClient().execute(request); final int status = response.getStatusLine().getStatusCode(); if (status != 200 && status != 203) {
@Test public void testRemoteContextCaching() throws Exception { final String[] urls = { "http://schema.org/", "http://schema.org/docs/jsonldcontext.json" }; for (final String url : urls) { final long start = System.currentTimeMillis(); for (int i = 1; i <= 1000; i++) { documentLoader.loadDocument(url); final long seconds = (System.currentTimeMillis() - start) / 1000; if (seconds > 60) { fail(String.format("Took %s seconds to access %s %s times", seconds, url, i)); break; } } } } }
@Test public void contextLoadedFromJarCache() throws Exception { // RDFToManifest.makeBaseURI(); // trigger static{} block @SuppressWarnings("unchecked") Map<String, Object> context = (Map<String, Object>) new DocumentLoader() .fromURL(new URL(CONTEXT)); // FIXME: jsonld-java 0.3 and later uses DocumentLoader instead of // JSONUtils // Map<String, Object> context = (Map<String, Object>) // JSONUtils.fromURL(new URL(CONTEXT)); Object retrievedFrom = context.get("http://purl.org/pav/retrievedFrom"); assertNotNull("Did not load context from cache: " + CONTEXT, retrievedFrom); } }
@Test public void injectContext() throws Exception { final Object jsonObject = JsonUtils.fromString( "{ \"@context\":\"http://nonexisting.example.com/thing\", \"pony\":5 }"); final JsonLdOptions options = new JsonLdOptions(); // Verify fails to find context by default try { JsonLdProcessor.expand(jsonObject, options); fail("Expected exception to occur"); } catch (final JsonLdError e) { // Success } // Inject context final DocumentLoader dl = new DocumentLoader(); dl.addInjectedDoc("http://nonexisting.example.com/thing", "{ \"@context\": { \"pony\":\"http://nonexisting.example.com/thing/pony\" } }"); options.setDocumentLoader(dl); // Execute final List<Object> expand = JsonLdProcessor.expand(jsonObject, options); // Verify result final Object v = ((Map<Object, Object>) ((List<Object>) ((Map<Object, Object>) expand .get(0)).get("http://nonexisting.example.com/thing/pony")).get(0)).get("@value"); assertEquals(5, v); }
public Exercise1(){ dl = new DocumentLoader(); loadData(); }
public HttpClient getHttpClient() { if (httpClient == null) { return getDefaultHttpClient(); } return httpClient; }
final InputStream in = openStreamFromURL(url); try { final JsonParser parser = jsonFactory.createParser(in);
@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()); }
final RemoteDocument tmp = opts.getDocumentLoader().loadDocument((String) input); input = tmp.document;
public Builder() { valueMapper = StandardJavaValueMapper.getInstance(); documentLoader = new DocumentLoader(); STANDARD_DOCUMENTS.forEach(documentLoader::addInjectedDoc); for (Bdio.Class bdioClass : Bdio.Class.values()) { if (bdioClass.embedded()) { embeddedTypes.add(bdioClass.toString()); } } }
final ArgumentCaptor<HttpUriRequest> httpRequest = ArgumentCaptor .forClass(HttpUriRequest.class); documentLoader.setHttpClient(fakeHttpClient(httpRequest)); try { final Object context = JsonUtils.fromURL(url, documentLoader.getHttpClient()); assertTrue(context instanceof Map); } finally { documentLoader.setHttpClient(null); assertSame(documentLoader.getHttpClient(), new DocumentLoader().getHttpClient());
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);
final RemoteDocument tmp = opts.getDocumentLoader().loadDocument((String) input); input = tmp.getDocument();
public RemoteDocument loadDocument(String url) throws JsonLdError { final RemoteDocument doc = new RemoteDocument(url, null); try { doc.setDocument(fromURL(new URL(url))); } catch (final Exception e) { new JsonLdError(JsonLdError.Error.LOADING_REMOTE_CONTEXT_FAILED, url); } return doc; }
/** * Supports "reverse injection" from an existing remote document. * <p> * Generally the document contents are already parsed JSON content, however this method will also accept * document contents represented as a string of JSON data. */ public Builder injectDocument(RemoteDocument document) { String url = document.getDocumentUrl(); if (document.getDocument() instanceof CharSequence) { // Avoid unnecessary JSON serialization return injectDocument(url, (CharSequence) document.getDocument()); } else { try { documentLoader.addInjectedDoc(url, JsonUtils.toString(document.getDocument())); return this; } catch (IOException e) { throw new JsonLdError(JsonLdError.Error.LOADING_INJECTED_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);