Skip to content

Commit b9677f1

Browse files
committed
Merge pull request #184 from slandelle/ahc-1.7.x
Fix proposal for #182 against 1.7.x
2 parents a7cdd0c + 39cb971 commit b9677f1

File tree

4 files changed

+85
-134
lines changed

4 files changed

+85
-134
lines changed

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

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@
3535

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

4039
private final URI uri;
4140
private final Collection<HttpResponseBodyPart> bodyParts;
4241
private final HttpResponseHeaders headers;
4342
private final HttpResponseStatus status;
44-
private final List<Cookie> cookies = new ArrayList<Cookie>();
43+
private List<Cookie> cookies;
4544

4645
public ApacheResponse(HttpResponseStatus status,
4746
HttpResponseHeaders headers,
@@ -77,21 +76,12 @@ public String getResponseBody() throws IOException {
7776
}
7877

7978
public String getResponseBody(String charset) throws IOException {
80-
String contentType = getContentType();
81-
if (contentType != null && charset == null) {
82-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
83-
}
84-
85-
if (charset == null) {
86-
charset = DEFAULT_CHARSET;
87-
}
88-
89-
return AsyncHttpProviderUtils.contentToString(bodyParts, charset);
79+
return AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset));
9080
}
91-
81+
9282
/* @Override */
9383
public InputStream getResponseBodyAsStream() throws IOException {
94-
if (bodyParts.size() > 0) {
84+
if (!bodyParts.isEmpty()) {
9585
return new HttpResponseBodyPartsInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()]));
9686
} else {
9787
return new ByteArrayInputStream("".getBytes());
@@ -107,18 +97,22 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
10797
/* @Override */
10898

10999
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
110-
String contentType = getContentType();
111-
if (contentType != null && charset == null) {
112-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
113-
}
114-
115-
if (charset == null) {
116-
charset = DEFAULT_CHARSET;
117-
}
100+
charset = computeCharset(charset);
118101

119102
String response = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
120103
return response.length() <= maxLength ? response : response.substring(0, maxLength);
121104
}
105+
106+
private String computeCharset(String charset) {
107+
String contentType = getContentType();
108+
if (charset == null) {
109+
if (contentType != null)
110+
charset = AsyncHttpProviderUtils.parseCharset(contentType);
111+
else
112+
charset = DEFAULT_CHARSET;
113+
}
114+
return charset;
115+
}
122116

123117
/* @Override */
124118

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

131125
public String getContentType() {
132-
if (headers == null) {
133-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
134-
}
135-
return headers.getHeaders().getFirstValue("Content-Type");
126+
return getHeader("Content-Type");
136127
}
137128

138129
/* @Override */
139130

140131
public String getHeader(String name) {
141-
if (headers == null) {
142-
throw new IllegalStateException();
143-
}
144-
return headers.getHeaders().getFirstValue(name);
132+
return headers != null? headers.getHeaders().getFirstValue(name): null;
145133
}
146134

147135
/* @Override */
148136

149137
public List<String> getHeaders(String name) {
150-
if (headers == null) {
151-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
152-
}
153-
return headers.getHeaders().get(name);
138+
return headers != null? headers.getHeaders().get(name): Collections.<String> emptyList();
154139
}
155140

156141
/* @Override */
157142

158143
public FluentCaseInsensitiveStringsMap getHeaders() {
159-
if (headers == null) {
160-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
161-
}
162-
return headers.getHeaders();
144+
return headers != null? headers.getHeaders(): new FluentCaseInsensitiveStringsMap();
163145
}
164146

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

173155
public List<Cookie> getCookies() {
174156
if (headers == null) {
175-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
157+
return Collections.emptyList();
176158
}
177-
if (cookies.isEmpty()) {
159+
if (cookies == null) {
160+
List<Cookie> localCookies = new ArrayList<Cookie>();
178161
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
179162
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
180163
// TODO: ask for parsed header
181164
List<String> v = header.getValue();
182165
for (String value : v) {
183166
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
184-
cookies.add(cookie);
167+
localCookies.add(cookie);
185168
}
186169
}
187170
}
171+
cookies = Collections.unmodifiableList(localCookies);
188172
}
189-
return Collections.unmodifiableList(cookies);
173+
return cookies;
190174
}
191175

192176
/**
193177
* {@inheritDoc}
194178
*/
195179
/* @Override */
196180
public boolean hasResponseStatus() {
197-
return (bodyParts != null ? true : false);
181+
return bodyParts != null;
198182
}
199183

200184
/**
201185
* {@inheritDoc}
202186
*/
203187
/* @Override */
204188
public boolean hasResponseHeaders() {
205-
return (headers != null ? true : false);
189+
return headers != null;
206190
}
207191

208192
/**
209193
* {@inheritDoc}
210194
*/
211195
/* @Override */
212196
public boolean hasResponseBody() {
213-
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
197+
return bodyParts != null && !bodyParts.isEmpty();
214198
}
215199
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public List<Cookie> getCookies() {
257257
cookies = convertCookies(builder.build());
258258

259259
} else {
260-
cookies = Collections.unmodifiableList(Collections.<Cookie>emptyList());
260+
cookies = Collections.emptyList();
261261
}
262262
}
263263
return cookies;

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

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@
3636

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

4140
private final URI uri;
4241
private final Collection<HttpResponseBodyPart> bodyParts;
4342
private final HttpResponseHeaders headers;
4443
private final HttpResponseStatus status;
45-
private final List<Cookie> cookies = new ArrayList<Cookie>();
44+
private List<Cookie> cookies;
4645
private AtomicBoolean contentComputed = new AtomicBoolean(false);
4746
private String content;
4847

@@ -81,28 +80,20 @@ public byte[] getResponseBodyAsBytes() throws IOException {
8180
}
8281

8382
public String getResponseBody(String charset) throws IOException {
84-
String contentType = getContentType();
85-
if (contentType != null && charset == null) {
86-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
87-
}
88-
89-
if (charset == null) {
90-
charset = DEFAULT_CHARSET;
91-
}
9283

93-
if (!contentComputed.get()) {
94-
content = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
84+
if (!contentComputed.get()) {
85+
content = AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset));
9586
}
9687
return content;
9788
}
98-
89+
9990
/* @Override */
10091
public InputStream getResponseBodyAsStream() throws IOException {
10192
if (contentComputed.get()) {
10293
return new ByteArrayInputStream(content.getBytes(DEFAULT_CHARSET));
10394
}
10495

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

118109
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
119-
String contentType = getContentType();
120-
if (contentType != null && charset == null) {
121-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
122-
}
123-
124-
if (charset == null) {
125-
charset = DEFAULT_CHARSET;
126-
}
110+
charset = computeCharset(charset);
127111

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

132116
return content.length() <= maxLength ? content : content.substring(0, maxLength);
133117
}
118+
119+
private String computeCharset(String charset) {
120+
String contentType = getContentType();
121+
if (charset == null) {
122+
if (contentType != null)
123+
charset = AsyncHttpProviderUtils.parseCharset(contentType);
124+
else
125+
charset = DEFAULT_CHARSET;
126+
}
127+
return charset;
128+
}
134129

135130
/* @Override */
136131

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

143138
public String getContentType() {
144-
if (headers == null) {
145-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
146-
}
147-
return headers.getHeaders().getFirstValue("Content-Type");
139+
return getHeader("Content-Type");
148140
}
149141

150142
/* @Override */
151143

152144
public String getHeader(String name) {
153-
if (headers == null) {
154-
throw new IllegalStateException();
155-
}
156-
return headers.getHeaders().getFirstValue(name);
145+
return headers != null? headers.getHeaders().getFirstValue(name): null;
157146
}
158147

159148
/* @Override */
160149

161150
public List<String> getHeaders(String name) {
162-
if (headers == null) {
163-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
164-
}
165-
return headers.getHeaders().get(name);
151+
return headers != null? headers.getHeaders().get(name): Collections.<String> emptyList();
166152
}
167153

168154
/* @Override */
169155

170156
public FluentCaseInsensitiveStringsMap getHeaders() {
171-
if (headers == null) {
172-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
173-
}
174-
return headers.getHeaders();
157+
return headers != null? headers.getHeaders(): new FluentCaseInsensitiveStringsMap();
175158
}
176159

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

185168
public List<Cookie> getCookies() {
186169
if (headers == null) {
187-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
170+
return Collections.emptyList();
188171
}
189172
if (cookies.isEmpty()) {
173+
List<Cookie> localCookies = new ArrayList<Cookie>();
190174
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
191175
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
192176
// TODO: ask for parsed header
193177
List<String> v = header.getValue();
194178
for (String value : v) {
195179
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
196-
cookies.add(cookie);
180+
localCookies.add(cookie);
197181
}
198182
}
199183
}
184+
cookies = Collections.unmodifiableList(localCookies);
200185
}
201-
return Collections.unmodifiableList(cookies);
186+
return cookies;
202187
}
203188

204189
/**
205190
* {@inheritDoc}
206191
*/
207192
/* @Override */
208193
public boolean hasResponseStatus() {
209-
return (bodyParts != null ? true : false);
194+
return bodyParts != null;
210195
}
211196

212197
/**
213198
* {@inheritDoc}
214199
*/
215200
/* @Override */
216201
public boolean hasResponseHeaders() {
217-
return (headers != null ? true : false);
202+
return headers != null;
218203
}
219204

220205
/**
221206
* {@inheritDoc}
222207
*/
223208
/* @Override */
224209
public boolean hasResponseBody() {
225-
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
210+
return bodyParts != null && !bodyParts.isEmpty();
226211
}
227212
}

0 commit comments

Comments
 (0)