View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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                  // random length, but make sure at least something is sent
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 BasicEntityDetailsls.html#BasicEntityDetails">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 }