]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3933 ws-client: upgrade minimal version of httpclient 4.2
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 6 Nov 2012 13:42:56 +0000 (14:42 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 6 Nov 2012 13:43:20 +0000 (14:43 +0100)
sonar-ws-client/pom.xml
sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient3Connector.java
sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient4Connector.java

index 6cb26c6cddf157a7e3bae240eafb4ab721a6a847..49d446dd589d7cbd56dae17d9aabecc3c53ebe55 100644 (file)
     <dependency>
       <groupId>org.apache.httpcomponents</groupId>
       <artifactId>httpclient</artifactId>
-      <version>4.0</version>
+      <version>4.2.1</version>
       <scope>provided</scope>
       <optional>true</optional>
     </dependency>
-    <dependency>
-      <!-- for httpclient 4.0 -->
-      <groupId>net.jcip</groupId>
-      <artifactId>jcip-annotations</artifactId>
-      <version>1.0</version>
-      <!--
-         Annotations are only _required_ during a build
-         They are not needed at run-time
-         Provided means that the dependency is not transitive.
-       -->
-      <scope>provided</scope>
-    </dependency>
 
     <!-- unit tests -->
     <dependency>
index cd4274e306b88ccf97415f9b885a661b3b2e0f5e..d880a279479a0a5aea3b83bbdc7e55441ee99a13 100644 (file)
@@ -69,6 +69,13 @@ public class HttpClient3Connector extends Connector {
     }
   }
 
+  /**
+   * @since 3.4
+   */
+  public HttpClient getHttpClient() {
+    return httpClient;
+  }
+
   @Override
   public String execute(Query<?> query) {
     return executeRequest(newGetRequest(query));
index 40d216d8944f1c95a8427b200fe159d29a8624ca..8f025b2b1ad47d9e23c515d8bc7687a21ae82641 100644 (file)
@@ -19,9 +19,6 @@
  */
 package org.sonar.wsclient.connectors;
 
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
@@ -35,6 +32,7 @@ import org.apache.http.auth.AuthState;
 import org.apache.http.auth.Credentials;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpDelete;
 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
 import org.apache.http.client.methods.HttpGet;
@@ -44,6 +42,7 @@ import org.apache.http.client.methods.HttpRequestBase;
 import org.apache.http.client.protocol.ClientContext;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.auth.BasicScheme;
+import org.apache.http.impl.client.AbstractHttpClient;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.params.CoreConnectionPNames;
 import org.apache.http.params.HttpConnectionParams;
@@ -59,15 +58,24 @@ import org.sonar.wsclient.services.DeleteQuery;
 import org.sonar.wsclient.services.Query;
 import org.sonar.wsclient.services.UpdateQuery;
 
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+
 /**
  * @since 2.1
  */
 public class HttpClient4Connector extends Connector {
 
   private Host server;
+  private AbstractHttpClient client;
 
   public HttpClient4Connector(Host server) {
     this.server = server;
+    initClient();
+  }
+
+  public HttpClient getHttpClient() {
+    return client;
   }
 
   @Override
@@ -92,7 +100,6 @@ public class HttpClient4Connector extends Connector {
 
   private String executeRequest(HttpRequestBase request) {
     String json = null;
-    DefaultHttpClient client = createClient();
     try {
       BasicHttpContext context = createLocalContext(client);
       HttpResponse response = client.execute(request, context);
@@ -103,8 +110,8 @@ public class HttpClient4Connector extends Connector {
 
         } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_NOT_FOUND) {
           throw new ConnectionException("HTTP error: " + response.getStatusLine().getStatusCode()
-              + ", msg: " + response.getStatusLine().getReasonPhrase()
-              + ", query: " + request.toString());
+            + ", msg: " + response.getStatusLine().getReasonPhrase()
+            + ", query: " + request.toString());
         }
       }
 
@@ -112,24 +119,29 @@ public class HttpClient4Connector extends Connector {
       throw new ConnectionException("Query: " + request.getURI(), e);
 
     } finally {
-      client.getConnectionManager().shutdown();
+      request.releaseConnection();
     }
     return json;
   }
 
-  private DefaultHttpClient createClient() {
-    DefaultHttpClient client = new DefaultHttpClient();
+  public void close() {
+    if (client != null) {
+      client.getConnectionManager().shutdown();
+    }
+  }
+
+  private void initClient() {
+    client = new DefaultHttpClient();
     HttpParams params = client.getParams();
     HttpConnectionParams.setConnectionTimeout(params, AbstractQuery.DEFAULT_TIMEOUT_MILLISECONDS);
     HttpConnectionParams.setSoTimeout(params, AbstractQuery.DEFAULT_TIMEOUT_MILLISECONDS);
     if (server.getUsername() != null) {
       client.getCredentialsProvider()
-          .setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(server.getUsername(), server.getPassword()));
+        .setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(server.getUsername(), server.getPassword()));
     }
-    return client;
   }
 
-  private BasicHttpContext createLocalContext(DefaultHttpClient client) {
+  private BasicHttpContext createLocalContext(AbstractHttpClient client) {
     BasicHttpContext localcontext = new BasicHttpContext();
 
     if (server.getUsername() != null) {
@@ -191,8 +203,8 @@ public class HttpClient4Connector extends Connector {
 
   static final class PreemptiveAuth implements HttpRequestInterceptor {
     public void process(
-        final HttpRequest request,
-        final HttpContext context) throws HttpException {
+      final HttpRequest request,
+      final HttpContext context) throws HttpException {
 
       AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
 
@@ -203,9 +215,9 @@ public class HttpClient4Connector extends Connector {
         HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
         if (authScheme != null) {
           Credentials creds = credsProvider.getCredentials(
-              new AuthScope(
-                  targetHost.getHostName(),
-                  targetHost.getPort()));
+            new AuthScope(
+              targetHost.getHostName(),
+              targetHost.getPort()));
           if (creds == null) {
             throw new HttpException("No credentials for preemptive authentication");
           }