Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/odpi/egeria into egeria-code
Browse files Browse the repository at this point in the history
  • Loading branch information
mandy-chessell committed Aug 30, 2022
2 parents 8af044a + 112ca23 commit 8d68778
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<artifactId>open-connector-framework</artifactId>
</dependency>

<dependency>
<groupId>org.odpi.egeria</groupId>
<artifactId>http-helper</artifactId>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,7 @@
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
Expand Down Expand Up @@ -161,20 +163,19 @@ public <T> 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<T> responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, returnClass);
ResponseEntity<T> responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, returnClass);

responseObject = responseEntity.getBody();
}


if (responseObject != null)
{
log.debug("Returning from {} with response object {}", methodName, responseObject);
Expand Down Expand Up @@ -233,7 +234,7 @@ public <T> 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 {}.",
Expand All @@ -245,20 +246,19 @@ public <T> 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<T> responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, returnClass, params);
ResponseEntity<T> responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, returnClass, params);

responseObject = responseEntity.getBody();
}


if (responseObject != null)
{
log.debug("Returning from {} with response object {}", methodName, responseObject);
Expand Down Expand Up @@ -322,30 +322,29 @@ public <T> 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<T> responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.POST, request, returnClass);

responseObject = responseEntity.getBody();
}


if (responseObject != null)
{
log.debug("Returning from {} with response object {}", methodName, responseObject);
Expand All @@ -355,7 +354,7 @@ public <T> T callPostRESTCallNoParams(String methodName,
log.debug("Returning from {} with no response object.", methodName);
}



return responseObject;
}
Expand Down Expand Up @@ -420,30 +419,29 @@ public <T> 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<T> responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.POST, request, returnClass, params);

responseObject = responseEntity.getBody();
}


if (responseObject != null)
{
log.debug("Returning from {} with response object {}", methodName, responseObject);
Expand Down Expand Up @@ -527,7 +525,7 @@ public <T> T callPutRESTCall(String methodName,
ResponseEntity<T> 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);
Expand Down Expand Up @@ -588,30 +586,29 @@ public <T> 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<T> responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.DELETE, request, returnClass);

responseObject = responseEntity.getBody();
}


if (responseObject != null)
{
log.debug("Returning from {} with response object {}", methodName, responseObject);
Expand Down Expand Up @@ -689,7 +686,7 @@ public <T> T callDeleteRESTCall(String methodName,
}
ResponseEntity<T> 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);
Expand Down Expand Up @@ -758,26 +755,28 @@ public <T> 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<T> responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.POST, request, responseType, params);

responseObject = responseEntity.getBody();


if (responseObject != null)
{
log.debug("Returning from {} with response object {}", methodName, responseObject);
Expand Down Expand Up @@ -847,19 +846,20 @@ public <T> 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<T> responseEntity = restTemplate.exchange(urlTemplate, HttpMethod.GET, request, responseType, params);

responseObject = responseEntity.getBody();


if (responseObject != null)
{
log.debug("Returning from {} with response object {}", methodName, responseObject);
Expand Down Expand Up @@ -936,7 +936,7 @@ public <T> T callDeleteRESTCall(String methodName,
}
ResponseEntity<T> 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);
Expand Down Expand Up @@ -1015,7 +1015,7 @@ public <T> T callPutRESTCall(String methodName,
ResponseEntity<T> 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);
Expand Down Expand Up @@ -1051,4 +1051,28 @@ public <T> T callPutRESTCall(String methodName,
error);
}
}

/**
* 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();

Map<String, String> threadLocalHeaders = HttpHeadersThreadLocal.getHeadersThreadLocal().get();

if (threadLocalHeaders != null) {
for (Map.Entry<String, String> entry : threadLocalHeaders.entrySet()) {
headers.set(entry.getKey(), entry.getValue());
}
}

if (basicAuthorizationHeader != null) {
headers.addAll(basicAuthorizationHeader);
}

return headers;
}
}

0 comments on commit 8d68778

Please sign in to comment.