1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
44
45
46
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
59
60
61
62
63
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 }