From 7f2aef305a4ea050550fbc996e440a32fe1d9c0c Mon Sep 17 00:00:00 2001
From: Dominika Zemanovicova <xzemanov@fi.muni.cz>
Date: Sat, 6 May 2023 16:45:56 +0200
Subject: [PATCH] Fix docker url

---
 application/confidentialClient/Dockerfile     |  1 +
 .../ConfidentialClientApplication.java        | 37 +++++++------------
 application/docker-compose.yml                | 14 +++++++
 application/module-certificate/Dockerfile     |  3 +-
 application/module-exercise/Dockerfile        |  1 +
 application/module-language-school/Dockerfile |  1 +
 application/module-mail/Dockerfile            |  1 +
 7 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/application/confidentialClient/Dockerfile b/application/confidentialClient/Dockerfile
index b4c5e364..33e77adf 100644
--- a/application/confidentialClient/Dockerfile
+++ b/application/confidentialClient/Dockerfile
@@ -1,3 +1,4 @@
 FROM docker.io/library/eclipse-temurin:17-jre-focal
 COPY ./target/confidentialClient-0.0.1-SNAPSHOT.jar /app.jar
+ENV DOCKER_RUNNING=true
 ENTRYPOINT ["java", "-jar", "/app.jar"]
\ No newline at end of file
diff --git a/application/confidentialClient/src/main/java/org/fuseri/confidentialclient/ConfidentialClientApplication.java b/application/confidentialClient/src/main/java/org/fuseri/confidentialclient/ConfidentialClientApplication.java
index e5c713eb..02690292 100644
--- a/application/confidentialClient/src/main/java/org/fuseri/confidentialclient/ConfidentialClientApplication.java
+++ b/application/confidentialClient/src/main/java/org/fuseri/confidentialclient/ConfidentialClientApplication.java
@@ -1,16 +1,12 @@
 package org.fuseri.confidentialclient;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
 import jakarta.servlet.ServletException;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.servlet.http.HttpServletResponse;
 import org.fuseri.model.dto.user.UserCreateDto;
-import org.fuseri.model.dto.user.UserDto;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.annotation.Bean;
-import org.springframework.http.MediaType;
 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
@@ -18,8 +14,8 @@ import org.springframework.security.oauth2.core.oidc.user.OidcUser;
 import org.springframework.security.web.SecurityFilterChain;
 import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
 import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
-import org.springframework.web.reactive.function.BodyInserters;
-import org.springframework.web.reactive.function.client.WebClient;
+import org.springframework.web.client.RestClientException;
+import org.springframework.web.client.RestTemplate;
 
 import java.io.IOException;
 
@@ -30,10 +26,6 @@ public class ConfidentialClientApplication {
         SpringApplication.run(ConfidentialClientApplication.class, args);
     }
 
-    private static String asJsonString(final Object obj) throws JsonProcessingException {
-        return new ObjectMapper().writeValueAsString(obj);
-    }
-
     @Bean
     public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
         httpSecurity
@@ -46,17 +38,15 @@ public class ConfidentialClientApplication {
                 .oauth2Login(x -> x
                         // our custom handler for successful logins
                         .successHandler(authenticationSuccessHandler())
-                )
-        ;
+                );
         return httpSecurity.build();
     }
         @Bean
         public AuthenticationSuccessHandler authenticationSuccessHandler() {
             return new SavedRequestAwareAuthenticationSuccessHandler() {
+
                 @Override
                 public void onAuthenticationSuccess(HttpServletRequest req, HttpServletResponse res, Authentication auth) throws ServletException, IOException {
-
-
                     if (auth instanceof OAuth2AuthenticationToken token
                             && token.getPrincipal() instanceof OidcUser user) {
                         var createDto = new UserCreateDto();
@@ -65,15 +55,13 @@ public class ConfidentialClientApplication {
                         createDto.setEmail(user.getEmail());
                         createDto.setUsername(user.getPreferredUsername());
 
-                        var result = WebClient.builder().baseUrl("http://localhost:8081/users/register").build().post()
-                                .contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(createDto))
-                                .retrieve();
-
+                        String url = buildRegisterUrl();
+                        RestTemplate userRegisterRestTemplate = new RestTemplate();
 
                         try {
-                            result.bodyToMono(UserDto.class).block();
-                        } catch (Exception e) {
-                            System.out.println("unable to save user");
+                            userRegisterRestTemplate.postForObject(url, createDto, UserCreateDto.class);
+                        } catch (RestClientException e) {
+                            throw new ServletException("Unable to register user" + System.getenv("DOCKER_RUNNING"), e);
                         }
                     }
                     super.onAuthenticationSuccess(req, res, auth);
@@ -81,6 +69,9 @@ public class ConfidentialClientApplication {
             };
         }
 
-
-
+        private static String buildRegisterUrl() {
+            boolean dockerRunning = Boolean.parseBoolean(System.getenv("DOCKER_RUNNING"));
+            String host = dockerRunning ? "host.docker.internal" : "localhost";
+            return "http://" + host + ":8081/users/register";
+        }
 }
diff --git a/application/docker-compose.yml b/application/docker-compose.yml
index 59ad6d94..8fe1bc82 100644
--- a/application/docker-compose.yml
+++ b/application/docker-compose.yml
@@ -10,6 +10,8 @@ services:
     image: xpokorn8/sprachschulsystem:certificate
     ports:
       - "8082:8082"
+    environment:
+      - DOCKER_RUNNING=true
 
   exercise:
     build: ./module-exercise
@@ -17,6 +19,8 @@ services:
     image: xpokorn8/sprachschulsystem:exercise
     ports:
       - "8083:8083"
+    environment:
+      - DOCKER_RUNNING=true
 
   language-school:
     build: ./module-language-school
@@ -24,6 +28,8 @@ services:
     image: xpokorn8/sprachschulsystem:language-school
     ports:
       - "8081:8081"
+    environment:
+      - DOCKER_RUNNING=true
 
   mail:
     build: ./module-mail
@@ -31,11 +37,15 @@ services:
     image: xpokorn8/sprachschulsystem:mail
     ports:
       - "8084:8084"
+    environment:
+      - DOCKER_RUNNING=true
       
   confidential-client:
     build: ./confidentialClient
     container_name: confidential-client
     image: xpokorn8/sprachschulsystem:confidential-client
+    environment:
+      - DOCKER_RUNNING=true
     ports:
       - "8080:8080"
 
@@ -45,6 +55,8 @@ services:
     volumes:
       - ./prometheus:/etc/prometheus
       - prometheus_data:/prometheus
+    environment:
+      - DOCKER_RUNNING=true
     expose:
       - 9090
     ports:
@@ -56,6 +68,8 @@ services:
       image: grafana/grafana:7.5.7
       ports:
         - "3000:3000"
+      environment:
+        - DOCKER_RUNNING=true
       restart: unless-stopped
       volumes:
         - ./grafana/provisioning/datasources:/etc/grafana/provisioning/datasources
diff --git a/application/module-certificate/Dockerfile b/application/module-certificate/Dockerfile
index 21e346d9..48a4910d 100644
--- a/application/module-certificate/Dockerfile
+++ b/application/module-certificate/Dockerfile
@@ -1,3 +1,4 @@
 FROM docker.io/library/eclipse-temurin:17-jre-focal
 COPY ./target/module-certificate-0.0.1-SNAPSHOT.jar /app.jar
-ENTRYPOINT ["java", "-jar", "/app.jar"]
+ENV DOCKER_RUNNING=true
+ENTRYPOINT ["java", "-jar", "/app.jar"]
\ No newline at end of file
diff --git a/application/module-exercise/Dockerfile b/application/module-exercise/Dockerfile
index 8e31eb6c..f5bd8def 100644
--- a/application/module-exercise/Dockerfile
+++ b/application/module-exercise/Dockerfile
@@ -1,3 +1,4 @@
 FROM docker.io/library/eclipse-temurin:17-jre-focal
 COPY ./target/module-exercise-0.0.1-SNAPSHOT.jar /app.jar
+ENV DOCKER_RUNNING=true
 ENTRYPOINT ["java", "-jar", "/app.jar"]
diff --git a/application/module-language-school/Dockerfile b/application/module-language-school/Dockerfile
index f2be765c..29382037 100644
--- a/application/module-language-school/Dockerfile
+++ b/application/module-language-school/Dockerfile
@@ -1,3 +1,4 @@
 FROM docker.io/library/eclipse-temurin:17-jre-focal
 COPY ./target/module-language-school-0.0.1-SNAPSHOT.jar /app.jar
+ENV DOCKER_RUNNING=true
 ENTRYPOINT ["java", "-jar", "/app.jar"]
diff --git a/application/module-mail/Dockerfile b/application/module-mail/Dockerfile
index 2e3cde27..dfaaed1d 100644
--- a/application/module-mail/Dockerfile
+++ b/application/module-mail/Dockerfile
@@ -1,3 +1,4 @@
 FROM docker.io/library/eclipse-temurin:17-jre-focal
 COPY ./target/module-mail-0.0.1-SNAPSHOT.jar /app.jar
+ENV DOCKER_RUNNING=true
 ENTRYPOINT ["java", "-jar", "/app.jar"]
-- 
GitLab