29
29
import java .io .IOException ;
30
30
import java .nio .ByteBuffer ;
31
31
import java .nio .channels .WritableByteChannel ;
32
- import java .time . Instant ;
32
+ import java .util . Date ;
33
33
import java .util .Locale ;
34
+ import java .util .stream .Collectors ;
35
+ import java .util .stream .Stream ;
34
36
import org .apache .http .Header ;
35
37
import org .apache .http .HeaderIterator ;
36
38
import org .apache .http .HttpEntity ;
40
42
import org .apache .http .StatusLine ;
41
43
import org .apache .http .entity .ContentType ;
42
44
import org .apache .http .entity .StringEntity ;
45
+ import org .apache .http .impl .client .BasicResponseHandler ;
46
+ import org .apache .http .message .BasicHttpResponse ;
43
47
import org .apache .http .message .BasicStatusLine ;
44
48
import org .apache .http .params .HttpParams ;
45
- import org .apache .http .util .EntityUtils ;
46
49
47
50
/**
48
51
* An {@link HttpResponse} suitable for tests. Can be configured with
51
54
* @author George Aristy (george.aristy@gmail.com)
52
55
* @version $Id$
53
56
* @since 0.0.1
54
- * @todo #79:30min The 'asString()' method needs a little more work (fix the
55
- * formatting on the date header value, etc) and then test the 'printTo()'
56
- * method in conjunction with the UnixServer.
57
57
*/
58
58
public final class Response implements HttpResponse {
59
+
59
60
/**
60
- * This response's status line .
61
+ * Its backbone, holding what we need .
61
62
*/
62
- private final StatusLine statusLine ;
63
- /**
64
- * This response's payload.
65
- */
66
- private final HttpEntity payload ;
67
-
63
+ private HttpResponse backbone ;
64
+
68
65
/**
69
66
* Ctor.
70
67
* <p>
@@ -82,17 +79,26 @@ public Response(final int status) {
82
79
* @param jsonPayload The json payload
83
80
*/
84
81
public Response (final int status , final String jsonPayload ) {
85
- this .statusLine = new BasicStatusLine (
86
- new ProtocolVersion ("HTTP" , 1 , 1 ), status , ""
82
+ this .backbone = new BasicHttpResponse (
83
+ new BasicStatusLine (
84
+ new ProtocolVersion ("HTTP" , 1 , 1 ), status , "REASON"
85
+ )
87
86
);
88
- this .payload = new StringEntity (
89
- jsonPayload , ContentType .APPLICATION_JSON
87
+ this .backbone .setEntity (
88
+ new StringEntity (
89
+ jsonPayload , ContentType .APPLICATION_JSON
90
+ )
91
+ );
92
+ this .backbone .setHeader ("Date" , new Date ().toString ());
93
+ this .backbone .setHeader (
94
+ "Content-Length" , String .valueOf (jsonPayload .getBytes ().length )
90
95
);
96
+ this .backbone .setHeader ("Content-Type" , "application/json" );
91
97
}
92
98
93
99
@ Override
94
100
public StatusLine getStatusLine () {
95
- return this .statusLine ;
101
+ return this .backbone . getStatusLine () ;
96
102
}
97
103
98
104
@ Override
@@ -124,7 +130,7 @@ public void setReasonPhrase(final String reason)
124
130
125
131
@ Override
126
132
public HttpEntity getEntity () {
127
- return this .payload ;
133
+ return this .backbone . getEntity () ;
128
134
}
129
135
130
136
@ Override
@@ -235,38 +241,39 @@ public void setParams(final HttpParams params) {
235
241
public void printTo (final WritableByteChannel channel ) throws IOException {
236
242
channel .write (
237
243
ByteBuffer .wrap (
238
- this .asString ().getBytes (Charsets .UTF8_CHARSET )
244
+ this .toString ().getBytes (Charsets .UTF8_CHARSET )
239
245
)
240
246
);
241
247
}
242
248
243
249
/**
244
250
* This response as a string.
245
251
* @return String representation of this {@link Response}.
246
- * @throws IOException If an error occurs.
247
252
*/
248
- private String asString () throws IOException {
253
+ @ Override
254
+ public String toString () {
249
255
final String CRLF = "" + (char ) 0x0D + (char ) 0x0A ;
250
- final StringBuilder builder = new StringBuilder ("HTTP/" )
251
- .append (this .statusLine .getProtocolVersion ())
252
- .append (" " )
253
- .append (this .statusLine .getStatusCode ())
254
- .append (" " )
255
- .append (this .statusLine .getReasonPhrase ())
256
- .append (CRLF )
257
- .append ("Date: " )
258
- .append (Instant .now ())
259
- .append (CRLF );
260
- if (this .payload .getContentLength () > 0 ) {
261
- builder .append ("ContentType: " )
262
- .append (this .payload .getContentType ().getValue ())
263
- .append (CRLF )
264
- .append ("Content-Length: " )
265
- .append (this .payload .getContentLength ())
266
- .append (CRLF )
267
- .append (CRLF )
268
- .append (EntityUtils .toString (this .payload ));
256
+ try {
257
+ final StringBuilder builder = new StringBuilder ()
258
+ .append (this .backbone .getStatusLine ().toString ())
259
+ .append (CRLF )
260
+ .append (
261
+ Stream .of (
262
+ this .backbone .getAllHeaders ()
263
+ ).map (header -> header .toString ())
264
+ .collect (Collectors .joining (CRLF ))
265
+ )
266
+ .append (CRLF ).append (CRLF )
267
+ .append (
268
+ new BasicResponseHandler ().handleEntity (
269
+ this .backbone .getEntity ()
270
+ )
271
+ );
272
+ return builder .toString ();
273
+ } catch (final IOException ex ) {
274
+ throw new IllegalStateException (
275
+ "IOException when reading the HTTP response. " + ex
276
+ );
269
277
}
270
- return builder .toString ();
271
278
}
272
279
}
0 commit comments