11package com .sap .cloud .security .ams .samples ;
22
3- import java .util .concurrent .TimeUnit ;
4- import java .util .concurrent .atomic .AtomicBoolean ;
5-
6- import com .sap .cloud .security .ams .samples .auth .*;
3+ import com .sap .cloud .security .ams .samples .auth .AuthHandler ;
4+ import com .sap .cloud .security .ams .samples .auth .Role ;
75import com .sap .cloud .security .ams .samples .db .SimpleDatabase ;
86import com .sap .cloud .security .ams .samples .model .HealthStatus ;
9- import com .sap .cloud .security .ams .samples .service .*;
7+ import com .sap .cloud .security .ams .samples .service .OrdersService ;
8+ import com .sap .cloud .security .ams .samples .service .PrivilegesService ;
9+ import com .sap .cloud .security .ams .samples .service .ProductsService ;
10+ import io .javalin .Javalin ;
11+ import org .slf4j .Logger ;
12+ import org .slf4j .LoggerFactory ;
1013
11- import org .slf4j .*;
14+ import java .util .concurrent .TimeUnit ;
15+ import java .util .concurrent .atomic .AtomicBoolean ;
1216
13- import io .javalin .Javalin ;
17+ import static io .javalin .apibuilder . ApiBuilder .* ;
1418
1519/**
1620 * Factory class for creating and configuring the Javalin application.
@@ -23,7 +27,7 @@ public class AppFactory {
2327
2428 /**
2529 * Create and configure the Javalin application
26- *
30+ *
2731 * @param authHandler The authentication handler to use
2832 * @return Configured Javalin application
2933 */
@@ -36,30 +40,35 @@ public static Javalin createApp(AuthHandler authHandler) {
3640 OrdersService ordersService = new OrdersService (database , authHandler );
3741 PrivilegesService privilegesService = new PrivilegesService (authHandler );
3842
39- Javalin app = Javalin .create ();
43+ Javalin app = Javalin .create (config -> {
44+ // Request lifecycle handlers
45+ config .routes .beforeMatched (authHandler );
46+ config .routes .after (authHandler ::clear );
4047
41- app . beforeMatched ( authHandler :: handle );
42- app . after ( authHandler :: clear );
43-
44- app . get ("/health" , ctx -> {
45- if (isReady .get ()) {
46- ctx .json (HealthStatus .up ());
47- } else {
48- ctx .status (503 ).json (HealthStatus .down ("Service is not ready" ));
49- }
50- });
48+ // Routes using apiBuilder syntax
49+ config . routes . apiBuilder (() -> {
50+ // Health endpoint
51+ get ("/health" , ctx -> {
52+ if (isReady .get ()) {
53+ ctx .json (HealthStatus .up ());
54+ } else {
55+ ctx .status (503 ).json (HealthStatus .down ("Service is not ready" ));
56+ }
57+ });
5158
52- // API endpoints
53- app .get ("/privileges" , privilegesService .getPrivileges (), Role .AUTHENTICATED );
54- app .get ("/products" , productsService .getProducts (), Role .READ_PRODUCTS );
55- app .get ("/orders" , ordersService .getOrders (), Role .READ_ORDERS );
56- app .post ("/orders" , ordersService .createOrder (), Role .CREATE_ORDERS );
57- app .delete ("/orders/{id}" , ordersService .deleteOrder (), Role .DELETE_ORDERS );
59+ // API endpoints
60+ get ("/privileges" , privilegesService .getPrivileges (), Role .AUTHENTICATED );
61+ get ("/products" , productsService .getProducts (), Role .READ_PRODUCTS );
62+ get ("/orders" , ordersService .getOrders (), Role .READ_ORDERS );
63+ post ("/orders" , ordersService .createOrder (), Role .CREATE_ORDERS );
64+ delete ("/orders/{id}" , ordersService .deleteOrder (), Role .DELETE_ORDERS );
65+ });
5866
59- // Global error handler
60- app .exception (Exception .class , (e , ctx ) -> {
61- LOG .error ("Unhandled exception" , e );
62- ctx .status (500 ).result ("Internal server error" );
67+ // Global error handler
68+ config .routes .exception (Exception .class , (e , ctx ) -> {
69+ LOG .error ("Unhandled exception" , e );
70+ ctx .status (500 ).result ("Internal server error" );
71+ });
6372 });
6473
6574 // Wait up to 30s for AMS to become ready
@@ -74,4 +83,4 @@ public static Javalin createApp(AuthHandler authHandler) {
7483
7584 return app ;
7685 }
77- }
86+ }
0 commit comments