13
13
package com .ning .http .client .providers .apache ;
14
14
15
15
import com .ning .http .client .Cookie ;
16
- import com .ning .http .client .FluentCaseInsensitiveStringsMap ;
17
16
import com .ning .http .client .HttpResponseBodyPart ;
18
17
import com .ning .http .client .HttpResponseHeaders ;
19
18
import com .ning .http .client .HttpResponseStatus ;
20
- import com .ning .http .client .Response ;
19
+ import com .ning .http .client .providers . ResponseBase ;
21
20
import com .ning .http .util .AsyncHttpProviderUtils ;
22
21
23
- import java .io .ByteArrayInputStream ;
24
22
import java .io .IOException ;
25
- import java .io .InputStream ;
26
- import java .io .SequenceInputStream ;
27
- import java .net .MalformedURLException ;
28
- import java .net .URI ;
29
23
import java .util .ArrayList ;
30
24
import java .util .Collections ;
31
25
import java .util .List ;
32
26
import java .util .Map ;
33
- import java .util .Vector ;
34
27
35
- public class ApacheResponse implements Response {
36
- private final static String DEFAULT_CHARSET = "ISO-8859-1" ;
28
+ public class ApacheResponse extends ResponseBase {
37
29
private final static String HEADERS_NOT_COMPUTED = "Response's headers hasn't been computed by your AsyncHandler." ;
38
30
39
- private final URI uri ;
40
- private final List <HttpResponseBodyPart > bodyParts ;
41
- private final HttpResponseHeaders headers ;
42
- private final HttpResponseStatus status ;
43
31
private final List <Cookie > cookies = new ArrayList <Cookie >();
44
32
45
33
public ApacheResponse (HttpResponseStatus status ,
46
34
HttpResponseHeaders headers ,
47
35
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 );
104
37
}
105
38
106
39
/* @Override */
@@ -126,55 +59,6 @@ public String getResponseBodyExcerpt(int maxLength, String charset) throws IOExc
126
59
}
127
60
128
61
/* @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
-
178
62
public List <Cookie > getCookies () {
179
63
if (headers == null ) {
180
64
throw new IllegalStateException (HEADERS_NOT_COMPUTED );
0 commit comments