Search in sources :

Example 1 with NanopubImpl

use of org.nanopub.NanopubImpl in project nanopub-server by tkuhn.

the class CollectNanopubs method processPage.

private void processPage(int page, boolean isLastPage, long ignoreBeforePos) throws Exception {
    parent.stillAlive();
    logger.info("Process page " + page + " from " + peerInfo.getPublicUrl());
    loaded = 0;
    nextNp = (page - 1) * peerPageSize;
    List<String> toLoad = new ArrayList<>();
    boolean downloadAsPackage = false;
    for (String nanopubUri : NanopubServerUtils.loadNanopubUriList(peerInfo, page)) {
        parent.stillAlive();
        if (nextNp >= ignoreBeforePos) {
            String ac = TrustyUriUtils.getArtifactCode(nanopubUri);
            if (ac != null && ourPattern.matchesUri(nanopubUri) && !db.hasNanopub(ac)) {
                toLoad.add(ac);
                if (!isLastPage && toLoad.size() > 5) {
                    // Download entire package if more than 5 nanopubs are new
                    downloadAsPackage = true;
                    nextNp = (page - 1) * peerPageSize;
                    break;
                }
            }
        }
        nextNp++;
    }
    RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5 * 1000).build();
    HttpClient c = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();
    watch = new StopWatch();
    watch.start();
    if (downloadAsPackage) {
        logger.info("Download page " + page + " as compressed package...");
        HttpGet get = new HttpGet(peerInfo.getPublicUrl() + "package.gz?page=" + page);
        get.setHeader("Accept", "application/x-gzip");
        HttpResponse resp = c.execute(get);
        InputStream in = null;
        try {
            if (wasSuccessful(resp)) {
                in = new GZIPInputStream(resp.getEntity().getContent());
            } else {
                logger.info("Failed. Trying uncompressed package...");
                // This is for compability with older versions; to be removed at some point...
                get = new HttpGet(peerInfo.getPublicUrl() + "package?page=" + page);
                get.setHeader("Accept", "application/trig");
                resp = c.execute(get);
                if (!wasSuccessful(resp)) {
                    logger.error("HTTP request failed: " + resp.getStatusLine().getReasonPhrase());
                    recordTime();
                    throw new RuntimeException(resp.getStatusLine().getReasonPhrase());
                }
                in = resp.getEntity().getContent();
            }
            MultiNanopubRdfHandler.process(RDFFormat.TRIG, in, new NanopubHandler() {

                @Override
                public void handleNanopub(Nanopub np) {
                    nextNp++;
                    if (watch.getTime() > 5 * 60 * 1000) {
                        // Downloading the whole package should never take more than 5 minutes.
                        logger.error("Downloading package took too long; interrupting");
                        recordTime();
                        throw new RuntimeException("Downloading package took too long; interrupting");
                    }
                    if (!ourPattern.matchesUri(np.getUri().stringValue()))
                        return;
                    try {
                        loadNanopub(np);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                }
            });
        } finally {
            if (in != null)
                in.close();
        }
    } else {
        logger.info("Download " + toLoad.size() + " nanopubs individually...");
        for (String ac : toLoad) {
            parent.stillAlive();
            HttpGet get = new HttpGet(peerInfo.getPublicUrl() + ac);
            get.setHeader("Accept", "application/trig");
            HttpResponse resp = c.execute(get);
            if (!wasSuccessful(resp)) {
                logger.error("HTTP request failed: " + resp.getStatusLine().getReasonPhrase());
                recordTime();
                throw new RuntimeException(resp.getStatusLine().getReasonPhrase());
            }
            InputStream in = null;
            try {
                in = resp.getEntity().getContent();
                loadNanopub(new NanopubImpl(in, RDFFormat.TRIG));
            } finally {
                if (in != null)
                    in.close();
            }
        }
    }
    recordTime();
    logger.info("Update peer state: " + peerInfo.getPublicUrl() + " at position " + nextNp);
    db.updatePeerState(peerInfo, nextNp);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Nanopub(org.nanopub.Nanopub) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) StopWatch(org.apache.commons.lang.time.StopWatch) GZIPInputStream(java.util.zip.GZIPInputStream) HttpClient(org.apache.http.client.HttpClient) NanopubHandler(org.nanopub.MultiNanopubRdfHandler.NanopubHandler) NanopubImpl(org.nanopub.NanopubImpl)

Example 2 with NanopubImpl

use of org.nanopub.NanopubImpl in project nanopub-server by tkuhn.

the class NanopubDb method getNanopub.

public Nanopub getNanopub(String artifactCode) {
    BasicDBObject query = new BasicDBObject("_id", artifactCode);
    DBCursor cursor = getNanopubCollection().find(query);
    if (!cursor.hasNext()) {
        return null;
    }
    String nanopubString = cursor.next().get("nanopub").toString();
    Nanopub np = null;
    try {
        np = new NanopubImpl(nanopubString, internalFormat);
    } catch (MalformedNanopubException ex) {
        throw new RuntimeException("Stored nanopub is not wellformed (this shouldn't happen)", ex);
    } catch (OpenRDFException ex) {
        throw new RuntimeException("Stored nanopub is corrupted (this shouldn't happen)", ex);
    }
    if (ServerConf.get().isCheckNanopubsOnGetEnabled() && !TrustyNanopubUtils.isValidTrustyNanopub(np)) {
        throw new RuntimeException("Stored nanopub is not trusty (this shouldn't happen)");
    }
    return np;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) Nanopub(org.nanopub.Nanopub) OpenRDFException(org.openrdf.OpenRDFException) MalformedNanopubException(org.nanopub.MalformedNanopubException) NanopubImpl(org.nanopub.NanopubImpl)

Example 3 with NanopubImpl

use of org.nanopub.NanopubImpl in project nanopub-server by tkuhn.

the class NanopubServlet method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
        setGeneralHeaders(resp);
        ServerRequest r = new ServerRequest(req);
        if (r.isEmpty()) {
            if (!ServerConf.getInfo().isPostNanopubsEnabled()) {
                resp.sendError(405, "Posting nanopubs is not supported by this nanopub server");
                return;
            }
            Nanopub np = null;
            try {
                np = new NanopubImpl(req.getInputStream(), Rio.getParserFormatForMIMEType(req.getContentType(), RDFFormat.TRIG));
            } catch (Exception ex) {
                resp.sendError(400, "Error reading nanopub: " + ex.getMessage());
            }
            if (np != null) {
                if (ourPattern.matchesUri(np.getUri().toString())) {
                    String code = TrustyUriUtils.getArtifactCode(np.getUri().toString());
                    try {
                        if (NanopubDb.get().getNanopub(code) == null) {
                            NanopubDb.get().loadNanopub(np);
                        }
                        resp.setHeader("Location", TrustyUriUtils.getArtifactCode(np.getUri().toString()));
                        resp.setStatus(201);
                    } catch (NotTrustyNanopubException ex) {
                        resp.sendError(400, "Nanopub is not trusty: " + ex.getMessage());
                    } catch (OversizedNanopubException ex) {
                        resp.sendError(400, "Nanopub is too large: " + ex.getMessage());
                    } catch (Exception ex) {
                        resp.sendError(500, "Error storing nanopub: " + ex.getMessage());
                    }
                } else {
                    resp.sendError(500, "Nanopub doesn't match pattern for this server: " + np.getUri());
                }
            }
        } else if (r.getRequestString().equals(PeerListPage.PAGE_NAME)) {
            if (!ServerConf.getInfo().isPostPeersEnabled()) {
                resp.sendError(405, "Posting peers is not supported by this nanopub server");
                return;
            }
            try {
                StringWriter sw = new StringWriter();
                IOUtils.copy(new InputStreamReader(req.getInputStream(), Charset.forName("UTF-8")), sw);
                NanopubDb.get().addPeer(sw.toString().trim());
                resp.setStatus(201);
            } catch (ServerInfoException ex) {
                resp.sendError(400, "Invalid peer URL: " + ex.getMessage());
            } catch (IOException ex) {
                resp.sendError(500, "Error adding peer: " + ex.getMessage());
            }
        } else {
            resp.sendError(400, "Invalid POST request: " + r.getFullRequest());
        }
    } finally {
        resp.getOutputStream().close();
        req.getInputStream().close();
    }
    check();
}
Also used : ServerInfoException(org.nanopub.extra.server.ServerInfo.ServerInfoException) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) Nanopub(org.nanopub.Nanopub) NotTrustyNanopubException(ch.tkuhn.nanopub.server.NanopubDb.NotTrustyNanopubException) OversizedNanopubException(ch.tkuhn.nanopub.server.NanopubDb.OversizedNanopubException) IOException(java.io.IOException) NanopubImpl(org.nanopub.NanopubImpl) ServerInfoException(org.nanopub.extra.server.ServerInfo.ServerInfoException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) OversizedNanopubException(ch.tkuhn.nanopub.server.NanopubDb.OversizedNanopubException) NotTrustyNanopubException(ch.tkuhn.nanopub.server.NanopubDb.NotTrustyNanopubException)

Example 4 with NanopubImpl

use of org.nanopub.NanopubImpl 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 5 with NanopubImpl

use of org.nanopub.NanopubImpl 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)5 NanopubImpl (org.nanopub.NanopubImpl)5 IOException (java.io.IOException)2 MakeTrustyNanopub (org.nanopub.trusty.MakeTrustyNanopub)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 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ServletException (javax.servlet.ServletException)1 NullOutputStream (org.apache.commons.io.output.NullOutputStream)1 StopWatch (org.apache.commons.lang.time.StopWatch)1