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 package org.apache.http.nio.protocol;
28
29 import java.io.IOException;
30 import java.util.concurrent.atomic.AtomicBoolean;
31
32 import org.apache.http.HttpEntity;
33 import org.apache.http.HttpEntityEnclosingRequest;
34 import org.apache.http.HttpException;
35 import org.apache.http.HttpRequest;
36 import org.apache.http.entity.ContentType;
37 import org.apache.http.nio.ContentDecoder;
38 import org.apache.http.nio.IOControl;
39 import org.apache.http.protocol.HttpContext;
40
41
42
43
44
45
46
47
48 public abstract class AbstractAsyncRequestConsumer<T> implements HttpAsyncRequestConsumer<T> {
49
50 private final AtomicBoolean completed;
51
52 private volatile T result;
53 private volatile Exception ex;
54
55 public AbstractAsyncRequestConsumer() {
56 super();
57 this.completed = new AtomicBoolean(false);
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71 protected abstract void onRequestReceived(
72 HttpRequest request) throws HttpException, IOException;
73
74
75
76
77
78
79
80
81 protected abstract void onEntityEnclosed(
82 HttpEntity entity, ContentType contentType) throws IOException;
83
84
85
86
87
88
89
90
91
92
93
94
95
96 protected abstract void onContentReceived(
97 ContentDecoder decoder, IOControl ioControl) throws IOException;
98
99
100
101
102
103
104
105
106
107 protected abstract T buildResult(HttpContext context) throws Exception;
108
109
110
111
112 protected abstract void releaseResources();
113
114
115
116
117
118
119
120 protected void onClose() throws IOException {
121 }
122
123
124
125
126 @Override
127 public final void requestReceived(
128 final HttpRequest request) throws HttpException, IOException {
129 onRequestReceived(request);
130 if (request instanceof HttpEntityEnclosingRequest) {
131 final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
132 if (entity != null) {
133 final ContentType contentType = ContentType.getOrDefault(entity);
134 onEntityEnclosed(entity, contentType);
135 }
136 }
137 }
138
139
140
141
142 @Override
143 public final void consumeContent(
144 final ContentDecoder decoder, final IOControl ioControl) throws IOException {
145 onContentReceived(decoder, ioControl);
146 }
147
148
149
150
151 @Override
152 public final void requestCompleted(final HttpContext context) {
153 if (this.completed.compareAndSet(false, true)) {
154 try {
155 this.result = buildResult(context);
156 } catch (final Exception ex) {
157 this.ex = ex;
158 } finally {
159 releaseResources();
160 }
161 }
162 }
163
164 @Override
165 public final void failed(final Exception ex) {
166 if (this.completed.compareAndSet(false, true)) {
167 this.ex = ex;
168 releaseResources();
169 }
170 }
171
172 @Override
173 public final void close() throws IOException {
174 if (this.completed.compareAndSet(false, true)) {
175 releaseResources();
176 onClose();
177 }
178 }
179
180 @Override
181 public Exception getException() {
182 return this.ex;
183 }
184
185 @Override
186 public T getResult() {
187 return this.result;
188 }
189
190 @Override
191 public boolean isDone() {
192 return this.completed.get();
193 }
194
195 }