11package com .flowci .core .job .controller ;
22
3+ import com .flowci .core .agent .domain .TtyCmd ;
34import com .flowci .core .auth .WebAuth ;
5+ import com .flowci .core .common .manager .SpringEventManager ;
6+ import com .flowci .core .job .event .TtyStatusUpdateEvent ;
47import com .flowci .core .job .service .TtyService ;
58import com .flowci .core .user .domain .User ;
69import com .flowci .exception .AuthenticationException ;
1114import org .springframework .messaging .handler .annotation .MessageExceptionHandler ;
1215import org .springframework .messaging .handler .annotation .MessageMapping ;
1316import org .springframework .messaging .handler .annotation .Payload ;
14- import org .springframework .messaging .simp .annotation .SendToUser ;
1517import org .springframework .stereotype .Controller ;
1618
1719@ Log4j2
@@ -21,37 +23,65 @@ public class TtyController {
2123 @ Autowired
2224 private WebAuth webAuth ;
2325
26+ @ Autowired
27+ private SpringEventManager eventManager ;
28+
2429 @ Autowired
2530 private TtyService ttyService ;
2631
2732 @ MessageExceptionHandler (AuthenticationException .class )
28- @ SendToUser ("/topic/tty/errors" )
29- public Exception handleExceptions (AuthenticationException e ) {
30- return e ;
33+ public void onAuthException (AuthenticationException e ) {
34+ TtyCmd .In in = (TtyCmd .In ) e .getExtra ();
35+
36+ TtyCmd .Out out = new TtyCmd .Out ()
37+ .setId (in .getId ())
38+ .setAction (in .getAction ())
39+ .setSuccess (false )
40+ .setError (e .getMessage ());
41+
42+ eventManager .publish (new TtyStatusUpdateEvent (this , out ));
3143 }
3244
3345 @ MessageMapping ("/tty/{jobId}/open" )
3446 public void open (@ DestinationVariable String jobId , MessageHeaders headers ) {
35- validate (headers );
36- ttyService .open (jobId );
47+ TtyCmd .In in = new TtyCmd .In ()
48+ .setId (jobId )
49+ .setAction (TtyCmd .Action .OPEN );
50+
51+ validate (in , headers );
52+ ttyService .execute (in );
3753 }
3854
3955 @ MessageMapping ("/tty/{jobId}/shell" )
4056 public void shell (@ DestinationVariable String jobId , @ Payload String script , MessageHeaders headers ) {
41- validate (headers );
42- ttyService .shell (jobId , script );
57+ TtyCmd .In in = new TtyCmd .In ()
58+ .setId (jobId )
59+ .setAction (TtyCmd .Action .SHELL )
60+ .setInput (script );
61+
62+ validate (in , headers );
63+ ttyService .execute (in );
4364 }
4465
4566 @ MessageMapping ("/tty/{jobId}/close" )
4667 public void close (@ DestinationVariable String jobId , MessageHeaders headers ) {
47- validate (headers );
48- ttyService .close (jobId );
68+ TtyCmd .In in = new TtyCmd .In ()
69+ .setId (jobId )
70+ .setAction (TtyCmd .Action .CLOSE );
71+
72+ validate (in , headers );
73+ ttyService .execute (in );
4974 }
5075
51- private void validate (MessageHeaders headers ) {
52- User user = webAuth .validate (headers );
53- if (!user .isAdmin ()) {
54- throw new AuthenticationException ("Admin permission is required" );
76+ private void validate (TtyCmd .In in , MessageHeaders headers ) {
77+ try {
78+ User user = webAuth .validate (headers );
79+ if (!user .isAdmin ()) {
80+ throw new AuthenticationException ("Admin permission is required" );
81+ }
82+ } catch (AuthenticationException e ) {
83+ e .setExtra (in );
84+ throw e ;
5585 }
5686 }
5787}
0 commit comments