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.nio.ByteBuffer;
31  import java.util.List;
32  import java.util.concurrent.atomic.AtomicReference;
33  
34  import org.apache.hc.core5.concurrent.CallbackContribution;
35  import org.apache.hc.core5.concurrent.FutureCallback;
36  import org.apache.hc.core5.function.Supplier;
37  import org.apache.hc.core5.http.EntityDetails;
38  import org.apache.hc.core5.http.Header;
39  import org.apache.hc.core5.http.HttpException;
40  import org.apache.hc.core5.http.HttpRequest;
41  import org.apache.hc.core5.http.Message;
42  import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
43  import org.apache.hc.core5.http.nio.AsyncRequestConsumer;
44  import org.apache.hc.core5.http.nio.CapacityChannel;
45  import org.apache.hc.core5.http.protocol.HttpContext;
46  import org.apache.hc.core5.util.Args;
47  
48  /**
49   * Basic implementation of {@link AsyncRequestConsumer} that represents the request message as
50   * a {@link Message} and relies on a {@link AsyncEntityConsumer} to process request entity stream.
51   *
52   * @since 5.0
53   */
54  public class BasicRequestConsumer<T> implements AsyncRequestConsumer<Message<HttpRequest, T>> {
55  
56      private final Supplier<AsyncEntityConsumer<T>> dataConsumerSupplier;
57      private final AtomicReference<AsyncEntityConsumer<T>> dataConsumerRef;
58  
59      public BasicRequestConsumer(final Supplier<AsyncEntityConsumer<T>> dataConsumerSupplier) {
60          this.dataConsumerSupplier = Args.notNull(dataConsumerSupplier, "Data consumer supplier");
61          this.dataConsumerRef = new AtomicReference<>(null);
62      }
63  
64      public BasicRequestConsumer(final AsyncEntityConsumer<T> dataConsumer) {
65          this(new Supplier<AsyncEntityConsumer<T>>() {
66  
67              @Override
68              public AsyncEntityConsumer<T> get() {
69                  return dataConsumer;
70              }
71  
72          });
73      }
74  
75      @Override
76      public void consumeRequest(
77              final HttpRequest request,
78              final EntityDetails entityDetails,
79              final HttpContext httpContext,
80              final FutureCallback<Message<HttpRequest, T>> resultCallback) throws HttpException, IOException {
81          Args.notNull(request, "Request");
82          if (entityDetails != null) {
83              final AsyncEntityConsumer<T> dataConsumer = dataConsumerSupplier.get();
84              if (dataConsumer == null) {
85                  throw new HttpException("Supplied data consumer is null");
86              }
87              dataConsumerRef.set(dataConsumer);
88  
89              dataConsumer.streamStart(entityDetails, new CallbackContribution<T>(resultCallback) {
90  
91                  @Override
92                  public void completed(final T body) {
93                      final Message<HttpRequest, T> result = new Message<>(request, body);
94                      if (resultCallback != null) {
95                          resultCallback.completed(result);
96                      }
97                  }
98  
99              });
100         } else {
101             final Message<HttpRequest, T> result = new Message<>(request, null);
102             if (resultCallback != null) {
103                 resultCallback.completed(result);
104             }
105         }
106     }
107 
108     @Override
109     public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
110         final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
111         dataConsumer.updateCapacity(capacityChannel);
112     }
113 
114     @Override
115     public void consume(final ByteBuffer src) throws IOException {
116         final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
117         dataConsumer.consume(src);
118     }
119 
120     @Override
121     public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
122         final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.get();
123         dataConsumer.streamEnd(trailers);
124     }
125 
126     @Override
127     public void failed(final Exception cause) {
128         releaseResources();
129     }
130 
131     @Override
132     public void releaseResources() {
133         final AsyncEntityConsumer<T> dataConsumer = dataConsumerRef.getAndSet(null);
134         if (dataConsumer != null) {
135             dataConsumer.releaseResources();
136         }
137     }
138 
139 }