From 31e2434952baf6a0d6d37543ef002a4334991fe9 Mon Sep 17 00:00:00 2001 From: Iliescu Cristian-Mihai Date: Wed, 24 Aug 2022 15:09:36 +0300 Subject: [PATCH 1/3] Http headers passed to federated queries Signed-off-by: Iliescu Cristian-Mihai --- .../spring-rest-client-connector/pom.xml | 5 + .../spring/SpringRESTClientConnector.java | 132 ++++++++++-------- 2 files changed, 80 insertions(+), 57 deletions(-) diff --git a/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/pom.xml b/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/pom.xml index ae114cdd5d8..5e4843eb7b2 100644 --- a/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/pom.xml +++ b/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/pom.xml @@ -43,6 +43,11 @@ open-connector-framework + + org.odpi.egeria + http-helper + + org.springframework spring-web diff --git a/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/src/main/java/org/odpi/openmetadata/adapters/connectors/restclients/spring/SpringRESTClientConnector.java b/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/src/main/java/org/odpi/openmetadata/adapters/connectors/restclients/spring/SpringRESTClientConnector.java index 58f3d20292d..2159ddbed79 100644 --- a/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/src/main/java/org/odpi/openmetadata/adapters/connectors/restclients/spring/SpringRESTClientConnector.java +++ b/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/src/main/java/org/odpi/openmetadata/adapters/connectors/restclients/spring/SpringRESTClientConnector.java @@ -8,6 +8,7 @@ import org.odpi.openmetadata.adapters.connectors.restclients.ffdc.exceptions.RESTServerException; import org.odpi.openmetadata.frameworks.connectors.properties.ConnectionProperties; import org.odpi.openmetadata.frameworks.connectors.properties.EndpointProperties; +import org.odpi.openmetadata.http.HttpHeadersThreadLocal; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.ParameterizedTypeReference; @@ -25,6 +26,7 @@ import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.List; +import java.util.Map; /** @@ -161,20 +163,19 @@ public T callGetRESTCallNoParams(String methodName, T responseObject; - if (basicAuthorizationHeader == null) - { + HttpHeaders headers = getHttpHeaders(); + + if (headers.isEmpty()) { responseObject = restTemplate.getForObject(urlTemplate, returnClass); - } - else - { - HttpEntity request = new HttpEntity<>(basicAuthorizationHeader); + } else { + HttpEntity request = new HttpEntity<>(headers); - ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, returnClass); + ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, returnClass); responseObject = responseEntity.getBody(); } - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -233,7 +234,7 @@ public T callGetRESTCall(String methodName, { try { - if(log.isDebugEnabled()) + if(log.isDebugEnabled()) { //avoid calling Arrays.toString if not debug level log.debug("Calling {} with URL template {} and parameters {}.", @@ -245,20 +246,19 @@ public T callGetRESTCall(String methodName, T responseObject; - if (basicAuthorizationHeader == null) - { + HttpHeaders headers = getHttpHeaders(); + + if (headers.isEmpty()) { responseObject = restTemplate.getForObject(urlTemplate, returnClass, params); - } - else - { - HttpEntity request = new HttpEntity<>(basicAuthorizationHeader); + } else { + HttpEntity request = new HttpEntity<>(headers); - ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, returnClass, params); + ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, returnClass, params); responseObject = responseEntity.getBody(); } - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -322,22 +322,21 @@ public T callPostRESTCallNoParams(String methodName, T responseObject; - if (basicAuthorizationHeader == null) - { + HttpHeaders headers = getHttpHeaders(); + + if (headers.isEmpty()) { responseObject = restTemplate.postForObject(urlTemplate, requestBody, returnClass); - } - else - { + } else { HttpEntity request; if (requestBody != null) { - request = new HttpEntity<>(requestBody, basicAuthorizationHeader); + request = new HttpEntity<>(requestBody, headers); } else { log.warn("Poorly formed POST call made by {}.", methodName); - request = new HttpEntity<>(basicAuthorizationHeader); + request = new HttpEntity<>(headers); } ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.POST, request, returnClass); @@ -345,7 +344,7 @@ public T callPostRESTCallNoParams(String methodName, responseObject = responseEntity.getBody(); } - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -355,7 +354,7 @@ public T callPostRESTCallNoParams(String methodName, log.debug("Returning from {} with no response object.", methodName); } - + return responseObject; } @@ -420,22 +419,21 @@ public T callPostRESTCall(String methodName, } T responseObject; - if (basicAuthorizationHeader == null) - { + HttpHeaders headers = getHttpHeaders(); + + if (headers.isEmpty()) { responseObject = restTemplate.postForObject(urlTemplate, requestBody, returnClass, params); - } - else - { + } else { HttpEntity request; if (requestBody != null) { - request = new HttpEntity<>(requestBody, basicAuthorizationHeader); + request = new HttpEntity<>(requestBody, headers); } else { log.warn("Poorly formed POST call made by {}.", methodName); - request = new HttpEntity<>(basicAuthorizationHeader); + request = new HttpEntity<>(headers); } ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.POST, request, returnClass, params); @@ -443,7 +441,7 @@ public T callPostRESTCall(String methodName, responseObject = responseEntity.getBody(); } - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -527,7 +525,7 @@ public T callPutRESTCall(String methodName, ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.PUT, request, returnClass, params); T responseObject = responseEntity.getBody(); - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -588,22 +586,21 @@ public T callDeleteRESTCallNoParams(String methodName, T responseObject = null; - if (basicAuthorizationHeader == null) - { + HttpHeaders headers = getHttpHeaders(); + + if (headers.isEmpty()) { restTemplate.delete(urlTemplate); - } - else - { + } else { HttpEntity request; if (requestBody != null) { - request = new HttpEntity<>(requestBody, basicAuthorizationHeader); + request = new HttpEntity<>(requestBody, headers); } else { log.warn("Poorly formed POST call made by {}.", methodName); - request = new HttpEntity<>(basicAuthorizationHeader); + request = new HttpEntity<>(headers); } ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.DELETE, request, returnClass); @@ -611,7 +608,7 @@ public T callDeleteRESTCallNoParams(String methodName, responseObject = responseEntity.getBody(); } - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -689,7 +686,7 @@ public T callDeleteRESTCall(String methodName, } ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.DELETE, request, returnClass, params); T responseObject = responseEntity.getBody(); - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -758,26 +755,28 @@ public T callPostRESTCall(String methodName, T responseObject; HttpEntity request; - if (basicAuthorizationHeader == null) - { + + HttpHeaders headers = getHttpHeaders(); + + if (headers.isEmpty()) { request = new HttpEntity<>(requestBody); } else { if (requestBody != null) { - request = new HttpEntity<>(requestBody, basicAuthorizationHeader); + request = new HttpEntity<>(requestBody, headers); } else { log.warn("Poorly formed POST call made by {}.", methodName); - request = new HttpEntity<>(basicAuthorizationHeader); + request = new HttpEntity<>(headers); } } ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.POST, request, responseType, params); responseObject = responseEntity.getBody(); - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -847,19 +846,20 @@ public T callGetRESTCall(String methodName, T responseObject; HttpEntity request; - if (basicAuthorizationHeader == null) { + + HttpHeaders headers = getHttpHeaders(); + + if (headers.isEmpty()) { request = HttpEntity.EMPTY; - } - else - { - request = new HttpEntity<>(basicAuthorizationHeader); + } else { + request = new HttpEntity<>(headers); } ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, responseType, params); responseObject = responseEntity.getBody(); - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -936,7 +936,7 @@ public T callDeleteRESTCall(String methodName, } ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.DELETE, request, responseType, params); T responseObject = responseEntity.getBody(); - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -1015,7 +1015,7 @@ public T callPutRESTCall(String methodName, ResponseEntity responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.PUT, request, responseType, params); T responseObject = responseEntity.getBody(); - + if (responseObject != null) { log.debug("Returning from {} with response object {}", methodName, responseObject); @@ -1051,4 +1051,22 @@ public T callPutRESTCall(String methodName, error); } } + + private HttpHeaders getHttpHeaders() { + HttpHeaders headers = new HttpHeaders(); + + Map threadLocalHeaders = HttpHeadersThreadLocal.getHeadersThreadLocal().get(); + + if (threadLocalHeaders != null) { + for (Map.Entry entry : threadLocalHeaders.entrySet()) { + headers.set(entry.getKey(), entry.getValue()); + } + } + + if (basicAuthorizationHeader != null) { + headers.addAll(basicAuthorizationHeader); + } + + return headers; + } } From d0620daa428158fb90c0c4c7218593994324e758 Mon Sep 17 00:00:00 2001 From: Iliescu Cristian-Mihai Date: Wed, 24 Aug 2022 15:34:30 +0300 Subject: [PATCH 2/3] added description Signed-off-by: Iliescu Cristian-Mihai --- .../restclients/spring/SpringRESTClientConnector.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/src/main/java/org/odpi/openmetadata/adapters/connectors/restclients/spring/SpringRESTClientConnector.java b/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/src/main/java/org/odpi/openmetadata/adapters/connectors/restclients/spring/SpringRESTClientConnector.java index 2159ddbed79..c9d729dbc98 100644 --- a/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/src/main/java/org/odpi/openmetadata/adapters/connectors/restclients/spring/SpringRESTClientConnector.java +++ b/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/src/main/java/org/odpi/openmetadata/adapters/connectors/restclients/spring/SpringRESTClientConnector.java @@ -1052,6 +1052,12 @@ public T callPutRESTCall(String methodName, } } + /** + * Creates the http headers for the requests. It checks if there are headers saved in the thread local or + * any basic authorisation headers and adds them to the list. + * + * @return http headers + */ private HttpHeaders getHttpHeaders() { HttpHeaders headers = new HttpHeaders(); From 4e7de6ac943f7ec55c861b49ad7210939fdd480f Mon Sep 17 00:00:00 2001 From: Iliescu Cristian-Mihai Date: Thu, 25 Aug 2022 10:50:10 +0300 Subject: [PATCH 3/3] added gradle dependency Signed-off-by: Iliescu Cristian-Mihai --- .../spring-rest-client-connector/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/build.gradle b/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/build.gradle index b30e89a6915..3e8f2a0a9af 100644 --- a/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/build.gradle +++ b/open-metadata-implementation/adapters/open-connectors/rest-client-connectors/spring-rest-client-connector/build.gradle @@ -9,6 +9,7 @@ dependencies { implementation project(':open-metadata-implementation:adapters:open-connectors:rest-client-connectors:rest-client-connectors-api') implementation project(':open-metadata-implementation:frameworks:open-connector-framework') implementation project(':open-metadata-implementation:frameworks:audit-log-framework') + implementation project(':open-metadata-implementation:adapters:authentication-plugins:http-helper') implementation 'org.springframework:spring-web' implementation 'org.codehaus.plexus:plexus-utils' implementation 'org.springframework:spring-core'