Skip to content

Commit 39cb971

Browse files
author
Stephane Landelle
committed
Minor clean up
1 parent c1c4bd1 commit 39cb971

File tree

3 files changed

+49
-57
lines changed

3 files changed

+49
-57
lines changed

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

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class ApacheResponse implements Response {
4040
private final Collection<HttpResponseBodyPart> bodyParts;
4141
private final HttpResponseHeaders headers;
4242
private final HttpResponseStatus status;
43-
private final List<Cookie> cookies = new ArrayList<Cookie>();
43+
private List<Cookie> cookies;
4444

4545
public ApacheResponse(HttpResponseStatus status,
4646
HttpResponseHeaders headers,
@@ -79,20 +79,9 @@ public String getResponseBody(String charset) throws IOException {
7979
return AsyncHttpProviderUtils.contentToString(bodyParts, computeCharset(charset));
8080
}
8181

82-
private String computeCharset(String charset) {
83-
String contentType = getContentType();
84-
if (charset == null) {
85-
if (contentType != null)
86-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
87-
else
88-
charset = DEFAULT_CHARSET;
89-
}
90-
return charset;
91-
}
92-
9382
/* @Override */
9483
public InputStream getResponseBodyAsStream() throws IOException {
95-
if (bodyParts.size() > 0) {
84+
if (!bodyParts.isEmpty()) {
9685
return new HttpResponseBodyPartsInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()]));
9786
} else {
9887
return new ByteArrayInputStream("".getBytes());
@@ -108,18 +97,22 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
10897
/* @Override */
10998

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

120102
String response = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
121103
return response.length() <= maxLength ? response : response.substring(0, maxLength);
122104
}
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+
}
123116

124117
/* @Override */
125118

@@ -130,7 +123,7 @@ public URI getUri() throws MalformedURLException {
130123
/* @Override */
131124

132125
public String getContentType() {
133-
return headers != null? headers.getHeaders().getFirstValue("Content-Type"): null;
126+
return getHeader("Content-Type");
134127
}
135128

136129
/* @Override */
@@ -163,19 +156,21 @@ public List<Cookie> getCookies() {
163156
if (headers == null) {
164157
return Collections.emptyList();
165158
}
166-
if (cookies.isEmpty()) {
159+
if (cookies == null) {
160+
List<Cookie> localCookies = new ArrayList<Cookie>();
167161
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
168162
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
169163
// TODO: ask for parsed header
170164
List<String> v = header.getValue();
171165
for (String value : v) {
172166
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
173-
cookies.add(cookie);
167+
localCookies.add(cookie);
174168
}
175169
}
176170
}
171+
cookies = Collections.unmodifiableList(localCookies);
177172
}
178-
return Collections.unmodifiableList(cookies);
173+
return cookies;
179174
}
180175

181176
/**
@@ -199,6 +194,6 @@ public boolean hasResponseHeaders() {
199194
*/
200195
/* @Override */
201196
public boolean hasResponseBody() {
202-
return bodyParts != null && bodyParts.size() > 0;
197+
return bodyParts != null && !bodyParts.isEmpty();
203198
}
204199
}

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

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class JDKResponse implements Response {
4141
private final Collection<HttpResponseBodyPart> bodyParts;
4242
private final HttpResponseHeaders headers;
4343
private final HttpResponseStatus status;
44-
private final List<Cookie> cookies = new ArrayList<Cookie>();
44+
private List<Cookie> cookies;
4545
private AtomicBoolean contentComputed = new AtomicBoolean(false);
4646
private String content;
4747

@@ -87,24 +87,13 @@ public String getResponseBody(String charset) throws IOException {
8787
return content;
8888
}
8989

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-
}
100-
10190
/* @Override */
10291
public InputStream getResponseBodyAsStream() throws IOException {
10392
if (contentComputed.get()) {
10493
return new ByteArrayInputStream(content.getBytes(DEFAULT_CHARSET));
10594
}
10695

107-
if (bodyParts.size() > 0) {
96+
if (!bodyParts.isEmpty()) {
10897
return new HttpResponseBodyPartsInputStream(bodyParts.toArray(new HttpResponseBodyPart[bodyParts.size()]));
10998
} else {
11099
return new ByteArrayInputStream("".getBytes());
@@ -118,21 +107,25 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
118107
}
119108

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

130112
if (!contentComputed.get()) {
131113
content = AsyncHttpProviderUtils.contentToString(bodyParts, charset == null ? DEFAULT_CHARSET : charset);
132114
}
133115

134116
return content.length() <= maxLength ? content : content.substring(0, maxLength);
135117
}
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+
}
136129

137130
/* @Override */
138131

@@ -143,7 +136,7 @@ public URI getUri() throws MalformedURLException {
143136
/* @Override */
144137

145138
public String getContentType() {
146-
return headers != null? headers.getHeaders().getFirstValue("Content-Type"): null;
139+
return getHeader("Content-Type");
147140
}
148141

149142
/* @Override */
@@ -177,18 +170,20 @@ public List<Cookie> getCookies() {
177170
return Collections.emptyList();
178171
}
179172
if (cookies.isEmpty()) {
173+
List<Cookie> localCookies = new ArrayList<Cookie>();
180174
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
181175
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
182176
// TODO: ask for parsed header
183177
List<String> v = header.getValue();
184178
for (String value : v) {
185179
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
186-
cookies.add(cookie);
180+
localCookies.add(cookie);
187181
}
188182
}
189183
}
184+
cookies = Collections.unmodifiableList(localCookies);
190185
}
191-
return Collections.unmodifiableList(cookies);
186+
return cookies;
192187
}
193188

194189
/**
@@ -212,6 +207,6 @@ public boolean hasResponseHeaders() {
212207
*/
213208
/* @Override */
214209
public boolean hasResponseBody() {
215-
return bodyParts != null && bodyParts.size() > 0;
210+
return bodyParts != null && !bodyParts.isEmpty();
216211
}
217212
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class NettyResponse implements Response {
4646
private final Collection<HttpResponseBodyPart> bodyParts;
4747
private final HttpResponseHeaders headers;
4848
private final HttpResponseStatus status;
49-
private final List<Cookie> cookies = new ArrayList<Cookie>();
49+
private List<Cookie> cookies;
5050

5151
public NettyResponse(HttpResponseStatus status,
5252
HttpResponseHeaders headers,
@@ -130,7 +130,7 @@ public URI getUri() throws MalformedURLException {
130130
/* @Override */
131131

132132
public String getContentType() {
133-
return headers != null? headers.getHeaders().getFirstValue("Content-Type"): null;
133+
return getHeader("Content-Type");
134134
}
135135

136136
/* @Override */
@@ -163,19 +163,21 @@ public List<Cookie> getCookies() {
163163
if (headers == null) {
164164
return Collections.emptyList();
165165
}
166-
if (cookies.isEmpty()) {
166+
if (cookies == null) {
167+
List<Cookie> localCookies = new ArrayList<Cookie>();
167168
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
168169
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
169170
// TODO: ask for parsed header
170171
List<String> v = header.getValue();
171172
for (String value : v) {
172173
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
173-
cookies.add(cookie);
174+
localCookies.add(cookie);
174175
}
175176
}
176177
}
178+
cookies = Collections.unmodifiableList(localCookies);
177179
}
178-
return Collections.unmodifiableList(cookies);
180+
return cookies;
179181
}
180182

181183
/**
@@ -199,7 +201,7 @@ public boolean hasResponseHeaders() {
199201
*/
200202
/* @Override */
201203
public boolean hasResponseBody() {
202-
return bodyParts != null && bodyParts.size() > 0;
204+
return bodyParts != null && !bodyParts.isEmpty();
203205
}
204206

205207
}

0 commit comments

Comments
 (0)