All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zalando.logbook.httpclient.LogbookHttpAsyncResponseConsumer Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
package org.zalando.logbook.httpclient;

import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
import org.apache.http.protocol.HttpContext;
import org.apiguardian.api.API;
import org.zalando.logbook.Logbook.ResponseProcessingStage;

import java.io.IOException;
import java.io.UncheckedIOException;

import static org.apiguardian.api.API.Status.EXPERIMENTAL;

@API(status = EXPERIMENTAL)
public final class LogbookHttpAsyncResponseConsumer extends ForwardingHttpAsyncResponseConsumer {

    private final HttpAsyncResponseConsumer consumer;
    private HttpResponse response;

    public LogbookHttpAsyncResponseConsumer(final HttpAsyncResponseConsumer consumer) {
        this.consumer = consumer;
    }

    @Override
    protected HttpAsyncResponseConsumer delegate() {
        return consumer;
    }

    @Override
    public void responseReceived(final HttpResponse response) throws IOException, HttpException {
        this.response = response;
        delegate().responseReceived(response);
    }

    @Override
    public void responseCompleted(final HttpContext context) {
        final ResponseProcessingStage stage = find(context);

        try {
            stage.process(new RemoteResponse(response)).write();
        } catch (final IOException e) {
            throw new UncheckedIOException(e);
        }

        delegate().responseCompleted(context);
    }

    private ResponseProcessingStage find(final HttpContext context) {
        return (ResponseProcessingStage) context.getAttribute(Attributes.STAGE);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy