44import io .javalin .http .Context ;
55import io .javalin .http .HttpStatus ;
66import org .jetbrains .annotations .NotNull ;
7- import java .util .HashMap ;
87import java .util .Map ;
98import java .util .function .Consumer ;
109
11- public class ContextValue extends MapValue {
10+ class ContextValue extends MapValue {
1211
1312 private final Context ctx ;
1413
1514 public ContextValue (@ NotNull Context ctx ) {
16- super (10 );
15+ super (32 );
1716 this .ctx = ctx ;
1817 init ();
1918 }
2019
2120 private void init () {
21+ set ("attribute" , this ::attribute );
22+ set ("basicAuthCredentials" , this ::basicAuthCredentials );
2223 set ("body" , Converters .voidToString (ctx ::body ));
24+ set ("bodyAsBytes" , this ::bodyAsBytes );
2325 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 );
2529 set ("contextPath" , Converters .voidToString (ctx ::contextPath ));
30+ set ("endpointHandlerPath" , Converters .voidToString (ctx ::endpointHandlerPath ));
31+ set ("formParam" , Converters .stringToString (ctx ::formParam ));
2632 set ("fullUrl" , Converters .voidToString (ctx ::fullUrl ));
33+ set ("handlerType" , Converters .voidToString (() -> ctx .handlerType ().name ()));
34+ set ("header" , this ::header );
2735 set ("host" , Converters .voidToString (ctx ::host ));
36+ set ("html" , stringToContext (ctx ::html ));
2837 set ("ip" , Converters .voidToString (ctx ::ip ));
38+ set ("json" , objectToContext (ctx ::json ));
39+ set ("jsonStream" , objectToContext (ctx ::jsonStream ));
2940 set ("matchedPath" , Converters .voidToString (ctx ::matchedPath ));
3041 set ("path" , Converters .voidToString (ctx ::path ));
42+ set ("port" , Converters .voidToInt (ctx ::port ));
3143 set ("protocol" , Converters .voidToString (ctx ::protocol ));
3244 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 ));
3351 set ("url" , Converters .voidToString (ctx ::url ));
3452 set ("userAgent" , Converters .voidToString (ctx ::userAgent ));
53+ }
3554
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+ }
3965
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+ }
4274
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+ }
46109 }
47110
48111 private Value render (Value [] args ) {
@@ -51,9 +114,8 @@ private Value render(Value[] args) {
51114 if (args .length == 1 ) {
52115 ctx .render (filePath );
53116 } 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 );
57119 ctx .render (filePath , data );
58120 }
59121 return this ;
@@ -66,6 +128,14 @@ private Value redirect(Value[] args) {
66128 return this ;
67129 }
68130
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+
69139 private Value result (Value [] args ) {
70140 Arguments .checkOrOr (0 , 1 , args .length );
71141 if (args .length == 0 ) {
@@ -81,6 +151,14 @@ private Value result(Value[] args) {
81151 }
82152 }
83153
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+
84162 private Value objectToContext (Consumer <Object > consumer ) {
85163 return new FunctionValue (args -> {
86164 Arguments .check (1 , args .length );
0 commit comments