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  
33  import org.apache.hc.core5.http.EntityDetails;
34  import org.apache.hc.core5.http.Header;
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.message.BasicHttpResponse;
39  import org.apache.hc.core5.http.nio.AsyncResponseProducer;
40  import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
41  import org.apache.hc.core5.http.nio.CapacityChannel;
42  import org.apache.hc.core5.http.nio.DataStreamChannel;
43  import org.apache.hc.core5.http.nio.ResponseChannel;
44  import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
45  import org.apache.hc.core5.http.protocol.HttpContext;
46  import org.apache.hc.core5.util.Args;
47  
48  /**
49   * {@link AsyncServerExchangeHandler} implementation that immediately responds
50   * with a predefined response generated by a {@link AsyncResponseProducer} and
51   * ignores any entity content enclosed in the request message.
52   *
53   * @since 5.0
54   */
55  public final class ImmediateResponseExchangeHandler implements AsyncServerExchangeHandler {
56  
57      private final AsyncResponseProducer responseProducer;
58  
59      public ImmediateResponseExchangeHandler(final AsyncResponseProducer responseProducer) {
60          this.responseProducer = Args.notNull(responseProducer, "Response producer");
61      }
62  
63      public ImmediateResponseExchangeHandler(final HttpResponse response, final String message) {
64          this(new BasicResponseProducer(response, AsyncEntityProducers.create(message)));
65      }
66  
67      public ImmediateResponseExchangeHandler(final int status, final String message) {
68          this(new BasicHttpResponse(status), message);
69      }
70  
71      @Override
72      public void handleRequest(
73              final HttpRequest request,
74              final EntityDetails entityDetails,
75              final ResponseChannel responseChannel,
76              final HttpContext context) throws HttpException, IOException {
77          responseProducer.sendResponse(responseChannel, context);
78      }
79  
80      @Override
81      public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
82          capacityChannel.update(Integer.MAX_VALUE);
83      }
84  
85      @Override
86      public void consume(final ByteBuffer src) throws IOException {
87      }
88  
89      @Override
90      public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
91      }
92  
93      @Override
94      public int available() {
95          return responseProducer.available();
96      }
97  
98      @Override
99      public void produce(final DataStreamChannel channel) throws IOException {
100         responseProducer.produce(channel);
101     }
102 
103     @Override
104     public void failed(final Exception cause) {
105         responseProducer.failed(cause);
106         releaseResources();
107     }
108 
109     @Override
110     public void releaseResources() {
111         responseProducer.releaseResources();
112     }
113 
114 }