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.reactive;
28  
29  import org.apache.hc.core5.annotation.Contract;
30  import org.apache.hc.core5.annotation.ThreadingBehavior;
31  import org.apache.hc.core5.http.ContentType;
32  import org.apache.hc.core5.http.nio.AsyncEntityProducer;
33  import org.apache.hc.core5.http.nio.DataStreamChannel;
34  import org.reactivestreams.Publisher;
35  
36  import java.io.IOException;
37  import java.nio.ByteBuffer;
38  import java.util.Set;
39  
40  /**
41   * An {@link AsyncEntityProducer} that subscribes to a {@code Publisher}
42   * instance, as defined by the Reactive Streams specification.
43   *
44   * @since 5.0
45   */
46  @Contract(threading = ThreadingBehavior.SAFE)
47  public final class ReactiveEntityProducer implements AsyncEntityProducer {
48  
49      private final ReactiveDataProducer reactiveDataProducer;
50  
51      private final long contentLength;
52      private final ContentType contentType;
53      private final String contentEncoding;
54  
55      /**
56       * Creates a new {@code ReactiveEntityProducer} with the given parameters.
57       *
58       * @param publisher the publisher of the entity stream.
59       * @param contentLength the length of the entity, or -1 if unknown (implies chunked encoding).
60       * @param contentType the {@code Content-Type} of the entity, or null if none.
61       * @param contentEncoding the {@code Content-Encoding} of the entity, or null if none.
62       */
63      public ReactiveEntityProducer(
64          final Publisher<ByteBuffer> publisher,
65          final long contentLength,
66          final ContentType contentType,
67          final String contentEncoding
68      ) {
69          this.reactiveDataProducer = new ReactiveDataProducer(publisher);
70          this.contentLength = contentLength;
71          this.contentType = contentType;
72          this.contentEncoding = contentEncoding;
73      }
74  
75      @Override
76      public int available() {
77          return reactiveDataProducer.available();
78      }
79  
80      @Override
81      public void produce(final DataStreamChannel channel) throws IOException {
82          reactiveDataProducer.produce(channel);
83      }
84  
85      @Override
86      public void releaseResources() {
87          reactiveDataProducer.releaseResources();
88      }
89  
90      @Override
91      public boolean isRepeatable() {
92          return false;
93      }
94  
95      @Override
96      public void failed(final Exception cause) {
97          releaseResources();
98      }
99  
100     @Override
101     public long getContentLength() {
102         return contentLength;
103     }
104 
105     @Override
106     public String getContentType() {
107         return contentType != null ? contentType.toString() : null;
108     }
109 
110     @Override
111     public String getContentEncoding() {
112         return contentEncoding;
113     }
114 
115     @Override
116     public boolean isChunked() {
117         return contentLength == -1;
118     }
119 
120     @Override
121     public Set<String> getTrailerNames() {
122         return null;
123     }
124 }