Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
OPENIG-246 OpenIG in reverse proxy configuration fails half the time …
Browse files Browse the repository at this point in the history
…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()
  • Loading branch information
sauthieg committed Aug 4, 2014
1 parent c125831 commit e7144c9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
13 changes: 13 additions & 0 deletions openig-core/src/main/java/org/forgerock/openig/http/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -77,20 +78,23 @@ 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();
}

@Test
public void getJson() throws Exception {
entity.setRawInputStream(mockJsonContent1);
assertThat(entity.getJson()).isEqualTo(JSON_VALUE1);
assertThat(entity.isEmpty()).isFalse();
verify(mockJsonContent1, never()).close();
}

@Test(expectedExceptions = ParseException.class)
public void getJsonWhenEntityContainsInvalidJsonThrowsParseException() throws Exception {
mockJsonContent1 = mockContent(INVALID_JSON);
entity.setRawInputStream(mockJsonContent1);
assertThat(entity.isEmpty()).isFalse();
try {
entity.getJson();
} finally {
Expand Down Expand Up @@ -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();
}

Expand All @@ -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();
}

Expand Down

0 comments on commit e7144c9

Please sign in to comment.