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