commit c1ad4a51473bc0e0744e5d71651f3f3d31242cf5
Author: stepanekp <1stepanekpetr@gmail.com>
Date:   Tue Mar 5 21:28:56 2024 +0100

    Metrics integrated into UI

diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/detecting/DetectingController.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/detecting/DetectingController.java
index 148868c..0db539a 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/detecting/DetectingController.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/controller/detecting/DetectingController.java
@@ -5,6 +5,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.*;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.client.HttpStatusCodeException;
+import org.springframework.web.client.RestClientException;
 import org.springframework.web.client.RestTemplate;
 import org.springframework.web.servlet.HandlerMapping;
 
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/errorHandler/CustomErrorHandler.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/errorHandler/CustomErrorHandler.java
new file mode 100644
index 0000000..956d7c8
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/errorHandler/CustomErrorHandler.java
@@ -0,0 +1,39 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.errorHandler;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.http.client.ClientHttpResponse;
+import org.springframework.web.client.DefaultResponseErrorHandler;
+import org.springframework.web.client.HttpClientErrorException;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @author Jiri Trefil
+ * Custom HTTP error handler for RestTemplate class
+ *
+ */
+public class CustomErrorHandler extends DefaultResponseErrorHandler {
+    /**
+     *
+     * Override default custom behavior of Response Handler to be able to read body of error responses
+     * because default error handle can only parse body of generic errors.
+     * @param response Http response containing error code
+     * @throws IOException if file descriptor (socket) is no longer valid in the response
+     */
+    @Override
+    public void handleError(ClientHttpResponse response) throws IOException {
+        if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {
+            final InputStream inputStream = response.getBody();
+            byte[] responseBody = inputStream.readAllBytes();
+            inputStream.close();
+            //String responseBody = StreamUtils.copyToString(response.getBody(),StandardCharsets.UTF_8);
+            throw new HttpClientErrorException(response.getStatusCode(),response.getStatusText(),responseBody,StandardCharsets.UTF_8);
+        } else {
+            super.handleError(response);
+        }
+    }
+
+
+}
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/errorHandler/CustomResponseErrorHandler.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/errorHandler/CustomResponseErrorHandler.java
new file mode 100644
index 0000000..6f60893
--- /dev/null
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/errorHandler/CustomResponseErrorHandler.java
@@ -0,0 +1,33 @@
+package cz.zcu.fav.kiv.antipatterndetectionapp.errorHandler;
+
+
+import org.springframework.http.HttpStatus;
+import org.springframework.http.client.ClientHttpResponse;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.ResponseErrorHandler;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.stream.Collectors;
+
+public class CustomResponseErrorHandler implements ResponseErrorHandler {
+    @Override
+    public void handleError(ClientHttpResponse response) throws IOException {
+        HttpStatus statusCode = response.getStatusCode();
+
+        if (statusCode == HttpStatus.CONFLICT) {
+            throw new HttpClientErrorException(statusCode, "Conflict error.");
+        } else if (statusCode == HttpStatus.BAD_REQUEST) {
+            throw new HttpClientErrorException(statusCode, "Bad Request.");
+        } else {
+            throw new HttpClientErrorException(statusCode, "Unexpected error occurred.");
+        }
+    }
+
+    @Override
+    public boolean hasError(ClientHttpResponse response) throws IOException {
+        return response.getStatusCode().isError();
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/httpErrorHandler/CustomErrorHandler.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/httpErrorHandler/CustomErrorHandler.java
deleted file mode 100644
index aaa0dcc..0000000
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/httpErrorHandler/CustomErrorHandler.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package cz.zcu.fav.kiv.antipatterndetectionapp.httpErrorHandler;
-
-import org.springframework.http.HttpStatus;
-import org.springframework.http.client.ClientHttpResponse;
-import org.springframework.util.StreamUtils;
-import org.springframework.web.client.DefaultResponseErrorHandler;
-import org.springframework.web.client.HttpClientErrorException;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-
-/**
- * @author Jiri Trefil
- * Custom HTTP error handler for RestTemplate class
- *
- */
-public class CustomErrorHandler extends DefaultResponseErrorHandler {
-    /**
-     *
-     * Override default custom behavior of Response Handler to be able to read body of error responses
-     * because default error handle can only parse body of generic errors.
-     * @param response Http response containing error code
-     * @throws IOException if file descriptor (socket) is no longer valid in the response
-     */
-    @Override
-    public void handleError(ClientHttpResponse response) throws IOException {
-        if (response.getStatusCode() == HttpStatus.UNAUTHORIZED) {
-            final InputStream inputStream = response.getBody();
-            byte[] responseBody = inputStream.readAllBytes();
-            inputStream.close();
-            //String responseBody = StreamUtils.copyToString(response.getBody(),StandardCharsets.UTF_8);
-            throw new HttpClientErrorException(response.getStatusCode(),response.getStatusText(),responseBody,StandardCharsets.UTF_8);
-        } else {
-            super.handleError(response);
-        }
-    }
-
-
-}
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/detecting/DetectingService.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/detecting/DetectingService.java
index 1aa35a1..8ee4a3a 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/detecting/DetectingService.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/service/detecting/DetectingService.java
@@ -3,6 +3,9 @@ package cz.zcu.fav.kiv.antipatterndetectionapp.service.detecting;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.*;
 import org.springframework.stereotype.Service;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.web.client.HttpStatusCodeException;
+import org.springframework.web.client.RestClientResponseException;
 import org.springframework.web.client.RestTemplate;
 
 @Service
@@ -15,14 +18,12 @@ public class DetectingService {
         HttpHeaders headers = new HttpHeaders();
         headers.setContentType(MediaType.APPLICATION_JSON);
 
-        HttpEntity<String> requestEntity = null;
-        if (requestBody != null) {
-            requestEntity = new HttpEntity<>(requestBody, headers);
-        } else {
-            requestEntity = new HttpEntity<>(headers);
-        }
+        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);
 
-        return restTemplate.exchange(url, method, requestEntity, String.class);
+        try {
+            return restTemplate.exchange(url, method, requestEntity, String.class);
+        } catch (RestClientResponseException e) {
+                return ResponseEntity.status(e.getRawStatusCode()).body(e.getStatusText());
+        }
     }
 }
-
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/spring/AppConfig.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/spring/AppConfig.java
index 35fb2bb..b8d9776 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/spring/AppConfig.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/spring/AppConfig.java
@@ -1,5 +1,6 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.spring;
 
+import cz.zcu.fav.kiv.antipatterndetectionapp.errorHandler.CustomResponseErrorHandler;
 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
@@ -57,6 +58,8 @@ public class AppConfig implements WebMvcConfigurer {
 
     @Bean
     public RestTemplate restTemplate() {
-        return new RestTemplate();
+        RestTemplate restTemplate = new RestTemplate();
+        restTemplate.setErrorHandler(new CustomResponseErrorHandler());
+        return restTemplate;
     }
 }
diff --git a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/utils/RequestBuilder.java b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/utils/RequestBuilder.java
index d7f941b..7c073f1 100644
--- a/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/utils/RequestBuilder.java
+++ b/src/main/java/cz/zcu/fav/kiv/antipatterndetectionapp/utils/RequestBuilder.java
@@ -1,6 +1,6 @@
 package cz.zcu.fav.kiv.antipatterndetectionapp.utils;
 
-import cz.zcu.fav.kiv.antipatterndetectionapp.httpErrorHandler.CustomErrorHandler;
+import cz.zcu.fav.kiv.antipatterndetectionapp.errorHandler.CustomErrorHandler;
 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
 import org.springframework.stereotype.Component;
 import org.springframework.util.StreamUtils;
