1
1
package org .togetherjava .jshellapi .rest ;
2
2
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
+
3
12
import org .springframework .beans .factory .annotation .Autowired ;
4
13
import org .springframework .http .HttpStatus ;
5
14
import org .springframework .web .bind .annotation .*;
17
26
@ RequestMapping ("jshell" )
18
27
@ RestController
19
28
public 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 ;
22
33
23
34
@ 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
+ })
24
47
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 )
28
74
throws DockerException {
29
- validateId (id );
30
75
return service .session (id , startupScriptId )
31
76
.eval (code )
32
77
.orElseThrow (
@@ -68,11 +113,12 @@ public JShellResult singleEval(
68
113
69
114
@ GetMapping ("/snippets/{id}" )
70
115
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 )
72
120
throws DockerException {
73
- validateId (id );
74
- if (!service .hasSession (id ))
75
- throw new ResponseStatusException (HttpStatus .NOT_FOUND , "Id " + id + " not found" );
121
+ checkId (id );
76
122
return service .session (id , null )
77
123
.snippets (includeStartupScript )
78
124
.orElseThrow (
@@ -82,10 +128,12 @@ public List<String> snippets(
82
128
}
83
129
84
130
@ 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 );
89
137
service .deleteSession (id );
90
138
}
91
139
@@ -94,21 +142,10 @@ public String startupScript(@PathVariable StartupScriptId id) {
94
142
return startupScriptsService .get (id );
95
143
}
96
144
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 )) {
109
147
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 );
112
149
}
113
150
}
114
151
}
0 commit comments