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