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     public HttpRequest generateRequest() {
112         return this.request;
113     }
114 
115     public HttpHost getTarget() {
116         return this.target;
117     }
118 
119     public void produceContent(
120             final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
121         if (this.producer != null) {
122             this.producer.produceContent(encoder, ioctrl);
123             if (encoder.isCompleted()) {
124                 this.producer.close();
125             }
126         }
127     }
128 
129     public void requestCompleted(final HttpContext context) {
130     }
131 
132     public void failed(final Exception ex) {
133     }
134 
135     public boolean isRepeatable() {
136         return this.producer == null || this.producer.isRepeatable();
137     }
138 
139     public void resetRequest() throws IOException {
140         if (this.producer != null) {
141             this.producer.close();
142         }
143     }
144 
145     public void close() throws IOException {
146         if (this.producer != null) {
147             this.producer.close();
148         }
149     }
150 
151     @Override
152     public String toString() {
153         final StringBuilder sb = new StringBuilder();
154         sb.append(this.target);
155         sb.append(' ');
156         sb.append(this.request);
157         if (this.producer != null) {
158             sb.append(' ');
159             sb.append(this.producer);
160         }
161         return sb.toString();
162     }
163 
164 }