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