Fix typo in IHttpResponse

This commit is contained in:
James 2016-12-19 11:29:05 -05:00
parent 74a73e74c5
commit 7838d511d0
6 changed files with 242 additions and 211 deletions

View File

@ -54,50 +54,43 @@ public class ApacheHttpResponse implements IHttpResponse {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ApacheHttpResponse.class);
private boolean myEntityBuffered = false;
private final HttpResponse myResponse;
private byte[] myEntityBytes;
private final HttpResponse myResponse;
public ApacheHttpResponse(HttpResponse theResponse) {
this.myResponse = theResponse;
}
@Override
public HttpResponse getResponse() {
return myResponse;
public void bufferEntitity() throws IOException {
bufferEntity();
}
@Override
public int getStatus() {
return myResponse.getStatusLine().getStatusCode();
}
@Override
public String getMimeType() {
ContentType ct = ContentType.get(myResponse.getEntity());
return ct != null ? ct.getMimeType() : null;
}
@Override
public Map<String, List<String>> getAllHeaders() {
Map<String, List<String>> headers = new HashMap<String, List<String>>();
if (myResponse.getAllHeaders() != null) {
for (Header next : myResponse.getAllHeaders()) {
String name = next.getName().toLowerCase();
List<String> list = headers.get(name);
if (list == null) {
list = new ArrayList<String>();
headers.put(name, list);
}
list.add(next.getValue());
}
public void bufferEntity() throws IOException {
if (myEntityBuffered) {
return;
}
InputStream respEntity = readEntity();
if (respEntity != null) {
this.myEntityBuffered = true;
try {
this.myEntityBytes = IOUtils.toByteArray(respEntity);
} catch (IllegalStateException e) {
throw new InternalErrorException(e);
}
}
return headers;
}
@Override
public String getStatusInfo() {
return myResponse.getStatusLine().getReasonPhrase();
public void close() {
if (myResponse instanceof CloseableHttpResponse) {
try {
((CloseableHttpResponse) myResponse).close();
} catch (IOException e) {
ourLog.debug("Failed to close response", e);
}
}
}
@Override
@ -123,6 +116,45 @@ public class ApacheHttpResponse implements IHttpResponse {
return reader;
}
@Override
public Map<String, List<String>> getAllHeaders() {
Map<String, List<String>> headers = new HashMap<String, List<String>>();
if (myResponse.getAllHeaders() != null) {
for (Header next : myResponse.getAllHeaders()) {
String name = next.getName().toLowerCase();
List<String> list = headers.get(name);
if (list == null) {
list = new ArrayList<String>();
headers.put(name, list);
}
list.add(next.getValue());
}
}
return headers;
}
@Override
public String getMimeType() {
ContentType ct = ContentType.get(myResponse.getEntity());
return ct != null ? ct.getMimeType() : null;
}
@Override
public HttpResponse getResponse() {
return myResponse;
}
@Override
public int getStatus() {
return myResponse.getStatusLine().getStatusCode();
}
@Override
public String getStatusInfo() {
return myResponse.getStatusLine().getReasonPhrase();
}
@Override
public InputStream readEntity() throws IOException {
if (this.myEntityBuffered) {
@ -133,31 +165,4 @@ public class ApacheHttpResponse implements IHttpResponse {
return null;
}
}
@Override
public void close() {
if (myResponse instanceof CloseableHttpResponse) {
try {
((CloseableHttpResponse) myResponse).close();
} catch (IOException e) {
ourLog.debug("Failed to close response", e);
}
}
}
@Override
public void bufferEntitity() throws IOException {
if (myEntityBuffered) {
return;
}
InputStream respEntity = readEntity();
if (respEntity != null) {
this.myEntityBuffered = true;
try {
this.myEntityBytes = IOUtils.toByteArray(respEntity);
} catch (IllegalStateException e) {
throw new InternalErrorException(e);
}
}
}
}

View File

@ -10,7 +10,7 @@ package ca.uhn.fhir.rest.client.api;
* 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
* 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,
@ -32,8 +32,9 @@ import java.util.Map;
public interface IHttpResponse {
/**
* Get the status code associated with the response.
* @return the response status code.
* Get the status code associated with the response.
*
* @return the response status code.
*/
public int getStatus();
@ -42,21 +43,23 @@ public interface IHttpResponse {
*/
Object getResponse();
/**
* Extracts {@code Content-Type} value from the response exactly as
* specified by the {@code Content-Type} header. Returns {@code null}
* if not specified.
*/
/**
* Extracts {@code Content-Type} value from the response exactly as
* specified by the {@code Content-Type} header. Returns {@code null}
* if not specified.
*/
public String getMimeType();
/**
* Get map of the response headers and corresponding string values.
* @return response headers as a map header keys and they values.
*/
* Get map of the response headers and corresponding string values.
*
* @return response headers as a map header keys and they values.
*/
public Map<String, List<String>> getAllHeaders();
/**
* Get the response status information reason phrase associated with the response.
* Get the response status information reason phrase associated with the response.
*
* @return the reason phrase.
*/
public String getStatusInfo();
@ -67,7 +70,7 @@ public interface IHttpResponse {
public Reader createReader() throws IOException;
/**
* Read the message entity input stream as an InputStream.
* Read the message entity input stream as an InputStream.
*/
public InputStream readEntity() throws IOException;
@ -76,27 +79,35 @@ public interface IHttpResponse {
*/
public void close();
/**
* Buffer the message entity data.
* <p>
* In case the message entity is backed by an unconsumed entity input stream,
* all the bytes of the original entity input stream are read and stored in a
* local buffer. The original entity input stream is consumed.
* </p>
* <p>
* In case the response entity instance is not backed by an unconsumed input stream
* an invocation of {@code bufferEntity} method is ignored and the method returns.
* </p>
* <p>
* This operation is idempotent, i.e. it can be invoked multiple times with
* the same effect which also means that calling the {@code bufferEntity()}
* method on an already buffered (and thus closed) message instance is legal
* and has no further effect.
* </p>
* <p>
* Buffering the message entity data allows for multiple invocations of
* {@code readEntity(...)} methods on the response instance.
*/
/**
* Buffer the message entity data.
* <p>
* In case the message entity is backed by an unconsumed entity input stream,
* all the bytes of the original entity input stream are read and stored in a
* local buffer. The original entity input stream is consumed.
* </p>
* <p>
* In case the response entity instance is not backed by an unconsumed input stream
* an invocation of {@code bufferEntity} method is ignored and the method returns.
* </p>
* <p>
* This operation is idempotent, i.e. it can be invoked multiple times with
* the same effect which also means that calling the {@code bufferEntity()}
* method on an already buffered (and thus closed) message instance is legal
* and has no further effect.
* </p>
* <p>
* Buffering the message entity data allows for multiple invocations of
* {@code readEntity(...)} methods on the response instance.
*
* @since 2.2
*/
void bufferEntity() throws IOException;
/**
* @deprecated This method was deprecated in HAPI FHIR 2.2 because its name has a typo. Use {@link #bufferEntity()} instead.
*/
@Deprecated
void bufferEntitity() throws IOException;
}

View File

@ -10,7 +10,7 @@ package ca.uhn.fhir.okhttp.client;
* 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
* 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,
@ -38,96 +38,101 @@ import java.util.Map;
*/
public class OkHttpRestfulResponse implements IHttpResponse {
private Response myResponse;
private boolean myEntityBuffered = false;
private byte[] myEntityBytes;
private boolean myEntityBuffered = false;
private byte[] myEntityBytes;
private Response myResponse;
public OkHttpRestfulResponse(Response theResponse) {
this.myResponse = theResponse;
}
public OkHttpRestfulResponse(Response theResponse) {
this.myResponse = theResponse;
}
@Override
public int getStatus() {
return myResponse.code();
}
@Override
public void bufferEntitity() throws IOException {
bufferEntity();
}
@Override
public Object getResponse() {
return myResponse;
}
@Override
public void bufferEntity() throws IOException {
if (myEntityBuffered) {
return;
}
InputStream responseEntity = readEntity();
if (responseEntity != null) {
myEntityBuffered = true;
try {
myEntityBytes = IOUtils.toByteArray(responseEntity);
} catch (IllegalStateException e) {
throw new InternalErrorException(e);
}
}
}
@Override
public String getMimeType() {
String contentType = myResponse.header(Constants.HEADER_CONTENT_TYPE);
MediaType mediaType = null;
if (contentType == null) {
if (myResponse.body() != null) {
mediaType = myResponse.body().contentType();
}
} else {
mediaType = MediaType.parse(contentType);
}
if (mediaType == null) {
return null;
}
@Override
public void close() {
myResponse.close();
}
return typeAndSubtypeOnly(mediaType).toString();
}
@Override
public Reader createReader() throws IOException {
if (!myEntityBuffered && myResponse.body() == null) {
return new StringReader("");
} else {
return new InputStreamReader(readEntity());
}
}
private MediaType typeAndSubtypeOnly(MediaType input) {
return MediaType.parse(input.type() + "/" + input.subtype());
}
@Override
public Map<String, List<String>> getAllHeaders() {
return myResponse.headers().toMultimap();
}
@Override
public Map<String, List<String>> getAllHeaders() {
return myResponse.headers().toMultimap();
}
@Override
public String getMimeType() {
String contentType = myResponse.header(Constants.HEADER_CONTENT_TYPE);
MediaType mediaType = null;
if (contentType == null) {
if (myResponse.body() != null) {
mediaType = myResponse.body().contentType();
}
} else {
mediaType = MediaType.parse(contentType);
}
@Override
public String getStatusInfo() {
return myResponse.message();
}
if (mediaType == null) {
return null;
}
@Override
public Reader createReader() throws IOException {
if (!myEntityBuffered && myResponse.body() == null) {
return new StringReader("");
} else {
return new InputStreamReader(readEntity());
}
}
return typeAndSubtypeOnly(mediaType).toString();
}
@Override
public InputStream readEntity() throws IOException {
if (this.myEntityBuffered) {
return new ByteArrayInputStream(myEntityBytes);
} else if (myResponse.body() != null) {
return myResponse.body().byteStream();
} else {
return null;
}
}
@Override
public Object getResponse() {
return myResponse;
}
@Override
public void close() {
myResponse.close();
}
@Override
public int getStatus() {
return myResponse.code();
}
@Override
public void bufferEntitity() throws IOException {
if (myEntityBuffered) {
return;
}
InputStream responseEntity = readEntity();
if (responseEntity != null) {
myEntityBuffered = true;
try {
myEntityBytes = IOUtils.toByteArray(responseEntity);
} catch (IllegalStateException e) {
throw new InternalErrorException(e);
}
}
}
@Override
public String getStatusInfo() {
return myResponse.message();
}
@Override
public InputStream readEntity() throws IOException {
if (this.myEntityBuffered) {
return new ByteArrayInputStream(myEntityBytes);
} else if (myResponse.body() != null) {
return myResponse.body().byteStream();
} else {
return null;
}
}
private MediaType typeAndSubtypeOnly(MediaType input) {
return MediaType.parse(input.type() + "/" + input.subtype());
}
}

View File

@ -1,5 +1,7 @@
package ca.uhn.fhir.jaxrs.client;
import java.io.IOException;
/*
* #%L
* HAPI FHIR JAX-RS Server
@ -39,31 +41,40 @@ import ca.uhn.fhir.rest.client.api.IHttpResponse;
*/
public class JaxRsHttpResponse implements IHttpResponse {
private final Response myResponse;
private boolean myBufferedEntity = false;
private final Response myResponse;
public JaxRsHttpResponse(Response theResponse) {
this.myResponse = theResponse;
}
@Override
public Response getResponse() {
return myResponse;
public void bufferEntitity() throws IOException {
bufferEntity();
}
@Override
public int getStatus() {
return myResponse.getStatus();
}
@Override
public String getMimeType() {
MediaType mediaType = myResponse.getMediaType();
if (mediaType == null) {
return null;
public void bufferEntity() throws IOException {
if(!myBufferedEntity && myResponse.hasEntity()) {
myBufferedEntity = true;
myResponse.bufferEntity();
} else {
myResponse.bufferEntity();
}
}
@Override
public void close() {
// automatically done by jax-rs
}
@Override
public Reader createReader() {
if (!myBufferedEntity && !myResponse.hasEntity()) {
return new StringReader("");
} else {
return new StringReader(myResponse.readEntity(String.class));
}
//Keep only type and subtype and do not include the parameters such as charset
return new MediaType(mediaType.getType(), mediaType.getSubtype()).toString();
}
@Override
@ -75,39 +86,35 @@ public class JaxRsHttpResponse implements IHttpResponse {
return theHeaders;
}
@Override
public String getMimeType() {
MediaType mediaType = myResponse.getMediaType();
if (mediaType == null) {
return null;
}
//Keep only type and subtype and do not include the parameters such as charset
return new MediaType(mediaType.getType(), mediaType.getSubtype()).toString();
}
@Override
public Response getResponse() {
return myResponse;
}
@Override
public int getStatus() {
return myResponse.getStatus();
}
@Override
public String getStatusInfo() {
return myResponse.getStatusInfo().getReasonPhrase();
}
@Override
public Reader createReader() {
if (!myBufferedEntity && !myResponse.hasEntity()) {
return new StringReader("");
} else {
return new StringReader(myResponse.readEntity(String.class));
}
}
@Override
public InputStream readEntity() {
return myResponse.readEntity(java.io.InputStream.class);
}
@Override
public void bufferEntitity() {
if(!myBufferedEntity && myResponse.hasEntity()) {
myBufferedEntity = true;
myResponse.bufferEntity();
} else {
myResponse.bufferEntity();
}
}
@Override
public void close() {
// automatically done by jax-rs
}

View File

@ -6,6 +6,5 @@
<wb-resource deploy-path="/" source-path="/target/generated-sources/tinder"/>
<wb-resource deploy-path="/" source-path="/src/test/java"/>
<wb-resource deploy-path="/" source-path="/src/test/resources"/>
<wb-resource deploy-path="/" source-path="/target/generated-sql"/>
</wb-module>
</project-modules>

View File

@ -157,6 +157,10 @@
GitHub user @mattiuusitalo for reporting and supplying
a test case!
</action>
<action type="fix">
Correct a typo in client
<![CDATA[<code>IHttpRequest</code>]]> class: "bufferEntitity" should be "bufferEntity".
</action>
</release>
<release version="2.1" date="2016-11-11">
<action type="add">