Skip to content

Commit b514542

Browse files
author
Stephane Landelle
committed
Minor clean up
1 parent cdb9c5a commit b514542

File tree

6 files changed

+81
-200
lines changed

6 files changed

+81
-200
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: 38 additions & 17 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;
@@ -19,6 +21,7 @@ public abstract class ResponseBase implements Response
1921
protected final List<HttpResponseBodyPart> bodyParts;
2022
protected final HttpResponseHeaders headers;
2123
protected final HttpResponseStatus status;
24+
private List<Cookie> cookies;
2225

2326
protected ResponseBase(HttpResponseStatus status,
2427
HttpResponseHeaders headers,
@@ -81,34 +84,52 @@ public String getResponseBody() throws IOException {
8184
}
8285

8386
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);
87+
return AsyncHttpProviderUtils.contentToString(bodyParts, calculateCharset(charset));
9488
}
9589

9690
/* @Override */
9791
public InputStream getResponseBodyAsStream() throws IOException {
9892
return AsyncHttpProviderUtils.contentAsStream(bodyParts);
9993
}
94+
95+
protected abstract List<Cookie> buildCookies();
96+
97+
public List<Cookie> getCookies() {
10098

101-
protected String calculateCharset() {
102-
String charset = null;
103-
String contentType = getContentType();
104-
if (contentType != null) {
105-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
99+
if (headers == null) {
100+
return Collections.emptyList();
106101
}
107102

108-
if (charset == null) {
109-
charset = DEFAULT_CHARSET;
103+
if (cookies == null) {
104+
cookies = buildCookies();
110105
}
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+
111121
return charset;
112122
}
113123

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+
}
114135
}

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

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.Map;
2727

2828
public class JDKResponse extends ResponseBase {
29-
private final List<Cookie> cookies = new ArrayList<Cookie>();
3029

3130
public JDKResponse(HttpResponseStatus status,
3231
HttpResponseHeaders headers,
@@ -47,46 +46,18 @@ public String getResponseBodyExcerpt(int maxLength, String charset) throws IOExc
4746
}
4847

4948
/* @Override */
50-
public List<Cookie> getCookies() {
51-
if (headers == null) {
52-
return Collections.emptyList();
53-
}
54-
if (cookies.isEmpty()) {
55-
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
56-
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
57-
// TODO: ask for parsed header
58-
List<String> v = header.getValue();
59-
for (String value : v) {
60-
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
61-
cookies.add(cookie);
62-
}
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);
6358
}
6459
}
6560
}
6661
return Collections.unmodifiableList(cookies);
6762
}
68-
69-
/**
70-
* {@inheritDoc}
71-
*/
72-
/* @Override */
73-
public boolean hasResponseStatus() {
74-
return (bodyParts != null ? true : false);
75-
}
76-
77-
/**
78-
* {@inheritDoc}
79-
*/
80-
/* @Override */
81-
public boolean hasResponseHeaders() {
82-
return (headers != null ? true : false);
83-
}
84-
85-
/**
86-
* {@inheritDoc}
87-
*/
88-
/* @Override */
89-
public boolean hasResponseBody() {
90-
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
91-
}
9263
}

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

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727

2828
public class ApacheResponse extends ResponseBase {
2929

30-
private final List<Cookie> cookies = new ArrayList<Cookie>();
31-
32-
public ApacheResponse(HttpResponseStatus status,
30+
public ApacheResponse(HttpResponseStatus status,
3331
HttpResponseHeaders headers,
3432
List<HttpResponseBodyPart> bodyParts) {
3533
super(status, headers, bodyParts);
@@ -44,60 +42,24 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
4442
/* @Override */
4543

4644
public String getResponseBodyExcerpt(int maxLength, String charset) throws IOException {
47-
String contentType = getContentType();
48-
if (contentType != null && charset == null) {
49-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
50-
}
51-
52-
if (charset == null) {
53-
charset = DEFAULT_CHARSET;
54-
}
55-
45+
charset = calculateCharset(charset);
5646
String response = AsyncHttpProviderUtils.contentToString(bodyParts, charset);
5747
return response.length() <= maxLength ? response : response.substring(0, maxLength);
5848
}
5949

6050
/* @Override */
61-
public List<Cookie> getCookies() {
62-
if (headers == null) {
63-
return Collections.emptyList();
64-
}
65-
if (cookies.isEmpty()) {
66-
for (Map.Entry<String, List<String>> header : headers.getHeaders().entrySet()) {
67-
if (header.getKey().equalsIgnoreCase("Set-Cookie")) {
68-
// TODO: ask for parsed header
69-
List<String> v = header.getValue();
70-
for (String value : v) {
71-
Cookie cookie = AsyncHttpProviderUtils.parseCookie(value);
72-
cookies.add(cookie);
73-
}
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);
7460
}
7561
}
7662
}
7763
return Collections.unmodifiableList(cookies);
7864
}
79-
80-
/**
81-
* {@inheritDoc}
82-
*/
83-
/* @Override */
84-
public boolean hasResponseStatus() {
85-
return (bodyParts != null ? true : false);
86-
}
87-
88-
/**
89-
* {@inheritDoc}
90-
*/
91-
/* @Override */
92-
public boolean hasResponseHeaders() {
93-
return (headers != null ? true : false);
94-
}
95-
96-
/**
97-
* {@inheritDoc}
98-
*/
99-
/* @Override */
100-
public boolean hasResponseBody() {
101-
return (bodyParts != null && bodyParts.size() > 0 ? true : false);
102-
}
10365
}

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)