RequestResponseLogger.java
1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package pl.labno.bernard.htmlunified;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.util.WebConnectionWrapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
public class RequestResponseLogger extends WebConnectionWrapper {
private static final Log LOG = LogFactory.getLog(RequestResponseLogger.class);
private boolean active = true;
public RequestResponseLogger(WebClient webClient) throws IllegalArgumentException {
super(webClient);
}
@Override
public WebResponse getResponse(WebRequest request) throws IOException {
final WebResponse response = super.getResponse(request);
if (active) {
LOG.info("request=" + request.getRequestBody() + "\nresponse=" + response.getContentAsString());
}
return response;
}
public void off() {
active = false;
}
public void on() {
active = true;
}
}