[java-support] branch master updated: OSJ-265: TLS socket factory clears client TLS credential too early
Brent Putman
putmanb at georgetown.edu
Fri Mar 22 22:08:52 EDT 2019
This is an automated email from the git hooks/post-receive script.
putmanb pushed a commit to branch master
in repository java-support.
View the commit online:
http://git.shibboleth.net/view/?p=java-support.git;a=commit;h=c2539d2d0149723eb4fa8893d1b56ef99a4c7bf7
The following commit(s) were added to refs/heads/master by this push:
new c2539d2 OSJ-265: TLS socket factory clears client TLS credential too early
c2539d2 is described below
commit c2539d2d0149723eb4fa8893d1b56ef99a4c7bf7
Author: Brent Putman <putmanb at georgetown.edu>
AuthorDate: Fri Mar 22 22:05:41 2019 -0400
OSJ-265: TLS socket factory clears client TLS credential too early
Have to account for the CloseableHttpClient type, which is what
the Apache HttpClientBuilder is actually defined to return.
Make the new HC wrapper impl extend that abstract class.
This actually makes the code simpler, as we now just have to implement
1 abstract doExceute(...) method, instead of 8 execute(..) ones.
---
.../httpclient/ContextHandlingHttpClient.java | 104 +++---------------
.../httpclient/ContextHandlingHttpClientTest.java | 119 ++++++++-------------
.../FileCachingHttpClientBuilderTest.java | 31 ++++++
.../InMemoryCachingHttpClientBuilderTest.java | 31 ++++++
4 files changed, 122 insertions(+), 163 deletions(-)
diff --git a/src/main/java/net/shibboleth/utilities/java/support/httpclient/ContextHandlingHttpClient.java b/src/main/java/net/shibboleth/utilities/java/support/httpclient/ContextHandlingHttpClient.java
index a90e544..1f09b32 100644
--- a/src/main/java/net/shibboleth/utilities/java/support/httpclient/ContextHandlingHttpClient.java
+++ b/src/main/java/net/shibboleth/utilities/java/support/httpclient/ContextHandlingHttpClient.java
@@ -25,15 +25,16 @@ import javax.annotation.Nonnull;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
-import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestWrapper;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -65,13 +66,13 @@ import net.shibboleth.utilities.java.support.logic.Constraint;
* </ol>
* </p>
*/
-class ContextHandlingHttpClient implements HttpClient {
+class ContextHandlingHttpClient extends CloseableHttpClient {
/** Logger. */
private Logger log = LoggerFactory.getLogger(ContextHandlingHttpClient.class);
/** The wrapped client instance. */
- @Nonnull private HttpClient httpClient;
+ @Nonnull private CloseableHttpClient httpClient;
/** Optional list of static handlers supplied to this class instance. */
@Nonnull private List<HttpClientContextHandler> handlers;
@@ -81,7 +82,7 @@ class ContextHandlingHttpClient implements HttpClient {
*
* @param client the wrapped client instance
*/
- public ContextHandlingHttpClient(@Nonnull final HttpClient client) {
+ public ContextHandlingHttpClient(@Nonnull final CloseableHttpClient client) {
this(client, null);
}
@@ -91,7 +92,7 @@ class ContextHandlingHttpClient implements HttpClient {
* @param client the wrapped client instance
* @param staticHandlers the list of static handlers
*/
- public ContextHandlingHttpClient(@Nonnull final HttpClient client,
+ public ContextHandlingHttpClient(@Nonnull final CloseableHttpClient client,
@Nonnull final List<HttpClientContextHandler> staticHandlers) {
httpClient = Constraint.isNotNull(client, "HttpClient was null");
handlers = staticHandlers != null ? staticHandlers : Collections.emptyList();
@@ -106,100 +107,25 @@ class ContextHandlingHttpClient implements HttpClient {
public ClientConnectionManager getConnectionManager() {
return httpClient.getConnectionManager();
}
-
- /** {@inheritDoc} */
- public HttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException {
- return httpClient.execute(request);
- }
-
- /** {@inheritDoc} */
- public HttpResponse execute(final HttpHost target, final HttpRequest request)
- throws IOException, ClientProtocolException {
- return httpClient.execute(target, request);
- }
-
- /** {@inheritDoc} */
- public <T> T execute(final HttpUriRequest request, final ResponseHandler<? extends T> responseHandler)
- throws IOException, ClientProtocolException {
- return httpClient.execute(request, responseHandler);
- }
-
+
/** {@inheritDoc} */
- public <T> T execute(final HttpHost target, final HttpRequest request,
- final ResponseHandler<? extends T> responseHandler)
- throws IOException, ClientProtocolException {
- return httpClient.execute(target, request, responseHandler);
+ public void close() throws IOException {
+ httpClient.close();
}
/** {@inheritDoc} */
- public HttpResponse execute(final HttpUriRequest uriRequest, final HttpContext context)
- throws IOException, ClientProtocolException {
-
- Throwable error = null;
-
- final HttpClientContext clientContext = HttpClientContext.adapt(context);
- try {
- invokeBefore(uriRequest, clientContext);
- return httpClient.execute(uriRequest, context);
- } catch (final Throwable t) {
- error = t;
- throw t;
- } finally {
- invokeAfter(uriRequest, clientContext, error);
- }
- }
-
- /** {@inheritDoc} */
- public HttpResponse execute(final HttpHost target, final HttpRequest request, final HttpContext context)
- throws IOException, ClientProtocolException {
-
- Throwable error = null;
-
- final HttpClientContext clientContext = HttpClientContext.adapt(context);
- final HttpUriRequest uriRequest = HttpUriRequest.class.isInstance(request)
- ? (HttpUriRequest)request : HttpRequestWrapper.wrap(request, target);
- try {
- invokeBefore(uriRequest, clientContext);
- return httpClient.execute(target, request, context);
- } catch (final Throwable t) {
- error = t;
- throw t;
- } finally {
- invokeAfter(uriRequest, clientContext, error);
- }
- }
-
- /** {@inheritDoc} */
- public <T> T execute(final HttpUriRequest uriRequest, final ResponseHandler<? extends T> responseHandler,
+ protected CloseableHttpResponse doExecute(final HttpHost target, final HttpRequest request,
final HttpContext context) throws IOException, ClientProtocolException {
Throwable error = null;
- final HttpClientContext clientContext = HttpClientContext.adapt(context);
- try {
- invokeBefore(uriRequest, clientContext);
- return httpClient.execute(uriRequest, responseHandler, context);
- } catch (final Throwable t) {
- error = t;
- throw t;
- } finally {
- invokeAfter(uriRequest, clientContext, error);
- }
- }
-
- /** {@inheritDoc} */
- public <T> T execute(final HttpHost target, final HttpRequest request,
- final ResponseHandler<? extends T> responseHandler, final HttpContext context)
- throws IOException, ClientProtocolException {
-
- Throwable error = null;
-
- final HttpClientContext clientContext = HttpClientContext.adapt(context);
+ final HttpClientContext clientContext =
+ HttpClientContext.adapt(context != null ? context : new BasicHttpContext());
final HttpUriRequest uriRequest = HttpUriRequest.class.isInstance(request)
? (HttpUriRequest)request : HttpRequestWrapper.wrap(request, target);
try {
invokeBefore(uriRequest, clientContext);
- return httpClient.execute(target, request, responseHandler, context);
+ return httpClient.execute(target, request, clientContext);
} catch (final Throwable t) {
error = t;
throw t;
@@ -376,4 +302,6 @@ class ContextHandlingHttpClient implements HttpClient {
}
}
+
+
}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/httpclient/ContextHandlingHttpClientTest.java b/src/test/java/net/shibboleth/utilities/java/support/httpclient/ContextHandlingHttpClientTest.java
index 1936ce8..4a31021 100644
--- a/src/test/java/net/shibboleth/utilities/java/support/httpclient/ContextHandlingHttpClientTest.java
+++ b/src/test/java/net/shibboleth/utilities/java/support/httpclient/ContextHandlingHttpClientTest.java
@@ -28,12 +28,13 @@ import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
@@ -46,6 +47,10 @@ import com.google.common.collect.Lists;
public class ContextHandlingHttpClientTest {
+ public static final CloseableHttpResponse STATIC_RESPONSE_HTTP = new MockCloseableHttpResponse(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_OK, "OK");
+
+ public static final Object STATIC_RESPONSE_HANDLER = new Object();
+
private ContextHandlingHttpClient client;
private TestContextHandler staticOne, staticTwo, staticThree;
@@ -80,16 +85,16 @@ public class ContextHandlingHttpClientTest {
context = HttpClientContext.create();
//Non-context execute methods
- Assert.assertSame(client.execute(request), MockHttpClient.STATIC_RESPONSE_HTTP);
- Assert.assertSame(client.execute(request, responseHandler), MockHttpClient.STATIC_RESPONSE_HANDLER);
- Assert.assertSame(client.execute(target, request), MockHttpClient.STATIC_RESPONSE_HTTP);
- Assert.assertSame(client.execute(target, request, responseHandler), MockHttpClient.STATIC_RESPONSE_HANDLER);
+ Assert.assertSame(client.execute(request), STATIC_RESPONSE_HTTP);
+ Assert.assertSame(client.execute(request, responseHandler), STATIC_RESPONSE_HANDLER);
+ Assert.assertSame(client.execute(target, request), STATIC_RESPONSE_HTTP);
+ Assert.assertSame(client.execute(target, request, responseHandler), STATIC_RESPONSE_HANDLER);
//Context execute methods
- Assert.assertSame(client.execute(request, context), MockHttpClient.STATIC_RESPONSE_HTTP);
- Assert.assertSame(client.execute(request, responseHandler, context), MockHttpClient.STATIC_RESPONSE_HANDLER);
- Assert.assertSame(client.execute(target, request, context), MockHttpClient.STATIC_RESPONSE_HTTP);
- Assert.assertSame(client.execute(target, request, responseHandler, context), MockHttpClient.STATIC_RESPONSE_HANDLER);
+ Assert.assertSame(client.execute(request, context), STATIC_RESPONSE_HTTP);
+ Assert.assertSame(client.execute(request, responseHandler, context), STATIC_RESPONSE_HANDLER);
+ Assert.assertSame(client.execute(target, request, context), STATIC_RESPONSE_HTTP);
+ Assert.assertSame(client.execute(target, request, responseHandler, context), STATIC_RESPONSE_HANDLER);
}
@Test
@@ -106,19 +111,19 @@ public class ContextHandlingHttpClientTest {
);
context = HttpClientContext.create();
- Assert.assertSame(client.execute(request, context), MockHttpClient.STATIC_RESPONSE_HTTP);
+ Assert.assertSame(client.execute(request, context), STATIC_RESPONSE_HTTP);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
context = HttpClientContext.create();
- Assert.assertSame(client.execute(request, responseHandler, context), MockHttpClient.STATIC_RESPONSE_HANDLER);
+ Assert.assertSame(client.execute(request, responseHandler, context), STATIC_RESPONSE_HANDLER);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
context = HttpClientContext.create();
- Assert.assertSame(client.execute(target, request, context), MockHttpClient.STATIC_RESPONSE_HTTP);
+ Assert.assertSame(client.execute(target, request, context), STATIC_RESPONSE_HTTP);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
context = HttpClientContext.create();
- Assert.assertSame(client.execute(target, request, responseHandler, context), MockHttpClient.STATIC_RESPONSE_HANDLER);
+ Assert.assertSame(client.execute(target, request, responseHandler, context), STATIC_RESPONSE_HANDLER);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
}
@@ -139,22 +144,22 @@ public class ContextHandlingHttpClientTest {
context = HttpClientContext.create();
HttpClientSupport.getDynamicContextHandlerList(context).addAll(handlers);
- Assert.assertSame(client.execute(request, context), MockHttpClient.STATIC_RESPONSE_HTTP);
+ Assert.assertSame(client.execute(request, context), STATIC_RESPONSE_HTTP);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
context = HttpClientContext.create();
HttpClientSupport.getDynamicContextHandlerList(context).addAll(handlers);
- Assert.assertSame(client.execute(request, responseHandler, context), MockHttpClient.STATIC_RESPONSE_HANDLER);
+ Assert.assertSame(client.execute(request, responseHandler, context), STATIC_RESPONSE_HANDLER);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
context = HttpClientContext.create();
HttpClientSupport.getDynamicContextHandlerList(context).addAll(handlers);
- Assert.assertSame(client.execute(target, request, context), MockHttpClient.STATIC_RESPONSE_HTTP);
+ Assert.assertSame(client.execute(target, request, context), STATIC_RESPONSE_HTTP);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
context = HttpClientContext.create();
HttpClientSupport.getDynamicContextHandlerList(context).addAll(handlers);
- Assert.assertSame(client.execute(target, request, responseHandler, context), MockHttpClient.STATIC_RESPONSE_HANDLER);
+ Assert.assertSame(client.execute(target, request, responseHandler, context), STATIC_RESPONSE_HANDLER);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
}
@@ -181,22 +186,22 @@ public class ContextHandlingHttpClientTest {
context = HttpClientContext.create();
HttpClientSupport.getDynamicContextHandlerList(context).addAll(handlers);
- Assert.assertSame(client.execute(request, context), MockHttpClient.STATIC_RESPONSE_HTTP);
+ Assert.assertSame(client.execute(request, context), STATIC_RESPONSE_HTTP);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
context = HttpClientContext.create();
HttpClientSupport.getDynamicContextHandlerList(context).addAll(handlers);
- Assert.assertSame(client.execute(request, responseHandler, context), MockHttpClient.STATIC_RESPONSE_HANDLER);
+ Assert.assertSame(client.execute(request, responseHandler, context), STATIC_RESPONSE_HANDLER);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
context = HttpClientContext.create();
HttpClientSupport.getDynamicContextHandlerList(context).addAll(handlers);
- Assert.assertSame(client.execute(target, request, context), MockHttpClient.STATIC_RESPONSE_HTTP);
+ Assert.assertSame(client.execute(target, request, context), STATIC_RESPONSE_HTTP);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
context = HttpClientContext.create();
HttpClientSupport.getDynamicContextHandlerList(context).addAll(handlers);
- Assert.assertSame(client.execute(target, request, responseHandler, context), MockHttpClient.STATIC_RESPONSE_HANDLER);
+ Assert.assertSame(client.execute(target, request, responseHandler, context), STATIC_RESPONSE_HANDLER);
Assert.assertEquals(context.getAttribute(TestContextHandler.TEST_KEY), control);
}
@@ -1850,11 +1855,7 @@ public class ContextHandlingHttpClientTest {
}
- private static class MockHttpClient implements HttpClient {
-
- public static final HttpResponse STATIC_RESPONSE_HTTP = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_OK, "OK");
-
- public static final Object STATIC_RESPONSE_HANDLER = new Object();
+ private static class MockHttpClient extends CloseableHttpClient {
private Throwable error;
@@ -1874,71 +1875,39 @@ public class ContextHandlingHttpClientTest {
public ClientConnectionManager getConnectionManager() {
return null;
}
-
- /** {@inheritDoc} */
- public HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException {
- return responseHTTP();
- }
-
- /** {@inheritDoc} */
- public HttpResponse execute(HttpUriRequest request, HttpContext context)
- throws IOException, ClientProtocolException {
- return responseHTTP();
- }
-
- /** {@inheritDoc} */
- public HttpResponse execute(HttpHost target, HttpRequest request) throws IOException, ClientProtocolException {
- return responseHTTP();
- }
-
- /** {@inheritDoc} */
- public HttpResponse execute(HttpHost target, HttpRequest request, HttpContext context)
- throws IOException, ClientProtocolException {
- return responseHTTP();
- }
-
- /** {@inheritDoc} */
- public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler)
- throws IOException, ClientProtocolException {
- return responseHandler();
- }
-
+
/** {@inheritDoc} */
- public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context)
- throws IOException, ClientProtocolException {
- return responseHandler();
+ public void close() throws IOException {
+ // nothing to do
}
/** {@inheritDoc} */
- public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler)
+ protected CloseableHttpResponse doExecute(HttpHost target, HttpRequest request, HttpContext context)
throws IOException, ClientProtocolException {
- return responseHandler();
- }
-
- /** {@inheritDoc} */
- public <T> T execute(HttpHost target, HttpRequest request, ResponseHandler<? extends T> responseHandler,
- HttpContext context) throws IOException, ClientProtocolException {
- return responseHandler();
- }
-
- private HttpResponse responseHTTP() throws IOException {
ThrowableHelper.checkAndThrowError(error);
return STATIC_RESPONSE_HTTP;
}
- private <T> T responseHandler() throws IOException {
- ThrowableHelper.checkAndThrowError(error);
- return (T) STATIC_RESPONSE_HANDLER;
- }
-
}
public static class MockResponseHandler implements ResponseHandler<Object> {
public Object handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
- return null;
+ return STATIC_RESPONSE_HANDLER;
+ }
+
+ }
+
+ public static class MockCloseableHttpResponse extends BasicHttpResponse implements CloseableHttpResponse {
+
+ public MockCloseableHttpResponse(ProtocolVersion ver, int code, String reason) {
+ super(ver, code, reason);
}
+ public void close() throws IOException {
+
+ }
+
}
public static class ThrowableHelper {
diff --git a/src/test/java/net/shibboleth/utilities/java/support/httpclient/FileCachingHttpClientBuilderTest.java b/src/test/java/net/shibboleth/utilities/java/support/httpclient/FileCachingHttpClientBuilderTest.java
new file mode 100644
index 0000000..7261218
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/httpclient/FileCachingHttpClientBuilderTest.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.httpclient;
+
+import org.apache.http.client.HttpClient;
+import org.testng.annotations.Test;
+
+public class FileCachingHttpClientBuilderTest {
+
+ @Test
+ public void testDefaults() throws Exception {
+ final FileCachingHttpClientBuilder builder = new FileCachingHttpClientBuilder();
+ final HttpClient client = builder.buildClient();
+ }
+
+}
diff --git a/src/test/java/net/shibboleth/utilities/java/support/httpclient/InMemoryCachingHttpClientBuilderTest.java b/src/test/java/net/shibboleth/utilities/java/support/httpclient/InMemoryCachingHttpClientBuilderTest.java
new file mode 100644
index 0000000..51d8015
--- /dev/null
+++ b/src/test/java/net/shibboleth/utilities/java/support/httpclient/InMemoryCachingHttpClientBuilderTest.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development,
+ * Inc. (UCAID) under one or more contributor license agreements. See the
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID 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 net.shibboleth.utilities.java.support.httpclient;
+
+import org.apache.http.client.HttpClient;
+import org.testng.annotations.Test;
+
+public class InMemoryCachingHttpClientBuilderTest {
+
+ @Test
+ public void testDefaults() throws Exception {
+ final InMemoryCachingHttpClientBuilder builder = new InMemoryCachingHttpClientBuilder();
+ final HttpClient client = builder.buildClient();
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list