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.nio.charset.UnsupportedCharsetException;
32  import java.util.List;
33  
34  import org.apache.hc.core5.concurrent.FutureCallback;
35  import org.apache.hc.core5.http.ContentType;
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.nio.AsyncEntityConsumer;
41  import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
42  import org.apache.hc.core5.http.nio.CapacityChannel;
43  import org.apache.hc.core5.util.Args;
44  
45  public abstract class AbstractAsyncResponseConsumer<T, E> implements AsyncResponseConsumer<T> {
46  
47      private final AsyncEntityConsumer<E> entityConsumer;
48  
49      private volatile T result;
50  
51      public AbstractAsyncResponseConsumer(final AsyncEntityConsumer<E> entityConsumer) {
52          Args.notNull(entityConsumer, "Entity consumer");
53          this.entityConsumer = entityConsumer;
54      }
55  
56      protected abstract T buildResult(HttpResponse response, E entity, ContentType contentType);
57  
58      @Override
59      public final void consumeResponse(
60              final HttpResponse response,
61              final EntityDetails entityDetails,
62              final FutureCallback<T> resultCallback) throws HttpException, IOException {
63          if (entityDetails != null) {
64              entityConsumer.streamStart(entityDetails, new FutureCallback<E>() {
65  
66                  @Override
67                  public void completed(final E entity) {
68                      final ContentType contentType;
69                      try {
70                          contentType = ContentType.parse(entityDetails.getContentType());
71                          result = buildResult(response, entity, contentType);
72                          resultCallback.completed(result);
73                      } catch (final UnsupportedCharsetException ex) {
74                          resultCallback.failed(ex);
75                      }
76                  }
77  
78                  @Override
79                  public void failed(final Exception ex) {
80                      resultCallback.failed(ex);
81                  }
82  
83                  @Override
84                  public void cancelled() {
85                      resultCallback.cancelled();
86                  }
87  
88              });
89          } else {
90              result = buildResult(response, null, null);
91              resultCallback.completed(result);
92              entityConsumer.releaseResources();
93          }
94  
95      }
96  
97      @Override
98      public final void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
99          entityConsumer.updateCapacity(capacityChannel);
100     }
101 
102     @Override
103     public final int consume(final ByteBuffer src) throws IOException {
104         return entityConsumer.consume(src);
105     }
106 
107     @Override
108     public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
109         entityConsumer.streamEnd(trailers);
110     }
111 
112     @Override
113     public T getResult() {
114         return result;
115     }
116 
117     @Override
118     public final void failed(final Exception cause) {
119         releaseResources();
120     }
121 
122     @Override
123     public final void releaseResources() {
124         entityConsumer.releaseResources();
125     }
126 
127 }