public static void writeNanopub(Nanopub nanopub, OutputStream out, RDFFormat format) throws RDFHandlerException { RDFWriter writer = Rio.createWriter(format, out); writer.startRDF(); String s = nanopub.getUri().toString(); writer.handleNamespace("this", s); writer.handleNamespace("sub", s + "."); writer.handleNamespace("blank", s + ".."); writer.handleNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); writer.handleNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); writer.handleNamespace("rdfg", "http://www.w3.org/2004/03/trix/rdfg-1/"); writer.handleNamespace("xsd", "http://www.w3.org/2001/XMLSchema#"); writer.handleNamespace("owl", "http://www.w3.org/2002/07/owl#"); writer.handleNamespace("dc", "http://purl.org/dc/terms/"); writer.handleNamespace("pav", "http://swan.mindinformatics.org/ontologies/1.2/pav/"); writer.handleNamespace("np", "http://www.nanopub.org/nschema#"); for (Statement st : NanopubUtils.getStatements(nanopub)) { writer.handleStatement(st); } writer.endRDF(); }
public void publishNanopub(Nanopub nanopub) throws IOException { if (serverInfo == null) { if (serverUrls == null || serverUrls.isEmpty()) { serverIterator = new ServerIterator(); } else { serverIterator = new ServerIterator(serverUrls); } serverInfo = serverIterator.next(); } artifactCode = TrustyUriUtils.getArtifactCode(nanopub.getUri().toString()); if (verbose) { System.out.println("---"); System.out.println("Trying to publish nanopub: " + artifactCode); } while (serverInfo != null) { String serverUrl = serverInfo.getPublicUrl(); try { if (!ServerInfo.load(serverUrl).isPostNanopubsEnabled()) { serverInfo = serverIterator.next(); continue; } } catch (ServerInfoException ex) { serverInfo = serverIterator.next(); continue; } if (verbose) { System.out.println("Trying server: " + serverUrl); } try { HttpPost post = new HttpPost(serverUrl); String nanopubString = NanopubUtils.writeToString(nanopub, RDFFormat.TRIG); post.setEntity(new StringEntity(nanopubString, "UTF-8")); post.setHeader("Content-Type", RDFFormat.TRIG.getDefaultMIMEType()); HttpResponse response = HttpClientBuilder.create().build().execute(post); int code = response.getStatusLine().getStatusCode(); if (code >= 200 && code < 300) { if (usedServers.containsKey(serverUrl)) { usedServers.put(serverUrl, usedServers.get(serverUrl) + 1); } else { usedServers.put(serverUrl, 1); } if (verbose) { System.out.println("Published: " + artifactCode); } return; } else { if (verbose) { System.out.println( "Response: " + code + " " + response.getStatusLine().getReasonPhrase()); } } } catch (IOException ex) { if (verbose) { System.out.println(ex.getClass().getName() + ": " + ex.getMessage()); } } catch (OpenRDFException ex) { if (verbose) { System.out.println(ex.getClass().getName() + ": " + ex.getMessage()); } } serverInfo = serverIterator.next(); } serverInfo = null; throw new IOException("Failed to publish the nanopub"); }