1+ package camp .pixels .ctf .highway ;
2+
3+ import java .io .PrintWriter ;
4+ import java .io .StringWriter ;
5+ import java .util .Base64 ;
6+ import org .apache .commons .lang3 .SerializationUtils ;
7+
8+ import static spark .Spark .*;
9+
10+ public class DisplayController {
11+
12+ public DisplayController (final DisplayService srv , int listen ) {
13+
14+ if (listen > 0 && listen < 65536 ) {
15+ port (listen );
16+ }
17+
18+ get ("/text" , (req , res ) -> {
19+ byte [] data = SerializationUtils .serialize (srv .getDisplay ());
20+ return Base64 .getEncoder ().encodeToString (data );
21+ });
22+
23+ post ("/text" , (req , res ) -> {
24+ String body = req .body ();
25+ body = body .replace ("\n " , "" ).replace ("\r \n " , "" );
26+ System .out .println ("Got request: [" + body + "]" );
27+ byte [] data = Base64 .getDecoder ().decode (body );
28+ Display d = SerializationUtils .deserialize (data );
29+ srv .setText (d );
30+ return "Message updated!" ;
31+ });
32+
33+ exception (IllegalArgumentException .class , (e , req , res ) -> {
34+ res .status (400 );
35+ res .body (e .getMessage ());
36+ });
37+
38+ exception (Exception .class , (e , req , res ) -> {
39+ StringWriter buffer = new StringWriter ();
40+ PrintWriter printer = new PrintWriter (buffer );
41+ e .printStackTrace (printer );
42+ res .status (500 );
43+ res .body (buffer .toString ());
44+ });
45+
46+ notFound ((req , res ) -> {
47+ return "Not found." ;
48+ });
49+ after ((req , res ) -> {
50+ res .type ("text/plain" );
51+ });
52+
53+ }
54+ }
0 commit comments