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 28 package org.apache.http.nio.protocol; 29 30 import java.io.Closeable; 31 import java.io.IOException; 32 33 import org.apache.http.HttpException; 34 import org.apache.http.HttpRequest; 35 import org.apache.http.HttpResponse; 36 import org.apache.http.concurrent.Cancellable; 37 import org.apache.http.nio.ContentDecoder; 38 import org.apache.http.nio.ContentEncoder; 39 import org.apache.http.nio.IOControl; 40 41 /** 42 * {@code HttpAsyncClientExchangeHandler} represents a callback interface whose 43 * methods get invoked when executing one or multiple HTTP message exchanges 44 * on the client side. 45 * <p> 46 * Individual {@code HttpAsyncClientExchangeHandler} are expected to make use of 47 * a {@link org.apache.http.protocol.HttpProcessor} to generate mandatory protocol 48 * headers for all outgoing messages and apply common, cross-cutting message 49 * transformations to all incoming and outgoing messages. 50 * {@code HttpAsyncClientExchangeHandler}s can delegate implementation 51 * of application specific content generation and processing to 52 * a {@link HttpAsyncRequestProducer} and a {@link HttpAsyncResponseConsumer}. 53 * 54 * @since 4.3 55 */ 56 public interface HttpAsyncClientExchangeHandler extends Closeable, Cancellable { 57 58 /** 59 * Invoked to generate a HTTP request message head. The message is expected 60 * to implement the {@link org.apache.http.HttpEntityEnclosingRequest} interface if it is 61 * to enclose a content entity. The {@link #produceContent(ContentEncoder, 62 * IOControl)} method will not be invoked if 63 * {@link org.apache.http.HttpEntityEnclosingRequest#getEntity()} returns 64 * {@code null}. 65 * 66 * @return HTTP request message. 67 * @throws HttpException in case of HTTP protocol violation 68 * @throws IOException in case of an I/O error 69 */ 70 HttpRequest generateRequest() throws IOException, HttpException; 71 72 /** 73 * Invoked to write out a chunk of content to the {@link ContentEncoder}. 74 * The {@link IOControl} interface can be used to suspend output event 75 * notifications if the producer is temporarily unable to produce more content. 76 * <p> 77 * When all content is finished, the producer <b>MUST</b> call 78 * {@link ContentEncoder#complete()}. Failure to do so may cause the entity 79 * to be incorrectly delimited. 80 * <p> 81 * Please note that the {@link ContentEncoder} object is not thread-safe and 82 * should only be used within the context of this method call. 83 * The {@link IOControl} object can be shared and used on other thread 84 * to resume output event notifications when more content is made available. 85 * 86 * @param encoder content encoder. 87 * @param ioControl I/O control of the underlying connection. 88 * @throws IOException in case of an I/O error 89 */ 90 void produceContent(ContentEncoder encoder, IOControl ioControl) throws IOException; 91 92 /** 93 * Invoked to signal that the request has been fully written out. 94 */ 95 void requestCompleted(); 96 97 /** 98 * Invoked when a HTTP response message is received. Please note 99 * that the {@link #consumeContent(ContentDecoder, IOControl)} method 100 * will be invoked only if the response messages has a content entity 101 * enclosed. 102 * 103 * @param response HTTP response message. 104 * @throws HttpException in case of HTTP protocol violation 105 * @throws IOException in case of an I/O error 106 */ 107 void responseReceived(HttpResponse response) throws IOException, HttpException; 108 109 /** 110 * Invoked to process a chunk of content from the {@link ContentDecoder}. 111 * The {@link IOControl} interface can be used to suspend input event 112 * notifications if the consumer is temporarily unable to process content. 113 * <p> 114 * The consumer can use the {@link ContentDecoder#isCompleted()} method 115 * to find out whether or not the message content has been fully consumed. 116 * <p> 117 * Please note that the {@link ContentDecoder} object is not thread-safe and 118 * should only be used within the context of this method call. 119 * The {@link IOControl} object can be shared and used on other thread 120 * to resume input event notifications when the consumer is capable of 121 * processing more content. 122 * 123 * @param decoder content decoder. 124 * @param ioControl I/O control of the underlying connection. 125 * @throws IOException in case of an I/O error 126 */ 127 void consumeContent(ContentDecoder decoder, IOControl ioControl) throws IOException; 128 129 /** 130 * Invoked to signal that the response has been fully processed. 131 */ 132 void responseCompleted() throws IOException, HttpException; 133 134 /** 135 * Invoked to signal that the connection has been terminated prematurely 136 * by the opposite end. 137 */ 138 void inputTerminated(); 139 140 /** 141 * Invoked to signal that the response processing terminated abnormally. 142 * 143 * @param ex exception 144 */ 145 void failed(Exception ex); 146 147 /** 148 * Determines whether or not the response processing completed. 149 */ 150 boolean isDone(); 151 152 }