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.nio.ByteBuffer;
31  import java.util.List;
32  
33  import org.apache.hc.core5.concurrent.FutureCallback;
34  import org.apache.hc.core5.http.EntityDetails;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.HttpRequest;
38  import org.apache.hc.core5.http.Message;
39  import org.apache.hc.core5.util.Args;
40  
41  /**
42   * @since 5.0
43   */
44  public class BasicRequestConsumer<T> implements AsyncRequestConsumer<Message<HttpRequest, T>> {
45  
46      private final AsyncEntityConsumer<T> dataConsumer;
47  
48      private volatile Message<HttpRequest, T> result;
49  
50      public BasicRequestConsumer(final AsyncEntityConsumer<T> dataConsumer) {
51          this.dataConsumer = Args.notNull(dataConsumer, "Data consumer");
52      }
53  
54      @Override
55      public void consumeRequest(
56              final HttpRequest request,
57              final EntityDetails entityDetails,
58              final FutureCallback<Message<HttpRequest, T>> resultCallback) throws HttpException, IOException {
59          Args.notNull(request, "Request");
60          if (entityDetails != null) {
61              dataConsumer.streamStart(entityDetails, new FutureCallback<T>() {
62  
63                  @Override
64                  public void completed(final T body) {
65                      result = new Message<>(request, body);
66                      if (resultCallback != null) {
67                          resultCallback.completed(result);
68                      }
69                      dataConsumer.releaseResources();
70                  }
71  
72                  @Override
73                  public void failed(final Exception ex) {
74                      if (resultCallback != null) {
75                          resultCallback.failed(ex);
76                      }
77                      dataConsumer.releaseResources();
78                  }
79  
80                  @Override
81                  public void cancelled() {
82                      if (resultCallback != null) {
83                          resultCallback.cancelled();
84                      }
85                      dataConsumer.releaseResources();
86                  }
87  
88              });
89          } else {
90              result = new Message<>(request, null);
91              if (resultCallback != null) {
92                  resultCallback.completed(result);
93              }
94              dataConsumer.releaseResources();
95          }
96      }
97  
98      @Override
99      public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
100         dataConsumer.updateCapacity(capacityChannel);
101     }
102 
103     @Override
104     public int consume(final ByteBuffer src) throws IOException {
105         return dataConsumer.consume(src);
106     }
107 
108     @Override
109     public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
110         dataConsumer.streamEnd(trailers);
111     }
112 
113     @Override
114     public void failed(final Exception cause) {
115         releaseResources();
116     }
117 
118     @Override
119     public Message<HttpRequest, T> getResult() {
120         return result;
121     }
122 
123     @Override
124     public void releaseResources() {
125         dataConsumer.releaseResources();
126     }
127 
128 }