use of ch.tkuhn.nanopub.server.NanopubDb.NotTrustyNanopubException 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();
}
Aggregations