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.Set;
39
40
41
42
43
44
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
57
58
59
60
61
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 }