diff options
Diffstat (limited to 'common-app-api/src/main/java/org/openecomp/sdc/common/http')
18 files changed, 275 insertions, 320 deletions
diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/ComparableHttpRequestRetryHandler.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/ComparableHttpRequestRetryHandler.java index 21c0f45e24..31363e50c1 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/ComparableHttpRequestRetryHandler.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/ComparableHttpRequestRetryHandler.java @@ -7,9 +7,9 @@ * 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. @@ -17,11 +17,12 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; + import org.apache.http.client.HttpRequestRetryHandler; public interface ComparableHttpRequestRetryHandler extends HttpRequestRetryHandler { + public default <T extends HttpRequestRetryHandler> boolean compare(T handler) { return (handler != null && getClass() == handler.getClass()); } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClient.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClient.java index 66a7050821..a7ae6e6671 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClient.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClient.java @@ -7,9 +7,9 @@ * 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. @@ -17,9 +17,11 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; +import java.io.IOException; +import java.net.URI; +import java.util.Properties; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; @@ -29,7 +31,13 @@ import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.AuthCache; import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.methods.*; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPatch; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.BasicAuthCache; @@ -41,117 +49,107 @@ import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.common.datastructure.FunctionalInterfaces.FunctionThrows; import org.openecomp.sdc.common.log.wrappers.Logger; -import java.io.IOException; -import java.net.URI; -import java.util.Properties; - public class HttpClient { + private static final Logger logger = Logger.getLogger(HttpClient.class.getName()); - private final CloseableHttpClient client; private final HttpClientConfigImmutable configImmutable; - + public HttpClient(CloseableHttpClient client, HttpClientConfigImmutable configImmutable) { this.client = client; - this.configImmutable = configImmutable; + this.configImmutable = configImmutable; } - - public <T> HttpResponse<T> get(String url, Properties headers, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException { + + public <T> HttpResponse<T> get(String url, Properties headers, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) + throws HttpExecuteException { HttpGet httpGet = new HttpGet(url); return execute(httpGet, headers, responseBuilder); } - <T> HttpResponse<T> put(String url, Properties headers, HttpEntity entity, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException { + <T> HttpResponse<T> put(String url, Properties headers, HttpEntity entity, + FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException { HttpPut httpPut = new HttpPut(url); httpPut.setEntity(entity); return execute(httpPut, headers, responseBuilder); } - <T> HttpResponse<T> post(String url, Properties headers, HttpEntity entity, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException { + <T> HttpResponse<T> post(String url, Properties headers, HttpEntity entity, + FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException { HttpPost httpPost = new HttpPost(url); httpPost.setEntity(entity); return execute(httpPost, headers, responseBuilder); } - <T> HttpResponse<T> patch(String url, Properties headers, HttpEntity entity, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException { + <T> HttpResponse<T> patch(String url, Properties headers, HttpEntity entity, + FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException { HttpPatch httpPatch = new HttpPatch(url); httpPatch.setEntity(entity); return execute(httpPatch, headers, responseBuilder); } - <T> HttpResponse<T> delete(String url, Properties headers, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException { + <T> HttpResponse<T> delete(String url, Properties headers, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) + throws HttpExecuteException { HttpDelete httpDelete = new HttpDelete(url); return execute(httpDelete, headers, responseBuilder); } - + void close() { try { client.close(); - } - catch (IOException e) { + } catch (IOException e) { logger.debug("Close http client failed with exception ", e); } } - - private <T> HttpResponse<T> execute(HttpRequestBase request, Properties headers, FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) throws HttpExecuteException { - if(configImmutable.getHeaders() != null) { + + private <T> HttpResponse<T> execute(HttpRequestBase request, Properties headers, + FunctionThrows<CloseableHttpResponse, HttpResponse<T>, Exception> responseBuilder) + throws HttpExecuteException { + if (configImmutable.getHeaders() != null) { configImmutable.getHeaders().forEach(request::addHeader); } - if (headers != null) { headers.forEach((k, v) -> request.addHeader(k.toString(), v.toString())); } - HttpClientContext httpClientContext = null; - if(request.getHeaders(HttpHeaders.AUTHORIZATION).length == 0) { + if (request.getHeaders(HttpHeaders.AUTHORIZATION).length == 0) { httpClientContext = createHttpContext(request.getURI()); } - logger.debug("Execute request {}", request.getRequestLine()); try (CloseableHttpResponse response = client.execute(request, httpClientContext)) { return responseBuilder.apply(response); - } - catch (Exception e) { - String description = String.format("Execute request %s failed with exception: %s", request.getRequestLine(), e.getMessage()); + } catch (Exception e) { + String description = String.format("Execute request %s failed with exception: %s", request.getRequestLine(), e.getMessage()); BeEcompErrorManager.getInstance().logInternalFlowError("ExecuteRestRequest", description, ErrorSeverity.ERROR); - logger.debug("{}: ",description, e); - + logger.debug("{}: ", description, e); throw new HttpExecuteException(description, e); - } + } } private HttpClientContext createHttpContext(URI uri) { - if(StringUtils.isEmpty(configImmutable.getBasicAuthUserName()) || StringUtils.isEmpty(configImmutable.getBasicAuthPassword())) { + if (StringUtils.isEmpty(configImmutable.getBasicAuthUserName()) || StringUtils.isEmpty(configImmutable.getBasicAuthPassword())) { return null; } - CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); int port = getPort(uri); - credentialsProvider.setCredentials(new AuthScope(uri.getHost(), port), - new UsernamePasswordCredentials(configImmutable.getBasicAuthUserName(), configImmutable.getBasicAuthPassword())); - + credentialsProvider.setCredentials(new AuthScope(uri.getHost(), port), + new UsernamePasswordCredentials(configImmutable.getBasicAuthUserName(), configImmutable.getBasicAuthPassword())); HttpClientContext localContext = HttpClientContext.create(); localContext.setCredentialsProvider(credentialsProvider); - AuthCache authCache = new BasicAuthCache(); HttpHost target = new HttpHost(uri.getHost(), port, "https"); authCache.put(target, (AuthScheme) new BasicScheme()); localContext.setAuthCache(authCache); - return localContext; } private int getPort(URI uri) { - int port = uri.getPort(); - if(port < 0) { - if(Constants.HTTPS.equals(uri.getScheme())) { + int port = uri.getPort(); + if (port < 0) { + if (Constants.HTTPS.equals(uri.getScheme())) { port = 443; - } - else - if (Constants.HTTP.equals(uri.getScheme())) { + } else if (Constants.HTTP.equals(uri.getScheme())) { port = 80; - } - else { + } else { port = AuthScope.ANY_PORT; logger.debug("Protocol \"{}\" is not supported, set port to {}", uri.getScheme(), port); } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClientConfigImmutable.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClientConfigImmutable.java index 9b21e1ac87..7498315041 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClientConfigImmutable.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClientConfigImmutable.java @@ -7,9 +7,9 @@ * 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. @@ -17,39 +17,33 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import org.openecomp.sdc.common.http.config.BasicAuthorization; import org.openecomp.sdc.common.http.config.ClientCertificate; import org.openecomp.sdc.common.http.config.HttpClientConfig; import org.openecomp.sdc.common.http.config.Timeouts; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - public final class HttpClientConfigImmutable { private final Map<String, String> headers; private final BasicAuthorization basicAuthorization; private final ClientCertificate clientCertificate; private final Timeouts timeouts; - private boolean enableMetricLogging; /* - * use ComparableHttpRequestRetryHandler.compare instead of default generated equals + * use ComparableHttpRequestRetryHandler.compare instead of default generated equals */ private final ComparableHttpRequestRetryHandler retryHandler; private final int numOfRetries; - - static HttpClientConfigImmutable getOrCreate(HttpClientConfig httpClientConfig) { - // TODO: retrieve from a pool if exist, otherwise create new - return new HttpClientConfigImmutable(httpClientConfig); - } + private boolean enableMetricLogging; public HttpClientConfigImmutable(HttpClientConfig httpClientConfig) { timeouts = httpClientConfig.getTimeouts() != null ? new Timeouts(httpClientConfig.getTimeouts()) : null; - basicAuthorization = httpClientConfig.getBasicAuthorization() != null ? new BasicAuthorization(httpClientConfig.getBasicAuthorization()) : null; + basicAuthorization = + httpClientConfig.getBasicAuthorization() != null ? new BasicAuthorization(httpClientConfig.getBasicAuthorization()) : null; clientCertificate = httpClientConfig.getClientCertificate() != null ? new ClientCertificate(httpClientConfig.getClientCertificate()) : null; headers = httpClientConfig.getHeaders() != null ? Collections.unmodifiableMap(new HashMap<>(httpClientConfig.getHeaders())) : null; retryHandler = httpClientConfig.getRetryHandler(); @@ -57,6 +51,11 @@ public final class HttpClientConfigImmutable { enableMetricLogging = httpClientConfig.isEnableMetricLogging(); } + static HttpClientConfigImmutable getOrCreate(HttpClientConfig httpClientConfig) { + // TODO: retrieve from a pool if exist, otherwise create new + return new HttpClientConfigImmutable(httpClientConfig); + } + public boolean isEnableMetricLogging() { return enableMetricLogging; } @@ -68,7 +67,7 @@ public final class HttpClientConfigImmutable { public int getNumOfRetries() { return numOfRetries; } - + String getBasicAuthPassword() { return basicAuthorization != null ? basicAuthorization.getPassword() : null; } @@ -88,7 +87,7 @@ public final class HttpClientConfigImmutable { ClientCertificate getClientCertificate() { return clientCertificate != null ? new ClientCertificate(clientCertificate) : null; } - + public int getReadTimeoutMs() { return timeouts.getReadTimeoutMs(); } @@ -119,43 +118,51 @@ public final class HttpClientConfigImmutable { @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } HttpClientConfigImmutable other = (HttpClientConfigImmutable) obj; if (basicAuthorization == null) { - if (other.basicAuthorization != null) + if (other.basicAuthorization != null) { return false; - } - else if (!basicAuthorization.equals(other.basicAuthorization)) + } + } else if (!basicAuthorization.equals(other.basicAuthorization)) { return false; + } if (clientCertificate == null) { - if (other.clientCertificate != null) + if (other.clientCertificate != null) { return false; - } - else if (!clientCertificate.equals(other.clientCertificate)) + } + } else if (!clientCertificate.equals(other.clientCertificate)) { return false; + } if (headers == null) { - if (other.headers != null) + if (other.headers != null) { return false; - } - else if (!headers.equals(other.headers)) + } + } else if (!headers.equals(other.headers)) { return false; + } if (retryHandler == null) { - if (other.retryHandler != null) + if (other.retryHandler != null) { return false; - } - else if (!retryHandler.compare(other.retryHandler)) + } + } else if (!retryHandler.compare(other.retryHandler)) { return false; + } if (timeouts == null) { - if (other.timeouts != null) + if (other.timeouts != null) { return false; - } - else if (!timeouts.equals(other.timeouts)) + } + } else if (!timeouts.equals(other.timeouts)) { return false; + } return true; } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClientFactory.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClientFactory.java index 982453246f..d45deb328b 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClientFactory.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpClientFactory.java @@ -7,9 +7,9 @@ * 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. @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; import org.apache.http.client.HttpRequestRetryHandler; @@ -25,12 +24,9 @@ import org.apache.http.client.UserTokenHandler; import org.apache.http.client.config.RequestConfig; import org.apache.http.conn.HttpClientConnectionManager; import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.common.http.config.ClientCertificate; -import org.openecomp.sdc.common.log.interceptors.ApacheClientLogRequestInterceptor; -import org.openecomp.sdc.common.log.interceptors.ApacheClientLogResponseInterceptor; import org.openecomp.sdc.common.log.wrappers.Logger; public class HttpClientFactory { @@ -38,36 +34,27 @@ public class HttpClientFactory { private static final Logger logger = Logger.getLogger(HttpClientFactory.class.getName()); private static final UserTokenHandler userTokenHandler = context -> null; private final HttpConnectionMngFactory connectionMngFactory; - + HttpClientFactory(HttpConnectionMngFactory connectionMngFactory) { this.connectionMngFactory = connectionMngFactory; } HttpClient createClient(String protocol, HttpClientConfigImmutable config) { logger.debug("Create {} client based on {}", protocol, config); - - ClientCertificate clientCertificate = Constants.HTTPS.equals(protocol) ? config.getClientCertificate() : null; + ClientCertificate clientCertificate = Constants.HTTPS.equals(protocol) ? config.getClientCertificate() : null; HttpClientConnectionManager connectionManager = connectionMngFactory.getOrCreate(clientCertificate); RequestConfig requestConfig = createClientTimeoutConfiguration(config); - CloseableHttpClient client = HttpClients.custom() - .setDefaultRequestConfig(requestConfig) - .setConnectionManager(connectionManager) - .setUserTokenHandler(userTokenHandler) - .setRetryHandler(resolveRetryHandler(config)) - .build(); - + CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager) + .setUserTokenHandler(userTokenHandler).setRetryHandler(resolveRetryHandler(config)).build(); return new HttpClient(client, config); } private HttpRequestRetryHandler resolveRetryHandler(HttpClientConfigImmutable config) { return config.getNumOfRetries() > 0 ? config.getRetryHandler() : null; } - + private RequestConfig createClientTimeoutConfiguration(HttpClientConfigImmutable config) { - return RequestConfig.custom() - .setConnectTimeout(config.getConnectTimeoutMs()) - .setSocketTimeout(config.getReadTimeoutMs()) - .setConnectionRequestTimeout(config.getConnectPoolTimeoutMs()) - .build(); + return RequestConfig.custom().setConnectTimeout(config.getConnectTimeoutMs()).setSocketTimeout(config.getReadTimeoutMs()) + .setConnectionRequestTimeout(config.getConnectPoolTimeoutMs()).build(); } } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpConnectionMngFactory.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpConnectionMngFactory.java index c5634eaf8c..966bf857c8 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpConnectionMngFactory.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpConnectionMngFactory.java @@ -7,9 +7,9 @@ * 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. @@ -17,9 +17,15 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.GeneralSecurityException; +import java.security.KeyStore; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.lang3.StringUtils; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; @@ -35,69 +41,50 @@ import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.common.http.config.ClientCertificate; import org.openecomp.sdc.common.log.wrappers.Logger; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.security.GeneralSecurityException; -import java.security.KeyStore; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - public class HttpConnectionMngFactory { private static final String P12_KEYSTORE_EXTENTION = ".p12"; private static final String PFX_KEYSTORE_EXTENTION = ".pfx"; private static final String JKS_KEYSTORE_EXTENTION = ".jks"; - private static final String P12_KEYSTORE_TYPE = "pkcs12"; private static final String JKS_KEYSTORE_TYPE = "jks"; - private static final Logger logger = Logger.getLogger(HttpConnectionMngFactory.class.getName()); private static final int DEFAULT_CONNECTION_POOL_SIZE = 30; private static final int DEFAULT_MAX_CONNECTION_PER_ROUTE = 5; private static final int VALIDATE_CONNECTION_AFTER_INACTIVITY_MS = 10000; - private Map<ClientCertificate, HttpClientConnectionManager> sslClientConnectionManagers = new ConcurrentHashMap<>(); private HttpClientConnectionManager plainClientConnectionManager; - + HttpConnectionMngFactory() { plainClientConnectionManager = createConnectionMng(null); } HttpClientConnectionManager getOrCreate(ClientCertificate clientCertificate) { - if(clientCertificate == null) { + if (clientCertificate == null) { return plainClientConnectionManager; } return sslClientConnectionManagers.computeIfAbsent(clientCertificate, k -> createConnectionMng(clientCertificate)); } - - private HttpClientConnectionManager createConnectionMng(ClientCertificate clientCertificate) { + private HttpClientConnectionManager createConnectionMng(ClientCertificate clientCertificate) { SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); SSLConnectionSocketFactory sslsf = null; try { sslContextBuilder.loadTrustMaterial(new TrustSelfSignedStrategy()); - - if(clientCertificate != null) { + if (clientCertificate != null) { setClientSsl(clientCertificate, sslContextBuilder); } sslsf = new SSLConnectionSocketFactory(sslContextBuilder.build(), NoopHostnameVerifier.INSTANCE); - } - catch (GeneralSecurityException e) { + } catch (GeneralSecurityException e) { logger.debug("Create SSL connection socket factory failed with exception, use default SSL factory ", e); sslsf = SSLConnectionSocketFactory.getSocketFactory(); } - Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() - .register(Constants.HTTP, PlainConnectionSocketFactory.getSocketFactory()) - .register(Constants.HTTPS, sslsf).build(); - + .register(Constants.HTTP, PlainConnectionSocketFactory.getSocketFactory()).register(Constants.HTTPS, sslsf).build(); PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry); - manager.setMaxTotal(DEFAULT_CONNECTION_POOL_SIZE); manager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTION_PER_ROUTE); manager.setValidateAfterInactivity(VALIDATE_CONNECTION_AFTER_INACTIVITY_MS); - return manager; } @@ -107,12 +94,11 @@ public class HttpConnectionMngFactory { KeyStore clientKeyStore = createClientKeyStore(clientCertificate.getKeyStore(), keyStorePassword); sslContextBuilder.loadKeyMaterial(clientKeyStore, keyStorePassword); logger.debug("#setClientSsl - Set Client Certificate authentication"); - } - catch (IOException | GeneralSecurityException e) { + } catch (IOException | GeneralSecurityException e) { logger.debug("#setClientSsl - Set Client Certificate authentication failed with exception, diasable client SSL authentication ", e); } } - + private KeyStore createClientKeyStore(String keyStorePath, char[] keyStorePassword) throws IOException, GeneralSecurityException { KeyStore keyStore = null; try (InputStream stream = new FileInputStream(keyStorePath)) { @@ -121,15 +107,14 @@ public class HttpConnectionMngFactory { } return keyStore; } - + private String getKeyStoreType(String keyStore) { - if(!StringUtils.isEmpty(keyStore)) { - if(keyStore.endsWith(P12_KEYSTORE_EXTENTION) || keyStore.endsWith(PFX_KEYSTORE_EXTENTION)) { + if (!StringUtils.isEmpty(keyStore)) { + if (keyStore.endsWith(P12_KEYSTORE_EXTENTION) || keyStore.endsWith(PFX_KEYSTORE_EXTENTION)) { return P12_KEYSTORE_TYPE; - } - else if(keyStore.endsWith(JKS_KEYSTORE_EXTENTION)) { + } else if (keyStore.endsWith(JKS_KEYSTORE_EXTENTION)) { return JKS_KEYSTORE_TYPE; - } + } } return KeyStore.getDefaultType(); } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpExecuteException.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpExecuteException.java index 8487e1ff72..9c1435a211 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpExecuteException.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpExecuteException.java @@ -7,9 +7,9 @@ * 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. @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; public class HttpExecuteException extends Exception { @@ -25,17 +24,17 @@ public class HttpExecuteException extends Exception { private static final long serialVersionUID = 1L; public HttpExecuteException(String message) { - super (message); + super(message); } public HttpExecuteException(String message, Throwable cause) { - super (message, cause); + super(message, cause); } public HttpExecuteException(Throwable cause) { super(cause); } - + @Override public Throwable getCause() { Throwable cause = super.getCause(); diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequest.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequest.java index 57e5cb2bed..7deac29d67 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequest.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequest.java @@ -7,9 +7,9 @@ * 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. @@ -17,22 +17,18 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; +import java.util.Properties; import org.apache.http.HttpEntity; import org.openecomp.sdc.common.http.config.HttpClientConfig; -import java.util.Properties; - //TODO- remove all static and use instance methods for better testing public abstract class HttpRequest { - static final Properties defaultHeaders = null; + static final Properties defaultHeaders = null; static final HttpClientConfig defaultConfig = new HttpClientConfig(); - - private HttpRequest() { } @@ -46,7 +42,7 @@ public abstract class HttpRequest { public static HttpResponse<String> get(String url, Properties headers) throws HttpExecuteException { return get(url, headers, defaultConfig); } - + public static HttpResponse<String> get(String url, HttpClientConfig config) throws HttpExecuteException { return get(url, defaultHeaders, config); } @@ -84,7 +80,7 @@ public abstract class HttpRequest { public static HttpResponse<String> put(String url, Properties headers, HttpEntity entity) throws HttpExecuteException { return put(url, headers, entity, defaultConfig); } - + public static HttpResponse<String> put(String url, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException { return put(url, defaultHeaders, entity, config); } @@ -103,7 +99,7 @@ public abstract class HttpRequest { public static HttpResponse<String> post(String url, Properties headers, HttpEntity entity) throws HttpExecuteException { return post(url, headers, entity, defaultConfig); } - + public static HttpResponse<String> post(String url, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException { return post(url, defaultHeaders, entity, config); } @@ -111,7 +107,7 @@ public abstract class HttpRequest { public static HttpResponse<String> post(String url, Properties headers, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException { return HttpRequestHandler.get().post(url, headers, entity, config); } - + /* * PATCH */ @@ -122,7 +118,7 @@ public abstract class HttpRequest { public static HttpResponse<String> patch(String url, Properties headers, HttpEntity entity) throws HttpExecuteException { return patch(url, headers, entity, defaultConfig); } - + public static HttpResponse<String> patch(String url, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException { return patch(url, defaultHeaders, entity, config); } @@ -130,7 +126,7 @@ public abstract class HttpRequest { public static HttpResponse<String> patch(String url, Properties headers, HttpEntity entity, HttpClientConfig config) throws HttpExecuteException { return HttpRequestHandler.get().patch(url, headers, entity, config); } - + /* * DELETE */ @@ -141,7 +137,7 @@ public abstract class HttpRequest { public static HttpResponse<String> delete(String url, Properties headers) throws HttpExecuteException { return delete(url, headers, defaultConfig); } - + public static HttpResponse<String> delete(String url, HttpClientConfig config) throws HttpExecuteException { return delete(url, defaultHeaders, config); } @@ -149,7 +145,7 @@ public abstract class HttpRequest { public static HttpResponse<String> delete(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException { return HttpRequestHandler.get().delete(url, headers, config); } - + public static void destroy() { HttpRequestHandler.get().destroy(); } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java index 73f0f15354..c52933b401 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpRequestHandler.java @@ -7,9 +7,9 @@ * 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. @@ -17,10 +17,13 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; import com.google.common.annotations.VisibleForTesting; +import java.io.InputStream; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; @@ -29,19 +32,13 @@ import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.common.datastructure.FunctionalInterfaces.FunctionThrows; import org.openecomp.sdc.common.http.config.HttpClientConfig; -import java.io.InputStream; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; - public class HttpRequestHandler { - private static HttpRequestHandler handlerInstance = new HttpRequestHandler(); + private static final String HTTPS_PREFIX = "https://"; private static final String HTTP_PREFIX = "http://"; - + private static HttpRequestHandler handlerInstance = new HttpRequestHandler(); private Map<HttpClientConfigImmutable, HttpClient> clients = new ConcurrentHashMap<>(); private HttpClientFactory clientFactory; - private FunctionThrows<CloseableHttpResponse, HttpResponse<byte[]>, Exception> byteResponseBuilder = (CloseableHttpResponse httpResponse) -> { HttpEntity entity = httpResponse.getEntity(); byte[] response = null; @@ -51,37 +48,37 @@ public class HttpRequestHandler { response = IOUtils.toByteArray(content); } } - return new HttpResponse<>(response, - httpResponse.getStatusLine().getStatusCode(), - httpResponse.getStatusLine().getReasonPhrase()); + return new HttpResponse<>(response, httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); }; - private FunctionThrows<CloseableHttpResponse, HttpResponse<String>, Exception> stringResponseBuilder = (CloseableHttpResponse httpResponse) -> { HttpEntity entity = httpResponse.getEntity(); String response = null; if (entity != null) { response = EntityUtils.toString(entity); } - return new HttpResponse<>(response, - httpResponse.getStatusLine().getStatusCode(), - httpResponse.getStatusLine().getReasonPhrase()); + return new HttpResponse<>(response, httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); }; private HttpRequestHandler() { HttpConnectionMngFactory connectionMngFactory = new HttpConnectionMngFactory(); clientFactory = new HttpClientFactory(connectionMngFactory); } - + public static HttpRequestHandler get() { return handlerInstance; } + @VisibleForTesting + public static void setTestInstance(HttpRequestHandler handlerInstance) { + HttpRequestHandler.handlerInstance = handlerInstance; + } + public HttpResponse<String> get(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException { HttpClient client = getOrCreateClient(url, config); return client.<String>get(url, headers, stringResponseBuilder); } - public HttpResponse<byte []> getAsByteArray(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException { + public HttpResponse<byte[]> getAsByteArray(String url, Properties headers, HttpClientConfig config) throws HttpExecuteException { HttpClient client = getOrCreateClient(url, config); return client.<byte[]>get(url, headers, byteResponseBuilder); } @@ -105,12 +102,12 @@ public class HttpRequestHandler { HttpClient client = getOrCreateClient(url, config != null ? config : HttpRequest.defaultConfig); return client.<String>delete(url, headers, stringResponseBuilder); } - + public void destroy() { clients.forEach((k, v) -> v.close()); clients.clear(); } - + private HttpClient getOrCreateClient(String url, HttpClientConfig config) throws HttpExecuteException { String protocol = getProtocol(url); HttpClientConfigImmutable httpClientConfigImmutable = HttpClientConfigImmutable.getOrCreate(config); @@ -121,20 +118,13 @@ public class HttpRequestHandler { return clientFactory.createClient(protocol, config); } - @VisibleForTesting - public static void setTestInstance(HttpRequestHandler handlerInstance) { - HttpRequestHandler.handlerInstance = handlerInstance; - } - private String getProtocol(String url) throws HttpExecuteException { if (url.startsWith(HTTPS_PREFIX)) { return Constants.HTTPS; - } - else if (url.startsWith(HTTP_PREFIX)) { + } else if (url.startsWith(HTTP_PREFIX)) { return Constants.HTTP; - } - else { + } else { throw new HttpExecuteException(String.format("Failed to create http client. Requested protocol is not supported \"%s\"", url)); - } + } } } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpResponse.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpResponse.java index c6061cf1c2..573c97f130 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpResponse.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/HttpResponse.java @@ -7,9 +7,9 @@ * 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. @@ -17,12 +17,12 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; import org.apache.commons.lang3.StringUtils; public class HttpResponse<T> { + private final T response; private final int statusCode; private final String description; @@ -32,7 +32,7 @@ public class HttpResponse<T> { this.statusCode = statusCode; this.description = StringUtils.EMPTY; } - + public HttpResponse(T response, int statusCode, String description) { this.response = response; this.statusCode = statusCode; @@ -63,6 +63,4 @@ public class HttpResponse<T> { builder.append("]"); return builder.toString(); } - - } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/Responses.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/Responses.java index 71f9ac2629..529502d7c9 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/Responses.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/Responses.java @@ -7,9 +7,9 @@ * 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. @@ -17,14 +17,14 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; import org.apache.http.HttpStatus; public final class Responses { + public static final HttpResponse<String> INTERNAL_SERVER_ERROR = new HttpResponse<>("Internal server error", HttpStatus.SC_INTERNAL_SERVER_ERROR); - + private Responses() { } } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/RestUtils.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/RestUtils.java index 7b39e3f8c9..47fc1a8422 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/RestUtils.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/RestUtils.java @@ -7,9 +7,9 @@ * 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. @@ -17,14 +17,12 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; -import org.apache.commons.codec.binary.Base64; -import org.apache.http.HttpHeaders; - import java.nio.charset.StandardCharsets; import java.util.Properties; +import org.apache.commons.codec.binary.Base64; +import org.apache.http.HttpHeaders; public class RestUtils { @@ -32,5 +30,4 @@ public class RestUtils { byte[] credentials = Base64.encodeBase64((username + ":" + password).getBytes(StandardCharsets.UTF_8)); headers.setProperty(HttpHeaders.AUTHORIZATION, "Basic " + new String(credentials, StandardCharsets.UTF_8)); } - } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/RetryHandlers.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/RetryHandlers.java index bbcd815e75..1c4a5a47a7 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/RetryHandlers.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/client/api/RetryHandlers.java @@ -7,9 +7,9 @@ * 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. @@ -17,19 +17,18 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.client.api; +import java.io.IOException; import org.apache.http.protocol.HttpContext; import org.openecomp.sdc.common.log.wrappers.Logger; -import java.io.IOException; - public class RetryHandlers { private static final Logger logger = Logger.getLogger(RetryHandlers.class.getName()); - private RetryHandlers(){} + private RetryHandlers() { + } public static ComparableHttpRequestRetryHandler getDefault(int numOfRetries) { return (IOException exception, int executionCount, HttpContext context) -> { @@ -38,6 +37,4 @@ public class RetryHandlers { return executionCount <= numOfRetries; }; } - - } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/BasicAuthorization.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/BasicAuthorization.java index 124c7ea0c8..c9147719a4 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/BasicAuthorization.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/BasicAuthorization.java @@ -7,9 +7,9 @@ * 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. @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.config; import fj.data.Either; @@ -25,6 +24,7 @@ import org.apache.commons.lang3.StringUtils; import org.onap.sdc.security.SecurityUtil; public class BasicAuthorization { + private String userName; private String password; @@ -36,36 +36,33 @@ public class BasicAuthorization { setPassword(basicAuthorization.password, false); } + public String getUserName() { + return userName; + } + public void setUserName(String userName) { validate(userName); this.userName = userName; } - public void setPassword(String password) { - setPassword(password, true); - } - - public String getUserName() { - return userName; - } - public String getPassword() { return password; } - + public void setPassword(String password) { + setPassword(password, true); + } + private void setPassword(String password, boolean isEncoded) { validate(password); - if(isEncoded) { + if (isEncoded) { Either<String, String> passkey = SecurityUtil.INSTANCE.decrypt(password); - if(passkey.isLeft()) { + if (passkey.isLeft()) { this.password = passkey.left().value(); - } - else { + } else { throw new IllegalArgumentException(passkey.right().value()); } - } - else { + } else { this.password = password; } } @@ -81,25 +78,30 @@ public class BasicAuthorization { @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } BasicAuthorization other = (BasicAuthorization) obj; if (password == null) { - if (other.password != null) + if (other.password != null) { return false; - } - else if (!password.equals(other.password)) + } + } else if (!password.equals(other.password)) { return false; + } if (userName == null) { - if (other.userName != null) + if (other.userName != null) { return false; - } - else if (!userName.equals(other.userName)) + } + } else if (!userName.equals(other.userName)) { return false; + } return true; } @@ -113,7 +115,7 @@ public class BasicAuthorization { } private void validate(String str) { - if(StringUtils.isEmpty(str)) { + if (StringUtils.isEmpty(str)) { throw new IllegalArgumentException("BasicAuthorization username and/or password cannot be empty"); } } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ClientCertificate.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ClientCertificate.java index c318a3b514..510c0a277c 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ClientCertificate.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ClientCertificate.java @@ -7,9 +7,9 @@ * 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. @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.config; import fj.data.Either; @@ -27,6 +26,7 @@ import org.onap.sdc.security.SecurityUtil; @EqualsAndHashCode public class ClientCertificate { + private String keyStore; private String keyStorePassword; @@ -37,28 +37,17 @@ public class ClientCertificate { setKeyStore(clientCertificate.getKeyStore()); setKeyStorePassword(clientCertificate.getKeyStorePassword(), false); } - - public void setKeyStore(String keyStore) { - validate(keyStore); - this.keyStore = keyStore; - } - - public void setKeyStorePassword(String keyStorePassword) { - setKeyStorePassword(keyStorePassword, true); - } private void setKeyStorePassword(String keyStorePassword, boolean isEncoded) { validate(keyStorePassword); - if(isEncoded) { + if (isEncoded) { Either<String, String> passkey = SecurityUtil.INSTANCE.decrypt(keyStorePassword); if (passkey.isLeft()) { this.keyStorePassword = passkey.left().value(); - } - else { + } else { throw new IllegalArgumentException(passkey.right().value()); } - } - else { + } else { this.keyStorePassword = keyStorePassword; } } @@ -67,10 +56,19 @@ public class ClientCertificate { return keyStore; } + public void setKeyStore(String keyStore) { + validate(keyStore); + this.keyStore = keyStore; + } + public String getKeyStorePassword() { return keyStorePassword; } + public void setKeyStorePassword(String keyStorePassword) { + setKeyStorePassword(keyStorePassword, true); + } + @Override public String toString() { StringBuilder builder = new StringBuilder(); @@ -79,9 +77,9 @@ public class ClientCertificate { builder.append("]"); return builder.toString(); } - + private void validate(String str) { - if(StringUtils.isEmpty(str)) { + if (StringUtils.isEmpty(str)) { throw new IllegalArgumentException("ClientCertificate keystore and/or kestorePassword cannot be empty"); } } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ExternalServiceConfig.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ExternalServiceConfig.java index dcc9b73bcd..268dfce045 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ExternalServiceConfig.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/ExternalServiceConfig.java @@ -7,9 +7,9 @@ * 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. @@ -17,18 +17,17 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.config; public class ExternalServiceConfig { - + private HttpRequestConfig httpRequestConfig; private HttpClientConfig httpClientConfig; public HttpRequestConfig getHttpRequestConfig() { return httpRequestConfig; } - + public void setHttpRequestConfig(HttpRequestConfig httpRequestConfig) { this.httpRequestConfig = httpRequestConfig; } @@ -51,5 +50,4 @@ public class ExternalServiceConfig { builder.append("]"); return builder.toString(); } - } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/HttpClientConfig.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/HttpClientConfig.java index a4f12e3eec..cfff386a2b 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/HttpClientConfig.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/HttpClientConfig.java @@ -7,9 +7,9 @@ * 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. @@ -17,14 +17,11 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.config; import com.fasterxml.jackson.annotation.JsonIgnore; -import org.nustaq.serialization.annotations.Serialize; -import org.openecomp.sdc.common.http.client.api.ComparableHttpRequestRetryHandler; - import java.util.Map; +import org.openecomp.sdc.common.http.client.api.ComparableHttpRequestRetryHandler; public class HttpClientConfig { @@ -37,9 +34,9 @@ public class HttpClientConfig { @JsonIgnore private boolean enableMetricLogging = false; - public HttpClientConfig() { + public HttpClientConfig() { } - + public HttpClientConfig(Timeouts timeouts) { setTimeouts(timeouts); } @@ -85,11 +82,11 @@ public class HttpClientConfig { public void setClientCertificate(ClientCertificate clientCertificate) { this.clientCertificate = clientCertificate; } - + public Map<String, String> getHeaders() { return headers; } - + public void setHeaders(Map<String, String> headers) { this.headers = headers; } @@ -97,7 +94,7 @@ public class HttpClientConfig { public int getNumOfRetries() { return numOfRetries; } - + public void setNumOfRetries(int numOfRetries) { this.numOfRetries = numOfRetries; } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/HttpRequestConfig.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/HttpRequestConfig.java index 040d068dfe..3b2cea87bb 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/HttpRequestConfig.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/HttpRequestConfig.java @@ -7,9 +7,9 @@ * 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. @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.config; import java.util.HashMap; @@ -27,22 +26,22 @@ public class HttpRequestConfig { private String serverRootUrl; private Map<String, String> resourceNamespaces; - + public String getServerRootUrl() { return serverRootUrl; } - + public void setServerRootUrl(String serverRootUrl) { this.serverRootUrl = serverRootUrl; } - + public Map<String, String> getResourceNamespaces() { - if(resourceNamespaces == null) { + if (resourceNamespaces == null) { resourceNamespaces = new HashMap<>(); } return resourceNamespaces; } - + public void setResourceNamespaces(Map<String, String> resourceNamespaces) { this.resourceNamespaces = resourceNamespaces; } diff --git a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/Timeouts.java b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/Timeouts.java index 39ef8555ad..1fa5f9864a 100644 --- a/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/Timeouts.java +++ b/common-app-api/src/main/java/org/openecomp/sdc/common/http/config/Timeouts.java @@ -7,9 +7,9 @@ * 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. @@ -17,21 +17,21 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.common.http.config; public class Timeouts { + public static final Timeouts DEFAULT; private static final int DEFAULT_TIMEOUT_MS = 15000; - private int connectTimeoutMs = DEFAULT_TIMEOUT_MS; - private int readTimeoutMs = DEFAULT_TIMEOUT_MS; - private int connectPoolTimeoutMs = DEFAULT_TIMEOUT_MS; - public static final Timeouts DEFAULT; static { - DEFAULT = new Timeouts(); + DEFAULT = new Timeouts(); } + private int connectTimeoutMs = DEFAULT_TIMEOUT_MS; + private int readTimeoutMs = DEFAULT_TIMEOUT_MS; + private int connectPoolTimeoutMs = DEFAULT_TIMEOUT_MS; + private Timeouts() { } @@ -49,30 +49,30 @@ public class Timeouts { public int getConnectTimeoutMs() { return connectTimeoutMs; } - + public void setConnectTimeoutMs(int connectTimeoutMs) { validate(connectTimeoutMs); this.connectTimeoutMs = connectTimeoutMs; } - + public int getReadTimeoutMs() { return readTimeoutMs; } - + public void setReadTimeoutMs(int readTimeoutMs) { validate(readTimeoutMs); this.readTimeoutMs = readTimeoutMs; } - + public int getConnectPoolTimeoutMs() { return connectPoolTimeoutMs; } - + public void setConnectPoolTimeoutMs(int connectPoolTimeoutMs) { validate(connectPoolTimeoutMs); this.connectPoolTimeoutMs = connectPoolTimeoutMs; } - + @Override public int hashCode() { final int prime = 31; @@ -82,25 +82,31 @@ public class Timeouts { result = prime * result + readTimeoutMs; return result; } - + @Override public boolean equals(Object obj) { - if (this == obj) + if (this == obj) { return true; - if (obj == null) + } + if (obj == null) { return false; - if (getClass() != obj.getClass()) + } + if (getClass() != obj.getClass()) { return false; + } Timeouts other = (Timeouts) obj; - if (connectPoolTimeoutMs != other.connectPoolTimeoutMs) + if (connectPoolTimeoutMs != other.connectPoolTimeoutMs) { return false; - if (connectTimeoutMs != other.connectTimeoutMs) + } + if (connectTimeoutMs != other.connectTimeoutMs) { return false; - if (readTimeoutMs != other.readTimeoutMs) + } + if (readTimeoutMs != other.readTimeoutMs) { return false; + } return true; } - + @Override public String toString() { StringBuilder builder = new StringBuilder(); @@ -113,9 +119,9 @@ public class Timeouts { builder.append("]"); return builder.toString(); } - + private void validate(int timeout) { - if(timeout <= 0) { + if (timeout <= 0) { throw new IllegalArgumentException("Timeout values cannot be less than zero"); } } |