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 ;
@@ -700,45 +702,45 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
700
702
exchange .setStatusCode (StatusCodes .NOT_FOUND );
701
703
}
702
704
} else if (exchange .getRelativePath ().equals ("/statement" )) {
703
- File statementFileEN = sourceFolderPath .resolve ("config/statement_en.html" ).toFile ();
704
- File statementFileFR = sourceFolderPath .resolve ("config/statement_fr.html" ).toFile ();
705
- if (exchange .getRequestMethod ().equalToString ("POST " )) {
706
- String statementEN = FileUtils . readFileToString ( statementFileEN , StandardCharsets . UTF_8 );
707
- String statementFR ;
708
- if (! statementFileFR . exists ()) {
709
- statementFR = "" ;
710
- } else {
711
- statementFR = FileUtils . readFileToString ( statementFileFR , StandardCharsets . UTF_8 );
712
- }
713
-
714
- exchange . getRequestReceiver (). receiveFullString (( e , data ) -> {
715
- if ( data . equals ( "FR" )) {
716
- exchange . getResponseSender (). send ( statementFR );
717
- } else {
718
- exchange .getResponseSender ().send ( statementEN );
719
- }
720
- }, StandardCharsets . UTF_8 );
721
- } else if ( exchange . getRequestMethod (). equalToString ( "PUT" )) {
722
- JsonParser parser = new JsonParser ();
723
- exchange . getRequestReceiver (). receiveFullString (( e , data ) -> {
724
- try {
725
- JsonObject result = parser . parse ( data ). getAsJsonObject ();
726
- String language = result . get ( "language" ). getAsString ();
727
- String statement = result . get ( "statement" ). getAsString ();
728
- if ( language . equals ( "FR" )) {
729
- createFileIfNotExists ( statementFileFR , e );
730
- FileUtils .write (statementFileFR , statement , StandardCharsets .UTF_8 );
731
- } else if (language .equals ("EN" )) {
732
- FileUtils .write (statementFileEN , statement , StandardCharsets .UTF_8 );
733
- }
734
- exchange .setStatusCode (StatusCodes .CREATED );
735
- } catch (IOException ex ) {
736
- sendException (e , ex , StatusCodes .BAD_REQUEST );
737
- }
738
- }, StandardCharsets .UTF_8 );
739
- } else {
740
- exchange .setStatusCode (StatusCodes .NOT_FOUND );
741
- }
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
+ }
742
744
}
743
745
} catch (MissingConfigException e ) {
744
746
sendException (exchange , e , StatusCodes .UNPROCESSABLE_ENTITY );
@@ -749,14 +751,25 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
749
751
}
750
752
}
751
753
752
- private void createFileIfNotExists (File statementFileFR , HttpServerExchange e ) {
753
- if (!statementFileFR .exists ()){
754
- try {
755
- statementFileFR .createNewFile ();
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 ;
756
765
} catch (IOException ex ) {
757
- sendException (e , ex , StatusCodes .INTERNAL_SERVER_ERROR );
766
+ sendException (e , ex , StatusCodes .INTERNAL_SERVER_ERROR );
767
+ // only return false in case of error
768
+ return false ;
758
769
}
759
- }
770
+ } else {
771
+ return true ;
772
+ }
760
773
}
761
774
762
775
private JsonObject extractDemoFromGameJson (File gameFile ) {
0 commit comments