use of org.apache.hc.core5.http.io.support.AbstractHttpServerAuthFilter in project httpcomponents-core by apache.
the class ClassicServerFilterExample method main.
public static void main(final String[] args) throws Exception {
int port = 8080;
if (args.length >= 1) {
port = Integer.parseInt(args[0]);
}
final SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(15, TimeUnit.SECONDS).setTcpNoDelay(true).build();
final HttpServer server = ServerBootstrap.bootstrap().setListenerPort(port).setSocketConfig(socketConfig).replaceFilter(StandardFilter.EXPECT_CONTINUE.name(), new AbstractHttpServerAuthFilter<String>(false) {
@Override
protected String parseChallengeResponse(final String authorizationValue, final HttpContext context) throws HttpException {
return authorizationValue;
}
@Override
protected boolean authenticate(final String challengeResponse, final URIAuthority authority, final String requestUri, final HttpContext context) {
return "let me pass".equals(challengeResponse);
}
@Override
protected String generateChallenge(final String challengeResponse, final URIAuthority authority, final String requestUri, final HttpContext context) {
return "who goes there?";
}
}).addFilterFirst("my-filter", (request, responseTrigger, context, chain) -> {
if (request.getRequestUri().equals("/back-door")) {
final ClassicHttpResponse response = new BasicClassicHttpResponse(HttpStatus.SC_OK);
response.setEntity(new StringEntity("Welcome", ContentType.TEXT_PLAIN));
responseTrigger.submitResponse(response);
} else {
chain.proceed(request, new HttpFilterChain.ResponseTrigger() {
@Override
public void sendInformation(final ClassicHttpResponse response) throws HttpException, IOException {
responseTrigger.sendInformation(response);
}
@Override
public void submitResponse(final ClassicHttpResponse response) throws HttpException, IOException {
response.addHeader("X-Filter", "My-Filter");
responseTrigger.submitResponse(response);
}
}, context);
}
}).register("*", (request, response, context) -> {
// do something useful
response.setCode(HttpStatus.SC_OK);
response.setEntity(new StringEntity("Hello"));
}).create();
server.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> server.close(CloseMode.GRACEFUL)));
System.out.println("Listening on port " + port);
server.awaitTermination(TimeValue.MAX_VALUE);
}
Aggregations