-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJShellController.java
101 lines (87 loc) · 4.04 KB
/
JShellController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.togetherjava.jshellapi.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import org.togetherjava.jshellapi.dto.JShellResult;
import org.togetherjava.jshellapi.dto.JShellResultWithId;
import org.togetherjava.jshellapi.dto.sessionstats.SessionStats;
import org.togetherjava.jshellapi.exceptions.DockerException;
import org.togetherjava.jshellapi.service.JShellService;
import org.togetherjava.jshellapi.service.JShellSessionService;
import org.togetherjava.jshellapi.service.StartupScriptId;
import org.togetherjava.jshellapi.service.StartupScriptsService;
import java.util.List;
@RequestMapping("jshell")
@RestController
public class JShellController {
private JShellSessionService service;
private StartupScriptsService startupScriptsService;
@PostMapping("/eval/{id}")
public JShellResult eval(@PathVariable String id,
@RequestParam(required = false) StartupScriptId startupScriptId,
@RequestBody String code) throws DockerException {
validateId(id);
return service.session(id, startupScriptId)
.eval(code)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT,
"An operation is already running"));
}
@PostMapping("/eval")
public JShellResultWithId eval(@RequestParam(required = false) StartupScriptId startupScriptId,
@RequestBody String code) throws DockerException {
JShellService jShellService = service.session(startupScriptId);
return new JShellResultWithId(jShellService.id(),
jShellService.eval(code)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT,
"An operation is already running")));
}
@PostMapping("/single-eval")
public JShellResult singleEval(@RequestParam(required = false) StartupScriptId startupScriptId,
@RequestBody String code) throws DockerException {
JShellService jShellService = service.oneTimeSession(startupScriptId);
return jShellService.eval(code)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT,
"An operation is already running"));
}
@GetMapping("/snippets/{id}")
public List<String> snippets(@PathVariable String id,
@RequestParam(required = false) boolean includeStartupScript) throws DockerException {
validateId(id);
if (!service.hasSession(id))
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Id " + id + " not found");
return service.session(id, null)
.snippets(includeStartupScript)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.CONFLICT,
"An operation is already running"));
}
@DeleteMapping("/{id}")
public void delete(@PathVariable String id) throws DockerException {
validateId(id);
if (!service.hasSession(id))
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Id " + id + " not found");
service.deleteSession(id);
}
@GetMapping("sessions")
public List<SessionStats> sessions() {
return service.fetchStats();
}
@GetMapping("/startup_script/{id}")
public String startupScript(@PathVariable StartupScriptId id) {
return startupScriptsService.get(id);
}
@Autowired
public void setService(JShellSessionService service) {
this.service = service;
}
@Autowired
public void setStartupScriptsService(StartupScriptsService startupScriptsService) {
this.startupScriptsService = startupScriptsService;
}
private static void validateId(String id) throws ResponseStatusException {
if (!id.matches("[a-zA-Z0-9][a-zA-Z0-9_.-]+")) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
"Id " + id + " doesn't match the regex [a-zA-Z0-9][a-zA-Z0-9_.-]+");
}
}
}