ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oleew...@apache.org
Subject [17/51] [abbrv] ambari git commit: AMBARI-18196. Logsearch: Upgrade from jersey 1.x to jersey 2.x (oleewere)
Date Tue, 13 Sep 2016 10:41:34 GMT
AMBARI-18196. Logsearch: Upgrade from jersey 1.x to jersey 2.x (oleewere)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/06da0715
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/06da0715
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/06da0715

Branch: refs/heads/logsearch-ga
Commit: 06da0715e91a0fc5f20352e12f5acdaf65e3f6be
Parents: 782c6b4
Author: oleewere <oleewere@gmail.com>
Authored: Tue Aug 23 18:55:59 2016 +0200
Committer: oleewere <oleewere@gmail.com>
Committed: Tue Sep 13 12:19:23 2016 +0200

----------------------------------------------------------------------
 .../ambari-logsearch-portal/pom.xml             |  54 +++++-
 .../logsearch/util/ExternalServerClient.java    | 191 ++++---------------
 .../src/main/webapp/WEB-INF/web.xml             |  12 +-
 3 files changed, 85 insertions(+), 172 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/06da0715/ambari-logsearch/ambari-logsearch-portal/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-portal/pom.xml b/ambari-logsearch/ambari-logsearch-portal/pom.xml
index 00167f5..2c7ac87 100755
--- a/ambari-logsearch/ambari-logsearch-portal/pom.xml
+++ b/ambari-logsearch/ambari-logsearch-portal/pom.xml
@@ -33,7 +33,7 @@
     <spring.version>4.2.5.RELEASE</spring.version>
     <spring.security.version>4.0.4.RELEASE</spring.security.version>
     <spring.ldap.version>2.0.4.RELEASE</spring.ldap.version>
-    <jersey.version>1.19</jersey.version>
+    <jersey.version>2.23.2</jersey.version>
     <jetty-version>9.2.11.v20150529</jetty-version>
     <swagger.version>1.5.8</swagger.version>
   </properties>
@@ -521,9 +521,9 @@
       <version>${spring.security.version}</version>
     </dependency>
     <dependency>
-      <groupId>com.sun.jersey.contribs</groupId>
-      <artifactId>jersey-spring</artifactId>
-      <version>${jersey.version}</version>
+      <groupId>org.glassfish.jersey.ext</groupId>
+      <artifactId>jersey-spring3</artifactId>
+      <version>2.23.2</version>
       <exclusions>
         <exclusion>
           <groupId>org.springframework</groupId>
@@ -532,10 +532,30 @@
       </exclusions>
     </dependency>
     <dependency>
-    <groupId>com.sun.jersey.contribs</groupId>
-    <artifactId>jersey-apache-client</artifactId>
-    <version>${jersey.version}</version>
-  </dependency>
+      <groupId>org.glassfish.jersey.connectors</groupId>
+      <artifactId>jersey-apache-connector</artifactId>
+      <version>${jersey.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.glassfish.jersey.core</groupId>
+      <artifactId>jersey-client</artifactId>
+      <version>${jersey.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.glassfish.jersey.media</groupId>
+      <artifactId>jersey-media-json-jettison</artifactId>
+      <version>${jersey.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.glassfish.jersey.media</groupId>
+      <artifactId>jersey-media-moxy</artifactId>
+      <version>${jersey.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.glassfish.jersey.core</groupId>
+      <artifactId>jersey-common</artifactId>
+      <version>${jersey.version}</version>
+    </dependency>
   <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>javax.servlet-api</artifactId>
@@ -590,6 +610,22 @@
           <groupId>org.mortbay.jetty</groupId>
           <artifactId>jetty-util</artifactId>
         </exclusion>
+        <exclusion>
+          <groupId>com.sun.jersey</groupId>
+          <artifactId>jetty-util</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>com.sun.jersey</groupId>
+          <artifactId>jersey-core</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>com.sun.jersey</groupId>
+          <artifactId>jersey-json</artifactId>
+        </exclusion>
+        <exclusion>
+          <groupId>com.sun.jersey</groupId>
+          <artifactId>jersey-server</artifactId>
+        </exclusion>
       </exclusions>
     </dependency>
     <dependency>
@@ -696,7 +732,7 @@
     </dependency>
     <dependency>
       <groupId>io.swagger</groupId>
-      <artifactId>swagger-jaxrs</artifactId>
+      <artifactId>swagger-jersey2-jaxrs</artifactId>
       <version>${swagger.version}</version>
       <exclusions>
         <exclusion>

http://git-wip-us.apache.org/repos/asf/ambari/blob/06da0715/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/ExternalServerClient.java
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/ExternalServerClient.java
b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/ExternalServerClient.java
index 32e547e..882a8bd 100644
--- a/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/ExternalServerClient.java
+++ b/ambari-logsearch/ambari-logsearch-portal/src/main/java/org/apache/ambari/logsearch/util/ExternalServerClient.java
@@ -18,39 +18,36 @@
  */
 package org.apache.ambari.logsearch.util;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.UnknownHostException;
 import java.util.List;
-import java.util.Map.Entry;
+import java.util.Map;
 
 import javax.annotation.PostConstruct;
-import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.ambari.logsearch.web.security.LogsearchAbstractAuthenticationProvider;
-import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
+import org.glassfish.jersey.client.JerseyClient;
+import org.glassfish.jersey.client.JerseyClientBuilder;
+import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
+import org.glassfish.jersey.filter.LoggingFilter;
 import org.springframework.stereotype.Component;
 
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.api.client.ClientHandlerException;
-import com.sun.jersey.api.client.ClientResponse;
-import com.sun.jersey.api.client.UniformInterfaceException;
-import com.sun.jersey.api.client.WebResource;
-import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
-import com.sun.jersey.api.client.filter.LoggingFilter;
-import com.sun.jersey.client.apache.ApacheHttpClient;
-import com.sun.jersey.client.apache.config.DefaultApacheHttpClientConfig;
-import com.sun.jersey.core.util.MultivaluedMapImpl;
-
 /**
  * Layer to send REST request to External server using jersey client
  */
 @Component
 public class ExternalServerClient {
   private static Logger LOG = Logger.getLogger(ExternalServerClient.class);
-  private static final ThreadLocal<Client> localJerseyClient = new ThreadLocal<Client>();
-  private DefaultApacheHttpClientConfig defaultConfig = new DefaultApacheHttpClientConfig();
+  private static final ThreadLocal<JerseyClient> localJerseyClient = new ThreadLocal<JerseyClient>(){
+    @Override
+    protected JerseyClient initialValue() {
+      return JerseyClientBuilder.createClient();
+    }
+  };
   private String hostURL = "http://host:ip";// default
   private boolean enableLog = false;// default
 
@@ -61,151 +58,39 @@ public class ExternalServerClient {
             + "external_auth.host_url", hostURL);
   }
 
-  private Client getJerseyClient() {
-    Client jerseyClient = localJerseyClient.get();
-    if (jerseyClient == null) {
-      jerseyClient = ApacheHttpClient.create(defaultConfig);
-      localJerseyClient.set(jerseyClient);
-    }
-    return jerseyClient;
-  }
-
   /**
-   * Send GET Request to  External server
-   * @param url
-   * @param klass
-   * @param queryParam
-   * @param username
-   * @param password
-   * @return Response Object 
-   * @throws UnknownHostException
-   * @throws Exception
+   * Send GET request to an external server
    */
   @SuppressWarnings({ "unchecked", "rawtypes" })
-  public Object sendGETRequest(String url, Class klass,
-      MultivaluedMapImpl queryParam, String username, String password)
-      throws UnknownHostException, Exception {
-    // add host url
+  public Object sendGETRequest(String url, Class klass, MultivaluedMap<String, String>
queryParam,
+                               String username, String password)
+      throws Exception {
     url = hostURL + url;
-    String parameters = getQueryParameter(queryParam);
-    LOG.debug("URL: " + url + " query parameters are : " + parameters);
-    WebResource.Builder builder = buildWebResourceBuilder(url, queryParam,
-        username, password);
-    try {
-      return builder.get(klass);
-    } catch (WebApplicationException webApplicationException) {
-      String errMsg = webApplicationExceptionHandler(webApplicationException,
-          url);
-      throw new Exception(errMsg);
-    } catch (UniformInterfaceException uniformInterfaceException) {
-      String errMsg = uniformInterfaceExceptionHandler(
-          uniformInterfaceException, url);
-      throw new Exception(errMsg);
-    } catch (ClientHandlerException clientHandlerException) {
-      String errMsg = clientHandlerExceptionHandler(clientHandlerException, url);
-      throw new Exception(errMsg);
-    } catch (Exception e) {
-      Object response = builder.get(Object.class);
-      String errMsg = "URL: " + url + response.toString();
-      LOG.error(errMsg);
-      throw new Exception(errMsg);
-    } finally {
-      cleanup();
-    }
-  }
+    JerseyClient client = localJerseyClient.get();
+    HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basicBuilder().build();
 
-  private WebResource.Builder buildWebResourceBuilder(String url,
-      MultivaluedMapImpl queryParam, String username, String password) {
-    WebResource webResource = getJerseyClient().resource(url);
-    // add filter
+    client.register(authFeature);
     if (enableLog) {
-      webResource.addFilter(new LoggingFilter());
+      client.register(LoggingFilter.class);
     }
-    getJerseyClient().addFilter(new HTTPBasicAuthFilter(username, password));
-    // add query param
-    if (queryParam != null) {
-      webResource = webResource.queryParams(queryParam);
-    }
-    WebResource.Builder builder = webResource.getRequestBuilder();
-    return builder;
-  }
 
-  private String webApplicationExceptionHandler(
-      WebApplicationException webApplicationException, String url) {
-    Object object = null;
-    try {
-      object = webApplicationException.getResponse().getEntity();
-    } catch (Exception e) {
-      LOG.error(e.getLocalizedMessage());
-    }
-    String errMsg = null;
-    if (object != null) {
-      errMsg = object.toString();
-    } else {
-      errMsg = webApplicationException.getMessage();
+    WebTarget target = client.target(url);
+    LOG.debug("URL: " + url);
+    for (Map.Entry<String, List<String>> entry : queryParam.entrySet()) {
+      target = target.queryParam(entry.getKey(), entry.getValue());
+      LOG.debug(
+        String.format("Query parameter: name - %s  ; value - %s ;" + entry.getKey(), StringUtils.join(entry.getValue(),',')));
     }
-    errMsg = "URL: " + url + errMsg;
-    LOG.error(errMsg);
-    return errMsg;
-  }
-
-  private String uniformInterfaceExceptionHandler(
-      UniformInterfaceException uniformInterfaceException, String url) {
-    Object object = null;
-    String errMsg = null;
-    ClientResponse clientResponse = uniformInterfaceException.getResponse();
+    target
+      .property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_USERNAME, username)
+      .property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_BASIC_PASSWORD, password);
+    Invocation.Builder invocationBuilder =  target.request(MediaType.APPLICATION_JSON_TYPE);
     try {
-      object = clientResponse.getEntity(Object.class);
-      if (object != null) {
-        errMsg = object.toString();
-      }
+      return invocationBuilder.get().readEntity(klass);
     } catch (Exception e) {
-      InputStream inputStream = clientResponse.getEntityInputStream();
-      try {
-        errMsg = IOUtils.toString(inputStream);
-      } catch (IOException e1) {
-        LOG.error(e.getLocalizedMessage());
-      }
-    }
-    if (errMsg == null) {
-      errMsg = uniformInterfaceException.getLocalizedMessage();
-    }
-    LOG.error("url :" + url + " Response : " + errMsg);
-    return errMsg;
-  }
-
-  private String clientHandlerExceptionHandler(
-      ClientHandlerException clientHandlerException, String url) {
-    String errMsg = clientHandlerException.getLocalizedMessage();
-    errMsg = "URL: " + url + errMsg;
-    LOG.error(errMsg);
-    return errMsg;
-  }
-
-  private String getQueryParameter(MultivaluedMapImpl queryParam) {
-    StringBuilder builder = new StringBuilder();
-    if (queryParam != null) {
-      builder.append(" Query param :");
-      for (Entry<String, List<String>> entry : queryParam.entrySet()) {
-        String name = entry.getKey();
-        builder.append(" name : " + name + " " + "values : [");
-        List<String> valuesList = entry.getValue();
-        if (valuesList != null) {
-          for (int index = 0; index < valuesList.size(); index++) {
-            String value = valuesList.get(index);
-            if (index > 0) {
-              builder.append(",");
-            }
-            builder.append(value);
-          }
-        }
-        builder.append("]");
-      }
+      throw new Exception(e.getCause());
+    } finally {
+      localJerseyClient.remove();
     }
-    return builder.toString();
-  }
-
-  private void cleanup() {
-    localJerseyClient.remove();
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/06da0715/ambari-logsearch/ambari-logsearch-portal/src/main/webapp/WEB-INF/web.xml
----------------------------------------------------------------------
diff --git a/ambari-logsearch/ambari-logsearch-portal/src/main/webapp/WEB-INF/web.xml b/ambari-logsearch/ambari-logsearch-portal/src/main/webapp/WEB-INF/web.xml
index aee16bf..dbe5210 100755
--- a/ambari-logsearch/ambari-logsearch-portal/src/main/webapp/WEB-INF/web.xml
+++ b/ambari-logsearch/ambari-logsearch-portal/src/main/webapp/WEB-INF/web.xml
@@ -48,18 +48,10 @@
 	<!-- Servlet mapping for REST -->
 	<servlet>
 		<servlet-name>REST service</servlet-name>
-		<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
+		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
 		<init-param>
 			<param-name>jersey.config.server.provider.packages</param-name>
-			<param-value>org.apache.ambari.logsearch.rest</param-value>
-		</init-param>
-		<init-param>
-			<param-name>com.sun.jersey.spi.container.ContainerRequestFilters</param-name>
-			<param-value>org.apache.ambari.logsearch.common.RESTAPIFilter</param-value>
-		</init-param>
-		<init-param>
-			<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
-			<param-value>org.apache.ambari.logsearch.common.RESTAPIFilter</param-value>
+			<param-value>org.apache.ambari.logsearch.rest,io.swagger.jaxrs.listing</param-value>
 		</init-param>
 		<load-on-startup>1</load-on-startup>
 	</servlet>


Mime
View raw message