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  
33  import org.apache.hc.core5.concurrent.FutureCallback;
34  import org.apache.hc.core5.http.EntityDetails;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.HttpRequest;
38  import org.apache.hc.core5.http.HttpResponse;
39  import org.apache.hc.core5.http.nio.AsyncPushConsumer;
40  import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
41  import org.apache.hc.core5.http.nio.CapacityChannel;
42  import org.apache.hc.core5.util.Args;
43  
44  /**
45   * @since 5.0
46   */
47  public abstract class AbstractAsyncPushHandler<T> implements AsyncPushConsumer {
48  
49      private final AsyncResponseConsumer<T> responseConsumer;
50  
51      public AbstractAsyncPushHandler(final AsyncResponseConsumer<T> responseConsumer) {
52          this.responseConsumer = Args.notNull(responseConsumer, "Response consumer");
53      }
54  
55      protected abstract void handleResponse(
56              final HttpRequest promise, final T responseMessage) throws IOException, HttpException;
57  
58      protected void handleError(final HttpRequest promise, final Exception cause) {
59      }
60  
61      @Override
62      public void consumePromise(
63              final HttpRequest promise,
64              final HttpResponse response,
65              final EntityDetails entityDetails) throws HttpException, IOException {
66          responseConsumer.consumeResponse(response, entityDetails, new FutureCallback<T>() {
67  
68              @Override
69              public void completed(final T result) {
70                  try {
71                      handleResponse(promise, result);
72                  } catch (final Exception ex) {
73                      failed(ex);
74                  }
75              }
76  
77              @Override
78              public void failed(final Exception cause) {
79                  handleError(promise, cause);
80                  releaseResources();
81              }
82  
83              @Override
84              public void cancelled() {
85                  releaseResources();
86              }
87  
88          });
89      }
90  
91      @Override
92      public final void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
93          responseConsumer.updateCapacity(capacityChannel);
94      }
95  
96      @Override
97      public final int consume(final ByteBuffer src) throws IOException {
98          return responseConsumer.consume(src);
99      }
100 
101     @Override
102     public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
103         responseConsumer.streamEnd(trailers);
104     }
105 
106     @Override
107     public final void failed(final Exception cause) {
108         responseConsumer.failed(cause);
109         releaseResources();
110     }
111 
112     @Override
113     public final void releaseResources() {
114         if (responseConsumer != null) {
115             responseConsumer.releaseResources();
116         }
117     }
118 
119 }