Procházet zdrojové kódy

AMBARI-17992: Ambari should remove all references to commons-httpclient

Nahappan Somasundaram před 9 roky
rodič
revize
f5337540d7

+ 6 - 4
ambari-funtest/pom.xml

@@ -330,10 +330,6 @@
       <groupId>commons-lang</groupId>
       <artifactId>commons-lang</artifactId>
     </dependency>
-    <dependency>
-      <groupId>commons-httpclient</groupId>
-      <artifactId>commons-httpclient</artifactId>
-    </dependency>
     <dependency>
       <groupId>commons-net</groupId>
       <artifactId>commons-net</artifactId>
@@ -553,5 +549,11 @@
         </exclusion>
       </exclusions>
     </dependency>
+    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
+    <dependency>
+      <groupId>org.apache.httpcomponents</groupId>
+      <artifactId>httpclient</artifactId>
+      <version>4.5.2</version>
+    </dependency>
   </dependencies>
 </project>

+ 72 - 103
ambari-funtest/src/test/java/org/apache/ambari/funtest/server/AmbariHttpWebRequest.java

@@ -23,24 +23,32 @@ import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import com.google.gson.stream.JsonReader;
 
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpException;
-import org.apache.commons.httpclient.HttpMethodBase;
-import org.apache.commons.httpclient.HttpStatus;
-import org.apache.commons.httpclient.methods.DeleteMethod;
-import org.apache.commons.httpclient.methods.GetMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.RequestLine;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.apache.http.entity.StringEntity;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-
 import org.apache.commons.codec.binary.Base64;
 
 import java.io.IOException;
 import java.io.StringReader;
 import java.util.Map;
+import java.lang.StringBuilder;
 
+import org.apache.commons.lang.StringUtils;
 
 /**
  * REST API Client that performs various operations on the Ambari Server
@@ -282,112 +290,73 @@ public class AmbariHttpWebRequest extends WebRequest {
      * @throws IOException
      */
     private WebResponse executeRequest() throws IOException {
-        HttpMethodBase methodBase = null;
-        String httpMethod;
-
-        httpMethod = getHttpMethod();
-
-        if (httpMethod.equals("GET")) {
-            methodBase = getGetMethod();
-        } else if (httpMethod.equals("POST")) {
-            methodBase = getPostMethod();
-        } else if (httpMethod.equals("PUT")) {
-            methodBase = getPutMethod();
-        } else if (httpMethod.equals("DELETE")) {
-            methodBase = getDeleteMethod();
-        } else {
-            new RuntimeException(String.format("Unsupported HTTP method: %s", httpMethod));
-        }
-
-        WebResponse response = new WebResponse();
-        HttpClient httpClient = new HttpClient();
-        Map<String, String> headers = getHeaders();
-
-        for (Map.Entry<String, String> header : headers.entrySet()) {
-            methodBase.addRequestHeader(header.getKey(), header.getValue());
-        }
-
-        methodBase.setQueryString(getQueryString());
+        final WebResponse response = new WebResponse();
+        CloseableHttpClient httpClient = HttpClients.createDefault();
 
         try {
-            int statusCode = httpClient.executeMethod(methodBase);
-            response.setStatusCode(statusCode);
-            response.setContent(methodBase.getResponseBodyAsString());
-        } finally {
-            methodBase.releaseConnection();
-        }
-
-        return response;
-    }
-
-    /**
-     * Constructs a GetMethod instance.
-     *
-     * @return - GetMethod.
-     */
-    private GetMethod getGetMethod() {
-        return new GetMethod(getUrl());
-    }
-
-    /**
-     * Constructs a PutMethod instance and sets the request data on it.
-     *
-     * @return - PutMethod.
-     */
-    @SuppressWarnings("deprecation")
-    private PutMethod getPutMethod() {
-        PutMethod putMethod = new PutMethod(getUrl());
+            HttpRequestBase requestBase = null;
+            String httpMethod = getHttpMethod();
+
+            if (httpMethod.equals("GET")) {
+                requestBase = new HttpGet(getRequestUrl());
+            } else if (httpMethod.equals("POST")) {
+                HttpPost httpPost = new HttpPost(getRequestUrl());
+                if (StringUtils.isNotEmpty(getContent())) {
+                    httpPost.setHeader("Content-Type", getContentType());
+                    httpPost.setEntity(new StringEntity(getContent(), getContentEncoding()));
+                }
+                requestBase = httpPost;
+            } else if (httpMethod.equals("PUT")) {
+                HttpPut httpPut = new HttpPut(getRequestUrl());
+                if (StringUtils.isNotEmpty(getContent())) {
+                    httpPut.setHeader("Content-Type", getContentType());
+                    httpPut.setEntity(new StringEntity(getContent(), getContentEncoding()));
+                }
+                requestBase = httpPut;
+            } else if (httpMethod.equals("DELETE")) {
+                requestBase = new HttpDelete(getRequestUrl());
+            } else {
+                new RuntimeException(String.format("Unsupported HTTP method: %s", httpMethod));
+            }
 
-        putMethod.setRequestBody(getContent());
+            Map<String, String> headers = getHeaders();
 
-        return putMethod;
-    }
+            for (Map.Entry<String, String> header : headers.entrySet()) {
+                requestBase.addHeader(header.getKey(), header.getValue());
+            }
 
-    /**
-     * Constructs a PostMethod and sets the request data on it.
-     *
-     * @return - PostMethod.
-     */
-    @SuppressWarnings("deprecation")
-    private PostMethod getPostMethod() {
-        PostMethod postMethod = new PostMethod(getUrl());
+            RequestLine requestLine = requestBase.getRequestLine();
+            LOG.debug(requestLine);
 
-        /*
-        RequestEntity requestEntity = new StringRequestEntity(
-                request.getContent(),
-                request.getContentType(),
-                request.getContentEncoding());
+            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
 
-        postMethod.setRequestEntity(requestEntity);
-        */
+                @Override
+                public String handleResponse(final HttpResponse httpResponse)
+                        throws ClientProtocolException, IOException {
+                    int statusCode = httpResponse.getStatusLine().getStatusCode();
+                    response.setStatusCode(statusCode);
+                    HttpEntity entity = httpResponse.getEntity();
+                    return entity != null ? EntityUtils.toString(entity) : null;
+                }
+            };
 
-        postMethod.setRequestBody(getContent());
+            String responseBody = httpClient.execute(requestBase, responseHandler);
+            response.setContent(responseBody);
+        } finally {
+            httpClient.close();
+        }
 
-        return postMethod;
+        return response;
     }
 
-    /**
-     * Constructs a DeleteMethod.
-     *
-     * @return - DeleteMethod.
-     */
-    private DeleteMethod getDeleteMethod() {
-        return new DeleteMethod(getUrl());
-    }
+    private String getRequestUrl() {
+        StringBuilder requestUrl = new StringBuilder(getUrl());
 
-    @SuppressWarnings("deprecation")
-    private RuntimeException createRuntimeException(HttpException httpException) {
-        String message = httpException.getMessage();
-        try {
-            JsonElement jsonElement = new JsonParser().parse(new JsonReader(new StringReader(httpException.getMessage())));
-            if (jsonElement != null && jsonElement.getAsJsonObject().has("message")) {
-                message = jsonElement.getAsJsonObject().get("message").getAsString();
-            }
-        } catch (Throwable t) {
+        if (StringUtils.isNotEmpty(getQueryString())) {
+            requestUrl.append("?");
+            requestUrl.append(getQueryString());
         }
-        if (httpException.getReasonCode() != HttpStatus.SC_OK) {
-            message = httpException.getReasonCode() + " " + httpException.getReason() + ": " + message;
-        }
-        return new RuntimeException(message, httpException);
+
+        return requestUrl.toString();
     }
 }

+ 1 - 1
ambari-funtest/src/test/java/org/apache/ambari/funtest/server/tests/DeleteServiceTest.java

@@ -41,7 +41,7 @@ import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
 import org.apache.ambari.server.orm.entities.ServiceDesiredStateEntityPK;
 import org.apache.ambari.server.state.State;
 
-import org.apache.commons.httpclient.HttpStatus;
+import org.apache.http.HttpStatus;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;

+ 16 - 11
ambari-funtest/src/test/java/org/apache/ambari/funtest/server/tests/GetStacksTest.java

@@ -23,18 +23,21 @@ import org.junit.Test;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertEquals;
 
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.methods.GetMethod;
-
-import java.io.StringReader;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonParser;
 import com.google.gson.stream.JsonReader;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonArray;
 
+import java.io.StringReader;
 import java.io.IOException;
 
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
 import org.apache.http.HttpStatus;
 
 /**
@@ -69,17 +72,19 @@ public class GetStacksTest extends ServerTestBase {
      */
     String stacksPath = "/api/v1/stacks";
     String stacksUrl = String.format(SERVER_URL_FORMAT, serverPort) + stacksPath;
-    HttpClient httpClient = new HttpClient();
-    GetMethod getMethod = new GetMethod(stacksUrl);
+    CloseableHttpClient httpClient = HttpClients.createDefault();
+    HttpGet httpGet = new HttpGet(stacksUrl);
+    httpGet.addHeader("Authorization", getBasicAdminAuthentication());
+    httpGet.addHeader("X-Requested-By", "ambari");
 
     try {
-      getMethod.addRequestHeader("Authorization", getBasicAdminAuthentication());
-      getMethod.addRequestHeader("X-Requested-By", "ambari");
-      int statusCode = httpClient.executeMethod(getMethod);
+      HttpResponse httpResponse = httpClient.execute(httpGet);
+      int statusCode = httpResponse.getStatusLine().getStatusCode();
 
       assertEquals(HttpStatus.SC_OK, statusCode); // HTTP status code 200
 
-      String responseBody = getMethod.getResponseBodyAsString();
+      HttpEntity entity = httpResponse.getEntity();
+      String responseBody = entity != null ? EntityUtils.toString(entity) : null;
 
       assertTrue(responseBody != null); // Make sure response body is valid
 
@@ -96,7 +101,7 @@ public class GetStacksTest extends ServerTestBase {
       assertTrue (stacksArray.size() > 0); // Should have at least one stack
 
     } finally {
-      getMethod.releaseConnection();
+      httpClient.close();
     }
   }
 }

+ 1 - 1
ambari-funtest/src/test/java/org/apache/ambari/funtest/server/tests/RoleBasedAccessControlBasicTest.java

@@ -32,7 +32,7 @@ import org.apache.ambari.funtest.server.api.cluster.GetAllClustersWebRequest;
 import org.apache.ambari.funtest.server.api.user.DeleteUserWebRequest;
 import org.apache.ambari.funtest.server.utils.ClusterUtils;
 import org.apache.ambari.funtest.server.utils.RestApiUtils;
-import org.apache.commons.httpclient.HttpStatus;
+import org.apache.http.HttpStatus;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.junit.Test;

+ 19 - 10
ambari-funtest/src/test/java/org/apache/ambari/funtest/server/tests/ServerTestBase.java

@@ -26,8 +26,7 @@ import org.apache.ambari.server.configuration.Configuration;
 import org.apache.ambari.server.controller.ControllerModule;
 import org.apache.ambari.server.orm.DBAccessor;
 import org.apache.commons.codec.binary.Base64;
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.methods.GetMethod;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.junit.After;
@@ -41,6 +40,14 @@ import java.sql.SQLException;
 import java.sql.SQLNonTransientConnectionException;
 import java.util.Properties;
 
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.apache.http.HttpStatus;
+
 /**
  * Base test infrastructure.
  */
@@ -215,24 +222,26 @@ public class ServerTestBase {
      * @return - True if the local server is responsive to queries.
      *           False, otherwise.
      */
-    private static boolean isServerUp() {
+    private static boolean isServerUp() throws IOException {
         String apiPath = "/api/v1/stacks";
 
         String apiUrl = String.format(SERVER_URL_FORMAT, serverPort) + apiPath;
-        HttpClient httpClient = new HttpClient();
-        GetMethod getMethod = new GetMethod(apiUrl);
+        CloseableHttpClient httpClient = HttpClients.createDefault();;
 
         try {
-            getMethod.addRequestHeader("Authorization", getBasicAdminAuthentication());
-            getMethod.addRequestHeader("X-Requested-By", "ambari");
-            int statusCode = httpClient.executeMethod(getMethod);
-            String response = getMethod.getResponseBodyAsString();
+            HttpGet httpGet = new HttpGet(apiUrl);
+            httpGet.addHeader("Authorization", getBasicAdminAuthentication());
+            httpGet.addHeader("X-Requested-By", "ambari");
+            HttpResponse httpResponse = httpClient.execute(httpGet);
+            int statusCode = httpResponse.getStatusLine().getStatusCode();
+            HttpEntity entity = httpResponse.getEntity();
+            String responseBody = entity != null ? EntityUtils.toString(entity) : null;
 
             return true;
         } catch (IOException ex) {
 
         } finally {
-            getMethod.releaseConnection();
+            httpClient.close();
         }
 
         return false;

+ 1 - 1
ambari-funtest/src/test/java/org/apache/ambari/funtest/server/utils/RestApiUtils.java

@@ -23,7 +23,7 @@ import com.google.gson.JsonParser;
 import com.google.gson.stream.JsonReader;
 import org.apache.ambari.funtest.server.WebRequest;
 import org.apache.ambari.funtest.server.WebResponse;
-import org.apache.commons.httpclient.HttpStatus;
+import org.apache.http.HttpStatus;
 
 import java.io.StringReader;
 

+ 0 - 5
ambari-logsearch/ambari-logsearch-portal/pom.xml

@@ -535,11 +535,6 @@
       <version>${solr.version}</version>
     </dependency>
     <!-- Hadoop -->
-    <dependency>
-      <groupId>commons-httpclient</groupId>
-      <artifactId>commons-httpclient</artifactId>
-      <version>3.0.1</version>
-    </dependency>
     <dependency>
       <groupId>org.apache.hadoop</groupId>
       <artifactId>hadoop-common</artifactId>

+ 0 - 5
ambari-project/pom.xml

@@ -313,11 +313,6 @@
         <artifactId>commons-lang</artifactId>
         <version>2.5</version>
       </dependency>
-      <dependency>
-        <groupId>commons-httpclient</groupId>
-        <artifactId>commons-httpclient</artifactId>
-        <version>3.1</version>
-      </dependency>
       <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>javax.servlet-api</artifactId>

+ 0 - 4
ambari-server/pom.xml

@@ -1128,10 +1128,6 @@
       <groupId>commons-lang</groupId>
       <artifactId>commons-lang</artifactId>
     </dependency>
-    <dependency>
-      <groupId>commons-httpclient</groupId>
-      <artifactId>commons-httpclient</artifactId>
-    </dependency>
     <dependency>
       <groupId>commons-net</groupId>
       <artifactId>commons-net</artifactId>

+ 1 - 1
ambari-server/src/main/java/org/apache/ambari/server/controller/metrics/timeline/MetricsRequestHelper.java

@@ -18,7 +18,7 @@
 package org.apache.ambari.server.controller.metrics.timeline;
 
 import org.apache.ambari.server.controller.internal.URLStreamProvider;
-import org.apache.commons.httpclient.HttpStatus;
+import org.apache.http.HttpStatus;
 import org.apache.hadoop.metrics2.sink.timeline.Precision;
 import org.apache.http.NameValuePair;
 import org.apache.hadoop.metrics2.sink.timeline.TimelineMetric;

+ 1 - 1
ambari-server/src/test/java/org/apache/ambari/server/api/services/LoggingServiceTest.java

@@ -20,7 +20,7 @@ package org.apache.ambari.server.api.services;
 import com.sun.jersey.core.util.MultivaluedMapImpl;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.logging.LoggingRequestHelperFactory;
-import org.apache.commons.httpclient.util.HttpURLConnection;
+import java.net.HttpURLConnection;
 import org.easymock.EasyMockSupport;
 import org.junit.Test;
 

+ 0 - 1
ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/AMSPropertyProviderTest.java

@@ -44,7 +44,6 @@ import org.apache.ambari.server.state.Cluster;
 import org.apache.ambari.server.state.Clusters;
 import org.apache.ambari.server.state.ComponentInfo;
 import org.apache.ambari.server.state.StackId;
-import org.apache.commons.httpclient.HttpConnection;
 import org.apache.http.client.utils.URIBuilder;
 import org.easymock.EasyMock;
 import org.junit.After;

+ 1 - 1
ambari-server/src/test/java/org/apache/ambari/server/controller/metrics/timeline/MetricsRequestHelperTest.java

@@ -18,7 +18,7 @@
 package org.apache.ambari.server.controller.metrics.timeline;
 
 import org.apache.ambari.server.controller.internal.URLStreamProvider;
-import org.apache.commons.httpclient.HttpStatus;
+import org.apache.http.HttpStatus;
 import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.metrics2.sink.timeline.TimelineMetric;
 import org.apache.hadoop.metrics2.sink.timeline.TimelineMetrics;