Search in sources :

Example 1 with Nanopub

use of org.nanopub.Nanopub 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 Nanopub

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

the class LoadFiles method checkFilesToLoad.

private void checkFilesToLoad() {
    logger.info("Check whether there are files to load...");
    for (File f : loadDir.listFiles()) {
        stillAlive();
        if (f.isDirectory())
            continue;
        logger.info("Try to load file: " + f);
        try {
            final File processingFile = new File(processingDir, f.getName());
            f.renameTo(processingFile);
            RDFFormat format = Rio.getParserFormatForFileName(processingFile.getName());
            MultiNanopubRdfHandler.process(format, processingFile, new NanopubHandler() {

                @Override
                public void handleNanopub(Nanopub np) {
                    if (!ourPattern.matchesUri(np.getUri().toString()))
                        return;
                    try {
                        db.loadNanopub(np);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    stillAlive();
                }
            });
            processingFile.renameTo(new File(doneDir, f.getName()));
            logger.info("File loaded: " + processingFile);
        } catch (Exception ex) {
            logger.error("Failed to load file: " + f, ex);
        }
    }
}
Also used : NanopubHandler(org.nanopub.MultiNanopubRdfHandler.NanopubHandler) Nanopub(org.nanopub.Nanopub) File(java.io.File) RDFFormat(org.openrdf.rio.RDFFormat)

Example 3 with Nanopub

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

the class NanopubDb method writePackageToStream.

public void writePackageToStream(long pageNo, boolean gzipped, OutputStream out) throws IOException {
    if (pageNo < 1 || pageNo >= journal.getCurrentPageNo()) {
        throw new IllegalArgumentException("Not a complete page: " + pageNo);
    }
    GridFSDBFile f = packageGridFs.findOne(pageNo + "");
    OutputStream packageOut = null;
    InputStream packageAsStream = null;
    try {
        if (f == null) {
            if (gzipped) {
                out = new GZIPOutputStream(out);
            }
            ByteArrayOutputStream bOut = new ByteArrayOutputStream();
            packageOut = new GZIPOutputStream(bOut);
            String pageContent = journal.getPageContent(pageNo);
            for (String uri : pageContent.split("\\n")) {
                Nanopub np = getNanopub(TrustyUriUtils.getArtifactCode(uri));
                String s;
                try {
                    s = NanopubUtils.writeToString(np, RDFFormat.TRIG);
                } catch (RDFHandlerException ex) {
                    throw new RuntimeException("Unexpected RDF handler exception", ex);
                }
                byte[] bytes = (s + "\n").getBytes();
                out.write(bytes);
                packageOut.write(bytes);
            }
            packageOut.close();
            packageAsStream = new ByteArrayInputStream(bOut.toByteArray());
            GridFSInputFile i = packageGridFs.createFile(packageAsStream);
            i.setFilename(pageNo + "");
            i.save();
        } else {
            if (gzipped) {
                f.writeTo(out);
            } else {
                GZIPInputStream in = new GZIPInputStream(f.getInputStream());
                byte[] buffer = new byte[1024];
                int len;
                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                in.close();
            }
        }
    } finally {
        if (out != null)
            out.close();
        if (packageOut != null)
            packageOut.close();
        if (packageAsStream != null)
            packageAsStream.close();
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) NullOutputStream(org.apache.commons.io.output.NullOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) Nanopub(org.nanopub.Nanopub) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) RDFHandlerException(org.openrdf.rio.RDFHandlerException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile)

Example 4 with Nanopub

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

use of org.nanopub.Nanopub 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)

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