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.io.support;
29  
30  import java.io.IOException;
31  
32  import org.apache.hc.core5.http.ClassicHttpRequest;
33  import org.apache.hc.core5.http.ClassicHttpResponse;
34  import org.apache.hc.core5.http.Header;
35  import org.apache.hc.core5.http.HeaderElements;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.HttpHeaders;
38  import org.apache.hc.core5.http.HttpStatus;
39  import org.apache.hc.core5.http.io.HttpServerRequestHandler;
40  import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
41  import org.apache.hc.core5.http.protocol.HttpContext;
42  import org.apache.hc.core5.util.Args;
43  
44  /**
45   * {@link HttpServerRequestHandler} implementation that adds support
46   * for the Expect-Continue handshake to an existing
47   * {@link HttpServerRequestHandler}.
48   *
49   * @since 5.0
50   */
51  public class BasicHttpServerExpectationDecorator implements HttpServerRequestHandler {
52  
53      private final HttpServerRequestHandler requestHandler;
54  
55      public BasicHttpServerExpectationDecorator(final HttpServerRequestHandler requestHandler) {
56          this.requestHandler = Args.notNull(requestHandler, "Request handler");
57      }
58  
59      /**
60       * Verifies the HTTP request and decides whether it meets server expectations and the request
61       * processing can continue.
62       *
63       * @param request the incoming HTTP request.
64       * @param context the actual execution context.
65       * @return {@code null} if the request meets expectations or a final HTTP response
66       *  with an error status representing the cause of expectation failure.
67       */
68      protected ClassicHttpResponse verify(final ClassicHttpRequest request, final HttpContext context) {
69          return null;
70      }
71  
72      @Override
73      public final void handle(
74              final ClassicHttpRequest request,
75              final ResponseTrigger responseTrigger,
76              final HttpContext context) throws HttpException, IOException {
77          final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
78          if (expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue())) {
79              final ClassicHttpResponse response = verify(request, context);
80              if (response == null) {
81                  responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
82              } else {
83                  responseTrigger.submitResponse(response);
84                  return;
85              }
86          }
87          requestHandler.handle(request, responseTrigger, context);
88      }
89  
90  }