1
18 package net.bull.javamelody;
19
20 import java.io.IOException;
21
22 import org.springframework.http.HttpRequest;
23 import org.springframework.http.client.ClientHttpRequestExecution;
24 import org.springframework.http.client.ClientHttpRequestInterceptor;
25 import org.springframework.http.client.ClientHttpResponse;
26 import org.springframework.web.client.RestTemplate;
27
28 import net.bull.javamelody.internal.model.Counter;
29
30
34 public class SpringRestTemplateInterceptor implements ClientHttpRequestInterceptor {
35 static final ClientHttpRequestInterceptor SINGLETON = new SpringRestTemplateInterceptor();
36
37 private static final Counter SPRING_COUNTER = MonitoringProxy.getSpringCounter();
38
39
40 @Override
41 public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body,
42 ClientHttpRequestExecution execution) throws IOException {
43 if (!SPRING_COUNTER.isDisplayed()) {
44 return execution.execute(httpRequest, body);
45 }
46
47 final String requestName = getRequestName(httpRequest);
48
49 boolean systemError = false;
50 try {
51 SPRING_COUNTER.bindContextIncludingCpu(requestName);
52
53
54 return execution.execute(httpRequest, body);
55 } catch (final IOException | Error e) {
56
57 systemError = true;
58 throw e;
59 } finally {
60
61 SPRING_COUNTER.addRequestForCurrentContext(systemError);
62 }
63 }
64
65 protected String getRequestName(HttpRequest httpRequest) {
66 String uri = httpRequest.getURI().toString();
67 final int index = uri.indexOf('?');
68 if (index != -1) {
69 uri = uri.substring(0, index);
70 }
71 return uri + ' ' + httpRequest.getMethod();
72 }
73 }
74