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.http.nio.protocol;
28  
29  import java.io.IOException;
30  
31  import org.apache.http.HttpEntity;
32  import org.apache.http.HttpException;
33  import org.apache.http.HttpResponse;
34  import org.apache.http.annotation.ThreadSafe;
35  import org.apache.http.entity.ContentType;
36  import org.apache.http.nio.ContentDecoder;
37  import org.apache.http.nio.IOControl;
38  import org.apache.http.protocol.HttpContext;
39  
40  /**
41   * Abstract {@link HttpAsyncResponseConsumer} implementation that relieves its
42   * subclasses form having to synchronize access to internal instance variables
43   * and provides a number of protected methods that they need to implement.
44   *
45   * @since 4.2
46   */
47  @ThreadSafe
48  public abstract class AbstractAsyncResponseConsumer<T> implements HttpAsyncResponseConsumer<T> {
49  
50      private volatile boolean completed;
51      private volatile T result;
52      private volatile Exception ex;
53  
54      public AbstractAsyncResponseConsumer() {
55          super();
56      }
57  
58      /**
59       * Invoked when a HTTP response message is received. Please note
60       * that the {@link #onContentReceived(ContentDecoder, IOControl)} method
61       * will be invoked only if the response messages has a content entity
62       * enclosed.
63       *
64       * @param response HTTP response message.
65       * @throws HttpException in case of HTTP protocol violation
66       * @throws IOException in case of an I/O error
67       */
68      protected abstract void onResponseReceived(
69              HttpResponse response) throws HttpException, IOException;
70  
71      /**
72       * Invoked to process a chunk of content from the {@link ContentDecoder}.
73       * The {@link IOControl} interface can be used to suspend input events
74       * if the consumer is temporarily unable to consume more content.
75       * <p/>
76       * The consumer can use the {@link ContentDecoder#isCompleted()} method
77       * to find out whether or not the message content has been fully consumed.
78       *
79       * @param decoder content decoder.
80       * @param ioctrl I/O control of the underlying connection.
81       * @throws IOException in case of an I/O error
82       */
83      protected abstract void onContentReceived(
84              ContentDecoder decoder, IOControl ioctrl) throws IOException;
85  
86      /**
87       * Invoked if the response message encloses a content entity.
88       *
89       * @param entity HTTP entity
90       * @param contentType expected content type.
91       * @throws IOException in case of an I/O error
92       */
93      protected abstract void onEntityEnclosed(
94              HttpEntity entity, ContentType contentType) throws IOException;
95  
96      /**
97       * Invoked to generate a result object from the received HTTP response
98       * message.
99       *
100      * @param context HTTP context.
101      * @return result of the response processing.
102      * @throws Exception in case of an abnormal termination.
103      */
104     protected abstract T buildResult(HttpContext context) throws Exception;
105 
106     /**
107      * Invoked to release all system resources currently allocated.
108      */
109     protected abstract void releaseResources();
110 
111     /**
112      * Invoked when the consumer is being closed.
113      * @throws IOException may be thrown by subclassses
114      *
115      * @since 4.3
116      */
117     protected void onClose() throws IOException {
118     }
119 
120     /**
121      * Use {@link #onResponseReceived(HttpResponse)} instead.
122      */
123     @Override
124     public final synchronized void responseReceived(
125             final HttpResponse response) throws IOException, HttpException {
126         onResponseReceived(response);
127         final HttpEntity entity = response.getEntity();
128         if (entity != null) {
129             final ContentType contentType = ContentType.getOrDefault(entity);
130             onEntityEnclosed(entity, contentType);
131         }
132     }
133 
134     /**
135      * Use {@link #onContentReceived(ContentDecoder, IOControl)} instead.
136      */
137     @Override
138     public final synchronized void consumeContent(
139             final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
140         onContentReceived(decoder, ioctrl);
141     }
142 
143     /**
144      * Use {@link #buildResult(HttpContext)} instead.
145      */
146     @Override
147     public final synchronized void responseCompleted(final HttpContext context) {
148         if (this.completed) {
149             return;
150         }
151         this.completed = true;
152         try {
153             this.result = buildResult(context);
154         } catch (final Exception ex) {
155             this.ex = ex;
156         } finally {
157             releaseResources();
158         }
159     }
160 
161     @Override
162     public final synchronized boolean cancel() {
163         if (this.completed) {
164             return false;
165         }
166         this.completed = true;
167         releaseResources();
168         return true;
169     }
170 
171     @Override
172     public final synchronized void failed(final Exception ex) {
173         if (this.completed) {
174             return;
175         }
176         this.completed = true;
177         this.ex = ex;
178         releaseResources();
179     }
180 
181     @Override
182     public final synchronized void close() throws IOException {
183         if (this.completed) {
184             return;
185         }
186         this.completed = true;
187         releaseResources();
188         onClose();
189     }
190 
191     @Override
192     public Exception getException() {
193         return this.ex;
194     }
195 
196     @Override
197     public T getResult() {
198         return this.result;
199     }
200 
201     @Override
202     public boolean isDone() {
203         return this.completed;
204     }
205 
206 }