[server] Add more methods to ContextValue

This commit is contained in:
aNNiMON 2023-12-18 21:44:48 +02:00 committed by Victor Melnik
parent cea49f3c9d
commit 7e9dc9038d
3 changed files with 52 additions and 2 deletions

View File

@ -161,11 +161,23 @@ types:
- name: html
args: 'html'
desc: sets result to the specified html string. Also sets Content-Type header to text/html
desc_ru: устанавливает указанныую html-строку в качестве результата. Также устанавливает заголовок Content-Type в text/html
desc_ru: устанавливает указанную html-строку в качестве результата. Также устанавливает заголовок Content-Type в text/html
- name: ip
args: ''
desc: returns an IP address
desc_ru: возвращает IP адрес
- name: isHttpMethod
args: ''
desc: returns true if the request is http method
desc_ru: возвращает true, если запрос — http метод
- name: isMultipartFormData
args: ''
desc: returns true if the request is multipart/formdata
desc_ru: возвращает true, если запрос — multipart/formdata
- name: isMultipart
args: ''
desc: returns true if the request is multipart
desc_ru: возвращает true, если запрос — multipart
- name: json
args: 'obj'
desc: serializes an object to json string and sets it as the result
@ -178,10 +190,18 @@ types:
args: ''
desc: returns a matched request path
desc_ru: возвращает совпавший путь запроса
- name: method
args: ''
desc: returns a method (GET, POST, ...)
desc_ru: возвращает метод (GET, POST, ...)
- name: path
args: ''
desc: returns a request path
desc_ru: возвращает путь запроса
- name: pathParam
args: 'key'
desc: returns a request path parameter
desc_ru: возвращает параметр пути запроса
- name: port
args: ''
desc: returns a port number
@ -190,6 +210,10 @@ types:
args: ''
desc: returns a protocol
desc_ru: возвращает протокол
- name: queryParam
args: 'key'
desc: returns a query parameter
desc_ru: возвращает параметр запроса
- name: queryString
args: ''
desc: returns a query string
@ -210,6 +234,10 @@ types:
args: 'value = ""'
desc: gets or sets a result. `value` can be a string or a byte array
desc_ru: получает или устанавливает результат. `value` может быть строкой или массивом байт
- name: status
args: 'status = ...'
desc: gets or sets a status code. `status` can be an integer status code (404, 500) or a string status name ("NOT_FOUND", "INTERNAL_SERVER_ERROR").
desc_ru: получает или устанавливает код статуса. `status` может быть числовым кодом (404, 500) или строкой имени статуса ("NOT_FOUND", "INTERNAL_SERVER_ERROR")
- name: statusCode
args: ''
desc: returns a response status code

View File

@ -35,17 +35,24 @@ class ContextValue extends MapValue {
set("host", Converters.voidToString(ctx::host));
set("html", stringToContext(ctx::html));
set("ip", Converters.voidToString(ctx::ip));
set("isHttpMethod", Converters.voidToBoolean(() -> ctx.method().isHttpMethod()));
set("isMultipart", Converters.voidToBoolean(ctx::isMultipart));
set("isMultipartFormData", Converters.voidToBoolean(ctx::isMultipartFormData));
set("json", objectToContext(ctx::json));
set("jsonStream", objectToContext(ctx::jsonStream));
set("matchedPath", Converters.voidToString(ctx::matchedPath));
set("method", Converters.voidToString(() -> ctx.method().name()));
set("path", Converters.voidToString(ctx::path));
set("pathParam", Converters.stringToString(ctx::pathParam));
set("port", Converters.voidToInt(ctx::port));
set("protocol", Converters.voidToString(ctx::protocol));
set("queryParam", Converters.stringToString(ctx::queryParam));
set("queryString", Converters.voidToString(ctx::queryString));
set("redirect", this::redirect);
set("removeCookie", this::removeCookie);
set("render", this::render);
set("result", this::result);
set("status", this::status);
set("statusCode", Converters.voidToInt(ctx::statusCode));
set("scheme", Converters.voidToString(ctx::scheme));
set("url", Converters.voidToString(ctx::url));
@ -151,6 +158,21 @@ class ContextValue extends MapValue {
}
}
private Value status(Value[] args) {
Arguments.checkOrOr(0, 1, args.length);
if (args.length == 0) {
return new StringValue(ctx.status().name());
} else {
final var arg = args[0];
if (arg.type() == Types.NUMBER) {
ctx.status(arg.asInt());
} else {
ctx.status(HttpStatus.valueOf(arg.asString()));
}
return this;
}
}
private Value stringToContext(Consumer<String> consumer) {
return new FunctionValue(args -> {
Arguments.check(1, args.length);

View File

@ -104,7 +104,7 @@ public class MapValue implements Value, Iterable<Map.Entry<Value, Value>> {
@Override
public Object asJavaObject() {
Map<Object, Object> result = new HashMap<>(map.size());
Map<Object, Object> result = new LinkedHashMap<>(map.size());
map.forEach((k, v) -> result.put(k.asJavaObject(), v.asJavaObject()));
return result;
}