|
| 1 | +package com.annimon.ownlang.modules.server; |
| 2 | + |
| 3 | +import com.annimon.ownlang.lib.*; |
| 4 | +import io.javalin.http.Context; |
| 5 | +import io.javalin.http.HttpStatus; |
| 6 | +import org.jetbrains.annotations.NotNull; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.function.Consumer; |
| 10 | + |
| 11 | +public class ContextValue extends MapValue { |
| 12 | + |
| 13 | + private final Context ctx; |
| 14 | + |
| 15 | + public ContextValue(@NotNull Context ctx) { |
| 16 | + super(10); |
| 17 | + this.ctx = ctx; |
| 18 | + init(); |
| 19 | + } |
| 20 | + |
| 21 | + private void init() { |
| 22 | + set("body", Converters.voidToString(ctx::body)); |
| 23 | + set("characterEncoding", Converters.voidToString(ctx::characterEncoding)); |
| 24 | + set("contentType", Converters.voidToString(ctx::contentType)); |
| 25 | + set("contextPath", Converters.voidToString(ctx::contextPath)); |
| 26 | + set("fullUrl", Converters.voidToString(ctx::fullUrl)); |
| 27 | + set("host", Converters.voidToString(ctx::host)); |
| 28 | + set("ip", Converters.voidToString(ctx::ip)); |
| 29 | + set("matchedPath", Converters.voidToString(ctx::matchedPath)); |
| 30 | + set("path", Converters.voidToString(ctx::path)); |
| 31 | + set("protocol", Converters.voidToString(ctx::protocol)); |
| 32 | + set("queryString", Converters.voidToString(ctx::queryString)); |
| 33 | + set("url", Converters.voidToString(ctx::url)); |
| 34 | + set("userAgent", Converters.voidToString(ctx::userAgent)); |
| 35 | + |
| 36 | + set("contentLength", Converters.voidToInt(ctx::contentLength)); |
| 37 | + set("port", Converters.voidToInt(ctx::port)); |
| 38 | + set("statusCode", Converters.voidToInt(ctx::statusCode)); |
| 39 | + |
| 40 | + set("json", objectToContext(ctx::json)); |
| 41 | + set("jsonStream", objectToContext(ctx::jsonStream)); |
| 42 | + |
| 43 | + set("render", this::render); |
| 44 | + set("result", this::result); |
| 45 | + set("redirect", this::redirect); |
| 46 | + } |
| 47 | + |
| 48 | + private Value render(Value[] args) { |
| 49 | + Arguments.checkOrOr(1, 2, args.length); |
| 50 | + String filePath = args[0].asString(); |
| 51 | + if (args.length == 1) { |
| 52 | + ctx.render(filePath); |
| 53 | + } else { |
| 54 | + MapValue map = (MapValue) args[1]; |
| 55 | + Map<String, Object> data = new HashMap<>(map.size()); |
| 56 | + map.getMap().forEach((k, v) -> data.put(k.asString(), v.asJavaObject())); |
| 57 | + ctx.render(filePath, data); |
| 58 | + } |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + private Value redirect(Value[] args) { |
| 63 | + Arguments.checkOrOr(1, 2, args.length); |
| 64 | + HttpStatus status = args.length == 1 ? HttpStatus.FOUND : HttpStatus.forStatus(args[1].asInt()); |
| 65 | + ctx.redirect(args[0].asString(), status); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + private Value result(Value[] args) { |
| 70 | + Arguments.checkOrOr(0, 1, args.length); |
| 71 | + if (args.length == 0) { |
| 72 | + return new StringValue(ctx.result()); |
| 73 | + } else { |
| 74 | + final var arg = args[0]; |
| 75 | + if (arg.type() == Types.ARRAY) { |
| 76 | + ctx.result(ValueUtils.toByteArray((ArrayValue) arg)); |
| 77 | + } else { |
| 78 | + ctx.result(arg.asString()); |
| 79 | + } |
| 80 | + return this; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private Value objectToContext(Consumer<Object> consumer) { |
| 85 | + return new FunctionValue(args -> { |
| 86 | + Arguments.check(1, args.length); |
| 87 | + consumer.accept(args[0].asJavaObject()); |
| 88 | + return this; |
| 89 | + }); |
| 90 | + } |
| 91 | +} |
0 commit comments