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.hc.core5.testing.reactive;
29
30 import java.io.IOException;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.nio.ByteBuffer;
34
35 import org.apache.hc.core5.function.Callback;
36 import org.apache.hc.core5.http.ContentType;
37 import org.apache.hc.core5.http.EntityDetails;
38 import org.apache.hc.core5.http.HeaderElements;
39 import org.apache.hc.core5.http.HttpException;
40 import org.apache.hc.core5.http.HttpHeaders;
41 import org.apache.hc.core5.http.HttpRequest;
42 import org.apache.hc.core5.http.HttpResponse;
43 import org.apache.hc.core5.http.HttpStatus;
44 import org.apache.hc.core5.http.MethodNotSupportedException;
45 import org.apache.hc.core5.http.ProtocolException;
46 import org.apache.hc.core5.http.impl.BasicEntityDetails;
47 import org.apache.hc.core5.http.message.BasicHeader;
48 import org.apache.hc.core5.http.message.BasicHttpResponse;
49 import org.apache.hc.core5.http.nio.ResponseChannel;
50 import org.apache.hc.core5.http.protocol.HttpContext;
51 import org.apache.hc.core5.reactive.ReactiveRequestProcessor;
52 import org.reactivestreams.Publisher;
53
54 import io.reactivex.Flowable;
55
56 public class ReactiveRandomProcessor implements ReactiveRequestProcessor {
57 public ReactiveRandomProcessor() {
58 }
59
60 @Override
61 public void processRequest(
62 final HttpRequest request,
63 final EntityDetails entityDetails,
64 final ResponseChannel responseChannel,
65 final HttpContext context,
66 final Publisher<ByteBuffer> requestBody,
67 final Callback<Publisher<ByteBuffer>> responseBodyCallback
68 ) throws HttpException, IOException {
69 final String method = request.getMethod();
70 if (!"GET".equalsIgnoreCase(method) &&
71 !"HEAD".equalsIgnoreCase(method) &&
72 !"POST".equalsIgnoreCase(method) &&
73 !"PUT".equalsIgnoreCase(method)) {
74 throw new MethodNotSupportedException(method + " not supported by " + getClass().getName());
75 }
76 final URI uri;
77 try {
78 uri = request.getUri();
79 } catch (final URISyntaxException ex) {
80 throw new ProtocolException(ex.getMessage(), ex);
81 }
82 final String path = uri.getPath();
83 final int slash = path.lastIndexOf('/');
84 if (slash != -1) {
85 final String payload = path.substring(slash + 1);
86 final long n;
87 if (!payload.isEmpty()) {
88 try {
89 n = Long.parseLong(payload);
90 } catch (final NumberFormatException ex) {
91 throw new ProtocolException("Invalid request path: " + path);
92 }
93 } else {
94
95 n = 1 + (int) (Math.random() * 79.0);
96 }
97
98 if (new BasicHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE).equals(request.getHeader(HttpHeaders.EXPECT))) {
99 responseChannel.sendInformation(new BasicHttpResponse(100), context);
100 }
101
102 final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
103 final Flowable<ByteBuffer> stream = ReactiveTestUtils.produceStream(n);
104 final String hash = ReactiveTestUtils.getStreamHash(n);
105 response.addHeader("response-hash-code", hash);
106 final BasicEntityDetails basicEntityDetails = new BasicEntityDetails(n, ContentType.APPLICATION_OCTET_STREAM);
107 responseChannel.sendResponse(response, basicEntityDetails, context);
108 responseBodyCallback.execute(stream);
109 } else {
110 throw new ProtocolException("Invalid request path: " + path);
111 }
112 }
113 }