Skip to content

Commit c1c4bd1

Browse files
author
Stephane Landelle
committed
Minor clean up
1 parent 4beb34d commit c1c4bd1

File tree

4 files changed

+45
-47
lines changed

4 files changed

+45
-47
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,18 @@ public String getResponseBody() throws IOException {
7676
}
7777

7878
public String getResponseBody(String charset) throws IOException {
79-
String contentType = getContentType();
80-
if (contentType != null && charset == null) {
81-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
82-
}
83-
79+
return AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset));
80+
}
81+
82+
private String computeCharset(String charset) {
83+
String contentType = getContentType();
8484
if (charset == null) {
85-
charset = DEFAULT_CHARSET;
85+
if (contentType != null)
86+
charset = AsyncHttpProviderUtils.parseCharset(contentType);
87+
else
88+
charset = DEFAULT_CHARSET;
8689
}
87-
88-
return AsyncHttpProviderUtils.contentToString(bodyParts, charset);
90+
return charset;
8991
}
9092

9193
/* @Override */
@@ -181,22 +183,22 @@ public List<Cookie> getCookies() {
181183
*/
182184
/* @Override */
183185
public boolean hasResponseStatus() {
184-
return (bodyParts != null ? true : false);
186+
return bodyParts != null;
185187
}
186188

187189
/**
188190
* {@inheritDoc}
189191
*/
190192
/* @Override */
191193
public boolean hasResponseHeaders() {
192-
return (headers != null ? true : false);
194+
return headers != null;
193195
}
194196

195197
/**
196198
* {@inheritDoc}
197199
*/
198200
/* @Override */
199201
public boolean hasResponseBody() {
200-
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
202+
return bodyParts != null && bodyParts.size() > 0;
201203
}
202204
}

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: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,23 @@ public byte[] getResponseBodyAsBytes() throws IOException {
8080
}
8181

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

92-
if (!contentComputed.get()) {
93-
content = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
84+
if (!contentComputed.get()) {
85+
content = AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset));
9486
}
9587
return content;
9688
}
89+
90+
private String computeCharset(String charset) {
91+
String contentType = getContentType();
92+
if (charset == null) {
93+
if (contentType != null)
94+
charset = AsyncHttpProviderUtils.parseCharset(contentType);
95+
else
96+
charset = DEFAULT_CHARSET;
97+
}
98+
return charset;
99+
}
97100

98101
/* @Override */
99102
public InputStream getResponseBodyAsStream() throws IOException {
@@ -193,22 +196,22 @@ public List<Cookie> getCookies() {
193196
*/
194197
/* @Override */
195198
public boolean hasResponseStatus() {
196-
return (bodyParts != null ? true : false);
199+
return bodyParts != null;
197200
}
198201

199202
/**
200203
* {@inheritDoc}
201204
*/
202205
/* @Override */
203206
public boolean hasResponseHeaders() {
204-
return (headers != null ? true : false);
207+
return headers != null;
205208
}
206209

207210
/**
208211
* {@inheritDoc}
209212
*/
210213
/* @Override */
211214
public boolean hasResponseBody() {
212-
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
215+
return bodyParts != null && bodyParts.size() > 0;
213216
}
214217
}

src/main/java/com/ning/http/client/providers/netty/NettyResponse.java

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,7 @@ public String getResponseBody() throws IOException {
8181
}
8282

8383
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-
}
92-
93-
return AsyncHttpProviderUtils.contentToString(bodyParts, charset);
84+
return AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset));
9485
}
9586

9687
/* @Override */
@@ -115,17 +106,19 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
115106
}
116107

117108
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
118-
String contentType = getContentType();
119-
if (contentType != null && charset == null) {
120-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
121-
}
122-
109+
String response = AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset));
110+
return response.length() <= maxLength ? response : response.substring(0, maxLength);
111+
}
112+
113+
private String computeCharset(String charset) {
114+
String contentType = getContentType();
123115
if (charset == null) {
124-
charset = DEFAULT_CHARSET;
116+
if (contentType != null)
117+
charset = AsyncHttpProviderUtils.parseCharset(contentType);
118+
else
119+
charset = DEFAULT_CHARSET;
125120
}
126-
127-
String response = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
128-
return response.length() <= maxLength ? response : response.substring(0, maxLength);
121+
return charset;
129122
}
130123

131124
/* @Override */
@@ -190,23 +183,23 @@ public List<Cookie> getCookies() {
190183
*/
191184
/* @Override */
192185
public boolean hasResponseStatus() {
193-
return (status != null ? true : false);
186+
return status != null;
194187
}
195188

196189
/**
197190
* {@inheritDoc}
198191
*/
199192
/* @Override */
200193
public boolean hasResponseHeaders() {
201-
return (headers != null ? true : false);
194+
return headers != null;
202195
}
203196

204197
/**
205198
* {@inheritDoc}
206199
*/
207200
/* @Override */
208201
public boolean hasResponseBody() {
209-
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
202+
return bodyParts != null && bodyParts.size() > 0;
210203
}
211204

212205
}

0 commit comments

Comments
 (0)