13
13
*/
14
14
package org .asynchttpclient .netty ;
15
15
16
+ import static io .netty .handler .codec .http .HttpHeaders .Names .*;
17
+ import static org .asynchttpclient .util .HttpUtils .*;
16
18
import static org .asynchttpclient .util .MiscUtils .isNonEmpty ;
17
19
import io .netty .handler .codec .http .HttpHeaders ;
18
20
19
21
import java .io .ByteArrayInputStream ;
20
22
import java .io .InputStream ;
23
+ import java .net .SocketAddress ;
21
24
import java .nio .ByteBuffer ;
22
25
import java .nio .charset .Charset ;
23
26
import java .util .ArrayList ;
24
27
import java .util .Collections ;
25
28
import java .util .List ;
29
+ import java .util .Map ;
26
30
27
31
import org .asynchttpclient .HttpResponseBodyPart ;
28
32
import org .asynchttpclient .HttpResponseHeaders ;
29
33
import org .asynchttpclient .HttpResponseStatus ;
30
- import org .asynchttpclient .ResponseBase ;
34
+ import org .asynchttpclient .Response ;
31
35
import org .asynchttpclient .cookie .Cookie ;
32
36
import org .asynchttpclient .cookie .CookieDecoder ;
37
+ import org .asynchttpclient .uri .Uri ;
33
38
34
39
/**
35
40
* Wrapper around the {@link org.asynchttpclient.Response} API.
36
41
*/
37
- public class NettyResponse extends ResponseBase {
42
+ public class NettyResponse implements Response {
43
+
44
+ private final List <HttpResponseBodyPart > bodyParts ;
45
+ private final HttpResponseHeaders headers ;
46
+ private final HttpResponseStatus status ;
47
+ private List <Cookie > cookies ;
38
48
39
49
public NettyResponse (HttpResponseStatus status ,//
40
50
HttpResponseHeaders headers ,//
41
51
List <HttpResponseBodyPart > bodyParts ) {
42
- super (status , headers , bodyParts );
52
+ this .bodyParts = bodyParts ;
53
+ this .headers = headers ;
54
+ this .status = status ;
43
55
}
44
56
45
- protected List <Cookie > buildCookies () {
57
+ private List <Cookie > buildCookies () {
46
58
47
- List <String > setCookieHeaders = headers .getHeaders ().getAll (HttpHeaders . Names . SET_COOKIE2 );
59
+ List <String > setCookieHeaders = headers .getHeaders ().getAll (SET_COOKIE2 );
48
60
49
61
if (!isNonEmpty (setCookieHeaders )) {
50
- setCookieHeaders = headers .getHeaders ().getAll (HttpHeaders . Names . SET_COOKIE );
62
+ setCookieHeaders = headers .getHeaders ().getAll (SET_COOKIE );
51
63
}
52
64
53
65
if (isNonEmpty (setCookieHeaders )) {
@@ -63,6 +75,94 @@ protected List<Cookie> buildCookies() {
63
75
return Collections .emptyList ();
64
76
}
65
77
78
+ @ Override
79
+ public final int getStatusCode () {
80
+ return status .getStatusCode ();
81
+ }
82
+
83
+ @ Override
84
+ public final String getStatusText () {
85
+ return status .getStatusText ();
86
+ }
87
+
88
+ @ Override
89
+ public final Uri getUri () {
90
+ return status .getUri ();
91
+ }
92
+
93
+ @ Override
94
+ public SocketAddress getRemoteAddress () {
95
+ return status .getRemoteAddress ();
96
+ }
97
+
98
+ @ Override
99
+ public SocketAddress getLocalAddress () {
100
+ return status .getLocalAddress ();
101
+ }
102
+
103
+ @ Override
104
+ public final String getContentType () {
105
+ return headers != null ? getHeader (CONTENT_TYPE ) : null ;
106
+ }
107
+
108
+ @ Override
109
+ public final String getHeader (String name ) {
110
+ return headers != null ? getHeaders ().get (name ) : null ;
111
+ }
112
+
113
+ @ Override
114
+ public final List <String > getHeaders (String name ) {
115
+ return headers != null ? getHeaders ().getAll (name ) : Collections .<String > emptyList ();
116
+ }
117
+
118
+ @ Override
119
+ public final HttpHeaders getHeaders () {
120
+ return headers != null ? headers .getHeaders () : HttpHeaders .EMPTY_HEADERS ;
121
+ }
122
+
123
+ @ Override
124
+ public final boolean isRedirected () {
125
+ switch (status .getStatusCode ()) {
126
+ case 301 :
127
+ case 302 :
128
+ case 303 :
129
+ case 307 :
130
+ case 308 :
131
+ return true ;
132
+ default :
133
+ return false ;
134
+ }
135
+ }
136
+
137
+ @ Override
138
+ public List <Cookie > getCookies () {
139
+
140
+ if (headers == null ) {
141
+ return Collections .emptyList ();
142
+ }
143
+
144
+ if (cookies == null ) {
145
+ cookies = buildCookies ();
146
+ }
147
+ return cookies ;
148
+
149
+ }
150
+
151
+ @ Override
152
+ public boolean hasResponseStatus () {
153
+ return status != null ;
154
+ }
155
+
156
+ @ Override
157
+ public boolean hasResponseHeaders () {
158
+ return headers != null && !headers .getHeaders ().isEmpty ();
159
+ }
160
+
161
+ @ Override
162
+ public boolean hasResponseBody () {
163
+ return isNonEmpty (bodyParts );
164
+ }
165
+
66
166
@ Override
67
167
public byte [] getResponseBodyAsBytes () {
68
168
return getResponseBodyAsByteBuffer ().array ();
@@ -87,6 +187,17 @@ public String getResponseBody() {
87
187
return getResponseBody (null );
88
188
}
89
189
190
+ private Charset computeCharset (Charset charset ) {
191
+
192
+ if (charset == null ) {
193
+ String contentType = getContentType ();
194
+ if (contentType != null )
195
+ charset = parseCharset (contentType ); // parseCharset can return
196
+ // null
197
+ }
198
+ return charset != null ? charset : DEFAULT_CHARSET ;
199
+ }
200
+
90
201
@ Override
91
202
public String getResponseBody (Charset charset ) {
92
203
return new String (getResponseBodyAsBytes (), computeCharset (charset ));
@@ -96,4 +207,19 @@ public String getResponseBody(Charset charset) {
96
207
public InputStream getResponseBodyAsStream () {
97
208
return new ByteArrayInputStream (getResponseBodyAsBytes ());
98
209
}
210
+
211
+ @ Override
212
+ public String toString () {
213
+ StringBuilder sb = new StringBuilder ();
214
+ sb .append (getClass ().getSimpleName ()).append (" {\n " )//
215
+ .append ("\t statusCode=" ).append (getStatusCode ()).append ("\n " )//
216
+ .append ("\t headers=\n " );
217
+
218
+ for (Map .Entry <String , String > header : getHeaders ()) {
219
+ sb .append ("\t \t " ).append (header .getKey ()).append (": " ).append (header .getValue ()).append ("\n " );
220
+ }
221
+ sb .append ("\t body=\n " ).append (getResponseBody ()).append ("\n " )//
222
+ .append ("}" ).toString ();
223
+ return sb .toString ();
224
+ }
99
225
}
0 commit comments