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  
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.HttpResponse;
35  import org.apache.http.nio.ContentEncoder;
36  import org.apache.http.nio.IOControl;
37  import org.apache.http.protocol.HttpContext;
38  
39  /**
40   * <tt>HttpAsyncResponseProducer</tt> is a callback interface whose methods
41   * get invoked to generate an HTTP response message and to stream message
42   * content to a non-blocking HTTP connection on the server side.
43   *
44   * @since 4.2
45   */
46  public interface HttpAsyncResponseProducer extends Closeable {
47  
48      /**
49       * Invoked to generate a HTTP response message head.
50       *
51       * @return HTTP response message.
52       * @throws HttpException in case of HTTP protocol violation
53       * @throws IOException in case of an I/O error
54       */
55      HttpResponse generateResponse();
56  
57      /**
58       * Invoked to write out a chunk of content to the {@link ContentEncoder}.
59       * The {@link IOControl} interface can be used to suspend output event
60       * notifications if the producer is temporarily unable to produce more content.
61       * <p/>
62       * When all content is finished, the producer <b>MUST</b> call
63       * {@link ContentEncoder#complete()}. Failure to do so may cause the entity
64       * to be incorrectly delimited.
65       * <p/>
66       * Please note that the {@link ContentEncoder} object is not thread-safe and
67       * should only be used within the context of this method call.
68       * The {@link IOControl} object can be shared and used on other thread
69       * to resume output event notifications when more content is made available.
70       *
71       * @param encoder content encoder.
72       * @param ioctrl I/O control of the underlying connection.
73       * @throws IOException in case of an I/O error
74       */
75      void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException;
76  
77      /**
78       * Invoked to signal that the response has been fully written out.
79       *
80       * @param context HTTP context
81       */
82      void responseCompleted(HttpContext context);
83  
84      /**
85       * Invoked to signal that the response processing terminated abnormally.
86       *
87       * @param ex exception
88       */
89      void failed(Exception ex);
90  
91  }