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