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 package org.apache.http.impl;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.apache.http.HttpConnectionMetrics;
34 import org.apache.http.io.HttpTransportMetrics;
35
36
37
38
39
40
41 public class HttpConnectionMetricsImpl implements HttpConnectionMetrics {
42
43 public static final String REQUEST_COUNT = "http.request-count";
44 public static final String RESPONSE_COUNT = "http.response-count";
45 public static final String SENT_BYTES_COUNT = "http.sent-bytes-count";
46 public static final String RECEIVED_BYTES_COUNT = "http.received-bytes-count";
47
48 private final HttpTransportMetrics inTransportMetric;
49 private final HttpTransportMetrics outTransportMetric;
50 private long requestCount = 0;
51 private long responseCount = 0;
52
53
54
55
56 private Map<String, Object> metricsCache;
57
58 public HttpConnectionMetricsImpl(
59 final HttpTransportMetrics inTransportMetric,
60 final HttpTransportMetrics outTransportMetric) {
61 super();
62 this.inTransportMetric = inTransportMetric;
63 this.outTransportMetric = outTransportMetric;
64 }
65
66
67
68 @Override
69 public long getReceivedBytesCount() {
70 return this.inTransportMetric != null ? this.inTransportMetric.getBytesTransferred() : -1;
71 }
72
73 @Override
74 public long getSentBytesCount() {
75 return this.outTransportMetric != null ? this.outTransportMetric.getBytesTransferred() : -1;
76 }
77
78 @Override
79 public long getRequestCount() {
80 return this.requestCount;
81 }
82
83 public void incrementRequestCount() {
84 this.requestCount++;
85 }
86
87 @Override
88 public long getResponseCount() {
89 return this.responseCount;
90 }
91
92 public void incrementResponseCount() {
93 this.responseCount++;
94 }
95
96 @Override
97 public Object getMetric(final String metricName) {
98 Object value = null;
99 if (this.metricsCache != null) {
100 value = this.metricsCache.get(metricName);
101 }
102 if (value == null) {
103 if (REQUEST_COUNT.equals(metricName)) {
104 value = Long.valueOf(requestCount);
105 } else if (RESPONSE_COUNT.equals(metricName)) {
106 value = Long.valueOf(responseCount);
107 } else if (RECEIVED_BYTES_COUNT.equals(metricName)) {
108 return this.inTransportMetric != null
109 ? Long.valueOf(this.inTransportMetric.getBytesTransferred())
110 : null;
111 } else if (SENT_BYTES_COUNT.equals(metricName)) {
112 return this.outTransportMetric != null
113 ? Long.valueOf(this.outTransportMetric.getBytesTransferred())
114 : null;
115 }
116 }
117 return value;
118 }
119
120 public void setMetric(final String metricName, final Object obj) {
121 if (this.metricsCache == null) {
122 this.metricsCache = new HashMap<String, Object>();
123 }
124 this.metricsCache.put(metricName, obj);
125 }
126
127 @Override
128 public void reset() {
129 if (this.outTransportMetric != null) {
130 this.outTransportMetric.reset();
131 }
132 if (this.inTransportMetric != null) {
133 this.inTransportMetric.reset();
134 }
135 this.requestCount = 0;
136 this.responseCount = 0;
137 this.metricsCache = null;
138 }
139
140 }