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