Search in sources :

Example 1 with HttpFilterChain

use of org.apache.hc.core5.http.io.HttpFilterChain 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);
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpRequestHandler(org.apache.hc.core5.http.io.HttpRequestHandler)

Example 2 with HttpFilterChain

use of org.apache.hc.core5.http.io.HttpFilterChain in project httpcomponents-core by apache.

the class HttpServerExpectationFilter method handle.

@Override
public final void handle(final ClassicHttpRequest request, final HttpFilterChain.ResponseTrigger responseTrigger, final HttpContext context, final HttpFilterChain chain) throws HttpException, IOException {
    final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
    final boolean expectContinue = expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue());
    if (expectContinue) {
        final boolean verified = verify(request, context);
        if (verified) {
            responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
        } else {
            final ClassicHttpResponse expectationFailed = new BasicClassicHttpResponse(HttpStatus.SC_EXPECTATION_FAILED);
            final HttpEntity responseContent = generateResponseContent(expectationFailed);
            expectationFailed.setEntity(responseContent);
            responseTrigger.submitResponse(expectationFailed);
            return;
        }
    }
    chain.proceed(request, responseTrigger, context);
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) Header(org.apache.hc.core5.http.Header) HttpEntity(org.apache.hc.core5.http.HttpEntity) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Example 3 with HttpFilterChain

use of org.apache.hc.core5.http.io.HttpFilterChain in project httpcomponents-core by apache.

the class AbstractHttpServerAuthFilter method handle.

@Override
public final void handle(final ClassicHttpRequest request, final HttpFilterChain.ResponseTrigger responseTrigger, final HttpContext context, final HttpFilterChain chain) throws HttpException, IOException {
    final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
    final T challengeResponse = h != null ? parseChallengeResponse(h.getValue(), context) : null;
    final URIAuthority authority = request.getAuthority();
    final String requestUri = request.getRequestUri();
    final boolean authenticated = authenticate(challengeResponse, authority, requestUri, context);
    final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
    final boolean expectContinue = expect != null && HeaderElements.CONTINUE.equalsIgnoreCase(expect.getValue());
    if (authenticated) {
        if (expectContinue) {
            responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
        }
        chain.proceed(request, responseTrigger, context);
    } else {
        final ClassicHttpResponse unauthorized = new BasicClassicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
        unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, authority, requestUri, context));
        final HttpEntity responseContent = generateResponseContent(unauthorized);
        unauthorized.setEntity(responseContent);
        if (respondImmediately || expectContinue || request.getEntity() == null) {
            // Respond immediately
            responseTrigger.submitResponse(unauthorized);
            // Consume request body later
            EntityUtils.consume(request.getEntity());
        } else {
            // Consume request body first
            EntityUtils.consume(request.getEntity());
            // Respond later
            responseTrigger.submitResponse(unauthorized);
        }
    }
}
Also used : BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) URIAuthority(org.apache.hc.core5.net.URIAuthority) Header(org.apache.hc.core5.http.Header) HttpEntity(org.apache.hc.core5.http.HttpEntity) BasicClassicHttpResponse(org.apache.hc.core5.http.message.BasicClassicHttpResponse)

Aggregations

ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)3 Header (org.apache.hc.core5.http.Header)2 HttpEntity (org.apache.hc.core5.http.HttpEntity)2 BasicClassicHttpResponse (org.apache.hc.core5.http.message.BasicClassicHttpResponse)2 HttpRequestHandler (org.apache.hc.core5.http.io.HttpRequestHandler)1 URIAuthority (org.apache.hc.core5.net.URIAuthority)1