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.HttpRequest;
40  import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
41  import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
42  import org.apache.hc.core5.http.nio.CapacityChannel;
43  import org.apache.hc.core5.util.Args;
44  
45  public abstract class AbstractAsyncRequesterConsumer<T, E> implements AsyncRequestConsumer<T> {
46  
47      private final AsyncEntityConsumer<E> entityConsumer;
48  
49      private volatile T result;
50  
51      public AbstractAsyncRequesterConsumer(final AsyncEntityConsumer<E> entityConsumer) {
52          Args.notNull(entityConsumer, "Entity consumer");
53          this.entityConsumer = entityConsumer;
54      }
55  
56      protected abstract T buildResult(HttpRequest request, E entity, ContentType contentType);
57  
58      @Override
59      public final void consumeRequest(
60              final HttpRequest request,
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(request, 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              resultCallback.completed(buildResult(request, null, null));
91              entityConsumer.releaseResources();
92          }
93  
94      }
95  
96      @Override
97      public final void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
98          entityConsumer.updateCapacity(capacityChannel);
99      }
100 
101     @Override
102     public final int consume(final ByteBuffer src) throws IOException {
103         return entityConsumer.consume(src);
104     }
105 
106     @Override
107     public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
108         entityConsumer.streamEnd(trailers);
109     }
110 
111     @Override
112     public T getResult() {
113         return result;
114     }
115 
116     @Override
117     public final void failed(final Exception cause) {
118         releaseResources();
119     }
120 
121     @Override
122     public final void releaseResources() {
123         entityConsumer.releaseResources();
124     }
125 
126 }