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