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<>(null);
63      }
64  
65      public BasicResponseConsumer(final AsyncEntityConsumer<T> dataConsumer) {
66          this(new Supplier<AsyncEntityConsumer<T>>() {
67  
68              @Override
69              public AsyncEntityConsumer<T> get() {
70                  return dataConsumer;
71              }
72  
73          });
74      }
75  
76      @Override
77      public void consumeResponse(
78              final HttpResponse response,
79              final EntityDetails entityDetails,
80              final HttpContext httpContext, final FutureCallback<Message<HttpResponse, T>> resultCallback) throws HttpException, IOException {
81          Args.notNull(response, "Response");
82  
83          if (entityDetails != null) {
84              final AsyncEntityConsumer<T> dataConsumer = dataConsumerSupplier.get();
85              if (dataConsumer == null) {
86                  throw new HttpException("Supplied data consumer is null");
87              }
88              dataConsumerRef.set(dataConsumer);
89              dataConsumer.streamStart(entityDetails, new CallbackContribution<T>(resultCallback) {
90  
91                  @Override
92                  public void completed(final T body) {
93                      final Message<HttpResponse, T> result = new Message<>(response, body);
94                      if (resultCallback != null) {
95                          resultCallback.completed(result);
96                      }
97                  }
98  
99              });
100         } else {
101             final Message<HttpResponse, T> result = new Message<>(response, null);
102             if (resultCallback != null) {
103                 resultCallback.completed(result);
104             }
105         }
106     }
107 
108     @Override
109     public void informationResponse(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
110     }
111 
112     @Override
113     public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
114         final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
115         dataConsumer.updateCapacity(capacityChannel);
116     }
117 
118     @Override
119     public void consume(final ByteBuffer src) throws IOException {
120         final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
121         dataConsumer.consume(src);
122     }
123 
124     @Override
125     public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
126         final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
127         dataConsumer.streamEnd(trailers);
128     }
129 
130     @Override
131     public void failed(final Exception cause) {
132         final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
133         if (dataConsumer != null) {
134             dataConsumer.failed(cause);
135         }
136         releaseResources();
137     }
138 
139     @Override
140     public void releaseResources() {
141         final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.getAndSet(null);
142         if (dataConsumer != null) {
143             dataConsumer.releaseResources();
144         }
145     }
146 
147 }