Creates and returns a REST command. @param session REST session @return code @throws IOException I/O exception
(final RESTSession session)
| 36 | * @throws IOException I/O exception |
| 37 | */ |
| 38 | public static RESTCmd get(final RESTSession session) throws IOException { |
| 39 | final HTTPConnection conn = session.conn; |
| 40 | final String encoding = conn.request.getCharacterEncoding(); |
| 41 | |
| 42 | // perform queries |
| 43 | final DBNode doc; |
| 44 | try(NewlineInput ni = new NewlineInput(conn.request.getInputStream(), encoding)) { |
| 45 | doc = new DBNode(new IOContent(ni.content())); |
| 46 | } catch(final IOException ex) { |
| 47 | throw HTTPStatus.BAD_REQUEST_X.get(ex); |
| 48 | } |
| 49 | |
| 50 | try { |
| 51 | final Context ctx = conn.context; |
| 52 | // handle request |
| 53 | final String cmd = value("local-name(*)", doc, ctx); |
| 54 | if(cmd.equals(COMMANDS)) { |
| 55 | final String script = DataBuilder.stripNamespace(doc, REST_URI, ctx).serialize().toString(); |
| 56 | return RESTCommands.get(session, script, false); |
| 57 | } |
| 58 | |
| 59 | // handle serialization parameters |
| 60 | final SerializerOptions sopts = conn.sopts(); |
| 61 | try(QueryProcessor qp = new QueryProcessor("*/*:parameter", ctx).context(doc)) { |
| 62 | for(final Item param : qp.value()) { |
| 63 | final String name = value("@name", param, ctx); |
| 64 | final String value = value("@value", param, ctx); |
| 65 | if(sopts.option(name) != null) { |
| 66 | sopts.assign(name, value); |
| 67 | } else { |
| 68 | throw HTTPStatus.UNKNOWN_PARAM_X.get(name); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // handle database options |
| 74 | try(QueryProcessor qp = new QueryProcessor("*/*:option", ctx).context(doc)) { |
| 75 | for(final Item item : qp.value()) { |
| 76 | final String name = value("@name", item, ctx).toUpperCase(Locale.ENGLISH); |
| 77 | final String value = value("@value", item, ctx); |
| 78 | ctx.options.assign(name, value); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // handle variables |
| 83 | final Map<String, Map.Entry<Object, String>> bindings = new HashMap<>(); |
| 84 | try(QueryProcessor qp = new QueryProcessor("*/*:variable", ctx).context(doc)) { |
| 85 | for(final Item item : qp.value()) { |
| 86 | final String name = value("@name", item, ctx); |
| 87 | final String value = value("@value", item, ctx); |
| 88 | final String type = value("@type", item, ctx); |
| 89 | bindings.compute(name, (k, v) -> { |
| 90 | final StringList list = new StringList(); |
| 91 | if(v != null) { |
| 92 | if(v.getKey() instanceof final String[] strings) { |
| 93 | for(final String string : strings) list.add(string); |
| 94 | } else if(v.getKey() instanceof String) { |
| 95 | list.add(k); |