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.hc.core5.http.nio;
28  
29  import java.io.IOException;
30  import java.net.URI;
31  
32  import org.apache.hc.core5.http.HttpException;
33  import org.apache.hc.core5.http.HttpHost;
34  import org.apache.hc.core5.http.HttpRequest;
35  import org.apache.hc.core5.http.message.BasicHttpRequest;
36  
37  public class BasicRequestProducer implements AsyncRequestProducer {
38  
39      private final HttpRequest request;
40      private final AsyncEntityProducer dataProducer;
41  
42      public BasicRequestProducer(final HttpRequest request, final AsyncEntityProducer dataProducer) {
43          this.request = request;
44          this.dataProducer = dataProducer;
45      }
46  
47      public BasicRequestProducer(final String method, final HttpHost host, final String path, final AsyncEntityProducer dataProducer) {
48          this(new BasicHttpRequest(method, host, path), dataProducer);
49      }
50  
51      public BasicRequestProducer(final String method, final HttpHost host, final String path) {
52          this(method, host, path, null);
53      }
54  
55      public BasicRequestProducer(final String method, final URI requestUri, final AsyncEntityProducer dataProducer) {
56          this(new BasicHttpRequest(method, requestUri), dataProducer);
57      }
58  
59      public BasicRequestProducer(final String method, final URI requestUri) {
60          this(method, requestUri, null);
61      }
62  
63      @Override
64      public void sendRequest(final RequestChannel requestChannel) throws HttpException, IOException {
65          requestChannel.sendRequest(request, dataProducer);
66      }
67  
68      @Override
69      public int available() {
70          return dataProducer != null ? dataProducer.available() : 0;
71      }
72  
73      @Override
74      public void produce(final DataStreamChannel channel) throws IOException {
75          if (dataProducer != null) {
76              dataProducer.produce(channel);
77          }
78      }
79  
80      @Override
81      public boolean isRepeatable() {
82          return dataProducer == null || dataProducer.isRepeatable();
83      }
84  
85      @Override
86      public void failed(final Exception cause) {
87          try {
88              if (dataProducer != null) {
89                  dataProducer.failed(cause);
90              }
91          } finally {
92              releaseResources();
93          }
94      }
95  
96      @Override
97      public void releaseResources() {
98          if (dataProducer != null) {
99              dataProducer.releaseResources();
100         }
101     }
102 }