MCPcopy Create free account
hub / github.com/BaseXdb/basex / RESTServlet

Class RESTServlet

basex-api/src/main/java/org/basex/http/rest/RESTServlet.java:19–56  ·  view source on GitHub ↗

This servlet receives and processes REST requests. @author BaseX Team, BSD License @author Christian Gruen

Source from the content-addressed store, hash-verified

17 * @author Christian Gruen
18 */
19public final class RESTServlet extends BaseXServlet {
20 @Override
21 protected void run(final HTTPConnection conn) throws Exception {
22 // open database if name was specified
23 final RESTSession session = new RESTSession(conn);
24 final String db = conn.db(), path = conn.dbpath();
25 if(!db.isEmpty()) session.add(new Open(db, path));
26
27 // generate and run commands
28 final RESTCmd cmd = command(session);
29 try {
30 cmd.execute(conn.context);
31 conn.log(SC_OK, "");
32 } catch(final BaseXException ex) {
33 // ignore error if code was assigned (same error message)
34 if(cmd.status == null) throw ex;
35 }
36
37 final HTTPStatus status = cmd.status;
38 if(status != null) throw status.get(cmd.info());
39 }
40
41 /**
42 * Creates and returns a REST command.
43 * @param session session
44 * @return code
45 * @throws QueryException query exception
46 * @throws IOException I/O exception
47 */
48 private static RESTCmd command(final RESTSession session) throws QueryException, IOException {
49 final String method = session.conn.method;
50 if(method.equals(Method.GET.name())) return RESTGet.get(session);
51 if(method.equals(Method.POST.name())) return RESTPost.get(session);
52 if(method.equals(Method.PUT.name())) return RESTPut.get(session);
53 if(method.equals(Method.DELETE.name())) return RESTDelete.get(session);
54 throw HTTPStatus.METHOD_NOT_SUPPORTED_X.get(session.conn.request.getMethod());
55 }
56}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected