From 9921b1751cde31721958d004935f2b7fc5e95513 Mon Sep 17 00:00:00 2001 From: jitendra007 Date: Tue, 21 Jul 2020 14:35:31 +0530 Subject: Code improvement for pending sonar issues Issue-ID: CLI-270 Signed-off-by: jitendra007 Change-Id: Ie27d3ff077fa1ef4d13788849aa82d695f183eeb --- .../cli/fw/http/auth/OnapCommandHttpAuthClient.java | 11 ++++++++++- .../onap/cli/fw/http/connect/OnapHttpConnection.java | 20 +++++++++----------- .../fw/http/schema/OnapCommandSchemaHttpLoader.java | 2 +- .../org/onap/cli/fw/http/OnapHttpConnectionTest.java | 2 +- .../onap/cli/fw/http/error/OnapCommandErrorTest.java | 7 +++++++ 5 files changed, 28 insertions(+), 14 deletions(-) (limited to 'profiles/http') diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java b/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java index e5f17bec..075c1304 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/auth/OnapCommandHttpAuthClient.java @@ -32,6 +32,10 @@ import org.onap.cli.fw.http.schema.OnapCommandSchemaHttpLoader; import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.fw.registrar.OnapCommandRegistrar; import org.onap.cli.fw.utils.OnapCommandUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.IOException; +import org.onap.cli.fw.http.error.OnapCommandLoginFailed; /** * Oclip Auth client helps to do login and logout. @@ -39,6 +43,7 @@ import org.onap.cli.fw.utils.OnapCommandUtils; */ public class OnapCommandHttpAuthClient { + private static Logger logger = LoggerFactory.getLogger(OnapCommandHttpAuthClient.class); //NOSONAR private OnapHttpCommand cmd = null; private OnapHttpConnection http = null; @@ -98,7 +103,11 @@ public class OnapCommandHttpAuthClient { logout.execute(); - this.http.close(); + try { + this.http.close(); + } catch (IOException e) { + throw new OnapCommandLoginFailed("Exception when closing httpclient"); + } } /** diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java b/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java index 732e6c0a..95ad7266 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/connect/OnapHttpConnection.java @@ -38,7 +38,6 @@ import org.apache.http.HttpResponse; import org.apache.http.annotation.Contract; import org.apache.http.annotation.ThreadingBehavior; import org.apache.http.client.CookieStore; -import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; @@ -47,14 +46,11 @@ 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.ClientContext; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.HttpClientConnectionManager; import org.apache.http.conn.socket.ConnectionSocketFactory; -import org.apache.http.conn.ssl.AllowAllHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.conn.ssl.X509HostnameVerifier; import org.apache.http.cookie.Cookie; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; @@ -71,7 +67,10 @@ import org.onap.cli.fw.http.conf.OnapCommandHttpConstants; import org.onap.cli.fw.http.error.OnapCommandHttpFailure; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.impl.client.CloseableHttpClient; +import javax.net.ssl.HostnameVerifier; /** * Helps to make http connection.
*/ @@ -79,7 +78,7 @@ public class OnapHttpConnection { private static Logger log = LoggerFactory.getLogger(OnapHttpConnection.class); - private HttpClient httpClient = null; + private CloseableHttpClient httpClient = null; Map mapCommonHeaders = new HashMap<> (); @@ -108,7 +107,7 @@ public class OnapHttpConnection { SSLContext sslContext = SSLContext.getInstance(OnapCommandHttpConstants.SSLCONTEST_TLS); sslContext.init(null, new TrustManager[] { new TrustAllX509TrustManager() }, new java.security.SecureRandom()); - X509HostnameVerifier hostnameVerifier = new AllowAllHostnameVerifier(); + HostnameVerifier hostnameVerifier = new NoopHostnameVerifier(); Registry socketFactoryRegistry = RegistryBuilder .create() .register("https", new SSLConnectionSocketFactory(sslContext, hostnameVerifier)).build(); @@ -320,7 +319,7 @@ public class OnapHttpConnection { CookieStore cookieStore = new BasicCookieStore(); updateInputFromCookies(input, cookieStore); HttpContext localContext = new BasicHttpContext(); - localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); + localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); this.initHttpClient(input.getUri().startsWith("https")); @@ -337,11 +336,10 @@ public class OnapHttpConnection { return result; } - @SuppressWarnings("deprecation") - public void close() { + public void close() throws IOException { this.mapCommonHeaders.clear(); if (this.httpClient != null) { - this.httpClient.getConnectionManager().shutdown(); + this.httpClient.close(); } } diff --git a/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java b/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java index e7a643ad..35501208 100644 --- a/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java +++ b/profiles/http/src/main/java/org/onap/cli/fw/http/schema/OnapCommandSchemaHttpLoader.java @@ -246,7 +246,7 @@ public class OnapCommandSchemaHttpLoader { validateHttpSccessCodes(errorList, (List) valMap.get(key1)); } List list = (ArrayList) valMap.get(key1); - cmd.setSuccessStatusCodes(list.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList())); + cmd.setSuccessStatusCodes(list.stream().map(Integer::parseInt).collect(Collectors.toList())); break; case OnapCommandHttpConstants.RESULT_MAP: diff --git a/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java b/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java index 2860388b..68995dba 100644 --- a/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java +++ b/profiles/http/src/test/java/org/onap/cli/fw/http/OnapHttpConnectionTest.java @@ -216,7 +216,7 @@ public class OnapHttpConnectionTest { } @Test() - public void httpUnSecuredCloseExceptionTest() throws OnapCommandHttpFailure { + public void httpUnSecuredCloseExceptionTest() throws IOException { inp.setMethod("other"); con = new OnapHttpConnection(); con.close(); diff --git a/profiles/http/src/test/java/org/onap/cli/fw/http/error/OnapCommandErrorTest.java b/profiles/http/src/test/java/org/onap/cli/fw/http/error/OnapCommandErrorTest.java index cd2131e9..e007af15 100644 --- a/profiles/http/src/test/java/org/onap/cli/fw/http/error/OnapCommandErrorTest.java +++ b/profiles/http/src/test/java/org/onap/cli/fw/http/error/OnapCommandErrorTest.java @@ -51,6 +51,13 @@ public class OnapCommandErrorTest { assertEquals("203::0x3001::Failed", failed.getMessage()); } + @Test + public void oclipCommandLoginFailedTest3() { + OnapCommandLoginFailed failed = new OnapCommandLoginFailed("Failed"); + + assertEquals("0x4001::Login failed, Failed", failed.getMessage()); + } + @Test public void oclipCommandLoginFailedTest1() { OnapCommandLoginFailed failed = new OnapCommandLoginFailed(new Exception("Failed")); -- cgit 1.2.3-korg