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