4
4
import io .javalin .http .Context ;
5
5
import io .javalin .http .HttpStatus ;
6
6
import org .jetbrains .annotations .NotNull ;
7
- import java .util .HashMap ;
8
7
import java .util .Map ;
9
8
import java .util .function .Consumer ;
10
9
11
- public class ContextValue extends MapValue {
10
+ class ContextValue extends MapValue {
12
11
13
12
private final Context ctx ;
14
13
15
14
public ContextValue (@ NotNull Context ctx ) {
16
- super (10 );
15
+ super (32 );
17
16
this .ctx = ctx ;
18
17
init ();
19
18
}
20
19
21
20
private void init () {
21
+ set ("attribute" , this ::attribute );
22
+ set ("basicAuthCredentials" , this ::basicAuthCredentials );
22
23
set ("body" , Converters .voidToString (ctx ::body ));
24
+ set ("bodyAsBytes" , this ::bodyAsBytes );
23
25
set ("characterEncoding" , Converters .voidToString (ctx ::characterEncoding ));
24
- set ("contentType" , Converters .voidToString (ctx ::contentType ));
26
+ set ("cookie" , this ::cookie );
27
+ set ("contentLength" , Converters .voidToInt (ctx ::contentLength ));
28
+ set ("contentType" , this ::contentType );
25
29
set ("contextPath" , Converters .voidToString (ctx ::contextPath ));
30
+ set ("endpointHandlerPath" , Converters .voidToString (ctx ::endpointHandlerPath ));
31
+ set ("formParam" , Converters .stringToString (ctx ::formParam ));
26
32
set ("fullUrl" , Converters .voidToString (ctx ::fullUrl ));
33
+ set ("handlerType" , Converters .voidToString (() -> ctx .handlerType ().name ()));
34
+ set ("header" , this ::header );
27
35
set ("host" , Converters .voidToString (ctx ::host ));
36
+ set ("html" , stringToContext (ctx ::html ));
28
37
set ("ip" , Converters .voidToString (ctx ::ip ));
38
+ set ("json" , objectToContext (ctx ::json ));
39
+ set ("jsonStream" , objectToContext (ctx ::jsonStream ));
29
40
set ("matchedPath" , Converters .voidToString (ctx ::matchedPath ));
30
41
set ("path" , Converters .voidToString (ctx ::path ));
42
+ set ("port" , Converters .voidToInt (ctx ::port ));
31
43
set ("protocol" , Converters .voidToString (ctx ::protocol ));
32
44
set ("queryString" , Converters .voidToString (ctx ::queryString ));
45
+ set ("redirect" , this ::redirect );
46
+ set ("removeCookie" , this ::removeCookie );
47
+ set ("render" , this ::render );
48
+ set ("result" , this ::result );
49
+ set ("statusCode" , Converters .voidToInt (ctx ::statusCode ));
50
+ set ("scheme" , Converters .voidToString (ctx ::scheme ));
33
51
set ("url" , Converters .voidToString (ctx ::url ));
34
52
set ("userAgent" , Converters .voidToString (ctx ::userAgent ));
53
+ }
35
54
36
- set ("contentLength" , Converters .voidToInt (ctx ::contentLength ));
37
- set ("port" , Converters .voidToInt (ctx ::port ));
38
- set ("statusCode" , Converters .voidToInt (ctx ::statusCode ));
55
+ private Value attribute (Value [] args ) {
56
+ Arguments .checkOrOr (1 , 2 , args .length );
57
+ String key = args [0 ].asString ();
58
+ if (args .length == 1 ) {
59
+ return ctx .attribute (key );
60
+ } else {
61
+ ctx .attribute (key , args [1 ]);
62
+ }
63
+ return this ;
64
+ }
39
65
40
- set ("json" , objectToContext (ctx ::json ));
41
- set ("jsonStream" , objectToContext (ctx ::jsonStream ));
66
+ private Value basicAuthCredentials (Value [] args ) {
67
+ Arguments .check (0 , args .length );
68
+ final var cred = ctx .basicAuthCredentials ();
69
+ return ArrayValue .of (new String [] {
70
+ cred .getUsername (),
71
+ cred .getPassword ()
72
+ });
73
+ }
42
74
43
- set ("render" , this ::render );
44
- set ("result" , this ::result );
45
- set ("redirect" , this ::redirect );
75
+ private Value bodyAsBytes (Value [] args ) {
76
+ Arguments .check (0 , args .length );
77
+ return ArrayValue .of (ctx .bodyAsBytes ());
78
+ }
79
+
80
+ private Value cookie (Value [] args ) {
81
+ Arguments .checkRange (1 , 3 , args .length );
82
+ if (args .length == 1 ) {
83
+ return new StringValue (ctx .cookie (args [0 ].asString ()));
84
+ }
85
+ int maxAge = args .length == 3 ? args [2 ].asInt () : -1 ;
86
+ ctx .cookie (args [0 ].asString (), args [1 ].asString (), maxAge );
87
+ return this ;
88
+ }
89
+
90
+ private Value contentType (Value [] args ) {
91
+ Arguments .checkOrOr (0 , 1 , args .length );
92
+ if (args .length == 0 ) {
93
+ return new StringValue (ctx .contentType ());
94
+ } else {
95
+ ctx .contentType (args [0 ].asString ());
96
+ return this ;
97
+ }
98
+ }
99
+
100
+ private Value header (Value [] args ) {
101
+ Arguments .checkOrOr (1 , 2 , args .length );
102
+ String name = args [0 ].asString ();
103
+ if (args .length == 1 ) {
104
+ return new StringValue (ctx .header (name ));
105
+ } else {
106
+ ctx .header (name , args [1 ].asString ());
107
+ return this ;
108
+ }
46
109
}
47
110
48
111
private Value render (Value [] args ) {
@@ -51,9 +114,8 @@ private Value render(Value[] args) {
51
114
if (args .length == 1 ) {
52
115
ctx .render (filePath );
53
116
} 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 ()));
117
+ MapValue map = ValueUtils .consumeMap (args [1 ], 1 );
118
+ Map <String , Object > data = map .convertMap (Value ::asString , Value ::asJavaObject );
57
119
ctx .render (filePath , data );
58
120
}
59
121
return this ;
@@ -66,6 +128,14 @@ private Value redirect(Value[] args) {
66
128
return this ;
67
129
}
68
130
131
+ private Value removeCookie (Value [] args ) {
132
+ Arguments .checkOrOr (1 , 2 , args .length );
133
+ String name = args [0 ].asString ();
134
+ String path = args .length == 2 ? args [1 ].asString () : "/" ;
135
+ ctx .removeCookie (name , path );
136
+ return this ;
137
+ }
138
+
69
139
private Value result (Value [] args ) {
70
140
Arguments .checkOrOr (0 , 1 , args .length );
71
141
if (args .length == 0 ) {
@@ -81,6 +151,14 @@ private Value result(Value[] args) {
81
151
}
82
152
}
83
153
154
+ private Value stringToContext (Consumer <String > consumer ) {
155
+ return new FunctionValue (args -> {
156
+ Arguments .check (1 , args .length );
157
+ consumer .accept (args [0 ].asString ());
158
+ return this ;
159
+ });
160
+ }
161
+
84
162
private Value objectToContext (Consumer <Object > consumer ) {
85
163
return new FunctionValue (args -> {
86
164
Arguments .check (1 , args .length );
0 commit comments