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.IOException;
31  
32  import org.apache.http.HttpEntity;
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.nio.entity.EntityAsyncContentProducer;
37  import org.apache.http.nio.entity.HttpAsyncContentProducer;
38  import org.apache.http.protocol.HttpContext;
39  import org.apache.http.util.Args;
40  
41  /**
42   * Basic implementation of {@link HttpAsyncResponseProducer}. The producer
43   * can make use of the {@link HttpAsyncContentProducer} interface to
44   * efficiently stream out message content to the underlying non-blocking HTTP
45   * connection, if it is implemented by the {@link HttpEntity} inclosed in
46   * the response.
47   *
48   * @see HttpAsyncContentProducer
49   *
50   * @since 4.2
51   */
52  public class BasicAsyncResponseProducer implements HttpAsyncResponseProducer {
53  
54      private final HttpResponse response;
55      private final HttpAsyncContentProducer producer;
56  
57      /**
58       * Creates a producer that can be used to transmit the given response
59       * message. The given content producer will be used to stream out message
60       * content. Please note that the response message is expected to enclose
61       * an {@link HttpEntity} whose properties are consistent with the behavior
62       * of the content producer.
63       *
64       * @param response response message.
65       * @param producer response content producer.
66       */
67      protected BasicAsyncResponseProducer(
68              final HttpResponse response,
69              final HttpAsyncContentProducer producer) {
70          super();
71          Args.notNull(response, "HTTP response");
72          Args.notNull(producer, "HTTP content producer");
73          this.response = response;
74          this.producer = producer;
75      }
76  
77      /**
78       * Creates a producer that can be used to transmit the given response
79       * message. If the response message encloses an {@link HttpEntity}
80       * it is also expected to implement {@link HttpAsyncContentProducer}.
81       *
82       * @param response response message.
83       */
84      public BasicAsyncResponseProducer(final HttpResponse response) {
85          super();
86          Args.notNull(response, "HTTP response");
87          this.response = response;
88          final HttpEntity entity = response.getEntity();
89          if (entity != null) {
90              if (entity instanceof HttpAsyncContentProducer) {
91                  this.producer = (HttpAsyncContentProducer) entity;
92              } else {
93                  this.producer = new EntityAsyncContentProducer(entity);
94              }
95          } else {
96              this.producer = null;
97          }
98      }
99  
100     @Override
101     public HttpResponse generateResponse() {
102         return this.response;
103     }
104 
105     @Override
106     public void produceContent(
107             final ContentEncoder encoder, final IOControl ioControl) throws IOException {
108         if (this.producer != null) {
109             this.producer.produceContent(encoder, ioControl);
110             if (encoder.isCompleted()) {
111                 this.producer.close();
112             }
113         }
114     }
115 
116     @Override
117     public void responseCompleted(final HttpContext context) {
118     }
119 
120     @Override
121     public void failed(final Exception ex) {
122     }
123 
124     @Override
125     public void close() throws IOException {
126         if (this.producer != null) {
127             this.producer.close();
128         }
129     }
130 
131     @Override
132     public String toString() {
133         final StringBuilder buf = new StringBuilder();
134         buf.append(this.response);
135         if (this.producer != null) {
136             buf.append(" ").append(this.producer);
137         }
138         return buf.toString();
139     }
140 
141 }