Search in sources :

Example 6 with Nanopub

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();
    }
}
Also used : Nanopub(org.nanopub.Nanopub) OutputStream(java.io.OutputStream) IOException(java.io.IOException) MalformedNanopubException(org.nanopub.MalformedNanopubException) RDFFormat(org.openrdf.rio.RDFFormat)

Example 7 with Nanopub

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();
        }
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) DatasetNode(org.eol.globi.domain.DatasetNode) InteractionNode(org.eol.globi.domain.InteractionNode) TaxonNode(org.eol.globi.domain.TaxonNode) Relationship(org.neo4j.graphdb.Relationship) Nanopub(org.nanopub.Nanopub) MakeTrustyNanopub(org.nanopub.trusty.MakeTrustyNanopub) InteractionNode(org.eol.globi.domain.InteractionNode) DatasetNode(org.eol.globi.domain.DatasetNode)

Example 8 with Nanopub

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;
}
Also used : Nanopub(org.nanopub.Nanopub) MakeTrustyNanopub(org.nanopub.trusty.MakeTrustyNanopub) OutputStream(java.io.OutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) RDFWriter(org.openrdf.rio.RDFWriter) IOException(java.io.IOException) NanopubImpl(org.nanopub.NanopubImpl)

Example 9 with Nanopub

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));
}
Also used : Nanopub(org.nanopub.Nanopub) MakeTrustyNanopub(org.nanopub.trusty.MakeTrustyNanopub) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) DatasetImpl(org.eol.globi.service.DatasetImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 10 with Nanopub

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Nanopub(org.nanopub.Nanopub) MakeTrustyNanopub(org.nanopub.trusty.MakeTrustyNanopub) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NanopubImpl(org.nanopub.NanopubImpl) Test(org.junit.Test)

Aggregations

Nanopub (org.nanopub.Nanopub)10 NanopubImpl (org.nanopub.NanopubImpl)5 IOException (java.io.IOException)4 OutputStream (java.io.OutputStream)4 MakeTrustyNanopub (org.nanopub.trusty.MakeTrustyNanopub)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 GZIPInputStream (java.util.zip.GZIPInputStream)3 InputStream (java.io.InputStream)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 NullOutputStream (org.apache.commons.io.output.NullOutputStream)2 Test (org.junit.Test)2 MalformedNanopubException (org.nanopub.MalformedNanopubException)2 NanopubHandler (org.nanopub.MultiNanopubRdfHandler.NanopubHandler)2 RDFFormat (org.openrdf.rio.RDFFormat)2 NotTrustyNanopubException (ch.tkuhn.nanopub.server.NanopubDb.NotTrustyNanopubException)1 OversizedNanopubException (ch.tkuhn.nanopub.server.NanopubDb.OversizedNanopubException)1 BasicDBObject (com.mongodb.BasicDBObject)1 DBCursor (com.mongodb.DBCursor)1 GridFSDBFile (com.mongodb.gridfs.GridFSDBFile)1