Skip to content

Fix proposal for #182 against 1.7.x #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@

public class ApacheResponse implements Response {
private final static String DEFAULT_CHARSET = "ISO-8859-1";
private final static String HEADERS_NOT_COMPUTED = "Response's headers hasn't been computed by your AsyncHandler.";

private final URI uri;
private final Collection<HttpResponseBodyPart> bodyParts;
private final HttpResponseHeaders headers;
private final HttpResponseStatus status;
private final List<Cookie> cookies = new ArrayList<Cookie>();
private List<Cookie> cookies;

public ApacheResponse(HttpResponseStatus status,
HttpResponseHeaders headers,
Expand Down Expand Up @@ -77,21 +76,12 @@ public String getResponseBody() throws IOException {
}

public String getResponseBody(String charset) throws IOException {
String contentType = getContentType();
if (contentType != null && charset == null) {
charset = AsyncHttpProviderUtils.parseCharset(contentType);
}

if (charset == null) {
charset = DEFAULT_CHARSET;
}

return AsyncHttpProviderUtils.contentToString(bodyParts, charset);
return AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset));
}

/* @Override */
public InputStream getResponseBodyAsStream() throws IOException {
if (bodyParts.size() > 0) {
if (!bodyParts.isEmpty()) {
return new HttpResponseBodyPartsInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()]));
} else {
return new ByteArrayInputStream("".getBytes());
Expand All @@ -107,18 +97,22 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
/* @Override */

public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
String contentType = getContentType();
if (contentType != null && charset == null) {
charset = AsyncHttpProviderUtils.parseCharset(contentType);
}

if (charset == null) {
charset = DEFAULT_CHARSET;
}
charset = computeCharset(charset);

String response = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
return response.length() <= maxLength ? response : response.substring(0, maxLength);
}

private String computeCharset(String charset) {
String contentType = getContentType();
if (charset == null) {
if (contentType != null)
charset = AsyncHttpProviderUtils.parseCharset(contentType);
else
charset = DEFAULT_CHARSET;
}
return charset;
}

/* @Override */

Expand All @@ -129,37 +123,25 @@ public URI getUri() throws MalformedURLException {
/* @Override */

public String getContentType() {
if (headers == null) {
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
}
return headers.getHeaders().getFirstValue("Content-Type");
return getHeader("Content-Type");
}

/* @Override */

public String getHeader(String name) {
if (headers == null) {
throw new IllegalStateException();
}
return headers.getHeaders().getFirstValue(name);
return headers != null? headers.getHeaders().getFirstValue(name): null;
}

/* @Override */

public List<String> getHeaders(String name) {
if (headers == null) {
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
}
return headers.getHeaders().get(name);
return headers != null? headers.getHeaders().get(name): Collections.<String> emptyList();
}

/* @Override */

public FluentCaseInsensitiveStringsMap getHeaders() {
if (headers == null) {
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
}
return headers.getHeaders();
return headers != null? headers.getHeaders(): new FluentCaseInsensitiveStringsMap();
}

/* @Override */
Expand All @@ -172,44 +154,46 @@ public boolean isRedirected() {

public List<Cookie> getCookies() {
if (headers == null) {
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
return Collections.emptyList();
}
if (cookies.isEmpty()) {
if (cookies == null) {
List<Cookie> localCookies = new ArrayList<Cookie>();
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
// TODO: ask for parsed header
List<String> v = header.getValue();
for (String value : v) {
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
cookies.add(cookie);
localCookies.add(cookie);
}
}
}
cookies = Collections.unmodifiableList(localCookies);
}
return Collections.unmodifiableList(cookies);
return cookies;
}

/**
* {@inheritDoc}
*/
/* @Override */
public boolean hasResponseStatus() {
return (bodyParts != null ? true : false);
return bodyParts != null;
}

/**
* {@inheritDoc}
*/
/* @Override */
public boolean hasResponseHeaders() {
return (headers != null ? true : false);
return headers != null;
}

/**
* {@inheritDoc}
*/
/* @Override */
public boolean hasResponseBody() {
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
return bodyParts != null && !bodyParts.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public List<Cookie> getCookies() {
cookies = convertCookies(builder.build());

} else {
cookies = Collections.unmodifiableList(Collections.<Cookie>emptyList());
cookies = Collections.emptyList();
}
}
return cookies;
Expand Down
73 changes: 29 additions & 44 deletions src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@

public class JDKResponse implements Response {
private final static String DEFAULT_CHARSET = "ISO-8859-1";
private final static String HEADERS_NOT_COMPUTED = "Response's headers hasn't been computed by your AsyncHandler.";

private final URI uri;
private final Collection<HttpResponseBodyPart> bodyParts;
private final HttpResponseHeaders headers;
private final HttpResponseStatus status;
private final List<Cookie> cookies = new ArrayList<Cookie>();
private List<Cookie> cookies;
private AtomicBoolean contentComputed = new AtomicBoolean(false);
private String content;

Expand Down Expand Up @@ -81,28 +80,20 @@ public byte[] getResponseBodyAsBytes() throws IOException {
}

public String getResponseBody(String charset) throws IOException {
String contentType = getContentType();
if (contentType != null && charset == null) {
charset = AsyncHttpProviderUtils.parseCharset(contentType);
}

if (charset == null) {
charset = DEFAULT_CHARSET;
}

if (!contentComputed.get()) {
content = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
if (!contentComputed.get()) {
content = AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset));
}
return content;
}

/* @Override */
public InputStream getResponseBodyAsStream() throws IOException {
if (contentComputed.get()) {
return new ByteArrayInputStream(content.getBytes(DEFAULT_CHARSET));
}

if (bodyParts.size() > 0) {
if (!bodyParts.isEmpty()) {
return new HttpResponseBodyPartsInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()]));
} else {
return new ByteArrayInputStream("".getBytes());
Expand All @@ -116,21 +107,25 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
}

public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
String contentType = getContentType();
if (contentType != null && charset == null) {
charset = AsyncHttpProviderUtils.parseCharset(contentType);
}

if (charset == null) {
charset = DEFAULT_CHARSET;
}
charset = computeCharset(charset);

if (!contentComputed.get()) {
content = AsyncHttpProviderUtils.contentToString(bodyParts, charset == null ? DEFAULT_CHARSET : charset);
}

return content.length() <= maxLength ? content : content.substring(0, maxLength);
}

private String computeCharset(String charset) {
String contentType = getContentType();
if (charset == null) {
if (contentType != null)
charset = AsyncHttpProviderUtils.parseCharset(contentType);
else
charset = DEFAULT_CHARSET;
}
return charset;
}

/* @Override */

Expand All @@ -141,37 +136,25 @@ public URI getUri() throws MalformedURLException {
/* @Override */

public String getContentType() {
if (headers == null) {
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
}
return headers.getHeaders().getFirstValue("Content-Type");
return getHeader("Content-Type");
}

/* @Override */

public String getHeader(String name) {
if (headers == null) {
throw new IllegalStateException();
}
return headers.getHeaders().getFirstValue(name);
return headers != null? headers.getHeaders().getFirstValue(name): null;
}

/* @Override */

public List<String> getHeaders(String name) {
if (headers == null) {
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
}
return headers.getHeaders().get(name);
return headers != null? headers.getHeaders().get(name): Collections.<String> emptyList();
}

/* @Override */

public FluentCaseInsensitiveStringsMap getHeaders() {
if (headers == null) {
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
}
return headers.getHeaders();
return headers != null? headers.getHeaders(): new FluentCaseInsensitiveStringsMap();
}

/* @Override */
Expand All @@ -184,44 +167,46 @@ public boolean isRedirected() {

public List<Cookie> getCookies() {
if (headers == null) {
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
return Collections.emptyList();
}
if (cookies.isEmpty()) {
List<Cookie> localCookies = new ArrayList<Cookie>();
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
// TODO: ask for parsed header
List<String> v = header.getValue();
for (String value : v) {
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
cookies.add(cookie);
localCookies.add(cookie);
}
}
}
cookies = Collections.unmodifiableList(localCookies);
}
return Collections.unmodifiableList(cookies);
return cookies;
}

/**
* {@inheritDoc}
*/
/* @Override */
public boolean hasResponseStatus() {
return (bodyParts != null ? true : false);
return bodyParts != null;
}

/**
* {@inheritDoc}
*/
/* @Override */
public boolean hasResponseHeaders() {
return (headers != null ? true : false);
return headers != null;
}

/**
* {@inheritDoc}
*/
/* @Override */
public boolean hasResponseBody() {
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
return bodyParts != null && !bodyParts.isEmpty();
}
}
Loading