package ch.tkuhn.nanopub.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class NanopubServlet extends HttpServlet {

	private static final long serialVersionUID = -4542560440919522982L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ServerRequest r = new ServerRequest(req);
		if (r.isEmpty()) {
			MainPage.show(r, resp);
		} else if (r.hasArtifactCode()) {
			NanopubPage.show(r, resp);
		} else if (r.hasListQuery()) {
			ListPage.show(r, resp);
		} else if (r.getFullRequest().equals("/style/plain.css")) {
			ResourcePage.show(r, resp, "style.css", "text/css");
		} else {
			resp.sendError(400, "Invalid request: " + r.getFullRequest());
		}
		resp.getOutputStream().close();
	}

}

@@ -228,16 +228,149 @@
 ponse;%0A%0A
+import net.trustyuri.TrustyUriUtils;%0A%0Aimport org.nanopub.Nanopub;%0Aimport org.nanopub.NanopubImpl;%0Aimport org.openrdf.rio.RDFFormat;%0A%0A
 public c
@@ -983,16 +983,20 @@
 Invalid 
+GET 
 request:
@@ -1063,10 +1063,820 @@
 ();%0A%09%7D%0A%0A
+%09@Override%0A%09protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException %7B%0A%09%09ServerRequest r = new ServerRequest(req);%0A%09%09if (!r.isEmpty()) %7B%0A%09%09%09resp.sendError(400, %22Invalid POST request: %22 + r.getFullRequest());%0A%09%09%7D%0A%09%09Nanopub np = null;%0A%09%09try %7B%0A%09%09%09np = new NanopubImpl(req.getInputStream(), RDFFormat.forMIMEType(req.getContentType(), RDFFormat.TRIG));%0A%09%09%7D catch (Exception ex) %7B%0A%09%09%09resp.sendError(400, %22Error reading nanopub: %22 + ex.getMessage());%0A%09%09%7D%0A%09%09if (np != null) %7B%0A%09%09%09String code = TrustyUriUtils.getArtifactCode(np.getUri().toString());%0A%09%09%09try %7B%0A%09%09%09%09if (NanopubDb.get().getNanopub(code) == null) %7B%0A%09%09%09%09%09NanopubDb.get().loadNanopub(np);%0A%09%09%09%09%7D%0A%09%09%09%7D catch (Exception ex) %7B%0A%09%09%09%09resp.sendError(500, %22Error storing nanopub: %22 + ex.getMessage());%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

Support POST requests to add nanopubs