Search in sources :

Example 1 with OversizedNanopubException

use of ch.tkuhn.nanopub.server.NanopubDb.OversizedNanopubException 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

NotTrustyNanopubException (ch.tkuhn.nanopub.server.NanopubDb.NotTrustyNanopubException)1 OversizedNanopubException (ch.tkuhn.nanopub.server.NanopubDb.OversizedNanopubException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 StringWriter (java.io.StringWriter)1 ServletException (javax.servlet.ServletException)1 Nanopub (org.nanopub.Nanopub)1 NanopubImpl (org.nanopub.NanopubImpl)1 ServerInfoException (org.nanopub.extra.server.ServerInfo.ServerInfoException)1