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  import org.apache.hc.core5.http.protocol.HttpContext;
37  
38  /**
39   * Basic implementation of {@link AsyncRequestProducer} that produces one fixed request
40   * and relies on a {@link AsyncEntityProducer} to generate request entity stream.
41   *
42   * @since 5.0
43   */
44  public class BasicRequestProducer implements AsyncRequestProducer {
45  
46      private final HttpRequest request;
47      private final AsyncEntityProducer dataProducer;
48  
49      public BasicRequestProducer(final HttpRequest request, final AsyncEntityProducer dataProducer) {
50          this.request = request;
51          this.dataProducer = dataProducer;
52      }
53  
54      public BasicRequestProducer(final String method, final HttpHost host, final String path, final AsyncEntityProducer dataProducer) {
55          this(new BasicHttpRequest(method, host, path), dataProducer);
56      }
57  
58      public BasicRequestProducer(final String method, final HttpHost host, final String path) {
59          this(method, host, path, null);
60      }
61  
62      public BasicRequestProducer(final String method, final URI requestUri, final AsyncEntityProducer dataProducer) {
63          this(new BasicHttpRequest(method, requestUri), dataProducer);
64      }
65  
66      public BasicRequestProducer(final String method, final URI requestUri) {
67          this(method, requestUri, null);
68      }
69  
70      @Override
71      public void sendRequest(final RequestChannel requestChannel, final HttpContext httpContext) throws HttpException, IOException {
72          requestChannel.sendRequest(request, dataProducer, httpContext);
73      }
74  
75      @Override
76      public int available() {
77          return dataProducer != null ? dataProducer.available() : 0;
78      }
79  
80      @Override
81      public void produce(final DataStreamChannel channel) throws IOException {
82          if (dataProducer != null) {
83              dataProducer.produce(channel);
84          }
85      }
86  
87      @Override
88      public boolean isRepeatable() {
89          return dataProducer == null || dataProducer.isRepeatable();
90      }
91  
92      @Override
93      public void failed(final Exception cause) {
94          try {
95              if (dataProducer != null) {
96                  dataProducer.failed(cause);
97              }
98          } finally {
99              releaseResources();
100         }
101     }
102 
103     @Override
104     public void releaseResources() {
105         if (dataProducer != null) {
106             dataProducer.releaseResources();
107         }
108     }
109 }