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.util.Set;
31  
32  import org.apache.hc.core5.annotation.Contract;
33  import org.apache.hc.core5.annotation.ThreadingBehavior;
34  import org.apache.hc.core5.http.EntityDetails;
35  import org.apache.hc.core5.http.HttpException;
36  import org.apache.hc.core5.http.HttpRequest;
37  import org.apache.hc.core5.http.HttpResponse;
38  import org.apache.hc.core5.http.HttpStatus;
39  import org.apache.hc.core5.http.message.BasicHttpResponse;
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.AsyncFilterHandler;
44  import org.apache.hc.core5.http.nio.AsyncPushProducer;
45  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
46  import org.apache.hc.core5.http.nio.DataStreamChannel;
47  import org.apache.hc.core5.http.nio.HandlerFactory;
48  import org.apache.hc.core5.http.nio.ResponseChannel;
49  import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
50  import org.apache.hc.core5.http.protocol.HttpContext;
51  import org.apache.hc.core5.util.Args;
52  
53  /**
54   * {@link AsyncFilterHandler} implementation represents a terminal handler
55   * in an asynchronous request processing pipeline that makes use of {@link HandlerFactory}
56   * to dispatch the request to a particular {@link AsyncServerExchangeHandler}.
57   *
58   * @since 5.0
59   */
60  @Contract(threading = ThreadingBehavior.STATELESS)
61  public final class TerminalAsyncServerFilter implements AsyncFilterHandler {
62  
63      final private HandlerFactory<AsyncServerExchangeHandler> handlerFactory;
64  
65      public TerminalAsyncServerFilter(final HandlerFactory<AsyncServerExchangeHandler> handlerFactory) {
66          this.handlerFactory = Args.notNull(handlerFactory, "Handler factory");
67      }
68  
69      @Override
70      public AsyncDataConsumer handle(
71              final HttpRequest request,
72              final EntityDetails entityDetails,
73              final HttpContext context,
74              final AsyncFilterChain.ResponseTrigger responseTrigger,
75              final AsyncFilterChain chain) throws HttpException, IOException {
76          final AsyncServerExchangeHandler exchangeHandler = handlerFactory.create(request, context);
77          if (exchangeHandler != null) {
78              exchangeHandler.handleRequest(request, entityDetails, new ResponseChannel() {
79  
80                  @Override
81                  public void sendInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
82                      responseTrigger.sendInformation(response);
83                  }
84  
85                  @Override
86                  public void sendResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
87                      responseTrigger.submitResponse(response, entityDetails != null ? new AsyncEntityProducer() {
88  
89                          @Override
90                          public void failed(final Exception cause) {
91                              exchangeHandler.failed(cause);
92                          }
93  
94                          @Override
95                          public boolean isRepeatable() {
96                              return false;
97                          }
98  
99                          @Override
100                         public long getContentLength() {
101                             return entityDetails.getContentLength();
102                         }
103 
104                         @Override
105                         public String getContentType() {
106                             return entityDetails.getContentType();
107                         }
108 
109                         @Override
110                         public String getContentEncoding() {
111                             return entityDetails.getContentEncoding();
112                         }
113 
114                         @Override
115                         public boolean isChunked() {
116                             return entityDetails.isChunked();
117                         }
118 
119                         @Override
120                         public Set<String> getTrailerNames() {
121                             return entityDetails.getTrailerNames();
122                         }
123 
124                         @Override
125                         public int available() {
126                             return exchangeHandler.available();
127                         }
128 
129                         @Override
130                         public void produce(final DataStreamChannel channel) throws IOException {
131                             exchangeHandler.produce(channel);
132                         }
133 
134                         @Override
135                         public void releaseResources() {
136                             exchangeHandler.releaseResources();
137                         }
138 
139                     } : null);
140                 }
141 
142                 @Override
143                 public void pushPromise(final HttpRequest promise, final AsyncPushProducer pushProducer, final HttpContext httpContext) throws HttpException, IOException {
144                     responseTrigger.pushPromise(promise, pushProducer);
145                 }
146 
147             }, context);
148             return exchangeHandler;
149         }
150         responseTrigger.submitResponse(new BasicHttpResponse(HttpStatus.SC_NOT_FOUND), AsyncEntityProducers.create("Not found"));
151         return null;
152     }
153 
154 }