Skip to content

Commit

Permalink
Merge pull request #17 from Nosto/remove-consumable-check
Browse files Browse the repository at this point in the history
Remove consumable check
  • Loading branch information
mridang committed Apr 27, 2016
2 parents 22ccc7c + 1de7fb2 commit bfc2f86
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 42 deletions.
1 change: 1 addition & 0 deletions framework/src/play/libs/WS.java
Original file line number Diff line number Diff line change
Expand Up @@ -756,5 +756,6 @@ public JsonElement getJson() {
}
}

public abstract byte[] getBytes();
}
}
62 changes: 20 additions & 42 deletions framework/src/play/libs/ws/WSAsync.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package play.libs.ws;

import javax.net.ssl.SSLContext;

import org.w3c.dom.Document;
import org.apache.commons.lang.NotImplementedException;

import com.ning.http.client.*;
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
import com.ning.http.client.AsyncHttpClientConfig.Builder;
Expand Down Expand Up @@ -616,8 +611,6 @@ public static class HttpAsyncResponse extends HttpResponse {

private Response response;

private boolean consumed;

/**
* you shouldnt have to create an HttpResponse yourself
*
Expand Down Expand Up @@ -662,67 +655,52 @@ public List<Header> getHeaders() {
return result;
}

/**
* get the response as a stream
*
* @return an inputstream
*/
@Override
public InputStream getStream() {
checkConsumed();
public String getString() {
try {
return response.getResponseBodyAsStream();
} catch (IllegalStateException e) {
return new ByteArrayInputStream(new byte[] {}); // Workaround
// AHC's bug on
// empty
// responses
return response.getResponseBody(getEncoding());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public Document getXml(String encoding) {
public String getString(String encoding) {
try {
checkConsumed();
return super.getXml(encoding);
} finally {
consumed = true;
return response.getResponseBody(encoding);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public String getString() {
public byte[] getBytes() {
try {
checkConsumed();
return response.getResponseBody();
return response.getResponseBodyAsBytes();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
consumed = true;
}
}

/**
* get the response as a stream
*
* @return an inputstream
*/
@Override
public String getString(String encoding) {
public InputStream getStream() {
try {
checkConsumed();
return response.getResponseBody(encoding);
return response.getResponseBodyAsStream();
} catch (IllegalStateException e) {
return new ByteArrayInputStream(new byte[] {}); // Workaround
// AHC's bug on
// empty
// responses
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
consumed = true;
}
}

private void checkConsumed() {
if (consumed) {
throw new IllegalStateException("Stream already consumed");
}
}


}

private static class WSOAuthConsumer extends AbstractOAuthConsumer {
Expand Down
5 changes: 5 additions & 0 deletions framework/src/play/libs/ws/WSUrlFetch.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ public String getString(String encoding) {
}
}

@Override
public byte[] getBytes() {
return body.getBytes();
}

/**
* get the response as a stream
*
Expand Down
5 changes: 5 additions & 0 deletions framework/test-src/play/libs/TestHttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public InputStream getStream() {
return null;
}

@Override
public byte[] getBytes() {
return this.queryContent.getBytes();
}

}

0 comments on commit bfc2f86

Please sign in to comment.