This servlet receives and processes REST requests. @author BaseX Team, BSD License @author Christian Gruen
| 17 | * @author Christian Gruen |
| 18 | */ |
| 19 | public 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 | } |
nothing calls this directly
no outgoing calls
no test coverage detected