diff options
author | jitendra007 <jitendra.sharma1@huawei.com> | 2020-07-21 14:35:31 +0530 |
---|---|---|
committer | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-08-04 09:44:38 +0530 |
commit | 9921b1751cde31721958d004935f2b7fc5e95513 (patch) | |
tree | e41a158075ba6f95af21fc9ef25425e0dca51851 /profiles/http | |
parent | fc9af3118881c83a0928e9a21135641808288f84 (diff) |
Code improvement for pending sonar issues
Issue-ID: CLI-270
Signed-off-by: jitendra007 <jitendra.sharma1@huawei.com>
Change-Id: Ie27d3ff077fa1ef4d13788849aa82d695f183eeb
Diffstat (limited to 'profiles/http')
5 files changed, 28 insertions, 14 deletions
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.<br> */ @@ -79,7 +78,7 @@ public class OnapHttpConnection { private static Logger log = LoggerFactory.getLogger(OnapHttpConnection.class); - private HttpClient httpClient = null; + private CloseableHttpClient httpClient = null; Map<String, String> 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<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder .<ConnectionSocketFactory>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<Object>) valMap.get(key1)); } List<String> 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 @@ -52,6 +52,13 @@ public class OnapCommandErrorTest { } @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")); |