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.hc.client5.testing.async;
28
29 import java.io.IOException;
30
31 import org.apache.hc.client5.http.async.methods.SimpleBody;
32 import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
33 import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
34 import org.apache.hc.core5.http.ContentType;
35 import org.apache.hc.core5.http.EntityDetails;
36 import org.apache.hc.core5.http.HttpException;
37 import org.apache.hc.core5.http.HttpRequest;
38 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
39 import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
40 import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
41 import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityConsumer;
42 import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
43 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
44 import org.apache.hc.core5.http.nio.support.AbstractAsyncRequesterConsumer;
45 import org.apache.hc.core5.http.nio.support.AbstractServerExchangeHandler;
46 import org.apache.hc.core5.http.nio.support.BasicResponseProducer;
47 import org.apache.hc.core5.http.protocol.HttpContext;
48 import org.apache.hc.core5.http.protocol.HttpCoreContext;
49
50 public abstract class AbstractSimpleServerExchangeHandler extends AbstractServerExchangeHandler<SimpleHttpRequest> {
51
52 protected abstract SimpleHttpResponse handle(SimpleHttpRequest request, HttpCoreContext context) throws HttpException;
53
54 @Override
55 protected final AsyncRequestConsumer<SimpleHttpRequest> supplyConsumer(
56 final HttpRequest request,
57 final EntityDetails entityDetails,
58 final HttpContext context) throws HttpException {
59 return new AbstractAsyncRequesterConsumer<SimpleHttpRequest, byte[]>(new BasicAsyncEntityConsumer()) {
60
61 @Override
62 protected SimpleHttpRequest buildResult(
63 final HttpRequest request,
64 final byte[] body,
65 final ContentType contentType) {
66 final SimpleHttpRequest simpleRequest = SimpleHttpRequest.copy(request);
67 if (body != null) {
68 simpleRequest.setBody(body, contentType);
69 }
70 return simpleRequest;
71 }
72
73 };
74 }
75
76 @Override
77 protected final void handle(
78 final SimpleHttpRequest request,
79 final AsyncServerRequestHandler.ResponseTrigger responseTrigger,
80 final HttpContext context) throws HttpException, IOException {
81 final SimpleHttpResponse response = handle(request, HttpCoreContext.adapt(context));
82 final SimpleBody body = response.getBody();
83 final AsyncEntityProducer entityProducer;
84 if (body != null) {
85 if (body.isText()) {
86 entityProducer = new StringAsyncEntityProducer(body.getBodyText(), body.getContentType());
87 } else {
88 entityProducer = new BasicAsyncEntityProducer(body.getBodyBytes(), body.getContentType());
89 }
90 } else {
91 entityProducer = null;
92 }
93 responseTrigger.submitResponse(new BasicResponseProducer(response, entityProducer), context);
94
95 }
96
97 }