1313package com .ning .http .client .providers .apache ;
1414
1515import com .ning .http .client .Cookie ;
16- import com .ning .http .client .FluentCaseInsensitiveStringsMap ;
1716import com .ning .http .client .HttpResponseBodyPart ;
1817import com .ning .http .client .HttpResponseHeaders ;
1918import com .ning .http .client .HttpResponseStatus ;
20- import com .ning .http .client .Response ;
19+ import com .ning .http .client .providers . ResponseBase ;
2120import com .ning .http .util .AsyncHttpProviderUtils ;
2221
23- import java .io .ByteArrayInputStream ;
2422import java .io .IOException ;
25- import java .io .InputStream ;
26- import java .io .SequenceInputStream ;
27- import java .net .MalformedURLException ;
28- import java .net .URI ;
2923import java .util .ArrayList ;
3024import java .util .Collections ;
3125import java .util .List ;
3226import 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