From 4beb34d2602e980917c4ef8e1becdf8229ba2867 Mon Sep 17 00:00:00 2001 From: Stephane Landelle Date: Tue, 18 Dec 2012 00:07:29 +0100 Subject: [PATCH 1/3] Fix proposal for #182 against 1.7.x --- .../providers/apache/ApacheResponse.java | 23 ++++--------------- .../client/providers/jdk/JDKResponse.java | 23 ++++--------------- .../client/providers/netty/NettyResponse.java | 23 ++++--------------- 3 files changed, 15 insertions(+), 54 deletions(-) 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..3fe9cd81b3 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,7 +35,6 @@ 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; @@ -129,37 +128,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 headers != null? headers.getHeaders().getFirstValue("Content-Type"): null; } /* @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,7 +159,7 @@ public boolean isRedirected() { public List getCookies() { if (headers == null) { - throw new IllegalStateException(HEADERS_NOT_COMPUTED); + return Collections.emptyList(); } if (cookies.isEmpty()) { for (Map.Entry> header : headers.getHeaders().entrySet()) { 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..392b99a44e 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,7 +36,6 @@ 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; @@ -141,37 +140,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 headers != null? headers.getHeaders().getFirstValue("Content-Type"): null; } /* @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,7 +171,7 @@ public boolean isRedirected() { public List getCookies() { if (headers == null) { - throw new IllegalStateException(HEADERS_NOT_COMPUTED); + return Collections.emptyList(); } if (cookies.isEmpty()) { for (Map.Entry> header : headers.getHeaders().entrySet()) { 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..12d41f9993 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,7 +41,6 @@ */ 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; @@ -138,37 +137,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 headers != null? headers.getHeaders().getFirstValue("Content-Type"): null; } /* @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,7 +168,7 @@ public boolean isRedirected() { public List getCookies() { if (headers == null) { - throw new IllegalStateException(HEADERS_NOT_COMPUTED); + return Collections.emptyList(); } if (cookies.isEmpty()) { for (Map.Entry> header : headers.getHeaders().entrySet()) { From c1c4bd130e837c820fc2df0b44f1e95b9fb772e2 Mon Sep 17 00:00:00 2001 From: Stephane Landelle Date: Tue, 18 Dec 2012 00:18:22 +0100 Subject: [PATCH 2/3] Minor clean up --- .../providers/apache/ApacheResponse.java | 24 ++++++------ .../providers/grizzly/GrizzlyResponse.java | 2 +- .../client/providers/jdk/JDKResponse.java | 29 ++++++++------- .../client/providers/netty/NettyResponse.java | 37 ++++++++----------- 4 files changed, 45 insertions(+), 47 deletions(-) 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 3fe9cd81b3..b5d31083f8 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 @@ -76,16 +76,18 @@ public String getResponseBody() throws IOException { } public String getResponseBody(String charset) throws IOException { - String contentType = getContentType(); - if (contentType != null && charset == null) { - charset = AsyncHttpProviderUtils.parseCharset(contentType); - } - + return AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset)); + } + + 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; } - - return AsyncHttpProviderUtils.contentToString(bodyParts, charset); + return charset; } /* @Override */ @@ -181,7 +183,7 @@ public List getCookies() { */ /* @Override */ public boolean hasResponseStatus() { - return (bodyParts != null ? true : false); + return bodyParts != null; } /** @@ -189,7 +191,7 @@ public boolean hasResponseStatus() { */ /* @Override */ public boolean hasResponseHeaders() { - return (headers != null ? true : false); + return headers != null; } /** @@ -197,6 +199,6 @@ public boolean hasResponseHeaders() { */ /* @Override */ public boolean hasResponseBody() { - return (bodyParts != null && bodyParts.size() > 0 ? true : false); + return bodyParts != null && bodyParts.size() > 0; } } 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 392b99a44e..5854a12126 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 @@ -80,20 +80,23 @@ 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; } + + 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 */ public InputStream getResponseBodyAsStream() throws IOException { @@ -193,7 +196,7 @@ public List getCookies() { */ /* @Override */ public boolean hasResponseStatus() { - return (bodyParts != null ? true : false); + return bodyParts != null; } /** @@ -201,7 +204,7 @@ public boolean hasResponseStatus() { */ /* @Override */ public boolean hasResponseHeaders() { - return (headers != null ? true : false); + return headers != null; } /** @@ -209,6 +212,6 @@ public boolean hasResponseHeaders() { */ /* @Override */ public boolean hasResponseBody() { - return (bodyParts != null && bodyParts.size() > 0 ? true : false); + return bodyParts != null && bodyParts.size() > 0; } } 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 12d41f9993..28f9eab2b6 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 @@ -81,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 */ @@ -115,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 */ @@ -190,7 +183,7 @@ public List getCookies() { */ /* @Override */ public boolean hasResponseStatus() { - return (status != null ? true : false); + return status != null; } /** @@ -198,7 +191,7 @@ public boolean hasResponseStatus() { */ /* @Override */ public boolean hasResponseHeaders() { - return (headers != null ? true : false); + return headers != null; } /** @@ -206,7 +199,7 @@ public boolean hasResponseHeaders() { */ /* @Override */ public boolean hasResponseBody() { - return (bodyParts != null && bodyParts.size() > 0 ? true : false); + return bodyParts != null && bodyParts.size() > 0; } } From 39cb971635c02473662373ece9cb4a85343f01ba Mon Sep 17 00:00:00 2001 From: Stephane Landelle Date: Tue, 18 Dec 2012 01:02:18 +0100 Subject: [PATCH 3/3] Minor clean up --- .../providers/apache/ApacheResponse.java | 47 +++++++++---------- .../client/providers/jdk/JDKResponse.java | 45 ++++++++---------- .../client/providers/netty/NettyResponse.java | 14 +++--- 3 files changed, 49 insertions(+), 57 deletions(-) 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 b5d31083f8..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 @@ -40,7 +40,7 @@ public class ApacheResponse implements Response { 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, @@ -79,20 +79,9 @@ public String getResponseBody(String charset) throws IOException { return AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset)); } - 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 */ 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()); @@ -108,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 */ @@ -130,7 +123,7 @@ public URI getUri() throws MalformedURLException { /* @Override */ public String getContentType() { - return headers != null? headers.getHeaders().getFirstValue("Content-Type"): null; + return getHeader("Content-Type"); } /* @Override */ @@ -163,19 +156,21 @@ public List getCookies() { if (headers == null) { 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; } /** @@ -199,6 +194,6 @@ public boolean hasResponseHeaders() { */ /* @Override */ public boolean hasResponseBody() { - return bodyParts != null && bodyParts.size() > 0; + return bodyParts != null && !bodyParts.isEmpty(); } } 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 5854a12126..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 @@ -41,7 +41,7 @@ public class JDKResponse implements Response { 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; @@ -87,24 +87,13 @@ public String getResponseBody(String charset) throws IOException { return content; } - 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 */ 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()); @@ -118,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); @@ -133,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 */ @@ -143,7 +136,7 @@ public URI getUri() throws MalformedURLException { /* @Override */ public String getContentType() { - return headers != null? headers.getHeaders().getFirstValue("Content-Type"): null; + return getHeader("Content-Type"); } /* @Override */ @@ -177,18 +170,20 @@ public List getCookies() { 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; } /** @@ -212,6 +207,6 @@ public boolean hasResponseHeaders() { */ /* @Override */ public boolean hasResponseBody() { - return bodyParts != null && bodyParts.size() > 0; + 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 28f9eab2b6..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 @@ -46,7 +46,7 @@ public class NettyResponse implements Response { 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, @@ -130,7 +130,7 @@ public URI getUri() throws MalformedURLException { /* @Override */ public String getContentType() { - return headers != null? headers.getHeaders().getFirstValue("Content-Type"): null; + return getHeader("Content-Type"); } /* @Override */ @@ -163,19 +163,21 @@ public List getCookies() { if (headers == null) { 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; } /** @@ -199,7 +201,7 @@ public boolean hasResponseHeaders() { */ /* @Override */ public boolean hasResponseBody() { - return bodyParts != null && bodyParts.size() > 0; + return bodyParts != null && !bodyParts.isEmpty(); } }