use of org.nanopub.Nanopub in project nanopub-server by tkuhn.
the class NanopubPage method show.
public void show() throws IOException {
Nanopub nanopub;
String ac = getReq().getArtifactCode();
try {
nanopub = NanopubDb.get().getNanopub(ac);
} catch (Exception ex) {
getResp().sendError(500, "Internal error: " + ex.getMessage());
logger.error(ex.getMessage(), ex);
return;
}
if (nanopub == null) {
getResp().sendError(404, "Nanopub not found: " + ac);
return;
}
setCanonicalLink("/" + ac);
boolean isIndexNanopub = IndexUtils.isIndex(nanopub);
String ext = getReq().getExtension();
String rf = getReq().getPresentationFormat();
RDFFormat format = null;
if (ext != null) {
format = Rio.getParserFormatForFileName("np." + ext);
if (format == null) {
getResp().sendError(400, "Unknown format: " + ext);
return;
}
} else if (rf == null) {
String suppFormats = "application/trig,application/x-trig,text/x-nquads,application/trix,application/ld+json";
if (isIndexNanopub) {
suppFormats += ",text/html";
} else {
suppFormats += ",text/plain";
}
String mimeType = Utils.getMimeType(getHttpReq(), suppFormats);
if (isIndexNanopub && "text/html".equals(mimeType)) {
showIndex(nanopub);
return;
}
if ("text/plain".equals(mimeType)) {
rf = "text/plain";
} else {
format = Rio.getParserFormatForMIMEType(mimeType);
}
}
if (format == null) {
format = RDFFormat.TRIG;
}
if (!format.supportsContexts()) {
getResp().sendError(400, "Unsuitable RDF format: " + ext);
return;
}
if (rf != null) {
getResp().setContentType(rf);
} else {
getResp().setContentType(format.getDefaultMIMEType());
getResp().addHeader("Content-Disposition", "filename=\"" + ac + "." + format.getDefaultFileExtension() + "\"");
}
OutputStream out = null;
try {
out = getResp().getOutputStream();
NanopubUtils.writeToStream(nanopub, out, format);
} catch (Exception ex) {
getResp().sendError(500, "Internal error: " + ex.getMessage());
logger.error(ex.getMessage(), ex);
} finally {
if (out != null)
out.close();
}
}
use of org.nanopub.Nanopub in project eol-globi-data by jhpoelen.
the class LinkerTrustyNanoPubs method doLink.
public void doLink() throws MalformedNanopubException, OpenRDFException, TrustyUriException {
LinkProgress progress = new LinkProgress(LOG::info);
progress.start();
Index<Node> datasets = graphDb.index().forNodes("datasets");
Index<Node> nanopubs = graphDb.index().forNodes("nanopubs");
for (Node node : datasets.query("*:*")) {
DatasetNode dataset = new DatasetNode(node);
Iterable<Relationship> rels = dataset.getUnderlyingNode().getRelationships(NodeUtil.asNeo4j(RelTypes.ACCESSED_AT), Direction.INCOMING);
for (Relationship rel : rels) {
InteractionNode interaction = new InteractionNode(rel.getStartNode());
String nanoPubString = writeNanoPub(dataset, interaction);
Nanopub trustyNanopub = generateTrustyNanopub(nanoPubString);
String artifactCode = TrustyUriUtils.getArtifactCode(trustyNanopub.getUri().toString());
IndexHits<Node> withSameCode = nanopubs.query("code:\"" + artifactCode + "\"");
if (!withSameCode.hasNext()) {
Transaction tx = graphDb.beginTx();
try {
Node npubNode = graphDb.createNode();
npubNode.setProperty("code", artifactCode);
interaction.getUnderlyingNode().createRelationshipTo(npubNode, NodeUtil.asNeo4j(RelTypes.SUPPORTS));
nanopubs.add(npubNode, "code", artifactCode);
tx.success();
} finally {
tx.finish();
}
}
withSameCode.close();
progress.progress();
}
}
}
use of org.nanopub.Nanopub in project eol-globi-data by jhpoelen.
the class LinkerTrustyNanoPubs method generateTrustyNanopub.
public Nanopub generateTrustyNanopub(String nanoPubString) throws MalformedNanopubException, OpenRDFException, TrustyUriException {
NanopubImpl nanopub = new NanopubImpl(nanoPubString, RDFFormat.TRIG);
Nanopub trustyNanopub = MakeTrustyNanopub.transform(nanopub);
OutputStream os = osFactory.outputStreamFor(trustyNanopub);
OutputStreamWriter writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
RDFWriter w = Rio.createWriter(RDFFormat.TRIG, writer);
NanopubUtils.propagateToHandler(trustyNanopub, w);
try {
writer.flush();
} catch (IOException e) {
// ignore
} finally {
IOUtils.closeQuietly(writer);
}
return trustyNanopub;
}
use of org.nanopub.Nanopub in project eol-globi-data by jhpoelen.
the class LinkerTrustyNanoPubsTest method dataOutput.
@Test
public void dataOutput() throws NodeFactoryException, OpenRDFException, IOException, MalformedNanopubException, TrustyUriException {
DatasetImpl dataset = new DatasetImpl("some/namespace", URI.create("http://example.com/dataset"));
populateDataset(dataset);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
LinkerTrustyNanoPubs linker = new LinkerTrustyNanoPubs(getGraphDb(), new NanopubOutputStreamFactory() {
@Override
public OutputStream outputStreamFor(Nanopub nanopub) {
try {
return new GZIPOutputStream(byteArrayOutputStream);
} catch (IOException e) {
throw new RuntimeException("kaboom!");
}
}
});
linker.link();
String actualTrig = toTrigString(new GZIPInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
String expectedTrig = toTrigString(getClass().getResourceAsStream("trusty.nanopub.trig"));
assertThat(actualTrig, is(expectedTrig));
}
use of org.nanopub.Nanopub in project eol-globi-data by jhpoelen.
the class LinkerTrustyNanoPubsTest method trustyURI.
@Test
public void trustyURI() throws OpenRDFException, IOException, MalformedNanopubException, TrustyUriException {
NanopubImpl nanopub = new NanopubImpl(getClass().getResourceAsStream("nanopub.trig"), RDFFormat.TRIG);
ByteArrayOutputStream actual = new ByteArrayOutputStream();
Nanopub trustyNanopub = MakeTrustyNanopub.writeAsTrustyNanopub(nanopub, RDFFormat.TRIG, actual);
String artifactCode = TrustyUriUtils.getArtifactCode(trustyNanopub.getUri().toString());
assertThat(artifactCode, is("RA7XQvcOGTux6HTndtjAiVWXjEMZbQMH4yJIxTMCV8sx4"));
String actualTrig = toTrigString(new ByteArrayInputStream(actual.toByteArray()));
String expectedTrig = toTrigString(getClass().getResourceAsStream("trusty.nanopub.trig"));
assertThat(actualTrig, is(expectedTrig));
}
Aggregations