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

org.swisspush.gateleen.logging.LoggingWriteStream Maven / Gradle / Ivy

The newest version!
package org.swisspush.gateleen.logging;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.streams.WriteStream;

/**
 * Wraps a WriteStream and feeds trsnferred Buffers into a LoggingHandler - interpreted either as
 * request or response body content
 */
public class LoggingWriteStream implements WriteStream {

    private final WriteStream wrappedWriteStream;
    private final LoggingHandler loggingHandler;
    private final boolean isRequest;

    /**
     * @param wrappedWriteStream the WriteStream to be wrapped. Transferred Buffers will be logged
     * @param loggingHandler     Log the transferred Buffers to this LoggingHandler
     * @param isRequest          true: feed the Buffers into LoggingHandler.appendRequestPayload()
     *                           false: feed them into LoggingHandler.appendResponsePayload()
     */
    public LoggingWriteStream(WriteStream wrappedWriteStream, LoggingHandler loggingHandler, boolean isRequest) {
        this.wrappedWriteStream = wrappedWriteStream;
        this.loggingHandler = loggingHandler;
        this.isRequest = isRequest;
    }

    @Override
    public WriteStream exceptionHandler(Handler handler) {
        wrappedWriteStream.exceptionHandler(handler);
        return this;
    }

    @Override
    public Future write(Buffer data) {
        Promise p = Promise.promise();
        write(data, event -> {
            if( event.failed() ) p.fail(event.cause());
            else p.complete();
        });
        return p.future();
    }

    @Override
    public void write(Buffer data, Handler> handler) {
        wrappedWriteStream.write(data, handler);
        if (isRequest) {
            loggingHandler.appendRequestPayload(data);
        } else {
            loggingHandler.appendResponsePayload(data);
        }
    }

    @Override
    public void end(Handler> handler) {
        wrappedWriteStream.end();
    }

    @Override
    public WriteStream setWriteQueueMaxSize(int maxSize) {
        wrappedWriteStream.setWriteQueueMaxSize(maxSize);
        return this;
    }

    @Override
    public boolean writeQueueFull() {
        return wrappedWriteStream.writeQueueFull();
    }

    @Override
    public WriteStream drainHandler(Handler handler) {
        wrappedWriteStream.drainHandler(handler);
        return this;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy