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.http.nio.support;
28
29 import java.io.IOException;
30 import java.util.Set;
31
32 import org.apache.hc.core5.annotation.Contract;
33 import org.apache.hc.core5.annotation.ThreadingBehavior;
34 import org.apache.hc.core5.http.EntityDetails;
35 import org.apache.hc.core5.http.HttpException;
36 import org.apache.hc.core5.http.HttpRequest;
37 import org.apache.hc.core5.http.HttpResponse;
38 import org.apache.hc.core5.http.HttpStatus;
39 import org.apache.hc.core5.http.message.BasicHttpResponse;
40 import org.apache.hc.core5.http.nio.AsyncDataConsumer;
41 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
42 import org.apache.hc.core5.http.nio.AsyncFilterChain;
43 import org.apache.hc.core5.http.nio.AsyncFilterHandler;
44 import org.apache.hc.core5.http.nio.AsyncPushProducer;
45 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
46 import org.apache.hc.core5.http.nio.DataStreamChannel;
47 import org.apache.hc.core5.http.nio.HandlerFactory;
48 import org.apache.hc.core5.http.nio.ResponseChannel;
49 import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
50 import org.apache.hc.core5.http.protocol.HttpContext;
51 import org.apache.hc.core5.util.Args;
52
53
54
55
56
57
58
59
60 @Contract(threading = ThreadingBehavior.STATELESS)
61 public final class TerminalAsyncServerFilter implements AsyncFilterHandler {
62
63 final private HandlerFactory<AsyncServerExchangeHandler> handlerFactory;
64
65 public TerminalAsyncServerFilter(final HandlerFactory<AsyncServerExchangeHandler> handlerFactory) {
66 this.handlerFactory = Args.notNull(handlerFactory, "Handler factory");
67 }
68
69 @Override
70 public AsyncDataConsumer handle(
71 final HttpRequest request,
72 final EntityDetails entityDetails,
73 final HttpContext context,
74 final AsyncFilterChain.ResponseTrigger responseTrigger,
75 final AsyncFilterChain chain) throws HttpException, IOException {
76 final AsyncServerExchangeHandler exchangeHandler = handlerFactory.create(request, context);
77 if (exchangeHandler != null) {
78 exchangeHandler.handleRequest(request, entityDetails, new ResponseChannel() {
79
80 @Override
81 public void sendInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
82 responseTrigger.sendInformation(response);
83 }
84
85 @Override
86 public void sendResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
87 responseTrigger.submitResponse(response, entityDetails != null ? new AsyncEntityProducer() {
88
89 @Override
90 public void failed(final Exception cause) {
91 exchangeHandler.failed(cause);
92 }
93
94 @Override
95 public boolean isRepeatable() {
96 return false;
97 }
98
99 @Override
100 public long getContentLength() {
101 return entityDetails.getContentLength();
102 }
103
104 @Override
105 public String getContentType() {
106 return entityDetails.getContentType();
107 }
108
109 @Override
110 public String getContentEncoding() {
111 return entityDetails.getContentEncoding();
112 }
113
114 @Override
115 public boolean isChunked() {
116 return entityDetails.isChunked();
117 }
118
119 @Override
120 public Set<String> getTrailerNames() {
121 return entityDetails.getTrailerNames();
122 }
123
124 @Override
125 public int available() {
126 return exchangeHandler.available();
127 }
128
129 @Override
130 public void produce(final DataStreamChannel channel) throws IOException {
131 exchangeHandler.produce(channel);
132 }
133
134 @Override
135 public void releaseResources() {
136 exchangeHandler.releaseResources();
137 }
138
139 } : null);
140 }
141
142 @Override
143 public void pushPromise(final HttpRequest promise, final AsyncPushProducer pushProducer, final HttpContext httpContext) throws HttpException, IOException {
144 responseTrigger.pushPromise(promise, pushProducer);
145 }
146
147 }, context);
148 return exchangeHandler;
149 }
150 responseTrigger.submitResponse(new BasicHttpResponse(HttpStatus.SC_NOT_FOUND), AsyncEntityProducers.create("Not found"));
151 return null;
152 }
153
154 }