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  package org.apache.http.nio.protocol;
28  
29  import java.io.IOException;
30  
31  import org.apache.http.HttpEntity;
32  import org.apache.http.HttpEntityEnclosingRequest;
33  import org.apache.http.HttpHost;
34  import org.apache.http.HttpRequest;
35  import org.apache.http.nio.ContentEncoder;
36  import org.apache.http.nio.IOControl;
37  import org.apache.http.nio.entity.EntityAsyncContentProducer;
38  import org.apache.http.nio.entity.HttpAsyncContentProducer;
39  import org.apache.http.protocol.HttpContext;
40  import org.apache.http.util.Args;
41  
42  /**
43   * Basic implementation of {@link HttpAsyncRequestProducer}. The producer
44   * can make use of the {@link HttpAsyncContentProducer} interface to
45   * efficiently stream out message content to the underlying non-blocking HTTP
46   * connection, if it is implemented by the enclosed {@link HttpEntity}.
47   *
48   * @see HttpAsyncContentProducer
49   *
50   * @since 4.2
51   */
52  public class BasicAsyncRequestProducer implements HttpAsyncRequestProducer {
53  
54      private final HttpHost target;
55      private final HttpRequest request;
56      private final HttpAsyncContentProducer producer;
57  
58      /**
59       * Creates a producer that can be used to transmit the given request
60       * message. The given content producer will be used to stream out message
61       * content. Please note that the request message is expected to enclose
62       * an {@link HttpEntity} whose properties are consistent with the behavior
63       * of the content producer.
64       *
65       * @param target target host.
66       * @param request request message.
67       * @param producer request content producer.
68       */
69      protected BasicAsyncRequestProducer(
70              final HttpHost target,
71              final HttpEntityEnclosingRequest request,
72              final HttpAsyncContentProducer producer) {
73          super();
74          Args.notNull(target, "HTTP host");
75          Args.notNull(request, "HTTP request");
76          Args.notNull(producer, "HTTP content producer");
77          this.target = target;
78          this.request = request;
79          this.producer = producer;
80      }
81  
82      /**
83       * Creates a producer that can be used to transmit the given request
84       * message. If the request message encloses an {@link HttpEntity}
85       * it is also expected to implement {@link HttpAsyncContentProducer}.
86       *
87       * @param target target host.
88       * @param request request message.
89       */
90      public BasicAsyncRequestProducer(final HttpHost target, final HttpRequest request) {
91          Args.notNull(target, "HTTP host");
92          Args.notNull(request, "HTTP request");
93          this.target = target;
94          this.request = request;
95          if (request instanceof HttpEntityEnclosingRequest) {
96              final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
97              if (entity != null) {
98                  if (entity instanceof HttpAsyncContentProducer) {
99                      this.producer = (HttpAsyncContentProducer) entity;
100                 } else {
101                     this.producer = new EntityAsyncContentProducer(entity);
102                 }
103             } else {
104                 this.producer = null;
105             }
106         } else {
107             this.producer = null;
108         }
109     }
110 
111     @Override
112     public HttpRequest generateRequest() {
113         return this.request;
114     }
115 
116     @Override
117     public HttpHost getTarget() {
118         return this.target;
119     }
120 
121     @Override
122     public void produceContent(
123             final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
124         if (this.producer != null) {
125             this.producer.produceContent(encoder, ioctrl);
126             if (encoder.isCompleted()) {
127                 this.producer.close();
128             }
129         }
130     }
131 
132     @Override
133     public void requestCompleted(final HttpContext context) {
134     }
135 
136     @Override
137     public void failed(final Exception ex) {
138     }
139 
140     @Override
141     public boolean isRepeatable() {
142         return this.producer == null || this.producer.isRepeatable();
143     }
144 
145     @Override
146     public void resetRequest() throws IOException {
147         if (this.producer != null) {
148             this.producer.close();
149         }
150     }
151 
152     @Override
153     public void close() throws IOException {
154         if (this.producer != null) {
155             this.producer.close();
156         }
157     }
158 
159     @Override
160     public String toString() {
161         final StringBuilder sb = new StringBuilder();
162         sb.append(this.target);
163         sb.append(' ');
164         sb.append(this.request);
165         if (this.producer != null) {
166             sb.append(' ');
167             sb.append(this.producer);
168         }
169         return sb.toString();
170     }
171 
172 }