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.http.protocol.HttpContext;
43  import org.apache.hc.core5.util.Args;
44  
45  /**
46   * Abstract push response handler.
47   *
48   * @since 5.0
49   *
50   * @param <T> response message representation.
51   */
52  public abstract class AbstractAsyncPushHandler<T> implements AsyncPushConsumer {
53  
54      private final AsyncResponseConsumer<T> responseConsumer;
55  
56      public AbstractAsyncPushHandler(final AsyncResponseConsumer<T> responseConsumer) {
57          this.responseConsumer = Args.notNull(responseConsumer, "Response consumer");
58      }
59  
60      /**
61       * Triggered to handle the push message with the given promised request.
62       *
63       * @param promise the promised request message.
64       * @param responseMessage the pushed response message.
65       */
66      protected abstract void handleResponse(
67              final HttpRequest promise, final T responseMessage) throws IOException, HttpException;
68  
69      /**
70       * Triggered to handle the exception thrown while processing a push response.
71       *
72       * @param promise the promised request message.
73       * @param cause the cause of error.
74       */
75      protected void handleError(final HttpRequest promise, final Exception cause) {
76      }
77  
78      @Override
79      public final void consumePromise(
80              final HttpRequest promise,
81              final HttpResponse response,
82              final EntityDetails entityDetails,
83              final HttpContext httpContext) throws HttpException, IOException {
84          responseConsumer.consumeResponse(response, entityDetails, httpContext, new FutureCallback<T>() {
85  
86              @Override
87              public void completed(final T result) {
88                  try {
89                      handleResponse(promise, result);
90                  } catch (final Exception ex) {
91                      failed(ex);
92                  }
93              }
94  
95              @Override
96              public void failed(final Exception cause) {
97                  handleError(promise, cause);
98                  releaseResources();
99              }
100 
101             @Override
102             public void cancelled() {
103                 releaseResources();
104             }
105 
106         });
107     }
108 
109     @Override
110     public final void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
111         responseConsumer.updateCapacity(capacityChannel);
112     }
113 
114     @Override
115     public final void consume(final ByteBuffer src) throws IOException {
116         responseConsumer.consume(src);
117     }
118 
119     @Override
120     public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
121         responseConsumer.streamEnd(trailers);
122     }
123 
124     @Override
125     public final void failed(final Exception cause) {
126         responseConsumer.failed(cause);
127         releaseResources();
128     }
129 
130     @Override
131     public final void releaseResources() {
132         if (responseConsumer != null) {
133             responseConsumer.releaseResources();
134         }
135     }
136 
137 }