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