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