Support for logging response content via `-Dnet.filebot.web.WebRequest.log.response=true`

This commit is contained in:
Reinhard Pointner 2016-05-16 23:21:17 +08:00
parent 0131bd0af5
commit 86b0776c2d
2 changed files with 22 additions and 1 deletions

View File

@ -81,7 +81,7 @@ public class CachedResource<K, R> implements Resource<R> {
try {
ByteBuffer data = retry(() -> fetch.fetch(url, lastModified), retryLimit, retryWait);
debug.finest(format("Received %,d bytes", data == null ? 0 : data.remaining()));
debug.finest(WebRequest.log(data));
// 304 Not Modified
if (data == null && element != null && element.getObjectValue() != null) {

View File

@ -25,6 +25,7 @@ import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
@ -53,6 +54,7 @@ import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import net.filebot.util.ByteBufferOutputStream;
import net.filebot.util.SystemProperty;
public final class WebRequest {
@ -332,6 +334,25 @@ public final class WebRequest {
};
}
public static Supplier<String> log(ByteBuffer data) {
return () -> {
if (data == null) {
return "Received 0 bytes";
}
String log = String.format(Locale.ROOT, "Received %,d bytes", data.remaining());
// log entire response content if enabled
SystemProperty<Boolean> response = SystemProperty.of("net.filebot.web.WebRequest.log.response", Boolean::parseBoolean, Boolean.FALSE);
if (response.get()) {
return log + System.lineSeparator() + UTF_8.decode(data.duplicate()) + System.lineSeparator();
}
return log;
};
}
private WebRequest() {
throw new UnsupportedOperationException();
}