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 int consume(final ByteBuffer src) throws IOException {
105         final AsyncResponseProducer responseProducer = responseProducerRef.get();
106         return responseProducer == null ? handler.consume(src) : Integer.MAX_VALUE;
107     }
108 
109     @Override
110     public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
111         final AsyncResponseProducer responseProducer = responseProducerRef.get();
112         if (responseProducer == null) {
113             handler.streamEnd(trailers);
114         }
115     }
116 
117     @Override
118     public final int available() {
119         final AsyncResponseProducer responseProducer = responseProducerRef.get();
120         return responseProducer == null ? handler.available() : responseProducer.available();
121     }
122 
123     @Override
124     public final void produce(final DataStreamChannel channel) throws IOException {
125         final AsyncResponseProducer responseProducer = responseProducerRef.get();
126         if (responseProducer == null) {
127             handler.produce(channel);
128         } else {
129             responseProducer.produce(channel);
130         }
131     }
132 
133     @Override
134     public final void failed(final Exception cause) {
135         try {
136             handler.failed(cause);
137             final AsyncResponseProducer dataProducer = responseProducerRef.getAndSet(null);
138             if (dataProducer != null) {
139                 dataProducer.failed(cause);
140             }
141         } finally {
142             releaseResources();
143         }
144     }
145 
146     @Override
147     public final void releaseResources() {
148         handler.releaseResources();
149         final AsyncResponseProducer dataProducer = responseProducerRef.getAndSet(null);
150         if (dataProducer != null) {
151             dataProducer.releaseResources();
152         }
153     }
154 
155 }