11package  org .togetherjava .jshellapi .rest ;
22
3+ import  io .swagger .v3 .oas .annotations .Operation ;
4+ import  io .swagger .v3 .oas .annotations .Parameter ;
5+ import  io .swagger .v3 .oas .annotations .media .Content ;
6+ import  io .swagger .v3 .oas .annotations .media .ExampleObject ;
7+ import  io .swagger .v3 .oas .annotations .media .Schema ;
8+ import  io .swagger .v3 .oas .annotations .responses .ApiResponse ;
9+ 
10+ import  jakarta .validation .constraints .Pattern ;
11+ 
312import  org .springframework .beans .factory .annotation .Autowired ;
413import  org .springframework .http .HttpStatus ;
514import  org .springframework .web .bind .annotation .*;
1726@ RequestMapping ("jshell" )
1827@ RestController 
1928public  class  JShellController  {
20-     private  JShellSessionService  service ;
21-     private  StartupScriptsService  startupScriptsService ;
29+     private  static  final  String  ID_REGEX  = "^[a-zA-Z0-9][a-zA-Z0-9_.-]+$" ;
30+ 
31+     @ Autowired  private  JShellSessionService  service ;
32+     @ Autowired  private  StartupScriptsService  startupScriptsService ;
2233
2334    @ PostMapping ("/eval/{id}" )
35+     @ Operation (
36+             summary  = "Evaluate code in a JShell session" ,
37+             description  =
38+                     "Evaluate code in a JShell session, create a session from this id, or use an" 
39+                             + " existing session if this id already exists." )
40+     @ ApiResponse (
41+             responseCode  = "200" ,
42+             content  = {
43+                 @ Content (
44+                         mediaType  = "application/json" ,
45+                         schema  = @ Schema (implementation  = JShellResult .class ))
46+             })
2447    public  JShellResult  eval (
25-             @ PathVariable  String  id ,
26-             @ RequestParam (required  = false ) StartupScriptId  startupScriptId ,
27-             @ RequestBody  String  code )
48+             @ Parameter (description  = "id of the session, must follow the regex "  + ID_REGEX )
49+                     @ Pattern (regexp  = ID_REGEX , message  = "'id' doesn't match regex "  + ID_REGEX )
50+                     @ PathVariable 
51+                     String  id ,
52+             @ Parameter (description  = "id of the startup script to use" )
53+                     @ RequestParam (required  = false )
54+                     StartupScriptId  startupScriptId ,
55+             @ io .swagger .v3 .oas .annotations .parameters .RequestBody (
56+                             content  = {
57+                                 @ Content (
58+                                         mediaType  = "text/plain" ,
59+                                         examples  = {
60+                                             @ ExampleObject (
61+                                                     name  = "Hello world example" ,
62+                                                     value  =
63+                                                             "System.out.println(\" Hello," 
64+                                                                     + " World!\" );" ),
65+                                             @ ExampleObject (
66+                                                     name  =
67+                                                             "Hello world example with startup" 
68+                                                                     + " script" ,
69+                                                     value  = "println(\" Hello, World!\" );" )
70+                                         })
71+                             })
72+                     @ RequestBody 
73+                     String  code )
2874            throws  DockerException  {
29-         validateId (id );
3075        return  service .session (id , startupScriptId )
3176                .eval (code )
3277                .orElseThrow (
@@ -68,11 +113,12 @@ public JShellResult singleEval(
68113
69114    @ GetMapping ("/snippets/{id}" )
70115    public  List <String > snippets (
71-             @ PathVariable  String  id , @ RequestParam (required  = false ) boolean  includeStartupScript )
116+             @ PathVariable 
117+                     @ Pattern (regexp  = ID_REGEX , message  = "'id' doesn't match regex "  + ID_REGEX )
118+                     String  id ,
119+             @ RequestParam (required  = false ) boolean  includeStartupScript )
72120            throws  DockerException  {
73-         validateId (id );
74-         if  (!service .hasSession (id ))
75-             throw  new  ResponseStatusException (HttpStatus .NOT_FOUND , "Id "  + id  + " not found" );
121+         checkId (id );
76122        return  service .session (id , null )
77123                .snippets (includeStartupScript )
78124                .orElseThrow (
@@ -82,10 +128,12 @@ public List<String> snippets(
82128    }
83129
84130    @ DeleteMapping ("/{id}" )
85-     public  void  delete (@ PathVariable  String  id ) throws  DockerException  {
86-         validateId (id );
87-         if  (!service .hasSession (id ))
88-             throw  new  ResponseStatusException (HttpStatus .NOT_FOUND , "Id "  + id  + " not found" );
131+     public  void  delete (
132+             @ PathVariable 
133+                     @ Pattern (regexp  = ID_REGEX , message  = "'id' doesn't match regex "  + ID_REGEX )
134+                     String  id )
135+             throws  DockerException  {
136+         checkId (id );
89137        service .deleteSession (id );
90138    }
91139
@@ -94,21 +142,10 @@ public String startupScript(@PathVariable StartupScriptId id) {
94142        return  startupScriptsService .get (id );
95143    }
96144
97-     @ Autowired 
98-     public  void  setService (JShellSessionService  service ) {
99-         this .service  = service ;
100-     }
101- 
102-     @ Autowired 
103-     public  void  setStartupScriptsService (StartupScriptsService  startupScriptsService ) {
104-         this .startupScriptsService  = startupScriptsService ;
105-     }
106- 
107-     private  static  void  validateId (String  id ) throws  ResponseStatusException  {
108-         if  (!id .matches ("[a-zA-Z0-9][a-zA-Z0-9_.-]+" )) {
145+     private  void  checkId (String  id ) {
146+         if  (!id .matches (ID_REGEX )) {
109147            throw  new  ResponseStatusException (
110-                     HttpStatus .BAD_REQUEST ,
111-                     "Id "  + id  + " doesn't match the regex [a-zA-Z0-9][a-zA-Z0-9_.-]+" );
148+                     HttpStatus .BAD_REQUEST , "Id "  + id  + " doesn't match regex "  + ID_REGEX );
112149        }
113150    }
114151}
0 commit comments