Repository: incubator-juneau
Updated Branches:
  refs/heads/master 0fadc07a8 -> e86b4cda4


Add URIBuilder support to client.

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

Branch: refs/heads/master
Commit: e86b4cda407feaa8e8ea98359f933528471510b0
Parents: 0fadc07
Author: JamesBognar <[email protected]>
Authored: Sat Apr 1 19:53:37 2017 -0400
Committer: JamesBognar <[email protected]>
Committed: Sat Apr 1 19:53:37 2017 -0400

----------------------------------------------------------------------
 .../org/apache/juneau/rest/client/RestCall.java | 115 ++++++++++++++++++-
 .../apache/juneau/rest/client/RestClient.java   |  53 +++++----
 2 files changed, 142 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e86b4cda/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
----------------------------------------------------------------------
diff --git 
a/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java 
b/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
index ab9e010..6a94723 100644
--- 
a/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
+++ 
b/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
@@ -23,6 +23,7 @@ import org.apache.http.*;
 import org.apache.http.client.*;
 import org.apache.http.client.config.*;
 import org.apache.http.client.methods.*;
+import org.apache.http.client.utils.*;
 import org.apache.http.impl.client.*;
 import org.apache.http.util.*;
 import org.apache.juneau.*;
@@ -78,15 +79,17 @@ public final class RestCall {
        private Object input;
        private Serializer serializer;
        private Parser parser;
+       private URIBuilder uriBuilder;
 
        /**
         * Constructs a REST call with the specified method name.
         *
         * @param client The client that created this request.
         * @param request The wrapped Apache HTTP client request object.
+        * @param uri The URI for this call.
         * @throws RestCallException If an exception or non-200 response code 
occurred during the connection attempt.
         */
-       protected RestCall(RestClient client, HttpRequestBase request) throws 
RestCallException {
+       protected RestCall(RestClient client, HttpRequestBase request, URI uri) 
throws RestCallException {
                this.client = client;
                this.request = request;
                for (RestCallInterceptor i : this.client.interceptors)
@@ -96,6 +99,114 @@ public final class RestCall {
                this.retryInterval = client.retryInterval;
                this.serializer = client.serializer;
                this.parser = client.parser;
+               uriBuilder = new URIBuilder(uri);
+       }
+
+       /**
+        * Sets the URI for this call.
+        * <p>
+        * Can be any of the following types:
+        *      <ul>
+        *              <li>{@link URI}
+        *              <li>{@link URL}
+        *              <li>{@link URIBuilder}
+        *              <li>Anything else converted to a string using {@link 
Object#toString()}.
+        *      </ul>
+        * Relative URL strings will be interpreted as relative to the root URL 
defined on the client.
+        *
+        * @param uri The URI to use for this call.
+        * This overrides the URI passed in from the client.
+        * @return This object (for method chaining).
+        * @throws RestCallException
+        */
+       public RestCall uri(Object uri) throws RestCallException {
+               try {
+                       if (uri != null)
+                               uriBuilder = new URIBuilder(client.toURI(uri));
+                       return this;
+               } catch (URISyntaxException e) {
+                       throw new RestCallException(e);
+               }
+       }
+
+       /**
+        * Sets the URI scheme.
+        *
+        * @param scheme The new URI host.
+        * @return This object (for method chaining).
+        */
+       public RestCall scheme(String scheme) {
+               uriBuilder.setScheme(scheme);
+               return this;
+       }
+
+       /**
+        * Sets the URI host.
+        *
+        * @param host The new URI host.
+        * @return This object (for method chaining).
+        */
+       public RestCall host(String host) {
+               uriBuilder.setHost(host);
+               return this;
+       }
+
+       /**
+        * Sets the URI port.
+        *
+        * @param port The new URI port.
+        * @return This object (for method chaining).
+        */
+       public RestCall port(int port) {
+               uriBuilder.setPort(port);
+               return this;
+       }
+
+       /**
+        * Adds a parameter to the URI query.
+        *
+        * @param name The parameter name.
+        * @param value The parameter value converted to a string using UON 
notation.
+        * @return This object (for method chaining).
+        * @throws RestCallException
+        */
+       public RestCall param(String name, Object value) throws 
RestCallException {
+               uriBuilder.addParameter(name, 
client.getUrlEncodingSerializer().serializeUrlPart(value));
+               return this;
+       }
+
+       /**
+        * Sets a custom URI query.
+        *
+        * @param query The new URI query string.
+        * @return This object (for method chaining).
+        */
+       public RestCall query(String query) {
+               uriBuilder.setCustomQuery(query);
+               return this;
+       }
+
+       /**
+        * Sets the URI user info.
+        *
+        * @param userInfo The new URI user info.
+        * @return This object (for method chaining).
+        */
+       public RestCall userInfo(String userInfo) {
+               uriBuilder.setUserInfo(userInfo);
+               return this;
+       }
+
+       /**
+        * Sets the URI user info.
+        *
+        * @param username The new URI username.
+        * @param password The new URI password.
+        * @return This object (for method chaining).
+        */
+       public RestCall userInfo(String username, String password) {
+               uriBuilder.setUserInfo(username, password);
+               return this;
        }
 
        /**
@@ -909,6 +1020,8 @@ public final class RestCall {
 
                try {
 
+                       request.setURI(uriBuilder.build());
+
                        if (input != null) {
                                if (! (request instanceof 
HttpEntityEnclosingRequestBase))
                                        throw new RestCallException(0, "Method 
does not support content entity.", request.getMethod(), request.getURI(), null);

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/e86b4cda/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
----------------------------------------------------------------------
diff --git 
a/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
 
b/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
index d926c8e..bbdb98e 100644
--- 
a/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
+++ 
b/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
@@ -12,8 +12,6 @@
 // 
***************************************************************************************************************************
 package org.apache.juneau.rest.client;
 
-import static org.apache.juneau.internal.ThrowableUtils.*;
-
 import java.io.*;
 import java.lang.reflect.*;
 import java.lang.reflect.Proxy;
@@ -24,6 +22,7 @@ import java.util.regex.*;
 
 import org.apache.http.*;
 import org.apache.http.client.methods.*;
+import org.apache.http.client.utils.*;
 import org.apache.http.entity.*;
 import org.apache.http.impl.client.*;
 import org.apache.juneau.*;
@@ -378,27 +377,26 @@ public class RestClient extends CoreObject {
                HttpRequestBase req = null;
                RestCall restCall = null;
                final String methodUC = method.toUpperCase(Locale.ENGLISH);
-               if (hasContent) {
-                       req = new HttpEntityEnclosingRequestBase() {
-                               @Override /* HttpRequest */
-                               public String getMethod() {
-                                       return methodUC;
-                               }
-                       };
-                       restCall = new RestCall(this, req);
-               } else {
-                       req = new HttpRequestBase() {
-                               @Override /* HttpRequest */
-                               public String getMethod() {
-                                       return methodUC;
-                               }
-                       };
-                       restCall = new RestCall(this, req);
-               }
                try {
-                       req.setURI(toURI(url));
-               } catch (URISyntaxException e) {
-                       throw new RestCallException(e);
+                       if (hasContent) {
+                               req = new HttpEntityEnclosingRequestBase() {
+                                       @Override /* HttpRequest */
+                                       public String getMethod() {
+                                               return methodUC;
+                                       }
+                               };
+                               restCall = new RestCall(this, req, toURI(url));
+                       } else {
+                               req = new HttpRequestBase() {
+                                       @Override /* HttpRequest */
+                                       public String getMethod() {
+                                               return methodUC;
+                                       }
+                               };
+                               restCall = new RestCall(this, req, toURI(url));
+                       }
+               } catch (URISyntaxException e1) {
+                       throw new RestCallException(e1);
                }
                for (Map.Entry<String,? extends Object> e : headers.entrySet())
                        restCall.header(e.getKey(), e.getValue());
@@ -484,13 +482,18 @@ public class RestClient extends CoreObject {
 
        private Pattern absUrlPattern = Pattern.compile("^\\w+\\:\\/\\/.*");
 
-       private URI toURI(Object url) throws URISyntaxException {
-               assertFieldNotNull(url, "url");
+       UrlEncodingSerializer getUrlEncodingSerializer() {
+               return urlEncodingSerializer;
+       }
+
+       URI toURI(Object url) throws URISyntaxException {
                if (url instanceof URI)
                        return (URI)url;
                if (url instanceof URL)
                        ((URL)url).toURI();
-               String s = url.toString();
+               if (url instanceof URIBuilder)
+                       return ((URIBuilder)url).build();
+               String s = url == null ? "" : url.toString();
                if (rootUrl != null && ! absUrlPattern.matcher(s).matches()) {
                        if (s.isEmpty())
                                s = rootUrl;

Reply via email to