From e7144c90b4f7869d1ecb22f803f52922e0766352 Mon Sep 17 00:00:00 2001 From: Guillaume Sauthier Date: Mon, 4 Aug 2014 12:58:35 +0000 Subject: [PATCH] OPENIG-246 OpenIG in reverse proxy configuration fails half the time with http code 501 * Request.getEntity() never returns null, so it can't be used to determine if the request had a content or not * Added an isEmpty() method for this purpose * Updated HttpClient to use Entity.isEmpty() --- .../main/java/org/forgerock/openig/http/Entity.java | 13 +++++++++++++ .../java/org/forgerock/openig/http/HttpClient.java | 2 +- .../java/org/forgerock/openig/http/EntityTest.java | 6 ++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/openig-core/src/main/java/org/forgerock/openig/http/Entity.java b/openig-core/src/main/java/org/forgerock/openig/http/Entity.java index 6333fa53..baee15f8 100644 --- a/openig-core/src/main/java/org/forgerock/openig/http/Entity.java +++ b/openig-core/src/main/java/org/forgerock/openig/http/Entity.java @@ -96,6 +96,19 @@ public final class Entity implements Closeable { setRawInputStream(EMPTY_STREAM); } + /** + * Returns {@literal true} if the entity is empty. + * This method does not read any actual content from the InputStream. + * @return {@literal true} if the entity is empty. + */ + public boolean isEmpty() { + try { + return trunk.available() == 0; + } catch (IOException e) { + return true; + } + } + /** * Closes all resources associated with this entity. Any open streams will * be closed, and the underlying content reset back to a zero length. diff --git a/openig-core/src/main/java/org/forgerock/openig/http/HttpClient.java b/openig-core/src/main/java/org/forgerock/openig/http/HttpClient.java index c641272e..1517402d 100644 --- a/openig-core/src/main/java/org/forgerock/openig/http/HttpClient.java +++ b/openig-core/src/main/java/org/forgerock/openig/http/HttpClient.java @@ -399,7 +399,7 @@ public void execute(final Exchange exchange) throws IOException { */ public Response execute(final Request request) throws IOException { final HttpRequestBase clientRequest = - request.getEntity() != null ? new EntityRequest(request) : new NonEntityRequest(request); + request.getEntity().isEmpty() ? new NonEntityRequest(request) : new EntityRequest(request); clientRequest.setURI(request.getUri().asURI()); // connection headers to suppress final CaseInsensitiveSet suppressConnection = new CaseInsensitiveSet(); diff --git a/openig-core/src/test/java/org/forgerock/openig/http/EntityTest.java b/openig-core/src/test/java/org/forgerock/openig/http/EntityTest.java index f84300c2..26563377 100644 --- a/openig-core/src/test/java/org/forgerock/openig/http/EntityTest.java +++ b/openig-core/src/test/java/org/forgerock/openig/http/EntityTest.java @@ -66,6 +66,7 @@ public void entityIsEmptyByDefault() throws Exception { assertThat(entity.getBytes()).isEmpty(); assertThat(entity.getString()).isEmpty(); assertThat(entity.toString()).isEmpty(); + assertThat(entity.isEmpty()).isTrue(); entity.push(); assertThat(entity.getRawInputStream().available()).isEqualTo(0); entity.pop(); @@ -77,6 +78,7 @@ public void getBytes() throws Exception { entity.setRawInputStream(mockJsonContent1); assertThat(entity.getBytes()).isEqualTo(bytes(JSON_CONTENT1)); assertThat(mockJsonContent1.available()).isEqualTo(JSON_CONTENT1.length()); + assertThat(entity.isEmpty()).isFalse(); verify(mockJsonContent1, never()).close(); } @@ -84,6 +86,7 @@ public void getBytes() throws Exception { public void getJson() throws Exception { entity.setRawInputStream(mockJsonContent1); assertThat(entity.getJson()).isEqualTo(JSON_VALUE1); + assertThat(entity.isEmpty()).isFalse(); verify(mockJsonContent1, never()).close(); } @@ -91,6 +94,7 @@ public void getJson() throws Exception { public void getJsonWhenEntityContainsInvalidJsonThrowsParseException() throws Exception { mockJsonContent1 = mockContent(INVALID_JSON); entity.setRawInputStream(mockJsonContent1); + assertThat(entity.isEmpty()).isFalse(); try { entity.getJson(); } finally { @@ -127,6 +131,7 @@ public void getRawInputStream() throws Exception { entity.setRawInputStream(mockJsonContent1); assertThat(entity.getRawInputStream()).isSameAs(mockJsonContent1); assertThat(mockJsonContent1.available()).isEqualTo(JSON_CONTENT1.length()); + assertThat(entity.isEmpty()).isFalse(); verify(mockJsonContent1, never()).close(); } @@ -135,6 +140,7 @@ public void getString() throws Exception { entity.setRawInputStream(mockJsonContent1); assertThat(entity.getString()).isEqualTo(JSON_CONTENT1); assertThat(mockJsonContent1.available()).isEqualTo(JSON_CONTENT1.length()); + assertThat(entity.isEmpty()).isFalse(); verify(mockJsonContent1, never()).close(); }