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