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 static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ApacheHttpResponse.class);
private boolean myEntityBuffered = false; private boolean myEntityBuffered = false;
private final HttpResponse myResponse;
private byte[] myEntityBytes; private byte[] myEntityBytes;
private final HttpResponse myResponse;
public ApacheHttpResponse(HttpResponse theResponse) { public ApacheHttpResponse(HttpResponse theResponse) {
this.myResponse = theResponse; this.myResponse = theResponse;
} }
@Override @Override
public HttpResponse getResponse() { public void bufferEntitity() throws IOException {
return myResponse; bufferEntity();
} }
@Override @Override
public int getStatus() { public void bufferEntity() throws IOException {
return myResponse.getStatusLine().getStatusCode(); if (myEntityBuffered) {
} return;
}
@Override InputStream respEntity = readEntity();
public String getMimeType() { if (respEntity != null) {
ContentType ct = ContentType.get(myResponse.getEntity()); this.myEntityBuffered = true;
return ct != null ? ct.getMimeType() : null; try {
} this.myEntityBytes = IOUtils.toByteArray(respEntity);
} catch (IllegalStateException e) {
@Override throw new InternalErrorException(e);
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 @Override
public String getStatusInfo() { public void close() {
return myResponse.getStatusLine().getReasonPhrase(); if (myResponse instanceof CloseableHttpResponse) {
try {
((CloseableHttpResponse) myResponse).close();
} catch (IOException e) {
ourLog.debug("Failed to close response", e);
}
}
} }
@Override @Override
@ -123,6 +116,45 @@ public class ApacheHttpResponse implements IHttpResponse {
return reader; 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 @Override
public InputStream readEntity() throws IOException { public InputStream readEntity() throws IOException {
if (this.myEntityBuffered) { if (this.myEntityBuffered) {
@ -133,31 +165,4 @@ public class ApacheHttpResponse implements IHttpResponse {
return null; 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 not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -32,8 +32,9 @@ import java.util.Map;
public interface IHttpResponse { public interface IHttpResponse {
/** /**
* Get the status code associated with the response. * Get the status code associated with the response.
* @return the response status code. *
* @return the response status code.
*/ */
public int getStatus(); public int getStatus();
@ -42,21 +43,23 @@ public interface IHttpResponse {
*/ */
Object getResponse(); Object getResponse();
/** /**
* Extracts {@code Content-Type} value from the response exactly as * Extracts {@code Content-Type} value from the response exactly as
* specified by the {@code Content-Type} header. Returns {@code null} * specified by the {@code Content-Type} header. Returns {@code null}
* if not specified. * if not specified.
*/ */
public String getMimeType(); public String getMimeType();
/** /**
* Get map of the response headers and corresponding string values. * Get map of the response headers and corresponding string values.
* @return response headers as a map header keys and they values. *
*/ * @return response headers as a map header keys and they values.
*/
public Map<String, List<String>> getAllHeaders(); 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. * @return the reason phrase.
*/ */
public String getStatusInfo(); public String getStatusInfo();
@ -67,7 +70,7 @@ public interface IHttpResponse {
public Reader createReader() throws IOException; 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; public InputStream readEntity() throws IOException;
@ -76,27 +79,35 @@ public interface IHttpResponse {
*/ */
public void close(); public void close();
/** /**
* Buffer the message entity data. * Buffer the message entity data.
* <p> * <p>
* In case the message entity is backed by an unconsumed entity input stream, * 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 * all the bytes of the original entity input stream are read and stored in a
* local buffer. The original entity input stream is consumed. * local buffer. The original entity input stream is consumed.
* </p> * </p>
* <p> * <p>
* In case the response entity instance is not backed by an unconsumed input stream * 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. * an invocation of {@code bufferEntity} method is ignored and the method returns.
* </p> * </p>
* <p> * <p>
* This operation is idempotent, i.e. it can be invoked multiple times with * This operation is idempotent, i.e. it can be invoked multiple times with
* the same effect which also means that calling the {@code bufferEntity()} * the same effect which also means that calling the {@code bufferEntity()}
* method on an already buffered (and thus closed) message instance is legal * method on an already buffered (and thus closed) message instance is legal
* and has no further effect. * and has no further effect.
* </p> * </p>
* <p> * <p>
* Buffering the message entity data allows for multiple invocations of * Buffering the message entity data allows for multiple invocations of
* {@code readEntity(...)} methods on the response instance. * {@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; 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 not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
@ -38,96 +38,101 @@ import java.util.Map;
*/ */
public class OkHttpRestfulResponse implements IHttpResponse { public class OkHttpRestfulResponse implements IHttpResponse {
private Response myResponse; private boolean myEntityBuffered = false;
private boolean myEntityBuffered = false; private byte[] myEntityBytes;
private byte[] myEntityBytes; private Response myResponse;
public OkHttpRestfulResponse(Response theResponse) { public OkHttpRestfulResponse(Response theResponse) {
this.myResponse = theResponse; this.myResponse = theResponse;
} }
@Override @Override
public int getStatus() { public void bufferEntitity() throws IOException {
return myResponse.code(); bufferEntity();
} }
@Override @Override
public Object getResponse() { public void bufferEntity() throws IOException {
return myResponse; if (myEntityBuffered) {
} return;
}
InputStream responseEntity = readEntity();
if (responseEntity != null) {
myEntityBuffered = true;
try {
myEntityBytes = IOUtils.toByteArray(responseEntity);
} catch (IllegalStateException e) {
throw new InternalErrorException(e);
}
}
}
@Override @Override
public String getMimeType() { public void close() {
String contentType = myResponse.header(Constants.HEADER_CONTENT_TYPE); myResponse.close();
MediaType mediaType = null; }
if (contentType == null) {
if (myResponse.body() != null) {
mediaType = myResponse.body().contentType();
}
} else {
mediaType = MediaType.parse(contentType);
}
if (mediaType == null) { @Override
return null; public Reader createReader() throws IOException {
} if (!myEntityBuffered && myResponse.body() == null) {
return new StringReader("");
} else {
return new InputStreamReader(readEntity());
}
}
return typeAndSubtypeOnly(mediaType).toString(); @Override
} public Map<String, List<String>> getAllHeaders() {
return myResponse.headers().toMultimap();
}
private MediaType typeAndSubtypeOnly(MediaType input) { @Override
return MediaType.parse(input.type() + "/" + input.subtype()); 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 if (mediaType == null) {
public Map<String, List<String>> getAllHeaders() { return null;
return myResponse.headers().toMultimap(); }
}
@Override return typeAndSubtypeOnly(mediaType).toString();
public String getStatusInfo() { }
return myResponse.message();
}
@Override @Override
public Reader createReader() throws IOException { public Object getResponse() {
if (!myEntityBuffered && myResponse.body() == null) { return myResponse;
return new StringReader(""); }
} else {
return new InputStreamReader(readEntity());
}
}
@Override @Override
public InputStream readEntity() throws IOException { public int getStatus() {
if (this.myEntityBuffered) { return myResponse.code();
return new ByteArrayInputStream(myEntityBytes); }
} else if (myResponse.body() != null) {
return myResponse.body().byteStream();
} else {
return null;
}
}
@Override @Override
public void close() { public String getStatusInfo() {
myResponse.close(); return myResponse.message();
} }
@Override @Override
public void bufferEntitity() throws IOException { public InputStream readEntity() throws IOException {
if (myEntityBuffered) { if (this.myEntityBuffered) {
return; return new ByteArrayInputStream(myEntityBytes);
} } else if (myResponse.body() != null) {
InputStream responseEntity = readEntity(); return myResponse.body().byteStream();
if (responseEntity != null) { } else {
myEntityBuffered = true; return null;
try { }
myEntityBytes = IOUtils.toByteArray(responseEntity); }
} catch (IllegalStateException e) {
throw new InternalErrorException(e); private MediaType typeAndSubtypeOnly(MediaType input) {
} return MediaType.parse(input.type() + "/" + input.subtype());
} }
}
} }

View File

@ -1,5 +1,7 @@
package ca.uhn.fhir.jaxrs.client; package ca.uhn.fhir.jaxrs.client;
import java.io.IOException;
/* /*
* #%L * #%L
* HAPI FHIR JAX-RS Server * HAPI FHIR JAX-RS Server
@ -39,21 +41,49 @@ import ca.uhn.fhir.rest.client.api.IHttpResponse;
*/ */
public class JaxRsHttpResponse implements IHttpResponse { public class JaxRsHttpResponse implements IHttpResponse {
private final Response myResponse;
private boolean myBufferedEntity = false; private boolean myBufferedEntity = false;
private final Response myResponse;
public JaxRsHttpResponse(Response theResponse) { public JaxRsHttpResponse(Response theResponse) {
this.myResponse = theResponse; this.myResponse = theResponse;
} }
@Override @Override
public Response getResponse() { public void bufferEntitity() throws IOException {
return myResponse; bufferEntity();
} }
@Override @Override
public int getStatus() { public void bufferEntity() throws IOException {
return myResponse.getStatus(); 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));
}
}
@Override
public Map<String, List<String>> getAllHeaders() {
Map<String, List<String>> theHeaders = new ConcurrentHashMap<String, List<String>>();
for (Entry<String, List<String>> iterable_element : myResponse.getStringHeaders().entrySet()) {
theHeaders.put(iterable_element.getKey().toLowerCase(), iterable_element.getValue());
}
return theHeaders;
} }
@Override @Override
@ -67,48 +97,25 @@ public class JaxRsHttpResponse implements IHttpResponse {
} }
@Override @Override
public Map<String, List<String>> getAllHeaders() { public Response getResponse() {
Map<String, List<String>> theHeaders = new ConcurrentHashMap<String, List<String>>(); return myResponse;
for (Entry<String, List<String>> iterable_element : myResponse.getStringHeaders().entrySet()) {
theHeaders.put(iterable_element.getKey().toLowerCase(), iterable_element.getValue());
}
return theHeaders;
} }
@Override
public int getStatus() {
return myResponse.getStatus();
}
@Override @Override
public String getStatusInfo() { public String getStatusInfo() {
return myResponse.getStatusInfo().getReasonPhrase(); return myResponse.getStatusInfo().getReasonPhrase();
} }
@Override
public Reader createReader() {
if (!myBufferedEntity && !myResponse.hasEntity()) {
return new StringReader("");
} else {
return new StringReader(myResponse.readEntity(String.class));
}
}
@Override @Override
public InputStream readEntity() { public InputStream readEntity() {
return myResponse.readEntity(java.io.InputStream.class); 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="/target/generated-sources/tinder"/>
<wb-resource deploy-path="/" source-path="/src/test/java"/> <wb-resource deploy-path="/" source-path="/src/test/java"/>
<wb-resource deploy-path="/" source-path="/src/test/resources"/> <wb-resource deploy-path="/" source-path="/src/test/resources"/>
<wb-resource deploy-path="/" source-path="/target/generated-sql"/>
</wb-module> </wb-module>
</project-modules> </project-modules>

View File

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