Skip to content

Commit 2ea71a6

Browse files
committed
Merge pull request AsyncHttpClient#104 from cowtowncoder/master
Second part of cleanup
2 parents 9b0e390 + 4b2d2d5 commit 2ea71a6

File tree

8 files changed

+171
-498
lines changed

8 files changed

+171
-498
lines changed

src/main/java/com/ning/http/client/Response.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.net.MalformedURLException;
2222
import java.net.URI;
2323
import java.util.ArrayList;
24-
import java.util.Collection;
2524
import java.util.Collections;
2625
import java.util.List;
2726

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.ning.http.client.providers;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.URI;
6+
import java.util.List;
7+
8+
import com.ning.http.client.FluentCaseInsensitiveStringsMap;
9+
import com.ning.http.client.HttpResponseBodyPart;
10+
import com.ning.http.client.HttpResponseHeaders;
11+
import com.ning.http.client.HttpResponseStatus;
12+
import com.ning.http.client.Response;
13+
import com.ning.http.util.AsyncHttpProviderUtils;
14+
15+
public abstract class ResponseBase implements Response
16+
{
17+
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.";
19+
20+
protected final List<HttpResponseBodyPart> bodyParts;
21+
protected final HttpResponseHeaders headers;
22+
protected final HttpResponseStatus status;
23+
24+
protected ResponseBase(HttpResponseStatus status,
25+
HttpResponseHeaders headers,
26+
List<HttpResponseBodyPart> bodyParts)
27+
{
28+
this.bodyParts = bodyParts;
29+
this.headers = headers;
30+
this.status = status;
31+
}
32+
33+
/* @Override */
34+
public final int getStatusCode() {
35+
return status.getStatusCode();
36+
}
37+
38+
/* @Override */
39+
public final String getStatusText() {
40+
return status.getStatusText();
41+
}
42+
43+
44+
/* @Override */
45+
public final URI getUri() /*throws MalformedURLException*/ {
46+
return status.getUrl();
47+
}
48+
49+
/* @Override */
50+
public final String getContentType() {
51+
return getHeader("Content-Type");
52+
}
53+
54+
/* @Override */
55+
public final String getHeader(String name) {
56+
return getHeaders().getFirstValue(name);
57+
}
58+
59+
/* @Override */
60+
public final List<String> getHeaders(String name) {
61+
return getHeaders().get(name);
62+
}
63+
64+
/* @Override */
65+
public final FluentCaseInsensitiveStringsMap getHeaders() {
66+
if (headers == null) {
67+
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
68+
}
69+
return headers.getHeaders();
70+
}
71+
72+
/* @Override */
73+
public final boolean isRedirected() {
74+
return (status.getStatusCode() >= 300) && (status.getStatusCode() <= 399);
75+
}
76+
77+
/* @Override */
78+
public byte[] getResponseBodyAsBytes() throws IOException {
79+
return AsyncHttpProviderUtils.contentToBytes(bodyParts);
80+
}
81+
82+
/* @Override */
83+
public String getResponseBody() throws IOException {
84+
return getResponseBody(DEFAULT_CHARSET);
85+
}
86+
87+
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);
98+
}
99+
100+
/* @Override */
101+
public InputStream getResponseBodyAsStream() throws IOException {
102+
return AsyncHttpProviderUtils.contentAsStream(bodyParts);
103+
}
104+
105+
}

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

Lines changed: 3 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -13,94 +13,27 @@
1313
package com.ning.http.client.providers.apache;
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;
20-
import com.ning.http.client.Response;
19+
import com.ning.http.client.providers.ResponseBase;
2120
import com.ning.http.util.AsyncHttpProviderUtils;
2221

23-
import java.io.ByteArrayInputStream;
2422
import java.io.IOException;
25-
import java.io.InputStream;
26-
import java.io.SequenceInputStream;
27-
import java.net.MalformedURLException;
28-
import java.net.URI;
2923
import java.util.ArrayList;
3024
import java.util.Collections;
3125
import java.util.List;
3226
import java.util.Map;
33-
import java.util.Vector;
3427

35-
public class ApacheResponse implements Response {
36-
private final static String DEFAULT_CHARSET = "ISO-8859-1";
28+
public class ApacheResponse extends ResponseBase {
3729
private final static String HEADERS_NOT_COMPUTED = "Response's headers hasn't been computed by your AsyncHandler.";
3830

39-
private final URI uri;
40-
private final List<HttpResponseBodyPart> bodyParts;
41-
private final HttpResponseHeaders headers;
42-
private final HttpResponseStatus status;
4331
private final List<Cookie> cookies = new ArrayList<Cookie>();
4432

4533
public ApacheResponse(HttpResponseStatus status,
4634
HttpResponseHeaders headers,
4735
List<HttpResponseBodyPart> bodyParts) {
48-
49-
this.bodyParts = bodyParts;
50-
this.headers = headers;
51-
this.status = status;
52-
53-
uri = this.status.getUrl();
54-
}
55-
56-
/* @Override */
57-
58-
public int getStatusCode() {
59-
return status.getStatusCode();
60-
}
61-
62-
/* @Override */
63-
64-
public String getStatusText() {
65-
return status.getStatusText();
66-
}
67-
68-
/* @Override */
69-
public byte[] getResponseBodyAsBytes() throws IOException {
70-
return AsyncHttpProviderUtils.contentToByte(bodyParts);
71-
}
72-
73-
/* @Override */
74-
public String getResponseBody() throws IOException {
75-
return getResponseBody(DEFAULT_CHARSET);
76-
}
77-
78-
public String getResponseBody(String charset) throws IOException {
79-
String contentType = getContentType();
80-
if (contentType != null && charset == null) {
81-
charset = AsyncHttpProviderUtils.parseCharset(contentType);
82-
}
83-
84-
if (charset == null) {
85-
charset = DEFAULT_CHARSET;
86-
}
87-
88-
return AsyncHttpProviderUtils.contentToString(bodyParts, charset);
89-
}
90-
91-
/* @Override */
92-
public InputStream getResponseBodyAsStream() throws IOException {
93-
switch (bodyParts.size()) {
94-
case 0:
95-
return new ByteArrayInputStream(new byte[0]);
96-
case 1:
97-
return bodyParts.get(0).readBodyPartBytes();
98-
}
99-
Vector<InputStream> streams = new Vector<InputStream>(bodyParts.size());
100-
for (HttpResponseBodyPart part : bodyParts) {
101-
streams.add(part.readBodyPartBytes());
102-
}
103-
return new SequenceInputStream(streams.elements());
36+
super(status, headers, bodyParts);
10437
}
10538

10639
/* @Override */
@@ -126,55 +59,6 @@ public String getResponseBodyExcerpt(int maxLength, String charset) throws IOExc
12659
}
12760

12861
/* @Override */
129-
130-
public URI getUri() throws MalformedURLException {
131-
return uri;
132-
}
133-
134-
/* @Override */
135-
136-
public String getContentType() {
137-
if (headers == null) {
138-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
139-
}
140-
return headers.getHeaders().getFirstValue("Content-Type");
141-
}
142-
143-
/* @Override */
144-
145-
public String getHeader(String name) {
146-
if (headers == null) {
147-
throw new IllegalStateException();
148-
}
149-
return headers.getHeaders().getFirstValue(name);
150-
}
151-
152-
/* @Override */
153-
154-
public List<String> getHeaders(String name) {
155-
if (headers == null) {
156-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
157-
}
158-
return headers.getHeaders().get(name);
159-
}
160-
161-
/* @Override */
162-
163-
public FluentCaseInsensitiveStringsMap getHeaders() {
164-
if (headers == null) {
165-
throw new IllegalStateException(HEADERS_NOT_COMPUTED);
166-
}
167-
return headers.getHeaders();
168-
}
169-
170-
/* @Override */
171-
172-
public boolean isRedirected() {
173-
return (status.getStatusCode() >= 300) && (status.getStatusCode() <= 399);
174-
}
175-
176-
/* @Override */
177-
17862
public List<Cookie> getCookies() {
17963
if (headers == null) {
18064
throw new IllegalStateException(HEADERS_NOT_COMPUTED);

0 commit comments

Comments
 (0)