1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.nio.protocol;
29
30 import java.io.IOException;
31
32 import org.apache.http.HttpEntity;
33 import org.apache.http.HttpResponse;
34 import org.apache.http.nio.ContentEncoder;
35 import org.apache.http.nio.IOControl;
36 import org.apache.http.nio.entity.EntityAsyncContentProducer;
37 import org.apache.http.nio.entity.HttpAsyncContentProducer;
38 import org.apache.http.protocol.HTTP;
39 import org.apache.http.protocol.HttpContext;
40
41
42
43
44
45
46 public class ErrorResponseProducer implements HttpAsyncResponseProducer {
47
48 private final HttpResponse response;
49 private final HttpEntity entity;
50 private final HttpAsyncContentProducer contentProducer;
51 private final boolean keepAlive;
52
53 public ErrorResponseProducer(
54 final HttpResponse response,
55 final HttpEntity entity,
56 final boolean keepAlive) {
57 super();
58 this.response = response;
59 this.entity = entity;
60 if (entity instanceof HttpAsyncContentProducer) {
61 this.contentProducer = (HttpAsyncContentProducer) entity;
62 } else {
63 this.contentProducer = new EntityAsyncContentProducer(entity);
64 }
65 this.keepAlive = keepAlive;
66 }
67
68 @Override
69 public HttpResponse generateResponse() {
70 if (this.keepAlive) {
71 response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
72 } else {
73 response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
74 }
75 response.setEntity(this.entity);
76 return response;
77 }
78
79 @Override
80 public void produceContent(
81 final ContentEncoder encoder, final IOControl ioControl) throws IOException {
82 this.contentProducer.produceContent(encoder, ioControl);
83 }
84
85 @Override
86 public void responseCompleted(final HttpContext context) {
87 }
88
89 @Override
90 public void failed(final Exception ex) {
91 }
92
93 @Override
94 public void close() throws IOException {
95 this.contentProducer.close();
96 }
97
98 }