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;
28  
29  import java.io.IOException;
30  import java.nio.ByteBuffer;
31  import java.util.List;
32  
33  import org.apache.hc.core5.concurrent.FutureCallback;
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.HttpResponse;
38  import org.apache.hc.core5.http.Message;
39  import org.apache.hc.core5.http.protocol.HttpContext;
40  import org.apache.hc.core5.util.Args;
41  
42  /**
43   * Basic implementation of {@link AsyncResponseConsumer} that represents response message as
44   * a {@link Message} and relies on a {@link AsyncEntityConsumer} to process response entity
45   * stream.
46   *
47   * @since 5.0
48   */
49  public class BasicResponseConsumer<T> implements AsyncResponseConsumer<Message<HttpResponse, T>> {
50  
51      private final AsyncEntityConsumer<T> dataConsumer;
52  
53      private volatile Message<HttpResponse, T> result;
54  
55      public BasicResponseConsumer(final AsyncEntityConsumer<T> dataConsumer) {
56          this.dataConsumer = Args.notNull(dataConsumer, "Consumer");
57      }
58  
59      @Override
60      public void consumeResponse(
61              final HttpResponse response,
62              final EntityDetails entityDetails,
63              final HttpContext httpContext, final FutureCallback<Message<HttpResponse, T>> resultCallback) throws HttpException, IOException {
64          Args.notNull(response, "Response");
65  
66          if (entityDetails != null) {
67              dataConsumer.streamStart(entityDetails, new FutureCallback<T>() {
68  
69                  @Override
70                  public void completed(final T body) {
71                      result = new Message<>(response, body);
72                      if (resultCallback != null) {
73                          resultCallback.completed(result);
74                      }
75                      dataConsumer.releaseResources();
76                  }
77  
78                  @Override
79                  public void failed(final Exception ex) {
80                      if (resultCallback != null) {
81                          resultCallback.failed(ex);
82                      }
83                  }
84  
85                  @Override
86                  public void cancelled() {
87                      if (resultCallback != null) {
88                          resultCallback.cancelled();
89                      }
90                  }
91  
92              });
93          } else {
94              result = new Message<>(response, null);
95              if (resultCallback != null) {
96                  resultCallback.completed(result);
97              }
98              dataConsumer.releaseResources();
99          }
100     }
101 
102     @Override
103     public void informationResponse(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
104     }
105 
106     @Override
107     public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
108         dataConsumer.updateCapacity(capacityChannel);
109     }
110 
111     @Override
112     public void consume(final ByteBuffer src) throws IOException {
113         dataConsumer.consume(src);
114     }
115 
116     @Override
117     public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
118         dataConsumer.streamEnd(trailers);
119     }
120 
121     @Override
122     public void failed(final Exception cause) {
123         releaseResources();
124     }
125 
126     @Override
127     public Message<HttpResponse, T> getResult() {
128         return result;
129     }
130 
131     @Override
132     public void releaseResources() {
133         dataConsumer.releaseResources();
134     }
135 
136 }