diff --git a/src/main/java/com/ning/http/client/providers/apache/ApacheResponse.java b/src/main/java/com/ning/http/client/providers/apache/ApacheResponse.java index 9516f89ee4..2cdf19f629 100644 --- a/src/main/java/com/ning/http/client/providers/apache/ApacheResponse.java +++ b/src/main/java/com/ning/http/client/providers/apache/ApacheResponse.java @@ -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 bodyParts; private final HttpResponseHeaders headers; private final HttpResponseStatus status; - private final List cookies = new ArrayList(); + private List cookies; public ApacheResponse(HttpResponseStatus status, HttpResponseHeaders headers, @@ -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()); @@ -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 */ @@ -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 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. 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 */ @@ -172,21 +154,23 @@ public boolean isRedirected() { public List getCookies() { if (headers == null) { - throw new IllegalStateException(HEADERS_NOT_COMPUTED); + return Collections.emptyList(); } - if (cookies.isEmpty()) { + if (cookies == null) { + List localCookies = new ArrayList(); for (Map.Entry> header : headers.getHeaders().entrySet()) { if (header.getKey().equalsIgnoreCase("Set-Cookie")) { // TODO: ask for parsed header List 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; } /** @@ -194,7 +178,7 @@ public List getCookies() { */ /* @Override */ public boolean hasResponseStatus() { - return (bodyParts != null ? true : false); + return bodyParts != null; } /** @@ -202,7 +186,7 @@ public boolean hasResponseStatus() { */ /* @Override */ public boolean hasResponseHeaders() { - return (headers != null ? true : false); + return headers != null; } /** @@ -210,6 +194,6 @@ public boolean hasResponseHeaders() { */ /* @Override */ public boolean hasResponseBody() { - return (bodyParts != null && bodyParts.size() > 0 ? true : false); + return bodyParts != null && !bodyParts.isEmpty(); } } diff --git a/src/main/java/com/ning/http/client/providers/grizzly/GrizzlyResponse.java b/src/main/java/com/ning/http/client/providers/grizzly/GrizzlyResponse.java index 52f3fece36..1f0b52784d 100644 --- a/src/main/java/com/ning/http/client/providers/grizzly/GrizzlyResponse.java +++ b/src/main/java/com/ning/http/client/providers/grizzly/GrizzlyResponse.java @@ -257,7 +257,7 @@ public List getCookies() { cookies = convertCookies(builder.build()); } else { - cookies = Collections.unmodifiableList(Collections.emptyList()); + cookies = Collections.emptyList(); } } return cookies; diff --git a/src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java b/src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java index f1fd4d2ce1..36700fde8d 100644 --- a/src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java +++ b/src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java @@ -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 bodyParts; private final HttpResponseHeaders headers; private final HttpResponseStatus status; - private final List cookies = new ArrayList(); + private List cookies; private AtomicBoolean contentComputed = new AtomicBoolean(false); private String content; @@ -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()); @@ -116,14 +107,7 @@ 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); @@ -131,6 +115,17 @@ public String getResponseBodyExcerpt(int maxLength, String charset) throws IOExc 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 */ @@ -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 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. 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 */ @@ -184,21 +167,23 @@ public boolean isRedirected() { public List getCookies() { if (headers == null) { - throw new IllegalStateException(HEADERS_NOT_COMPUTED); + return Collections.emptyList(); } if (cookies.isEmpty()) { + List localCookies = new ArrayList(); for (Map.Entry> header : headers.getHeaders().entrySet()) { if (header.getKey().equalsIgnoreCase("Set-Cookie")) { // TODO: ask for parsed header List 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; } /** @@ -206,7 +191,7 @@ public List getCookies() { */ /* @Override */ public boolean hasResponseStatus() { - return (bodyParts != null ? true : false); + return bodyParts != null; } /** @@ -214,7 +199,7 @@ public boolean hasResponseStatus() { */ /* @Override */ public boolean hasResponseHeaders() { - return (headers != null ? true : false); + return headers != null; } /** @@ -222,6 +207,6 @@ public boolean hasResponseHeaders() { */ /* @Override */ public boolean hasResponseBody() { - return (bodyParts != null && bodyParts.size() > 0 ? true : false); + return bodyParts != null && !bodyParts.isEmpty(); } } diff --git a/src/main/java/com/ning/http/client/providers/netty/NettyResponse.java b/src/main/java/com/ning/http/client/providers/netty/NettyResponse.java index 5af231d626..01c61d0165 100644 --- a/src/main/java/com/ning/http/client/providers/netty/NettyResponse.java +++ b/src/main/java/com/ning/http/client/providers/netty/NettyResponse.java @@ -41,13 +41,12 @@ */ public class NettyResponse 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 bodyParts; private final HttpResponseHeaders headers; private final HttpResponseStatus status; - private final List cookies = new ArrayList(); + private List cookies; public NettyResponse(HttpResponseStatus status, HttpResponseHeaders headers, @@ -82,16 +81,7 @@ 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 */ @@ -116,17 +106,19 @@ 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); - } - + String response = AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset)); + return response.length() <= maxLength ? response : response.substring(0, maxLength); + } + + private String computeCharset(String charset) { + String contentType = getContentType(); if (charset == null) { - charset = DEFAULT_CHARSET; + if (contentType != null) + charset = AsyncHttpProviderUtils.parseCharset(contentType); + else + charset = DEFAULT_CHARSET; } - - String response = AsyncHttpProviderUtils.contentToString(bodyParts, charset); - return response.length() <= maxLength ? response : response.substring(0, maxLength); + return charset; } /* @Override */ @@ -138,37 +130,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 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. 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 */ @@ -181,21 +161,23 @@ public boolean isRedirected() { public List getCookies() { if (headers == null) { - throw new IllegalStateException(HEADERS_NOT_COMPUTED); + return Collections.emptyList(); } - if (cookies.isEmpty()) { + if (cookies == null) { + List localCookies = new ArrayList(); for (Map.Entry> header : headers.getHeaders().entrySet()) { if (header.getKey().equalsIgnoreCase("Set-Cookie")) { // TODO: ask for parsed header List 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; } /** @@ -203,7 +185,7 @@ public List getCookies() { */ /* @Override */ public boolean hasResponseStatus() { - return (status != null ? true : false); + return status != null; } /** @@ -211,7 +193,7 @@ public boolean hasResponseStatus() { */ /* @Override */ public boolean hasResponseHeaders() { - return (headers != null ? true : false); + return headers != null; } /** @@ -219,7 +201,7 @@ public boolean hasResponseHeaders() { */ /* @Override */ public boolean hasResponseBody() { - return (bodyParts != null && bodyParts.size() > 0 ? true : false); + return bodyParts != null && !bodyParts.isEmpty(); } }