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  /**
56   * Factory for {@link AsyncServerExchangeHandler} instances that delegate request processing
57   * to a {@link AsyncServerFilterChainElement}.
58   *
59   * @since 5.0
60   */
61  public final class AsyncServerFilterChainExchangeHandlerFactory implements HandlerFactory<AsyncServerExchangeHandler> {
62  
63      private final AsyncServerFilterChainElement filterChain;
64  
65      public AsyncServerFilterChainExchangeHandlerFactory(final AsyncServerFilterChainElement filterChain) {
66          this.filterChain = Args.notNull(filterChain, "Filter chain");
67      }
68  
69      @Override
70      public AsyncServerExchangeHandler create(final HttpRequest request, final HttpContext context) throws HttpException {
71          return new AsyncServerExchangeHandler() {
72  
73              private final AtomicReference<AsyncDataConsumer> dataConsumerRef = new AtomicReference<>();
74              private final AtomicReference<AsyncResponseProducer> responseProducerRef = new AtomicReference<>();
75  
76              @Override
77              public void handleRequest(
78                      final HttpRequest request,
79                      final EntityDetails entityDetails,
80                      final ResponseChannel responseChannel,
81                      final HttpContext context) throws HttpException, IOException {
82                  dataConsumerRef.set(filterChain.handle(request, entityDetails, context, new AsyncFilterChain.ResponseTrigger() {
83  
84                      @Override
85                      public void sendInformation(
86                              final HttpResponse response) throws HttpException, IOException {
87                          responseChannel.sendInformation(response, context);
88                      }
89  
90                      @Override
91                      public void submitResponse(
92                              final HttpResponse response,
93                              final AsyncEntityProducer entityProducer) throws HttpException, IOException {
94                          final AsyncResponseProducer responseProducer = new BasicResponseProducer(response, entityProducer);
95                          responseProducerRef.set(responseProducer);
96                          responseProducer.sendResponse(responseChannel, context);
97                      }
98  
99                      @Override
100                     public void pushPromise(final HttpRequest promise, final AsyncPushProducer responseProducer) throws HttpException, IOException {
101                         responseChannel.pushPromise(promise, context, responseProducer);
102                     }
103 
104                 }));
105             }
106 
107             @Override
108             public void failed(final Exception cause) {
109                 final AsyncResponseProducer handler = responseProducerRef.get();
110                 if (handler != null) {
111                     handler.failed(cause);
112                 }
113             }
114 
115             @Override
116             public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
117                 final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
118                 if (dataConsumer != null) {
119                     dataConsumer.updateCapacity(capacityChannel);
120                 } else {
121                     capacityChannel.update(Integer.MAX_VALUE);
122                 }
123             }
124 
125             @Override
126             public void consume(final ByteBuffer src) throws IOException {
127                 final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
128                 if (dataConsumer != null) {
129                     dataConsumer.consume(src);
130                 }
131             }
132 
133             @Override
134             public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
135                 final AsyncDataConsumer dataConsumer = dataConsumerRef.get();
136                 if (dataConsumer != null) {
137                     dataConsumer.streamEnd(trailers);
138                 }
139             }
140 
141             @Override
142             public int available() {
143                 final AsyncResponseProducer responseProducer = responseProducerRef.get();
144                 Asserts.notNull(responseProducer, "Response producer");
145                 return responseProducer.available();
146             }
147 
148             @Override
149             public void produce(final DataStreamChannel channel) throws IOException {
150                 final AsyncResponseProducer responseProducer = responseProducerRef.get();
151                 Asserts.notNull(responseProducer, "Response producer");
152                 responseProducer.produce(channel);
153             }
154 
155             @Override
156             public void releaseResources() {
157                 final AsyncDataConsumer dataConsumer = dataConsumerRef.getAndSet(null);
158                 if (dataConsumer != null) {
159                     dataConsumer.releaseResources();
160                 }
161                 final AsyncResponseProducer responseProducer = responseProducerRef.getAndSet(null);
162                 if (responseProducer != null) {
163                     responseProducer.releaseResources();
164                 }
165             }
166         };
167     }
168 
169 }