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     public final synchronized void responseReceived(
124             final HttpResponse response) throws IOException, HttpException {
125         onResponseReceived(response);
126         final HttpEntity entity = response.getEntity();
127         if (entity != null) {
128             final ContentType contentType = ContentType.getOrDefault(entity);
129             onEntityEnclosed(entity, contentType);
130         }
131     }
132 
133     /**
134      * Use {@link #onContentReceived(ContentDecoder, IOControl)} instead.
135      */
136     public final synchronized void consumeContent(
137             final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
138         onContentReceived(decoder, ioctrl);
139     }
140 
141     /**
142      * Use {@link #buildResult(HttpContext)} instead.
143      */
144     public final synchronized void responseCompleted(final HttpContext context) {
145         if (this.completed) {
146             return;
147         }
148         this.completed = true;
149         try {
150             this.result = buildResult(context);
151         } catch (final Exception ex) {
152             this.ex = ex;
153         } finally {
154             releaseResources();
155         }
156     }
157 
158     public final synchronized boolean cancel() {
159         if (this.completed) {
160             return false;
161         }
162         this.completed = true;
163         releaseResources();
164         return true;
165     }
166 
167     public final synchronized void failed(final Exception ex) {
168         if (this.completed) {
169             return;
170         }
171         this.completed = true;
172         this.ex = ex;
173         releaseResources();
174     }
175 
176     public final synchronized void close() throws IOException {
177         if (this.completed) {
178             return;
179         }
180         this.completed = true;
181         releaseResources();
182         onClose();
183     }
184 
185     public Exception getException() {
186         return this.ex;
187     }
188 
189     public T getResult() {
190         return this.result;
191     }
192 
193     public boolean isDone() {
194         return this.completed;
195     }
196 
197 }