2424import java .util .Enumeration ;
2525import java .util .HashSet ;
2626import java .util .List ;
27+ import java .util .Optional ;
2728import java .util .Properties ;
2829import java .util .Set ;
2930import java .util .regex .Matcher ;
5051import com .google .gson .JsonArray ;
5152import com .google .gson .JsonElement ;
5253import com .google .gson .JsonIOException ;
54+ import com .google .gson .JsonNull ;
5355import com .google .gson .JsonObject ;
5456import com .google .gson .JsonParser ;
5557import com .google .gson .JsonPrimitive ;
@@ -682,8 +684,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
682684 }
683685
684686 exchange .setStatusCode (StatusCodes .OK );
685- }
686- if (exchange .getRelativePath ().equals ("/stub" )) {
687+ } else if (exchange .getRelativePath ().equals ("/stub" )) {
687688 File stubFile = sourceFolderPath .resolve ("config/stub.txt" ).toFile ();
688689 if (exchange .getRequestMethod ().equalToString ("GET" )) {
689690 String stub = FileUtils .readFileToString (stubFile , StandardCharsets .UTF_8 );
@@ -700,6 +701,46 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
700701 } else {
701702 exchange .setStatusCode (StatusCodes .NOT_FOUND );
702703 }
704+ } else if (exchange .getRelativePath ().equals ("/statement" )) {
705+ File statementFileEN = sourceFolderPath .resolve ("config/statement_en.html" ).toFile ();
706+ File statementFileFR = sourceFolderPath .resolve ("config/statement_fr.html" ).toFile ();
707+ if (exchange .getRequestMethod ().equalToString ("GET" )) {
708+ JsonObject statements = new JsonObject ();
709+ String statementEN = FileUtils .readFileToString (statementFileEN , StandardCharsets .UTF_8 );
710+ String statementFR ;
711+ if (!statementFileFR .exists ()) {
712+ statementFR = null ;
713+ } else {
714+ statementFR = FileUtils .readFileToString (statementFileFR , StandardCharsets .UTF_8 );
715+ }
716+ JsonElement statementElement = toJsonElement (statementFR );
717+ statements .add ("EN" , new JsonPrimitive (statementEN ));
718+ statements .add ("FR" , statementElement );
719+ exchange .getResponseSender ().send (statements .toString ());
720+ } else if (exchange .getRequestMethod ().equalToString ("PUT" )) {
721+ JsonParser parser = new JsonParser ();
722+ exchange .getRequestReceiver ().receiveFullString ((e , data ) -> {
723+ try {
724+ JsonObject result = parser .parse (data ).getAsJsonObject ();
725+ String language = result .get ("language" ).getAsString ();
726+ String statement = result .get ("statement" ).getAsString ();
727+ if (language .equals ("FR" )) {
728+ if (!createFileIfNotExists (statementFileFR , e )) {
729+ // terminate if error in createFileIfNotExists
730+ return ;
731+ }
732+ FileUtils .write (statementFileFR , statement , StandardCharsets .UTF_8 );
733+ } else if (language .equals ("EN" )) {
734+ FileUtils .write (statementFileEN , statement , StandardCharsets .UTF_8 );
735+ }
736+ exchange .setStatusCode (StatusCodes .CREATED );
737+ } catch (IOException ex ) {
738+ sendException (e , ex , StatusCodes .BAD_REQUEST );
739+ }
740+ }, StandardCharsets .UTF_8 );
741+ } else {
742+ exchange .setStatusCode (StatusCodes .NOT_FOUND );
743+ }
703744 }
704745 } catch (MissingConfigException e ) {
705746 sendException (exchange , e , StatusCodes .UNPROCESSABLE_ENTITY );
@@ -710,6 +751,27 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
710751 }
711752 }
712753
754+ private JsonElement toJsonElement (String statementFR ) {
755+ return Optional .ofNullable (statementFR )
756+ .map (s -> (JsonElement ) new JsonPrimitive (s ))
757+ .orElse (JsonNull .INSTANCE );
758+ }
759+
760+ private boolean createFileIfNotExists (File statementFileFR , HttpServerExchange e ) {
761+ if (!statementFileFR .exists ()) {
762+ try {
763+ statementFileFR .createNewFile ();
764+ return true ;
765+ } catch (IOException ex ) {
766+ sendException (e , ex , StatusCodes .INTERNAL_SERVER_ERROR );
767+ // only return false in case of error
768+ return false ;
769+ }
770+ } else {
771+ return true ;
772+ }
773+ }
774+
713775 private JsonObject extractDemoFromGameJson (File gameFile ) {
714776 JsonObject result = new JsonObject ();
715777 JsonParser parser = new JsonParser ();
0 commit comments