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.core5.http.nio.support;
28  
29  import java.io.IOException;
30  import java.nio.ByteBuffer;
31  import java.util.List;
32  import java.util.concurrent.atomic.AtomicReference;
33  
34  import org.apache.hc.core5.http.EntityDetails;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.HttpHeaders;
38  import org.apache.hc.core5.http.HttpRequest;
39  import org.apache.hc.core5.http.HttpStatus;
40  import org.apache.hc.core5.http.message.BasicHttpResponse;
41  import org.apache.hc.core5.http.nio.AsyncResponseProducer;
42  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
43  import org.apache.hc.core5.http.nio.CapacityChannel;
44  import org.apache.hc.core5.http.nio.DataStreamChannel;
45  import org.apache.hc.core5.http.nio.ResponseChannel;
46  import org.apache.hc.core5.http.protocol.HttpContext;
47  import org.apache.hc.core5.util.Args;
48  
49  /**
50   * {@link AsyncServerExchangeHandler} implementation that adds support
51   * for the Expect-Continue handshake to an existing
52   * {@link AsyncServerExchangeHandler}.
53   *
54   * @since 5.0
55   */
56  public class BasicAsyncServerExpectationDecorator implements AsyncServerExchangeHandler {
57  
58      private final AsyncServerExchangeHandler handler;
59      private final AtomicReference<AsyncResponseProducer> responseProducerRef;
60  
61      public BasicAsyncServerExpectationDecorator(final AsyncServerExchangeHandler handler) {
62          this.handler = Args.notNull(handler, "Handler");
63          this.responseProducerRef = new AtomicReference<>(null);
64      }
65  
66      protected AsyncResponseProducer verify(
67              final HttpRequest request,
68              final HttpContext context) throws IOException, HttpException {
69          return null;
70      }
71  
72      @Override
73      public final void handleRequest(
74              final HttpRequest request,
75              final EntityDetails entityDetails,
76              final ResponseChannel responseChannel,
77              final HttpContext context) throws HttpException, IOException {
78          if (entityDetails != null) {
79              final Header h = request.getFirstHeader(HttpHeaders.EXPECT);
80              if (h != null && "100-continue".equalsIgnoreCase(h.getValue())) {
81                  final AsyncResponseProducer producer = verify(request, context);
82                  if (producer != null) {
83                      responseProducerRef.set(producer);
84                      producer.sendResponse(responseChannel, context);
85                      return;
86                  }
87                  responseChannel.sendInformation(new BasicHttpResponse(HttpStatus.SC_CONTINUE), context);
88              }
89          }
90          handler.handleRequest(request, entityDetails, responseChannel, context);
91      }
92  
93      @Override
94      public final void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
95          final AsyncResponseProducer responseProducer = responseProducerRef.get();
96          if (responseProducer == null) {
97              handler.updateCapacity(capacityChannel);
98          } else {
99              capacityChannel.update(Integer.MAX_VALUE);
100         }
101     }
102 
103     @Override
104     public final void consume(final ByteBuffer src) throws IOException {
105         final AsyncResponseProducer responseProducer = responseProducerRef.get();
106         if (responseProducer == null) {
107             handler.consume(src);
108         }
109     }
110 
111     @Override
112     public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
113         final AsyncResponseProducer responseProducer = responseProducerRef.get();
114         if (responseProducer == null) {
115             handler.streamEnd(trailers);
116         }
117     }
118 
119     @Override
120     public final int available() {
121         final AsyncResponseProducer responseProducer = responseProducerRef.get();
122         return responseProducer == null ? handler.available() : responseProducer.available();
123     }
124 
125     @Override
126     public final void produce(final DataStreamChannel channel) throws IOException {
127         final AsyncResponseProducer responseProducer = responseProducerRef.get();
128         if (responseProducer == null) {
129             handler.produce(channel);
130         } else {
131             responseProducer.produce(channel);
132         }
133     }
134 
135     @Override
136     public final void failed(final Exception cause) {
137         try {
138             handler.failed(cause);
139             final AsyncResponseProducer dataProducer = responseProducerRef.getAndSet(null);
140             if (dataProducer != null) {
141                 dataProducer.failed(cause);
142             }
143         } finally {
144             releaseResources();
145         }
146     }
147 
148     @Override
149     public final void releaseResources() {
150         handler.releaseResources();
151         final AsyncResponseProducer dataProducer = responseProducerRef.getAndSet(null);
152         if (dataProducer != null) {
153             dataProducer.releaseResources();
154         }
155     }
156 
157 }