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