diff options
Diffstat (limited to 'aai-rest')
8 files changed, 205 insertions, 27 deletions
diff --git a/aai-rest/pom.xml b/aai-rest/pom.xml index 526bf2f2..d0173039 100644 --- a/aai-rest/pom.xml +++ b/aai-rest/pom.xml @@ -29,11 +29,11 @@ <parent> <groupId>org.onap.aai.aai-common</groupId> <artifactId>aai-common</artifactId> - <version>1.4.1-SNAPSHOT</version> + <version>1.4.2-SNAPSHOT</version> </parent> <artifactId>aai-rest</artifactId> <name>aai-rest</name> - <version>1.4.1-SNAPSHOT</version> + <version>1.4.2-SNAPSHOT</version> <properties> <onap.nexus.url>https://nexus.onap.org</onap.nexus.url> diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/AAIRestClient.java b/aai-rest/src/main/java/org/onap/aai/restclient/AAIRestClient.java new file mode 100644 index 00000000..652f399a --- /dev/null +++ b/aai-rest/src/main/java/org/onap/aai/restclient/AAIRestClient.java @@ -0,0 +1,88 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.restclient; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Component; +import org.springframework.util.MultiValueMap; + +import java.util.Collections; +import java.util.Map; + +@Component(value=ClientType.AAI) +public class AAIRestClient extends TwoWaySSLRestClient{ + + private static EELFLogger logger = EELFManager.getInstance().getLogger(AAIRestClient.class); + + @Value("${aai.base.url}") + private String baseUrl; + + @Value("${aai.ssl.key-store}") + private String keystorePath; + + @Value("${aai.ssl.trust-store}") + private String truststorePath; + + @Value("${aai.ssl.key-store-password}") + private String keystorePassword; + + @Value("${aai.ssl.trust-store-password}") + private String truststorePassword; + + @Override + public String getBaseUrl() { + return baseUrl; + } + + @Override + protected String getKeystorePath() { + return keystorePath; + } + + @Override + protected String getTruststorePath() { + return truststorePath; + } + + @Override + protected char[] getKeystorePassword() { + return keystorePassword.toCharArray(); + } + + @Override + protected char[] getTruststorePassword() { + return truststorePassword.toCharArray(); + } + + @Override + public MultiValueMap<String, String> getHeaders(Map<String, String> headers) { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + httpHeaders.add("Real-Time", "true"); + headers.forEach(httpHeaders::add); + return httpHeaders; + } + +} diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/ClientType.java b/aai-rest/src/main/java/org/onap/aai/restclient/ClientType.java new file mode 100644 index 00000000..855bab71 --- /dev/null +++ b/aai-rest/src/main/java/org/onap/aai/restclient/ClientType.java @@ -0,0 +1,24 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.restclient; + +public class ClientType { + public static final String AAI = "aai-rest-client"; +} diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/NoAuthRestClient.java b/aai-rest/src/main/java/org/onap/aai/restclient/NoAuthRestClient.java index 7e3524d7..47fe99d8 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/NoAuthRestClient.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/NoAuthRestClient.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,7 +42,7 @@ public abstract class NoAuthRestClient extends RestClient{ .requestFactory(new HttpComponentsClientHttpRequestFactory(client)) .build(); - restTemplate.setErrorHandler(new RestClientResponseErrorHandler(getLogger())); + restTemplate.setErrorHandler(new RestClientResponseErrorHandler()); } @Override diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/OneWaySSLRestClient.java b/aai-rest/src/main/java/org/onap/aai/restclient/OneWaySSLRestClient.java index e502e5e5..96cd51e8 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/OneWaySSLRestClient.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/OneWaySSLRestClient.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,8 @@ */ package org.onap.aai.restclient; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; @@ -36,6 +38,8 @@ import java.security.KeyStore; public abstract class OneWaySSLRestClient extends RestClient { + private static EELFLogger logger = EELFManager.getInstance().getLogger(OneWaySSLRestClient.class); + private RestTemplate restTemplate; @PostConstruct @@ -59,7 +63,7 @@ public abstract class OneWaySSLRestClient extends RestClient { .requestFactory(new HttpComponentsClientHttpRequestFactory(client)) .build(); - restTemplate.setErrorHandler(new RestClientResponseErrorHandler(getLogger())); + restTemplate.setErrorHandler(new RestClientResponseErrorHandler()); } diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/RestClient.java b/aai-rest/src/main/java/org/onap/aai/restclient/RestClient.java index a17880f3..6ac51b2f 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/RestClient.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/RestClient.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Modifications Copyright © 2018 IBM. * ================================================================================ @@ -32,6 +32,8 @@ import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; +import java.net.URI; +import java.net.URISyntaxException; import java.util.Map; public abstract class RestClient { @@ -40,17 +42,82 @@ public abstract class RestClient { @Value("${spring.application.name}") protected String appName; - public ResponseEntity execute(String uri, HttpMethod method, Map<String, String> headers, String body) throws RestClientException { - HttpEntity httpEntity; - log.debug("Headers: " + headers.toString()); + /** + * Execute the given http method against the uri with passed headers + * @param uri properly encoded, can include query params also properly encoded + * @param method http method of the request + * @param headers headers for the request + * @param body body of the request + * @return response of request + * @throws RestClientException on internal rest template exception or invalid url + */ + public ResponseEntity execute(String uri, HttpMethod method, Map<String,String> headers, String body) throws RestClientException { + + HttpEntity<String> httpEntity; + log.debug ("Headers: {}", headers); if (body == null) { - httpEntity = new HttpEntity(getHeaders(headers)); + httpEntity = new HttpEntity<>(getHeaders(headers)); } else { - httpEntity = new HttpEntity(body, getHeaders(headers)); + httpEntity = new HttpEntity<>(body, getHeaders(headers)); } - String url = getBaseUrl() + uri; - return getRestTemplate().exchange(url, method, httpEntity, String.class); + + // verify that either the base url ends with '/' or uri starts with '/', adjust uri accordingly. + if (getBaseUrl().endsWith("/") && uri.startsWith("/")) { + uri = uri.replaceFirst("/", ""); + } else if (!getBaseUrl().endsWith("/") && !uri.startsWith("/")) { + uri = "/" + uri; + } + + URI url; + try { + url = new URI(getBaseUrl() + uri); + } catch (URISyntaxException e) { + log.error("URL syntax error with url {}{}", getBaseUrl(), uri); + throw new RestClientException(e.getMessage()); + } + log.debug("METHOD={},URL={},http={}" + method, url, httpEntity); + + ResponseEntity responseEntity = getRestTemplate().exchange(url, method, httpEntity, String.class); + log.debug("RESPONSE={}", responseEntity); + return responseEntity; + } + + /** + * Execute the given http method against the uri with passed headers + * @param uri properly encoded, can include query params also properly encoded + * @param method http method of the request + * @param headers headers for the request + * @param body body of the request + * @return response of request + * @throws RestClientException on internal rest template exception or invalid url + */ + public ResponseEntity execute(String uri, String method, Map<String,String> headers, String body) throws RestClientException{ + return execute(uri, HttpMethod.valueOf(method), headers, body); + } + + /** + * Execute the given http method against the uri with passed headers + * @param uri properly encoded, can include query params also properly encoded + * @param method http method of the request + * @param headers headers for the request + * @return response of request + * @throws RestClientException on internal rest template exception or invalid url + */ + public ResponseEntity execute(String uri, HttpMethod method, Map<String,String> headers) throws RestClientException{ + return execute(uri, method, headers, null); + } + + /** + * Execute the given http method against the uri with passed headers + * @param uri properly encoded, can include query params also properly encoded + * @param method http method of the request + * @param headers headers for the request + * @return response of request + * @throws RestClientException on internal rest template exception or invalid url + */ + public ResponseEntity execute(String uri, String method, Map<String,String> headers) throws RestClientException{ + return execute(uri, HttpMethod.valueOf(method), headers, null); } public ResponseEntity executeResource(String uri, HttpMethod method, Map<String, String> headers, String body) throws RestClientException { @@ -66,10 +133,6 @@ public abstract class RestClient { return getRestTemplate().exchange(url, method, httpEntity, Resource.class); } - public ResponseEntity execute(String uri, String method, Map<String, String> headers) throws RestClientException { - return execute(uri, HttpMethod.valueOf(method), headers, null); - } - public ResponseEntity getGetRequest(String content, String uri, Map<String, String> headersMap) { return this.execute( uri, @@ -94,6 +157,4 @@ public abstract class RestClient { protected abstract MultiValueMap<String, String> getHeaders(Map<String, String> headers); - protected abstract EELFLogger getLogger(); - } diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/RestClientResponseErrorHandler.java b/aai-rest/src/main/java/org/onap/aai/restclient/RestClientResponseErrorHandler.java index 1d6486b0..2271a1f5 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/RestClientResponseErrorHandler.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/RestClientResponseErrorHandler.java @@ -20,6 +20,7 @@ package org.onap.aai.restclient; import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpResponse; import org.springframework.web.client.ResponseErrorHandler; @@ -28,11 +29,7 @@ import java.io.IOException; public class RestClientResponseErrorHandler implements ResponseErrorHandler { - private EELFLogger logger; - - public RestClientResponseErrorHandler(EELFLogger logger) { - this.logger = logger; - } + private static EELFLogger logger = EELFManager.getInstance().getLogger(RestClientResponseErrorHandler.class); @Override public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException { diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/TwoWaySSLRestClient.java b/aai-rest/src/main/java/org/onap/aai/restclient/TwoWaySSLRestClient.java index 2fe9500f..dd5deaa6 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/TwoWaySSLRestClient.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/TwoWaySSLRestClient.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * org.onap.aai * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,8 @@ */ package org.onap.aai.restclient; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; @@ -36,6 +38,8 @@ import java.security.KeyStore; public abstract class TwoWaySSLRestClient extends RestClient { + private static EELFLogger logger = EELFManager.getInstance().getLogger(TwoWaySSLRestClient.class); + private RestTemplate restTemplate; @PostConstruct @@ -62,7 +66,7 @@ public abstract class TwoWaySSLRestClient extends RestClient { .requestFactory(new HttpComponentsClientHttpRequestFactory(client)) .build(); - restTemplate.setErrorHandler(new RestClientResponseErrorHandler(getLogger())); + restTemplate.setErrorHandler(new RestClientResponseErrorHandler()); } |