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