juneau-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamesbog...@apache.org
Subject juneau git commit: (JUNEAU-76) Improvements to Context/Builder APIs
Date Sat, 23 Dec 2017 23:34:13 GMT
Repository: juneau
Updated Branches:
  refs/heads/master dce74703a -> f2174e066


(JUNEAU-76) Improvements to Context/Builder APIs

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

Branch: refs/heads/master
Commit: f2174e06681af44c880a2fc5c352a868f9673487
Parents: dce7470
Author: JamesBognar <jamesbognar@apache.org>
Authored: Sat Dec 23 18:34:09 2017 -0500
Committer: JamesBognar <jamesbognar@apache.org>
Committed: Sat Dec 23 18:34:09 2017 -0500

----------------------------------------------------------------------
 .../java/org/apache/juneau/SessionArgs.java     |  5 ++
 .../examples/rest/DockerRegistryResource.java   |  2 +-
 .../examples/rest/TumblrParserResource.java     |  2 +-
 .../examples/rest/addressbook/ClientTest.java   |  4 +-
 .../examples/rest/SamplesMicroservice.java      |  4 +-
 .../juneau/rest/test/TestMicroservice.java      |  2 +-
 .../org/apache/juneau/rest/client/RestCall.java |  3 +-
 .../apache/juneau/rest/client/RestClient.java   | 37 ++++++++++--
 .../juneau/rest/client/RestClientBuilder.java   | 60 +++++---------------
 .../org/apache/juneau/rest/client/package.html  |  2 +-
 10 files changed, 61 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/juneau/blob/f2174e06/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
----------------------------------------------------------------------
diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
index b59d2de..17f4e8d 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/SessionArgs.java
@@ -17,6 +17,11 @@ package org.apache.juneau;
  */
 public class SessionArgs {
 
+	/**
+	 * Default empty session arguments.
+	 */
+	public static final SessionArgs DEFAULT = new SessionArgs(ObjectMap.EMPTY_MAP);
+	
 	final ObjectMap properties;
 
 	/**

http://git-wip-us.apache.org/repos/asf/juneau/blob/f2174e06/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DockerRegistryResource.java
----------------------------------------------------------------------
diff --git a/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DockerRegistryResource.java
b/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DockerRegistryResource.java
index bab34bf..1e5f395 100644
--- a/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DockerRegistryResource.java
+++ b/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/DockerRegistryResource.java
@@ -59,7 +59,7 @@ public class DockerRegistryResource extends Resource {
 	public void initRegistry(RestConfig config) throws Exception {
 		ConfigFile cf = config.getConfigFile();
 		registryUrl = cf.getString("DockerRegistry/url");
-		rc = new RestClientBuilder().build();
+		rc = RestClient.create().build();
 	}
 
 	@Override /* Servlet */

http://git-wip-us.apache.org/repos/asf/juneau/blob/f2174e06/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/TumblrParserResource.java
----------------------------------------------------------------------
diff --git a/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/TumblrParserResource.java
b/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/TumblrParserResource.java
index 1bb3e93..12997c9 100644
--- a/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/TumblrParserResource.java
+++ b/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/TumblrParserResource.java
@@ -53,7 +53,7 @@ public class TumblrParserResource extends Resource {
 	@RestMethod(name=GET, path="/{blogName}", summary="Parse the specified blog")
 	public ObjectList parseBlog(@Path String blogName) throws Exception {
 		ObjectList l = new ObjectList();
-		RestClient rc = new RestClientBuilder().build();
+		RestClient rc = RestClient.create().build();
 		try {
 			String site = "http://" + blogName + ".tumblr.com/api/read/json";
 			ObjectMap m = rc.doGet(site).getResponse(ObjectMap.class);

http://git-wip-us.apache.org/repos/asf/juneau/blob/f2174e06/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/addressbook/ClientTest.java
----------------------------------------------------------------------
diff --git a/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/addressbook/ClientTest.java
b/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/addressbook/ClientTest.java
index 4470f1d..289f8fa 100644
--- a/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/addressbook/ClientTest.java
+++ b/juneau-examples/juneau-examples-rest/src/main/java/org/apache/juneau/examples/rest/addressbook/ClientTest.java
@@ -30,8 +30,8 @@ public class ClientTest {
 			System.out.println("Running client test...");
 
 			// Create a client to handle XML requests and responses.
-			RestClient client = new RestClientBuilder().build();
-			RestClient xmlClient = new RestClientBuilder(XmlSerializer.DEFAULT_NS, XmlParser.DEFAULT).build();
+			RestClient client = RestClient.create().build();
+			RestClient xmlClient = RestClient.create(XmlSerializer.DEFAULT_NS, XmlParser.DEFAULT).build();
 			try {
 				String root = "http://localhost:10000/addressBook";
 

http://git-wip-us.apache.org/repos/asf/juneau/blob/f2174e06/juneau-examples/juneau-examples-rest/src/test/java/org/apache/juneau/examples/rest/SamplesMicroservice.java
----------------------------------------------------------------------
diff --git a/juneau-examples/juneau-examples-rest/src/test/java/org/apache/juneau/examples/rest/SamplesMicroservice.java
b/juneau-examples/juneau-examples-rest/src/test/java/org/apache/juneau/examples/rest/SamplesMicroservice.java
index 1b0f15a..bc1eb0e 100644
--- a/juneau-examples/juneau-examples-rest/src/test/java/org/apache/juneau/examples/rest/SamplesMicroservice.java
+++ b/juneau-examples/juneau-examples-rest/src/test/java/org/apache/juneau/examples/rest/SamplesMicroservice.java
@@ -86,9 +86,7 @@ public class SamplesMicroservice {
 	 */
 	public static RestClientBuilder client() {
 		try {
-			return new RestClientBuilder()
-				.rootUrl(microserviceURI)
-			;
+			return RestClient.create().rootUrl(microserviceURI);
 		} catch (Exception e) {
 			throw new RuntimeException(e);
 		}

http://git-wip-us.apache.org/repos/asf/juneau/blob/f2174e06/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/TestMicroservice.java
----------------------------------------------------------------------
diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/TestMicroservice.java
b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/TestMicroservice.java
index 6d2f4fd..6c5a0bb 100644
--- a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/TestMicroservice.java
+++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/TestMicroservice.java
@@ -102,7 +102,7 @@ public class TestMicroservice {
 		try {
 			final RequestLine[] currentRequest = new RequestLine[1];
 			final StatusLine[] currentResponse = new StatusLine[1];
-			return new RestClientBuilder()
+			return RestClient.create()
 				.rootUrl(microserviceURI)
 				.setRetryHandler(
 					new HttpRequestRetryHandler() {

http://git-wip-us.apache.org/repos/asf/juneau/blob/f2174e06/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
index be4d0f7..402babf 100644
--- a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
+++ b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCall.java
@@ -69,7 +69,7 @@ import org.apache.juneau.utils.*;
  * </ul>
  */
 @SuppressWarnings({ "unchecked" })
-public final class RestCall {
+public final class RestCall extends Session {
 
 	private final RestClient client;                       // The client that created this call.
 	private final HttpRequestBase request;                 // The request.
@@ -106,6 +106,7 @@ public final class RestCall {
 	 * @throws RestCallException If an exception or non-200 response code occurred during the
connection attempt.
 	 */
 	protected RestCall(RestClient client, HttpRequestBase request, URI uri) throws RestCallException
{
+		super(SessionArgs.DEFAULT);
 		this.client = client;
 		this.request = request;
 		for (RestCallInterceptor i : this.client.intercepters)

http://git-wip-us.apache.org/repos/asf/juneau/blob/f2174e06/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
index 4ccb585..075180c 100644
--- a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
+++ b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClient.java
@@ -275,6 +275,7 @@ public class RestClient extends BeanContext {
 	private static final ConcurrentHashMap<Class,PartSerializer> partSerializerCache =
new ConcurrentHashMap<>();
 	
 	private final Map<String,String> headers;
+	private final HttpClientBuilder httpClientBuilder;
 	private final CloseableHttpClient httpClient;
 	private final boolean keepHttpClientOpen, debug;
 	private final UrlEncodingSerializer urlEncodingSerializer;  // Used for form posts only.
@@ -299,25 +300,53 @@ public class RestClient extends BeanContext {
 	/**
 	 * Instantiates a new clean-slate {@link RestClientBuilder} object.
 	 * 
+	 * @return A new {@link RestClientBuilder} object.
+	 */
+	public static RestClientBuilder create() {
+		return new RestClientBuilder(PropertyStore.DEFAULT, null);
+	}
+	
+	/**
+	 * Instantiates a new {@link RestClientBuilder} object using the specified serializer and
parser.
+	 * 
 	 * <p>
-	 * This is equivalent to simply calling <code><jk>new</jk> RestClientBuilder()</code>.
+	 * Shortcut for calling <code>RestClient.<jsm>create</jsm>().serializer(s).parser(p);</code>
 	 * 
+	 * @param s The serializer to use for output.
+	 * @param p The parser to use for input.
 	 * @return A new {@link RestClientBuilder} object.
 	 */
-	public static RestClientBuilder create() {
-		return new RestClientBuilder();
+	public static RestClientBuilder create(Serializer s, Parser p) {
+		return create().serializer(s).parser(p);
 	}
 
+	/**
+	 * Instantiates a new {@link RestClientBuilder} object using the specified serializer and
parser.
+	 * 
+	 *
+	 * <p>
+	 * Shortcut for calling <code><jk>new</jk> RestClientBuilder().serializer(s).parser(p);</code>
+	 *
+	 * @param s The serializer class to use for output.
+	 * @param p The parser class to use for input.
+	 * @return A new {@link RestClientBuilder} object.
+	 */
+	public static RestClientBuilder create(Class<? extends Serializer> s, Class<? extends
Parser> p) {
+		return create().serializer(s).parser(p);
+	}	
+
 	@Override /* Context */
 	public RestClientBuilder builder() {
-		return new RestClientBuilder(getPropertyStore());
+		return new RestClientBuilder(getPropertyStore(), httpClientBuilder);
 	}
 
 	@SuppressWarnings("unchecked")
 	RestClient(
 			PropertyStore ps,
+			HttpClientBuilder httpClientBuilder,
 			CloseableHttpClient httpClient) {
 		super(ps);
+		this.httpClientBuilder = httpClientBuilder;
 		this.httpClient = httpClient;
 		this.keepHttpClientOpen = getProperty(RESTCLIENT_keepHttpClientOpen, boolean.class, false);
 		this.headers = getMapProperty(RESTCLIENT_headers, String.class);

http://git-wip-us.apache.org/repos/asf/juneau/blob/f2174e06/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
index b9c18b0..cdba040 100644
--- a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
+++ b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestClientBuilder.java
@@ -51,72 +51,40 @@ import org.apache.juneau.uon.*;
 
 /**
  * Builder class for the {@link RestClient} class.
+ * 
+ * <p>
+ * Instances of this class are created by the following methods:
+ * <ul>
+ * 	<li>{@link RestClient#create()} - Create from scratch.
+ * 	<li>{@link RestClient#create(Serializer,Parser)} - Create from scratch using specified
serializer/parser.
+ * 	<li>{@link RestClient#create(Class,Class)} - Create from scratch using specified
serializer/parser classes.
+ * 	<li>{@link RestClient#builder()} - Copy settings from an existing client.
+ * </ul>
  */
 public class RestClientBuilder extends BeanContextBuilder {
 
 	private HttpClientConnectionManager httpClientConnectionManager;
-	private HttpClientBuilder httpClientBuilder = createHttpClientBuilder();
+	private HttpClientBuilder httpClientBuilder;
 	private CloseableHttpClient httpClient;
 	private SSLOpts sslOpts;
 	private boolean pooled;
 
 	/**
-	 * Constructor, default settings.
-	 */
-	public RestClientBuilder() {
-		super();
-	}
-
-	/**
-	 * Constructor, default settings.
-	 *
-	 * <p>
-	 * Shortcut for calling <code><jk>new</jk> RestClientBuilder().serializer(s).parser(p);</code>
-	 *
-	 * @param s The serializer to use for output.
-	 * @param p The parser to use for input.
-	 */
-	public RestClientBuilder(Serializer s, Parser p) {
-		super();
-		serializer(s);
-		parser(p);
-	}
-
-	/**
-	 * Constructor, default settings.
-	 *
-	 * <p>
-	 * Shortcut for calling <code><jk>new</jk> RestClientBuilder().serializer(s).parser(p);</code>
-	 *
-	 * @param s The serializer class to use for output.
-	 * @param p The parser class to use for input.
-	 */
-	public RestClientBuilder(Class<? extends Serializer> s, Class<? extends Parser>
p) {
-		super();
-		serializer(s);
-		parser(p);
-	}
-
-	/**
 	 * Constructor.
-	 *
-	 * @param ps The initial configuration settings for this builder.
 	 */
-	public RestClientBuilder(PropertyStore ps) {
+	RestClientBuilder(PropertyStore ps, HttpClientBuilder httpClientBuilder) {
 		super(ps);
+		this.httpClientBuilder = httpClientBuilder != null ? httpClientBuilder : createHttpClientBuilder();
 	}
 
 	@SuppressWarnings("resource")
 	@Override /* ContextBuilder */
 	public RestClient build() {
 		try {
-			CloseableHttpClient httpClient = this.httpClient;
-			if (httpClient == null)
-				httpClient = createHttpClient();
-			
+			CloseableHttpClient c = httpClient != null ? httpClient : createHttpClient();
 			PropertyStore ps = psb.build();
 
-			return new RestClient(ps, httpClient);
+			return new RestClient(ps, httpClientBuilder, c);
 		} catch (Exception e) {
 			throw new RuntimeException(e);
 		}

http://git-wip-us.apache.org/repos/asf/juneau/blob/f2174e06/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/package.html
----------------------------------------------------------------------
diff --git a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/package.html
b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/package.html
index ac990f9..5ffe6c5 100644
--- a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/package.html
+++ b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/package.html
@@ -933,7 +933,7 @@
 	String r = rc.doGet(<js>"/baz"</js>).getResponseAsString();  <jc>// Gets
"http://localhost:9080/foobar/baz"</jc>
 		</p>
 		<p>
-			The {@link org.apache.juneau.rest.client.RestClientBuilder#property(String,Object)} method
can be used to 
+			The {@link org.apache.juneau.rest.client.RestClientBuilder#set(String,Object)} method
can be used to 
 			set serializer and parser properties.
 			For example, if you're parsing a response into POJOs and you want to ignore fields that
aren't on the
 			POJOs, you can use the {@link org.apache.juneau.BeanContext#BEAN_ignoreUnknownBeanProperties}
property.


Mime
View raw message