Skip to content

Commit ad14a83

Browse files
committed
Merge pull request #183 from slandelle/master
Fix proposal for #182 against master
2 parents d8254d3 + b514542 commit ad14a83

File tree

6 files changed

+85
-210
lines changed

6 files changed

+85
-210
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ nbproject
1515
.DS_Store
1616
target
1717
test-output
18-
/META-INF/MANIFEST.MF
18+
MANIFEST.MF
1919
work
2020
atlassian-ide-plugin.xml

api/src/main/java/com/ning/http/client/providers/ResponseBase.java

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import java.io.IOException;
44
import java.io.InputStream;
55
import java.net.URI;
6+
import java.util.Collections;
67
import java.util.List;
78

9+
import com.ning.http.client.Cookie;
810
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
911
import com.ning.http.client.HttpResponseBodyPart;
1012
import com.ning.http.client.HttpResponseHeaders;
@@ -15,11 +17,11 @@
1517
public abstract class ResponseBase implements Response
1618
{
1719
protected final static String DEFAULT_CHARSET = "ISO-8859-1";
18-
protected final static String HEADERS_NOT_COMPUTED = "Response's headers hasn't been computed by your AsyncHandler.";
1920

2021
protected final List<HttpResponseBodyPart> bodyParts;
2122
protected final HttpResponseHeaders headers;
2223
protected final HttpResponseStatus status;
24+
private List<Cookie> cookies;
2325

2426
protected ResponseBase(HttpResponseStatus status,
2527
HttpResponseHeaders headers,
@@ -48,25 +50,22 @@ public final URI getUri() /*throws MalformedURLException*/ {
4850

4951
/* @Override */
5052
public final String getContentType() {
51-
return getHeader("Content-Type");
53+
return headers != null? getHeader("Content-Type"): null;
5254
}
5355

5456
/* @Override */
5557
public final String getHeader(String name) {
56-
return getHeaders().getFirstValue(name);
58+
return headers != null? getHeaders().getFirstValue(name): null;
5759
}
5860

5961
/* @Override */
6062
public final List<String> getHeaders(String name) {
61-
return getHeaders().get(name);
63+
return headers != null? getHeaders().get(name): null;
6264
}
6365

6466
/* @Override */
6567
public final FluentCaseInsensitiveStringsMap getHeaders() {
66-
if (headers == null) {
67-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
68-
}
69-
return headers.getHeaders();
68+
return headers != null? headers.getHeaders(): new FluentCaseInsensitiveStringsMap();
7069
}
7170

7271
/* @Override */
@@ -85,34 +84,52 @@ public String getResponseBody() throws IOException {
8584
}
8685

8786
public String getResponseBody(String charset) throws IOException {
88-
String contentType = getContentType();
89-
if (contentType != null && charset == null) {
90-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
91-
}
92-
93-
if (charset == null) {
94-
charset = DEFAULT_CHARSET;
95-
}
96-
97-
return AsyncHttpProviderUtils.contentToString(bodyParts, charset);
87+
return AsyncHttpProviderUtils.contentToString(bodyParts, calculateCharset(charset));
9888
}
9989

10090
/* @Override */
10191
public InputStream getResponseBodyAsStream() throws IOException {
10292
return AsyncHttpProviderUtils.contentAsStream(bodyParts);
10393
}
94+
95+
protected abstract List<Cookie> buildCookies();
96+
97+
public List<Cookie> getCookies() {
10498

105-
protected String calculateCharset() {
106-
String charset = null;
107-
String contentType = getContentType();
108-
if (contentType != null) {
109-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
99+
if (headers == null) {
100+
return Collections.emptyList();
110101
}
111102

112-
if (charset == null) {
113-
charset = DEFAULT_CHARSET;
103+
if (cookies == null) {
104+
cookies = buildCookies();
114105
}
106+
return cookies;
107+
108+
}
109+
110+
protected String calculateCharset(String charset) {
111+
112+
if (charset == null) {
113+
String contentType = getContentType();
114+
if (contentType != null) {
115+
charset = AsyncHttpProviderUtils.parseCharset(contentType);
116+
} else {
117+
charset = DEFAULT_CHARSET;
118+
}
119+
}
120+
115121
return charset;
116122
}
117123

124+
public boolean hasResponseStatus() {
125+
return status != null;
126+
}
127+
128+
public boolean hasResponseHeaders() {
129+
return headers != null && !headers.getHeaders().isEmpty();
130+
}
131+
132+
public boolean hasResponseBody() {
133+
return bodyParts != null && !bodyParts.isEmpty();
134+
}
118135
}

api/src/main/java/com/ning/http/client/providers/jdk/JDKResponse.java

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package com.ning.http.client.providers.jdk;
1414

1515
import com.ning.http.client.Cookie;
16-
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
1716
import com.ning.http.client.HttpResponseBodyPart;
1817
import com.ning.http.client.HttpResponseHeaders;
1918
import com.ning.http.client.HttpResponseStatus;
@@ -27,7 +26,6 @@
2726
import java.util.Map;
2827

2928
public class JDKResponse extends ResponseBase {
30-
private final List<Cookie> cookies = new ArrayList<Cookie>();
3129

3230
public JDKResponse(HttpResponseStatus status,
3331
HttpResponseHeaders headers,
@@ -48,46 +46,18 @@ public String getResponseBodyExcerpt(int maxLength, String charset) throws IOExc
4846
}
4947

5048
/* @Override */
51-
public List<Cookie> getCookies() {
52-
if (headers == null) {
53-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
54-
}
55-
if (cookies.isEmpty()) {
56-
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
57-
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
58-
// TODO: ask for parsed header
59-
List<String> v = header.getValue();
60-
for (String value : v) {
61-
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
62-
cookies.add(cookie);
63-
}
49+
public List<Cookie> buildCookies() {
50+
List<Cookie> cookies = new ArrayList<Cookie>();
51+
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
52+
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
53+
// TODO: ask for parsed header
54+
List<String> v = header.getValue();
55+
for (String value : v) {
56+
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
57+
cookies.add(cookie);
6458
}
6559
}
6660
}
6761
return Collections.unmodifiableList(cookies);
6862
}
69-
70-
/**
71-
* {@inheritDoc}
72-
*/
73-
/* @Override */
74-
public boolean hasResponseStatus() {
75-
return (bodyParts != null ? true : false);
76-
}
77-
78-
/**
79-
* {@inheritDoc}
80-
*/
81-
/* @Override */
82-
public boolean hasResponseHeaders() {
83-
return (headers != null ? true : false);
84-
}
85-
86-
/**
87-
* {@inheritDoc}
88-
*/
89-
/* @Override */
90-
public boolean hasResponseBody() {
91-
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
92-
}
9363
}

providers/apache/src/main/java/com/ning/http/client/providers/apache/ApacheResponse.java

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@
2626
import java.util.Map;
2727

2828
public class ApacheResponse extends ResponseBase {
29-
private final static String HEADERS_NOT_COMPUTED = "Response's headers hasn't been computed by your AsyncHandler.";
3029

31-
private final List<Cookie> cookies = new ArrayList<Cookie>();
32-
33-
public ApacheResponse(HttpResponseStatus status,
30+
public ApacheResponse(HttpResponseStatus status,
3431
HttpResponseHeaders headers,
3532
List<HttpResponseBodyPart> bodyParts) {
3633
super(status, headers, bodyParts);
@@ -45,60 +42,24 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
4542
/* @Override */
4643

4744
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
48-
String contentType = getContentType();
49-
if (contentType != null && charset == null) {
50-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
51-
}
52-
53-
if (charset == null) {
54-
charset = DEFAULT_CHARSET;
55-
}
56-
45+
charset = calculateCharset(charset);
5746
String response = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
5847
return response.length() <= maxLength ? response : response.substring(0, maxLength);
5948
}
6049

6150
/* @Override */
62-
public List<Cookie> getCookies() {
63-
if (headers == null) {
64-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
65-
}
66-
if (cookies.isEmpty()) {
67-
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
68-
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
69-
// TODO: ask for parsed header
70-
List<String> v = header.getValue();
71-
for (String value : v) {
72-
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
73-
cookies.add(cookie);
74-
}
51+
public List<Cookie> buildCookies() {
52+
List<Cookie> cookies = new ArrayList<Cookie>();
53+
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
54+
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
55+
// TODO: ask for parsed header
56+
List<String> v = header.getValue();
57+
for (String value : v) {
58+
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
59+
cookies.add(cookie);
7560
}
7661
}
7762
}
7863
return Collections.unmodifiableList(cookies);
7964
}
80-
81-
/**
82-
* {@inheritDoc}
83-
*/
84-
/* @Override */
85-
public boolean hasResponseStatus() {
86-
return (bodyParts != null ? true : false);
87-
}
88-
89-
/**
90-
* {@inheritDoc}
91-
*/
92-
/* @Override */
93-
public boolean hasResponseHeaders() {
94-
return (headers != null ? true : false);
95-
}
96-
97-
/**
98-
* {@inheritDoc}
99-
*/
100-
/* @Override */
101-
public boolean hasResponseBody() {
102-
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
103-
}
10465
}

providers/grizzly/src/main/java/com/ning/http/client/providers/grizzly/GrizzlyResponse.java

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545
public class GrizzlyResponse extends ResponseBase {
4646
private final Buffer responseBody;
4747

48-
private List<Cookie> cookies;
49-
50-
5148
// ------------------------------------------------------------ Constructors
5249

5350

@@ -93,9 +90,7 @@ public InputStream getResponseBodyAsStream() throws IOException {
9390
* {@inheritDoc}
9491
*/
9592
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
96-
if (charset == null) {
97-
charset = calculateCharset();
98-
}
93+
charset = calculateCharset(charset);
9994
final int len = Math.min(responseBody.remaining(), maxLength);
10095
final int pos = responseBody.position();
10196
return responseBody.toStringContent(getCharset(charset), pos, len + pos);
@@ -148,55 +143,21 @@ public String getResponseBody() throws IOException {
148143
/**
149144
* {@inheritDoc}
150145
*/
151-
public List<Cookie> getCookies() {
152-
153-
if (headers == null) {
154-
return Collections.emptyList();
155-
}
156-
157-
if (cookies == null) {
158-
List<String> values = headers.getHeaders().get("set-cookie");
159-
if (values != null && !values.isEmpty()) {
160-
CookiesBuilder.ServerCookiesBuilder builder =
161-
new CookiesBuilder.ServerCookiesBuilder(false);
162-
for (String header : values) {
163-
builder.parse(header);
164-
}
165-
cookies = convertCookies(builder.build());
146+
public List<Cookie> buildCookies() {
166147

167-
} else {
168-
cookies = Collections.unmodifiableList(Collections.<Cookie>emptyList());
148+
List<String> values = headers.getHeaders().get("set-cookie");
149+
if (values != null && !values.isEmpty()) {
150+
CookiesBuilder.ServerCookiesBuilder builder = new CookiesBuilder.ServerCookiesBuilder(false);
151+
for (String header : values) {
152+
builder.parse(header);
169153
}
170-
}
171-
return cookies;
172-
173-
}
174-
175-
176-
/**
177-
* {@inheritDoc}
178-
*/
179-
public boolean hasResponseStatus() {
180-
return (status != null);
181-
}
182-
183-
184-
/**
185-
* {@inheritDoc}
186-
*/
187-
public boolean hasResponseHeaders() {
188-
return (headers != null && !headers.getHeaders().isEmpty());
189-
}
154+
return convertCookies(builder.build());
190155

191-
192-
/**
193-
* {@inheritDoc}
194-
*/
195-
public boolean hasResponseBody() {
196-
return (bodyParts != null && !bodyParts.isEmpty());
156+
} else {
157+
return Collections.unmodifiableList(Collections.<Cookie>emptyList());
158+
}
197159
}
198160

199-
200161
// --------------------------------------------------------- Private Methods
201162

202163

0 commit comments

Comments
 (0)