Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 2a3292027 -> e4b5f2428


Unclutter brooklyn-rest-client BrooklynApi

Organize constructors in an order that makes sense.
Use the painless way of RESTeasy to provide credentials within HTTP
requests.


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/99ff8c9c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/99ff8c9c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/99ff8c9c

Branch: refs/heads/master
Commit: 99ff8c9c0a8f9a6af14db8e469271619f6472b3b
Parents: f3e0430
Author: Ciprian Ciubotariu <[email protected]>
Authored: Fri Mar 13 23:25:22 2015 +0200
Committer: Ciprian Ciubotariu <[email protected]>
Committed: Fri Mar 13 23:25:22 2015 +0200

----------------------------------------------------------------------
 .../java/brooklyn/rest/client/BrooklynApi.java  | 35 +++++++++++---------
 1 file changed, 19 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/99ff8c9c/usage/rest-client/src/main/java/brooklyn/rest/client/BrooklynApi.java
----------------------------------------------------------------------
diff --git 
a/usage/rest-client/src/main/java/brooklyn/rest/client/BrooklynApi.java 
b/usage/rest-client/src/main/java/brooklyn/rest/client/BrooklynApi.java
index 1dbdf6c..7bcff46 100644
--- a/usage/rest-client/src/main/java/brooklyn/rest/client/BrooklynApi.java
+++ b/usage/rest-client/src/main/java/brooklyn/rest/client/BrooklynApi.java
@@ -50,6 +50,9 @@ import brooklyn.rest.api.SensorApi;
 import brooklyn.rest.api.ServerApi;
 import brooklyn.rest.api.UsageApi;
 import brooklyn.rest.api.VersionApi;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.Credentials;
+import org.apache.http.auth.UsernamePasswordCredentials;
 
 /**
  * @author Adam Lowe
@@ -69,32 +72,32 @@ public class BrooklynApi {
     }
 
     public BrooklynApi(URL endpoint, String username, String password) {
-        this(endpoint.toString(), username, password);
+        this(endpoint.toString(), new UsernamePasswordCredentials(username, 
password));
     }
 
-    public BrooklynApi(final String endpoint, final String username, final 
String password) {
+    public BrooklynApi(String endpoint, String username, String password) {
+        this(endpoint, new UsernamePasswordCredentials(username, password));
+    }
+
+    public BrooklynApi(URL endpoint, Credentials credentials) {
+        this(endpoint.toString(), credentials);
+    }
+
+    public BrooklynApi(String endpoint, Credentials credentials) {
         URL target = null;
         try {
             target = new URL(checkNotNull(endpoint, "endpoint"));
         } catch (MalformedURLException e) {
             throw new IllegalArgumentException(e);
         }
-        this.target = endpoint;
 
-        // Resteasy is a big pain.
-        DefaultHttpClient client = new DefaultHttpClient();
-        if (username != null && password != null) {
-            clientExecutor = new ApacheHttpClient4Executor(client) {
-                @Override
-                public ClientResponse execute(ClientRequest request) throws 
Exception {
-                    String token = username + ":" + password;
-                    String base64Token = 
Base64.encodeBase64String(token.getBytes(Charsets.UTF_8));
-                    request.header("Authorization", "Basic " + base64Token);
-                    return super.execute(request);
-                }
-            };
+        this.target = endpoint;
+        if (credentials != null) {
+            DefaultHttpClient httpClient = new DefaultHttpClient();
+            httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, 
credentials);
+            this.clientExecutor = new ApacheHttpClient4Executor(httpClient);
         } else {
-            clientExecutor = new ApacheHttpClient4Executor(client);
+            this.clientExecutor = ClientRequest.getDefaultExecutor();
         }
     }
 

Reply via email to