
me.vcoder.httplogger.HttpExchangeTracer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of http-logger Show documentation
Show all versions of http-logger Show documentation
HTTP Logger for Spring Library
The newest version!
package me.vcoder.httplogger;
import java.net.URI;
import java.security.Principal;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.springframework.http.HttpHeaders;
/**
* @author baodn
* Created on 11 Apr 2018
*/
public class HttpExchangeTracer {
private final Set includes;
/**
* Creates a new {@code HttpExchangeTracer} that will use the given {@code includes}
* to determine the contents of its traces.
* @param includes the includes
*/
public HttpExchangeTracer(Set includes) {
this.includes = includes;
}
/**
* Begins the tracing of the exchange that was initiated by the given {@code request}
* being received.
* @param request the received request
* @return the HTTP trace for the
*/
public final HttpTrace receivedRequest(TraceableRequest request) {
return new HttpTrace(new FilteredTraceableRequest(request));
}
/**
* Ends the tracing of the exchange that is being concluded by sending the given
* {@code response}.
* @param trace the trace for the exchange
* @param response the response that concludes the exchange
* @param principal a supplier for the exchange's principal
* @param sessionId a supplier for the id of the exchange's session
*/
public final void sendingResponse(HttpTrace trace, TraceableResponse response,
Supplier principal, Supplier sessionId) {
setIfIncluded(Include.TIME_TAKEN,
() -> System.currentTimeMillis() - trace.getTimestamp().toEpochMilli(),
trace::setTimeTaken);
setIfIncluded(Include.SESSION_ID, sessionId, trace::setSessionId);
setIfIncluded(Include.PRINCIPAL, principal, trace::setPrincipal);
trace.setResponse(
new HttpTrace.Response(new FilteredTraceableResponse(response)));
}
private T getIfIncluded(Include include, Supplier valueSupplier) {
return this.includes.contains(include) ? valueSupplier.get() : null;
}
private void setIfIncluded(Include include, Supplier supplier,
Consumer consumer) {
if (this.includes.contains(include)) {
consumer.accept(supplier.get());
}
}
private Map> getHeadersIfIncluded(Include include,
Supplier
© 2015 - 2025 Weber Informatics LLC | Privacy Policy