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.http.nio.support;
29  
30  import java.io.IOException;
31  import java.nio.ByteBuffer;
32  import java.util.List;
33  import java.util.concurrent.atomic.AtomicReference;
34  
35  import org.apache.hc.core5.http.EntityDetails;
36  import org.apache.hc.core5.http.Header;
37  import org.apache.hc.core5.http.HttpException;
38  import org.apache.hc.core5.http.HttpRequest;
39  import org.apache.hc.core5.http.HttpResponse;
40  import org.apache.hc.core5.http.nio.AsyncDataConsumer;
41  import org.apache.hc.core5.http.nio.AsyncEntityProducer;
42  import org.apache.hc.core5.http.nio.AsyncFilterChain;
43  import org.apache.hc.core5.http.nio.AsyncPushProducer;
44  import org.apache.hc.core5.http.nio.AsyncResponseProducer;
45  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
46  import org.apache.hc.core5.http.nio.BasicResponseProducer;
47  import org.apache.hc.core5.http.nio.CapacityChannel;
48  import org.apache.hc.core5.http.nio.DataStreamChannel;
49  import org.apache.hc.core5.http.nio.HandlerFactory;
50  import org.apache.hc.core5.http.nio.ResponseChannel;
51  import org.apache.hc.core5.http.protocol.HttpContext;
52  import org.apache.hc.core5.util.Args;
53  import org.apache.hc.core5.util.Asserts;
54  
55  public final class AsyncServerFilterChainExchangeHandlerFactory implements HandlerFactory<AsyncServerExchangeHandler> {
56  
57      private final AsyncServerFilterChainElement filterChain;
58  
59      public AsyncServerFilterChainExchangeHandlerFactory(final AsyncServerFilterChainElement filterChain) {
60          this.filterChain = Args.notNull(filterChain, "Filter chain");
61      }
62  
63      @Override
64      public AsyncServerExchangeHandler create(final HttpRequest request, final HttpContext context) throws HttpException {
65          return new AsyncServerExchangeHandler() {
66  
67              private final AtomicReference<AsyncDataConsumer> dataConsumerRef = new AtomicReference<>();
68              private final AtomicReference<AsyncResponseProducer> responseProducerRef = new AtomicReference<>();
69  
70              @Override
71              public void handleRequest(
72                      final HttpRequest request,
73                      final EntityDetails entityDetails,
74                      final ResponseChannel responseChannel,
75                      final HttpContext context) throws HttpException, IOException {
76                  dataConsumerRef.set(filterChain.handle(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger() {
77  
78                      @Override
79                      public void sendInformation(
80                              final HttpResponse response) throws HttpException, IOException {
81                          responseChannel.sendInformation(response);
82                      }
83  
84                      @Override
85                      public void submitResponse(
86                              final HttpResponse response,
87                              final AsyncEntityProducer entityProducer) throws HttpException, IOException {
88                          final AsyncResponseProducer responseProducer = new BasicResponseProducer(response, entityProducer);
89                          responseProducerRef.set(responseProducer);
90                          responseProducer.sendResponse(responseChannel);
91                      }
92  
93                      @Override
94                      public void pushPromise(final HttpRequest promise, final AsyncPushProducer responseProducer) throws HttpException, IOException {
95                          responseChannel.pushPromise(promise, responseProducer);
96                      }
97  
98                  }));
99              }
100 
101             @Override
102             public void failed(final Exception cause) {
103                 final AsyncResponseProducer handler = responseProducerRef.get();
104                 if (handler != null) {
105                     handler.failed(cause);
106                 }
107             }
108 
109             @Override
110             public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
111                 final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
112                 if (dataConsumer != null) {
113                     dataConsumer.updateCapacity(capacityChannel);
114                 } else {
115                     capacityChannel.update(Integer.MAX_VALUE);
116                 }
117             }
118 
119             @Override
120             public int consume(final ByteBuffer src) throws IOException {
121                 final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
122                 if (dataConsumer != null) {
123                     return dataConsumer.consume(src);
124                 } else {
125                     return Integer.MAX_VALUE;
126                 }
127             }
128 
129             @Override
130             public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
131                 final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
132                 if (dataConsumer != null) {
133                     dataConsumer.streamEnd(trailers);
134                 }
135             }
136 
137             @Override
138             public int available() {
139                 final AsyncResponseProducer responseProducer = responseProducerRef.get();
140                 Asserts.notNull(responseProducer, "Response producer");
141                 return responseProducer.available();
142             }
143 
144             @Override
145             public void produce(final DataStreamChannel channel) throws IOException {
146                 final AsyncResponseProducer responseProducer = responseProducerRef.get();
147                 Asserts.notNull(responseProducer, "Response producer");
148                 responseProducer.produce(channel);
149             }
150 
151             @Override
152             public void releaseResources() {
153                 final AsyncDataConsumer dataConsumer = dataConsumerRef.getAndSet(null);
154                 if (dataConsumer != null) {
155                     dataConsumer.releaseResources();
156                 }
157                 final AsyncResponseProducer responseProducer = responseProducerRef.getAndSet(null);
158                 if (responseProducer != null) {
159                     responseProducer.releaseResources();
160                 }
161             }
162         };
163     }
164 
165 }