36
36
37
37
public class JDKResponse implements Response {
38
38
private final static String DEFAULT_CHARSET = "ISO-8859-1" ;
39
- private final static String HEADERS_NOT_COMPUTED = "Response's headers hasn't been computed by your AsyncHandler." ;
40
39
41
40
private final URI uri ;
42
41
private final Collection <HttpResponseBodyPart > bodyParts ;
43
42
private final HttpResponseHeaders headers ;
44
43
private final HttpResponseStatus status ;
45
- private final List <Cookie > cookies = new ArrayList < Cookie >() ;
44
+ private List <Cookie > cookies ;
46
45
private AtomicBoolean contentComputed = new AtomicBoolean (false );
47
46
private String content ;
48
47
@@ -81,28 +80,20 @@ public byte[] getResponseBodyAsBytes() throws IOException {
81
80
}
82
81
83
82
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
83
93
- if (!contentComputed .get ()) {
94
- content = AsyncHttpProviderUtils .contentToString (bodyParts , charset );
84
+ if (!contentComputed .get ()) {
85
+ content = AsyncHttpProviderUtils .contentToString (bodyParts , computeCharset ( charset ) );
95
86
}
96
87
return content ;
97
88
}
98
-
89
+
99
90
/* @Override */
100
91
public InputStream getResponseBodyAsStream () throws IOException {
101
92
if (contentComputed .get ()) {
102
93
return new ByteArrayInputStream (content .getBytes (DEFAULT_CHARSET ));
103
94
}
104
95
105
- if (bodyParts .size () > 0 ) {
96
+ if (! bodyParts .isEmpty () ) {
106
97
return new HttpResponseBodyPartsInputStream (bodyParts .toArray (new HttpResponseBodyPart [bodyParts .size ()]));
107
98
} else {
108
99
return new ByteArrayInputStream ("" .getBytes ());
@@ -116,21 +107,25 @@ public String getResponseBodyExcerpt(int maxLength) throws IOException {
116
107
}
117
108
118
109
public String getResponseBodyExcerpt (int maxLength , String charset ) throws IOException {
119
- String contentType = getContentType ();
120
- if (contentType != null && charset == null ) {
121
- charset = AsyncHttpProviderUtils .parseCharset (contentType );
122
- }
123
-
124
- if (charset == null ) {
125
- charset = DEFAULT_CHARSET ;
126
- }
110
+ charset = computeCharset (charset );
127
111
128
112
if (!contentComputed .get ()) {
129
113
content = AsyncHttpProviderUtils .contentToString (bodyParts , charset == null ? DEFAULT_CHARSET : charset );
130
114
}
131
115
132
116
return content .length () <= maxLength ? content : content .substring (0 , maxLength );
133
117
}
118
+
119
+ private String computeCharset (String charset ) {
120
+ String contentType = getContentType ();
121
+ if (charset == null ) {
122
+ if (contentType != null )
123
+ charset = AsyncHttpProviderUtils .parseCharset (contentType );
124
+ else
125
+ charset = DEFAULT_CHARSET ;
126
+ }
127
+ return charset ;
128
+ }
134
129
135
130
/* @Override */
136
131
@@ -141,37 +136,25 @@ public URI getUri() throws MalformedURLException {
141
136
/* @Override */
142
137
143
138
public String getContentType () {
144
- if (headers == null ) {
145
- throw new IllegalStateException (HEADERS_NOT_COMPUTED );
146
- }
147
- return headers .getHeaders ().getFirstValue ("Content-Type" );
139
+ return getHeader ("Content-Type" );
148
140
}
149
141
150
142
/* @Override */
151
143
152
144
public String getHeader (String name ) {
153
- if (headers == null ) {
154
- throw new IllegalStateException ();
155
- }
156
- return headers .getHeaders ().getFirstValue (name );
145
+ return headers != null ? headers .getHeaders ().getFirstValue (name ): null ;
157
146
}
158
147
159
148
/* @Override */
160
149
161
150
public List <String > getHeaders (String name ) {
162
- if (headers == null ) {
163
- throw new IllegalStateException (HEADERS_NOT_COMPUTED );
164
- }
165
- return headers .getHeaders ().get (name );
151
+ return headers != null ? headers .getHeaders ().get (name ): Collections .<String > emptyList ();
166
152
}
167
153
168
154
/* @Override */
169
155
170
156
public FluentCaseInsensitiveStringsMap getHeaders () {
171
- if (headers == null ) {
172
- throw new IllegalStateException (HEADERS_NOT_COMPUTED );
173
- }
174
- return headers .getHeaders ();
157
+ return headers != null ? headers .getHeaders (): new FluentCaseInsensitiveStringsMap ();
175
158
}
176
159
177
160
/* @Override */
@@ -184,44 +167,46 @@ public boolean isRedirected() {
184
167
185
168
public List <Cookie > getCookies () {
186
169
if (headers == null ) {
187
- throw new IllegalStateException ( HEADERS_NOT_COMPUTED );
170
+ return Collections . emptyList ( );
188
171
}
189
172
if (cookies .isEmpty ()) {
173
+ List <Cookie > localCookies = new ArrayList <Cookie >();
190
174
for (Map .Entry <String , List <String >> header : headers .getHeaders ().entrySet ()) {
191
175
if (header .getKey ().equalsIgnoreCase ("Set-Cookie" )) {
192
176
// TODO: ask for parsed header
193
177
List <String > v = header .getValue ();
194
178
for (String value : v ) {
195
179
Cookie cookie = AsyncHttpProviderUtils .parseCookie (value );
196
- cookies .add (cookie );
180
+ localCookies .add (cookie );
197
181
}
198
182
}
199
183
}
184
+ cookies = Collections .unmodifiableList (localCookies );
200
185
}
201
- return Collections . unmodifiableList ( cookies ) ;
186
+ return cookies ;
202
187
}
203
188
204
189
/**
205
190
* {@inheritDoc}
206
191
*/
207
192
/* @Override */
208
193
public boolean hasResponseStatus () {
209
- return ( bodyParts != null ? true : false ) ;
194
+ return bodyParts != null ;
210
195
}
211
196
212
197
/**
213
198
* {@inheritDoc}
214
199
*/
215
200
/* @Override */
216
201
public boolean hasResponseHeaders () {
217
- return ( headers != null ? true : false ) ;
202
+ return headers != null ;
218
203
}
219
204
220
205
/**
221
206
* {@inheritDoc}
222
207
*/
223
208
/* @Override */
224
209
public boolean hasResponseBody () {
225
- return ( bodyParts != null && bodyParts .size () > 0 ? true : false );
210
+ return bodyParts != null && ! bodyParts .isEmpty ( );
226
211
}
227
212
}
0 commit comments