fixing warnings from checkstyle in common-app-api
[sdc.git] / common-app-api / src / main / java / org / openecomp / sdc / common / http / client / api / HttpClient.java
index d384893..9ed3efd 100644 (file)
@@ -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.
@@ -29,7 +29,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;
@@ -46,16 +52,18 @@ import java.net.URI;
 import java.util.Properties;
 
 public class HttpClient {
-    private static final Logger logger = Logger.getLogger(HttpClient.class.getName());
-    
+    private static final Logger LOGGER = Logger.getLogger(HttpClient.class.getName());
+    public static final int HTTPS_PORT = 443;
+    public static final int HTTP_PORT = 80;
+
     private final CloseableHttpClient client;
     private final HttpClientConfigImmutable configImmutable;
-    
+
     HttpClient(CloseableHttpClient client, HttpClientConfigImmutable configImmutable) {
         this.client = client;
-        this.configImmutable = configImmutable; 
+        this.configImmutable = configImmutable;
     }
-    
+
     <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);
@@ -83,18 +91,17 @@ public class HttpClient {
         HttpDelete httpDelete = new HttpDelete(url);
         return execute(httpDelete, headers, responseBuilder);
     }
-    
+
     void close() {
         try {
             client.close();
-        }
-        catch (IOException e) {
-            logger.debug("Close http client failed with exception ", 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) {
+        if (configImmutable.getHeaders() != null) {
             configImmutable.getHeaders().forEach(request::addHeader);
         }
 
@@ -103,31 +110,30 @@ public class HttpClient {
         }
 
         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());
+        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), 
+        credentialsProvider.setCredentials(new AuthScope(uri.getHost(), port),
                 new UsernamePasswordCredentials(configImmutable.getBasicAuthUserName(), configImmutable.getBasicAuthPassword()));
 
         HttpClientContext localContext = HttpClientContext.create();
@@ -141,18 +147,15 @@ public class HttpClient {
     }
 
     private int getPort(URI uri) {
-        int port = uri.getPort(); 
-        if(port < 0) {
-            if(Constants.HTTPS.equals(uri.getScheme())) {
-                port = 443;
-            }
-            else
-            if (Constants.HTTP.equals(uri.getScheme())) {
-                port = 80;
-            }
-            else {
+        int port = uri.getPort();
+        if (port < 0) {
+            if (Constants.HTTPS.equals(uri.getScheme())) {
+                port = HTTPS_PORT;
+            } else if (Constants.HTTP.equals(uri.getScheme())) {
+                port = HTTP_PORT;
+            } else {
                 port = AuthScope.ANY_PORT;
-                logger.debug("Protocol \"{}\" is not supported, set port to {}", uri.getScheme(), port);
+                LOGGER.debug("Protocol \"{}\" is not supported, set port to {}", uri.getScheme(), port);
             }
         }
         return port;