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  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  }