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.HTTP;
39  import org.apache.http.protocol.HttpContext;
40  
41  /**
42   * This class changed from package private to public in version 4.4.8.
43   *
44   * @since 4.4.8
45   */
46  public class ErrorResponseProducer implements HttpAsyncResponseProducer {
47  
48      private final HttpResponse response;
49      private final HttpEntity entity;
50      private final HttpAsyncContentProducer contentProducer;
51      private final boolean keepAlive;
52  
53      public ErrorResponseProducer(
54              final HttpResponse response,
55              final HttpEntity entity,
56              final boolean keepAlive) {
57          super();
58          this.response = response;
59          this.entity = entity;
60          if (entity instanceof HttpAsyncContentProducer) {
61              this.contentProducer = (HttpAsyncContentProducer) entity;
62          } else {
63              this.contentProducer = new EntityAsyncContentProducer(entity);
64          }
65          this.keepAlive = keepAlive;
66      }
67  
68      @Override
69      public HttpResponse generateResponse() {
70          if (this.keepAlive) {
71              response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
72          } else {
73              response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
74          }
75          response.setEntity(this.entity);
76          return response;
77      }
78  
79      @Override
80      public void produceContent(
81              final ContentEncoder encoder, final IOControl ioControl) throws IOException {
82          this.contentProducer.produceContent(encoder, ioControl);
83      }
84  
85      @Override
86      public void responseCompleted(final HttpContext context) {
87      }
88  
89      @Override
90      public void failed(final Exception ex) {
91      }
92  
93      @Override
94      public void close() throws IOException {
95          this.contentProducer.close();
96      }
97  
98  }