org.springframework.http.client.LoggingClientHttpRequestInterceptor Maven / Gradle / Ivy
package org.springframework.http.client;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import org.springframework.util.StopWatch;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.SystemPropertyUtils;
public class LoggingClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
protected final Log logger = LogFactory.getLog(getClass());
private static final int DEFAULT_MAX_CONTENT_LENGTH = 50;
private final String lineSeparator = SystemPropertyUtils.resolvePlaceholders("${line.separator}");
private ClientHttpResponse response;
private int maxContentLength = DEFAULT_MAX_CONTENT_LENGTH;
/**
* @see org.springframework.http.client.SimpleBufferingClientHttpRequest
*/
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(lineSeparator);
stringBuilder.append(request.getMethod());
stringBuilder.append(' ');
stringBuilder.append(request.getURI());
StopWatch stopWatch = new StopWatch(new String(stringBuilder).substring(lineSeparator.length()));
stopWatch.start();
try (InputStream inputStream = new ByteArrayInputStream(body)) {
stringBuilder.append(lineSeparator);
for (Map.Entry> entry : request.getHeaders().entrySet()) {
String headerName = entry.getKey();
stringBuilder.append(headerName);
stringBuilder.append(": ");
if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265
stringBuilder.append(StringUtils.collectionToDelimitedString(entry.getValue(), "; "));
}
else {
stringBuilder.append(StringUtils.collectionToCommaDelimitedString(entry.getValue()));
}
stringBuilder.append(lineSeparator);
}
String content = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
if (StringUtils.hasText(content)) {
stringBuilder.append(lineSeparator);
stringBuilder.append(lineSeparator);
stringBuilder.append(content.substring(0, Math.min(content.length(), this.maxContentLength)));
stringBuilder.append(lineSeparator);
}
return execution.execute(request, body);
}
catch (IOException e) {
if (logger.isTraceEnabled()) {
logger.trace("HTTP execute fail... from " + stopWatch.getId() + " (" + e + ")", e);
}
else if (logger.isWarnEnabled()) {
logger.warn("HTTP execute fail... from " + stopWatch.getId() + " (" + e + ")");
}
if (this.response == null) {
throw e;
}
return response;
}
finally {
stopWatch.stop();
if (logger.isTraceEnabled()) {
logger.trace(stopWatch.prettyPrint());
logger.trace(new String(stringBuilder));
}
}
}
public void setStatus(HttpStatus status) {
this.response = status == null ? null : new StatusClientHttpResponse(status);
}
public void setMaxContentLength(int maxContentLength) {
Assert.isTrue(maxContentLength >= 0, "'maxContentLength' should be larger than or equal to 0");
this.maxContentLength = maxContentLength;
}
}