You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2020/01/22 17:15:35 UTC

[juneau] branch master updated: JUNEAU-182 Add a RestCallHandler interface to the RestClient API

This is an automated email from the ASF dual-hosted git repository.

jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git


The following commit(s) were added to refs/heads/master by this push:
     new c5af1c2  JUNEAU-182 Add a RestCallHandler interface to the RestClient API
c5af1c2 is described below

commit c5af1c2832f67966359258c89e2db7072e53abdf
Author: JamesBognar <ja...@apache.org>
AuthorDate: Wed Jan 22 12:15:19 2020 -0500

    JUNEAU-182 Add a RestCallHandler interface to the RestClient API
---
 juneau-doc/docs/ReleaseNotes/8.1.4.html            | 45 +++++++++++++++
 .../apache/juneau/rest/client/RestCallHandler.java | 65 ++++++++++++++++++++++
 .../org/apache/juneau/rest/client/RestClient.java  | 49 ++++++++++++++--
 .../juneau/rest/client/RestClientBuilder.java      | 38 +++++++++++++
 4 files changed, 192 insertions(+), 5 deletions(-)

diff --git a/juneau-doc/docs/ReleaseNotes/8.1.4.html b/juneau-doc/docs/ReleaseNotes/8.1.4.html
new file mode 100644
index 0000000..4615401
--- /dev/null
+++ b/juneau-doc/docs/ReleaseNotes/8.1.4.html
@@ -0,0 +1,45 @@
+<!--
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *  
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+ -->
+
+8.1.4 (TBD)
+
+<p>
+</p>
+
+<h5 class='topic w800'>juneau-marshall</h5>
+<ul class='spaced-list'>
+</ul>
+
+<h5 class='topic w800'>juneau-rest-server</h5>
+<ul class='spaced-list'>
+</ul>
+
+<h5 class='topic w800'>juneau-rest-server-springboot</h5>
+<ul class='spaced-list'>
+	<li>
+		{@link oajr.springboot.JuneauRestInitializer} now provides a no-arg constructor so that it can be used in
+		the <c><ja>@ConfigurationContext</ja>(initializers=JuneauRestInitializer.<jk>class</jk>)</c> when unit testing
+		using <ja>@SpringBootTest</ja>.
+</ul>
+
+<h5 class='topic w800'>juneau-rest-client</h5>
+<ul class='spaced-list'>
+	<li>
+		New {@link oajrc.RestCallHandler} interface for custom handling of HTTP requests.
+</ul>
+
+<h5 class='topic w800'>juneau-doc</h5>
+<ul class='spaced-list'>
+</ul>
diff --git a/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCallHandler.java b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCallHandler.java
new file mode 100644
index 0000000..15cf54d
--- /dev/null
+++ b/juneau-rest/juneau-rest-client/src/main/java/org/apache/juneau/rest/client/RestCallHandler.java
@@ -0,0 +1,65 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                                                              *
+// *                                                                                                                         *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
+// *                                                                                                                         *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the License.                                              *
+// ***************************************************************************************************************************
+package org.apache.juneau.rest.client;
+
+import java.io.*;
+
+import org.apache.http.*;
+import org.apache.http.client.*;
+import org.apache.http.client.methods.*;
+
+/**
+ * Interface that allows you to override the handling of HTTP requests.
+ *
+ * <p>
+ * Providing this implementation is the equivalent to overriding the {@link RestClient#execute(HttpEntityEnclosingRequestBase)}
+ * and {@link RestClient#execute(HttpRequestBase)} methods.
+ *
+ * <p>
+ * This can also be accomplished by providing your own {@link RestClientBuilder#connectionManager(org.apache.http.conn.HttpClientConnectionManager)},
+ * but this provides a simpler way of handling the requests yourself.
+ *
+ * <ul class='seealso'>
+ * 	<li class='jf'>{@link RestClient#RESTCLIENT_callHandler}
+ * 	<li class='jf'>{@link RestClientBuilder#callHandler(Class)}
+ * 	<li class='jf'>{@link RestClientBuilder#callHandler(RestCallHandler)}
+ * </ul>
+ */
+public interface RestCallHandler {
+
+	/**
+	 * Execute the specified body request (e.g. POST/PUT).
+	 *
+	 * <p>
+	 * Subclasses can override this method to provide specialized handling.
+	 *
+	 * @param req The HTTP request.
+	 * @return The HTTP response.
+	 * @throws IOException Stream exception occurred.
+	 * @throws ClientProtocolException Signals an error in the HTTP protocol.
+	 */
+	HttpResponse execute(HttpEntityEnclosingRequestBase req) throws ClientProtocolException, IOException;
+
+	/**
+	 * Execute the specified no-body request (e.g. GET/DELETE).
+	 *
+	 * <p>
+	 * Subclasses can override this method to provide specialized handling.
+	 *
+	 * @param req The HTTP request.
+	 * @return The HTTP response.
+	 * @throws IOException Stream exception occurred.
+	 * @throws ClientProtocolException Signals an error in the HTTP protocol.
+	 */
+	HttpResponse execute(HttpRequestBase req) throws ClientProtocolException, IOException;
+}
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 6642c9b..947d772 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
@@ -81,6 +81,28 @@ public class RestClient extends BeanContext implements Closeable {
 	private static final String PREFIX = "RestClient.";
 
 	/**
+	 * Configuration property:  REST call handler.
+	 *
+	 * <h5 class='section'>Property:</h5>
+	 * <ul class='spaced-list'>
+	 * 	<li><b>ID:</b>  {@link org.apache.juneau.rest.client.RestClient#RESTCLIENT_callHandler RESTCLIENT_callHandler}
+	 * 	<li><b>Name:</b>  <js>"RestClient.callHandler.o"</js>
+	 * 	<li><b>Data type:</b>  {@link RestCallHandler}
+	 * 	<li><b>Default:</b>  <c><jk>null</jk></c>
+	 * 	<li><b>Methods:</b>
+	 * 		<ul>
+	 * 			<li class='jm'>{@link org.apache.juneau.rest.client.RestClientBuilder#callHandler(Class)}
+	 * 			<li class='jm'>{@link org.apache.juneau.rest.client.RestClientBuilder#callHandler(RestCallHandler)}
+	 * 		</ul>
+	 * </ul>
+	 *
+	 * <h5 class='section'>Description:</h5>
+	 * <p>
+	 * Allows you to provide a custom handler for making HTTP calls.
+	 */
+	public static final String RESTCLIENT_callHandler = PREFIX + "callHandler.o";
+
+	/**
 	 * Configuration property:  Debug.
 	 *
 	 * <h5 class='section'>Property:</h5>
@@ -467,11 +489,12 @@ public class RestClient extends BeanContext implements Closeable {
 
 	private final Map<String,String> headers, query;
 	private final HttpClientBuilder httpClientBuilder;
-	private final CloseableHttpClient httpClient;
+	final CloseableHttpClient httpClient;
 	private final boolean keepHttpClientOpen, debug;
 	private final UrlEncodingSerializer urlEncodingSerializer;  // Used for form posts only.
 	private final HttpPartSerializer partSerializer;
 	private final HttpPartParser partParser;
+	private final RestCallHandler callHandler;
 	private final String rootUrl;
 	private volatile boolean isClosed = false;
 	private final StackTraceElement[] creationStack;
@@ -575,6 +598,22 @@ public class RestClient extends BeanContext implements Closeable {
 		this.partParser = getInstanceProperty(RESTCLIENT_partParser, HttpPartParser.class, OpenApiParser.class, ResourceResolver.FUZZY, ps);
 		this.executorService = getInstanceProperty(RESTCLIENT_executorService, ExecutorService.class, null);
 
+		RestCallHandler callHandler = getInstanceProperty(RESTCLIENT_callHandler, RestCallHandler.class, null);
+		if (callHandler == null) {
+			callHandler = new RestCallHandler() {
+				@Override
+				public HttpResponse execute(HttpEntityEnclosingRequestBase req) throws ClientProtocolException, IOException {
+					return RestClient.this.httpClient.execute(req);
+				}
+
+				@Override
+				public HttpResponse execute(HttpRequestBase req) throws ClientProtocolException, IOException {
+					return RestClient.this.httpClient.execute(req);
+				}
+			};
+		}
+		this.callHandler = callHandler;
+
 		RestCallInterceptor[] rci = getInstanceArrayProperty(RESTCLIENT_interceptors, RestCallInterceptor.class, new RestCallInterceptor[0]);
 		if (debug)
 			rci = ArrayUtils.append(rci, RestCallLogger.DEFAULT);
@@ -641,10 +680,10 @@ public class RestClient extends BeanContext implements Closeable {
 	 * @param req The HTTP request.
 	 * @return The HTTP response.
 	 * @throws IOException Stream exception occurred.
-	 * @throws ClientProtocolException ignals an error in the HTTP protocol.
+	 * @throws ClientProtocolException Signals an error in the HTTP protocol.
 	 */
 	protected HttpResponse execute(HttpRequestBase req) throws ClientProtocolException, IOException {
-		return httpClient.execute(req);
+		return callHandler.execute(req);
 	}
 
 	/**
@@ -656,10 +695,10 @@ public class RestClient extends BeanContext implements Closeable {
 	 * @param req The HTTP request.
 	 * @return The HTTP response.
 	 * @throws IOException Stream exception occurred.
-	 * @throws ClientProtocolException ignals an error in the HTTP protocol.
+	 * @throws ClientProtocolException Signals an error in the HTTP protocol.
 	 */
 	protected HttpResponse execute(HttpEntityEnclosingRequestBase req) throws ClientProtocolException, IOException {
-		return httpClient.execute(req);
+		return callHandler.execute(req);
 	}
 
 	/**
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 3d2c582..87529e0 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
@@ -1137,6 +1137,44 @@ public class RestClientBuilder extends BeanContextBuilder {
 	//-----------------------------------------------------------------------------------------------------------------
 
 	/**
+	 * Configuration property:  REST call handler.
+	 *
+	 * <p>
+	 * Allows you to provide a custom handler for making HTTP calls.
+	 *
+	 * <ul class='seealso'>
+	 * 	<li class='jf'>{@link RestClient#RESTCLIENT_callHandler}
+	 * </ul>
+	 *
+	 * @param value
+	 * 	The new value for this setting.
+	 * 	<br>The default value is <jk>null</jk>.
+	 * @return This object (for method chaining).
+	 */
+	public RestClientBuilder callHandler(Class<? extends RestCallHandler> value) {
+		return set(RESTCLIENT_callHandler, value);
+	}
+
+	/**
+	 * Configuration property:  REST call handler.
+	 *
+	 * <p>
+	 * Allows you to provide a custom handler for making HTTP calls.
+	 *
+	 * <ul class='seealso'>
+	 * 	<li class='jf'>{@link RestClient#RESTCLIENT_callHandler}
+	 * </ul>
+	 *
+	 * @param value
+	 * 	The new value for this setting.
+	 * 	<br>The default value is <jk>null</jk>.
+	 * @return This object (for method chaining).
+	 */
+	public RestClientBuilder callHandler(RestCallHandler value) {
+		return set(RESTCLIENT_callHandler, value);
+	}
+
+	/**
 	 * Configuration property:  Executor service.
 	 *
 	 * <p>