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