use of org.apache.hc.core5.http.io.HttpRequestHandler in project httpcomponents-core by apache.
the class ServerBootstrap method create.
public HttpServer create() {
final RequestHandlerRegistry<HttpRequestHandler> handlerRegistry = new RequestHandlerRegistry<>(canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName(), () -> lookupRegistry != null ? lookupRegistry : UriPatternType.newMatcher(UriPatternType.URI_PATTERN));
for (final HandlerEntry<HttpRequestHandler> entry : handlerList) {
handlerRegistry.register(entry.hostname, entry.uriPattern, entry.handler);
}
final HttpServerRequestHandler requestHandler;
if (!filters.isEmpty()) {
final NamedElementChain<HttpFilterHandler> filterChainDefinition = new NamedElementChain<>();
filterChainDefinition.addLast(new TerminalServerFilter(handlerRegistry, this.responseFactory != null ? this.responseFactory : DefaultClassicHttpResponseFactory.INSTANCE), StandardFilter.MAIN_HANDLER.name());
filterChainDefinition.addFirst(new HttpServerExpectationFilter(), StandardFilter.EXPECT_CONTINUE.name());
for (final FilterEntry<HttpFilterHandler> entry : filters) {
switch(entry.position) {
case AFTER:
filterChainDefinition.addAfter(entry.existing, entry.filterHandler, entry.name);
break;
case BEFORE:
filterChainDefinition.addBefore(entry.existing, entry.filterHandler, entry.name);
break;
case REPLACE:
filterChainDefinition.replace(entry.existing, entry.filterHandler);
break;
case FIRST:
filterChainDefinition.addFirst(entry.filterHandler, entry.name);
break;
case LAST:
// Don't add last, after TerminalServerFilter, as that does not delegate to the chain
// Instead, add the filter just before it, making it effectively the last filter
filterChainDefinition.addBefore(StandardFilter.MAIN_HANDLER.name(), entry.filterHandler, entry.name);
break;
}
}
NamedElementChain<HttpFilterHandler>.Node current = filterChainDefinition.getLast();
HttpServerFilterChainElement filterChain = null;
while (current != null) {
filterChain = new HttpServerFilterChainElement(current.getValue(), filterChain);
current = current.getPrevious();
}
requestHandler = new HttpServerFilterChainRequestHandler(filterChain);
} else {
requestHandler = new BasicHttpServerExpectationDecorator(new BasicHttpServerRequestHandler(handlerRegistry, this.responseFactory != null ? this.responseFactory : DefaultClassicHttpResponseFactory.INSTANCE));
}
final HttpService httpService = new HttpService(this.httpProcessor != null ? this.httpProcessor : HttpProcessors.server(), requestHandler, this.connStrategy != null ? this.connStrategy : DefaultConnectionReuseStrategy.INSTANCE, this.streamListener);
ServerSocketFactory serverSocketFactoryCopy = this.serverSocketFactory;
if (serverSocketFactoryCopy == null) {
if (this.sslContext != null) {
serverSocketFactoryCopy = this.sslContext.getServerSocketFactory();
} else {
serverSocketFactoryCopy = ServerSocketFactory.getDefault();
}
}
HttpConnectionFactory<? extends DefaultBHttpServerConnection> connectionFactoryCopy = this.connectionFactory;
if (connectionFactoryCopy == null) {
final String scheme = serverSocketFactoryCopy instanceof SSLServerSocketFactory ? URIScheme.HTTPS.id : URIScheme.HTTP.id;
connectionFactoryCopy = new DefaultBHttpServerConnectionFactory(scheme, this.http1Config, this.charCodingConfig);
}
return new HttpServer(Math.max(this.listenerPort, 0), httpService, this.localAddress, this.socketConfig != null ? this.socketConfig : SocketConfig.DEFAULT, serverSocketFactoryCopy, connectionFactoryCopy, sslSetupHandler != null ? sslSetupHandler : new DefaultTlsSetupHandler(), this.exceptionListener != null ? this.exceptionListener : ExceptionListener.NO_OP);
}
use of org.apache.hc.core5.http.io.HttpRequestHandler in project httpcomponents-core by apache.
the class BasicHttpServerRequestHandler method handle.
@Override
public void handle(final ClassicHttpRequest request, final ResponseTrigger responseTrigger, final HttpContext context) throws HttpException, IOException {
final ClassicHttpResponse response = responseFactory.newHttpResponse(HttpStatus.SC_OK);
final HttpRequestHandler handler = handlerMapper != null ? handlerMapper.resolve(request, context) : null;
if (handler != null) {
handler.handle(request, response, context);
} else {
response.setCode(HttpStatus.SC_NOT_IMPLEMENTED);
}
responseTrigger.submitResponse(response);
}
use of org.apache.hc.core5.http.io.HttpRequestHandler in project httpcomponents-core by apache.
the class TerminalServerFilter method handle.
@Override
public void handle(final ClassicHttpRequest request, final HttpFilterChain.ResponseTrigger responseTrigger, final HttpContext context, final HttpFilterChain chain) throws HttpException, IOException {
final ClassicHttpResponse response = responseFactory.newHttpResponse(HttpStatus.SC_OK);
final HttpRequestHandler handler = handlerMapper.resolve(request, context);
if (handler != null) {
handler.handle(request, response, context);
} else {
response.setCode(HttpStatus.SC_NOT_IMPLEMENTED);
}
responseTrigger.submitResponse(response);
}
use of org.apache.hc.core5.http.io.HttpRequestHandler in project datagear by datageartech.
the class HttpDataSetTest method initTestHttpServer.
@BeforeClass
public static void initTestHttpServer() throws Throwable {
server = ServerBootstrap.bootstrap().setListenerPort(PORT).register("/testSimple", new HttpRequestHandler() {
@Override
public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context) throws HttpException, IOException {
StringEntity responseEntity = new StringEntity("[{name: 'aaa', value: 11}, {name: '名称b', value: 22}]", ContentType.APPLICATION_JSON);
response.setEntity(responseEntity);
}
}).register("/testParam", new HttpRequestHandler() {
@Override
public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context) throws HttpException, IOException {
Map<String, String> params = parseRequestParams(request);
String p0 = params.get(PARAM_NAME_0);
String p1 = params.get(PARAM_NAME_1);
StringEntity responseEntity = new StringEntity("[{name: '" + PARAM_NAME_0 + "', value: '" + p0 + "'}, {name: '" + PARAM_NAME_1 + "', value: '" + p1 + "'}]", ContentType.APPLICATION_JSON);
response.setEntity(responseEntity);
}
}).register("/testJson", new HttpRequestHandler() {
@Override
public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context) throws HttpException, IOException {
String reqJson = getRequestStringContent(request);
StringEntity responseEntity = new StringEntity(reqJson, ContentType.APPLICATION_JSON);
response.setEntity(responseEntity);
}
}).register("/testHeader", new HttpRequestHandler() {
@Override
public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context) throws HttpException, IOException {
Header h0 = request.getHeader(PARAM_NAME_0);
Header h1 = request.getHeader(PARAM_NAME_1);
StringEntity responseEntity = new StringEntity("[{name: '" + PARAM_NAME_0 + "', value: '" + h0.getValue() + "'}, {name: '" + PARAM_NAME_1 + "', value: '" + h1.getValue() + "'}]", ContentType.APPLICATION_JSON);
response.setEntity(responseEntity);
}
}).register("/testResponseJsonPath", new HttpRequestHandler() {
@Override
public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context) throws HttpException, IOException {
StringEntity responseEntity = new StringEntity("{ path0: { path1: [ { path2: [{name: 'aaa', value: 11}, {name: '名称b', value: 22}] } ] } }", ContentType.APPLICATION_JSON);
response.setEntity(responseEntity);
}
}).create();
server.start();
httpClient = HttpClients.createDefault();
}
Aggregations