24
24
import java .util .Enumeration ;
25
25
import java .util .HashSet ;
26
26
import java .util .List ;
27
+ import java .util .Optional ;
27
28
import java .util .Properties ;
28
29
import java .util .Set ;
29
30
import java .util .regex .Matcher ;
50
51
import com .google .gson .JsonArray ;
51
52
import com .google .gson .JsonElement ;
52
53
import com .google .gson .JsonIOException ;
54
+ import com .google .gson .JsonNull ;
53
55
import com .google .gson .JsonObject ;
54
56
import com .google .gson .JsonParser ;
55
57
import com .google .gson .JsonPrimitive ;
@@ -682,8 +684,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
682
684
}
683
685
684
686
exchange .setStatusCode (StatusCodes .OK );
685
- }
686
- if (exchange .getRelativePath ().equals ("/stub" )) {
687
+ } else if (exchange .getRelativePath ().equals ("/stub" )) {
687
688
File stubFile = sourceFolderPath .resolve ("config/stub.txt" ).toFile ();
688
689
if (exchange .getRequestMethod ().equalToString ("GET" )) {
689
690
String stub = FileUtils .readFileToString (stubFile , StandardCharsets .UTF_8 );
@@ -700,6 +701,46 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
700
701
} else {
701
702
exchange .setStatusCode (StatusCodes .NOT_FOUND );
702
703
}
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
+ }
703
744
}
704
745
} catch (MissingConfigException e ) {
705
746
sendException (exchange , e , StatusCodes .UNPROCESSABLE_ENTITY );
@@ -710,6 +751,27 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
710
751
}
711
752
}
712
753
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
+
713
775
private JsonObject extractDemoFromGameJson (File gameFile ) {
714
776
JsonObject result = new JsonObject ();
715
777
JsonParser parser = new JsonParser ();
0 commit comments