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