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