-
Notifications
You must be signed in to change notification settings - Fork 39
/
HttpRequest.java
556 lines (502 loc) · 17.6 KB
/
HttpRequest.java
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* Copyright 2016 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.logging;
import com.google.api.core.ApiFunction;
import com.google.cloud.StringEnumType;
import com.google.cloud.StringEnumValue;
import com.google.common.base.MoreObjects;
import com.google.common.base.Strings;
import java.io.Serializable;
import java.util.Objects;
import org.threeten.bp.Duration;
/**
* Objects of this class represent information about the (optional) HTTP request associated with a
* log entry.
*
* @see <a href=
* "https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/LogEntry#httprequest">
* Http Request</a>
*/
public final class HttpRequest implements Serializable {
private static final long serialVersionUID = -274998005454709817L;
public static final HttpRequest EMPTY = newBuilder().build();
private final RequestMethod requestMethod;
private final String requestUrl;
private final Long requestSize;
private final Integer status;
private final Long responseSize;
private final String userAgent;
private final String remoteIp;
private final String serverIp;
private final String referer;
private final boolean cacheLookup;
private final boolean cacheHit;
private final boolean cacheValidatedWithOriginServer;
private final Long cacheFillBytes;
private final Duration latency;
/** The HTTP request method. */
public static final class RequestMethod extends StringEnumValue {
private static final long serialVersionUID = 2403969065179486996L;
private RequestMethod(String constant) {
super(constant);
}
private static final ApiFunction<String, RequestMethod> CONSTRUCTOR =
new ApiFunction<String, RequestMethod>() {
@Override
public RequestMethod apply(String constant) {
return new RequestMethod(constant);
}
};
private static final StringEnumType<RequestMethod> type =
new StringEnumType<RequestMethod>(RequestMethod.class, CONSTRUCTOR);
public static final RequestMethod GET = type.createAndRegister("GET");
public static final RequestMethod HEAD = type.createAndRegister("HEAD");
public static final RequestMethod PUT = type.createAndRegister("PUT");
public static final RequestMethod POST = type.createAndRegister("POST");
/**
* Get the RequestMethod for the given String constant, and throw an exception if the constant
* is not recognized.
*/
public static RequestMethod valueOfStrict(String constant) {
return type.valueOfStrict(constant);
}
/** Get the RequestMethod for the given String constant, and allow unrecognized values. */
public static RequestMethod valueOf(String constant) {
return type.valueOf(constant);
}
/** Return the known values for RequestMethod. */
public static RequestMethod[] values() {
return type.values();
}
}
/** A builder for {@code HttpRequest} objects. */
public static final class Builder {
private RequestMethod requestMethod;
private String requestUrl;
private Long requestSize;
private Integer status;
private Long responseSize;
private String userAgent;
private String remoteIp;
private String serverIp;
private String referer;
private boolean cacheLookup;
private boolean cacheHit;
private boolean cacheValidatedWithOriginServer;
private Long cacheFillBytes;
private Duration latency;
Builder() {}
Builder(HttpRequest request) {
this.requestMethod = request.requestMethod;
this.requestUrl = request.requestUrl;
this.requestSize = request.requestSize;
this.status = request.status;
this.responseSize = request.responseSize;
this.userAgent = request.userAgent;
this.remoteIp = request.remoteIp;
this.serverIp = request.serverIp;
this.referer = request.referer;
this.cacheLookup = request.cacheLookup;
this.cacheHit = request.cacheHit;
this.cacheValidatedWithOriginServer = request.cacheValidatedWithOriginServer;
this.cacheFillBytes = request.cacheFillBytes;
this.latency = request.latency;
}
/** Sets the HTTP request method. */
public Builder setRequestMethod(RequestMethod requestMethod) {
this.requestMethod = requestMethod;
return this;
}
/**
* Sets the requested URL. Request URL contains the scheme ({@code http}, {@code https}), the
* host name, the path and the query portion of the URL that was requested. Example: {@code
* http://example.com/some/info?color=red}.
*/
public Builder setRequestUrl(String requestUrl) {
this.requestUrl = requestUrl;
return this;
}
/**
* Sets the size of the HTTP request message in bytes, including the request headers and the
* request body.
*/
public Builder setRequestSize(long requestSize) {
this.requestSize = requestSize;
return this;
}
/** Sets the response code indicating the status of response. */
public Builder setStatus(int status) {
this.status = status;
return this;
}
/**
* Sets the size of the HTTP response message sent back to the client, in bytes, including the
* response headers and the response body.
*/
public Builder setResponseSize(long responseSize) {
this.responseSize = responseSize;
return this;
}
/**
* Sets the user agent sent by the client. Example: {@code Mozilla/4.0 (compatible; MSIE 6.0;
* Windows 98; Q312461; .NET CLR 1.0.3705)}.
*/
public Builder setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
/**
* Sets the IP address (IPv4 or IPv6) of the client that issued the HTTP request. Examples:
* {@code 192.168.1.1}, {@code FE80::0202:B3FF:FE1E:8329}.
*/
public Builder setRemoteIp(String remoteIp) {
this.remoteIp = remoteIp;
return this;
}
/**
* Sets the IP address (IPv4 or IPv6) of the origin server that the request was sent to.
* Examples: {@code 192.168.1.1}, {@code FE80::0202:B3FF:FE1E:8329}.
*/
public Builder setServerIp(String serverIp) {
this.serverIp = serverIp;
return this;
}
/**
* Sets the referer URL of the request, as defined in HTTP/1.1 Header Field Definitions.
*
* @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">HTTP/1.1 Header Field
* Definitions</a>
*/
public Builder setReferer(String referer) {
this.referer = referer;
return this;
}
/** Sets whether or not a cache lookup was attempted. If not set, {@code false} is used. */
public Builder setCacheLookup(boolean cacheLookup) {
this.cacheLookup = cacheLookup;
return this;
}
/**
* Sets whether or not an entity was served from cache (with or without validation). If not set,
* {@code false} is used.
*/
public Builder setCacheHit(boolean cacheHit) {
this.cacheHit = cacheHit;
return this;
}
/**
* Sets whether or not the response was validated with the origin server before being served
* from cache. This field is only meaningful if {@link #setCacheHit(boolean)} is set to {@code
* true}. If not set, {@code false} is used.
*/
public Builder setCacheValidatedWithOriginServer(boolean cacheValidatedWithOriginServer) {
this.cacheValidatedWithOriginServer = cacheValidatedWithOriginServer;
return this;
}
/**
* Sets the number of HTTP response bytes inserted into cache. Set only when a cache fill was
* attempted.
*/
public Builder setCacheFillBytes(long cacheFillBytes) {
this.cacheFillBytes = cacheFillBytes;
return this;
}
/**
* Sets the latency on the server, from the time the request was received until the response was
* sent.
*/
public Builder setLatency(Duration latency) {
this.latency = latency;
return this;
}
/** Creates a {@code HttpRequest} object for this builder. */
public HttpRequest build() {
return new HttpRequest(this);
}
}
HttpRequest(Builder builder) {
this.requestMethod = builder.requestMethod;
this.requestUrl = builder.requestUrl;
this.requestSize = builder.requestSize;
this.status = builder.status;
this.responseSize = builder.responseSize;
this.userAgent = builder.userAgent;
this.remoteIp = builder.remoteIp;
this.serverIp = builder.serverIp;
this.referer = builder.referer;
this.cacheLookup = builder.cacheLookup;
this.cacheHit = builder.cacheHit;
this.cacheValidatedWithOriginServer = builder.cacheValidatedWithOriginServer;
this.cacheFillBytes = builder.cacheFillBytes;
this.latency = builder.latency;
}
/** Returns the HTTP request method. */
public RequestMethod getRequestMethod() {
return requestMethod;
}
/**
* Returns the requested URL. Request URL contains the scheme ({@code http}, {@code https}), the
* host name, the path and the query portion of the URL that was requested. Example: {@code
* http://example.com/some/info?color=red}.
*/
public String getRequestUrl() {
return requestUrl;
}
/**
* Returns the size of the HTTP request message in bytes, including the request headers and the
* request body.
*/
public Long getRequestSize() {
return requestSize;
}
/** Returns the response code indicating the status of response. */
public Integer getStatus() {
return status;
}
/**
* Returns the size of the HTTP response message sent back to the client, in bytes, including the
* response headers and the response body.
*/
public Long getResponseSize() {
return responseSize;
}
/**
* Returns the user agent sent by the client. Example: {@code Mozilla/4.0 (compatible; MSIE 6.0;
* Windows 98; Q312461; .NET CLR 1.0.3705)}.
*/
public String getUserAgent() {
return userAgent;
}
/**
* Returns the IP address (IPv4 or IPv6) of the client that issued the HTTP request. Examples:
* {@code 192.168.1.1}, {@code FE80::0202:B3FF:FE1E:8329}.
*/
public String getRemoteIp() {
return remoteIp;
}
/**
* Returns the IP address (IPv4 or IPv6) of the origin server that the request was sent to.
* Examples: {@code 192.168.1.1}, {@code FE80::0202:B3FF:FE1E:8329}.
*/
public String getServerIp() {
return serverIp;
}
/**
* Returns the referer URL of the request, as defined in HTTP/1.1 Header Field Definitions.
*
* @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html">HTTP/1.1 Header Field
* Definitions</a>
*/
public String getReferer() {
return referer;
}
/**
* Returns whether or not a cache lookup was attempted. If not set, this method returns {@code
* false}.
*/
public boolean cacheLookup() {
return cacheLookup;
}
/**
* Returns whether or not an entity was served from cache (with or without validation). If not
* set, this method returns {@code false}.
*/
public boolean cacheHit() {
return cacheHit;
}
/**
* Returns whether or not the response was validated with the origin server before being served
* from cache. If not set, this method returns {@code false}. This field is only meaningful if
* {@link #cacheHit()} is set to {@code true}.
*/
public boolean cacheValidatedWithOriginServer() {
return cacheValidatedWithOriginServer;
}
/**
* Returns the number of HTTP response bytes inserted into cache. Set only when a cache fill was
* attempted.
*/
public Long getCacheFillBytes() {
return cacheFillBytes;
}
/**
* Returns the processing latency on the server, from the time the request was received until the
* response was sent.
*
* @return the latency, for null if not populated.
*/
public Duration getLatency() {
return latency;
}
@Override
public int hashCode() {
return Objects.hash(
requestMethod,
requestUrl,
requestSize,
status,
responseSize,
userAgent,
serverIp,
cacheLookup,
cacheFillBytes,
remoteIp,
referer,
cacheHit,
cacheValidatedWithOriginServer,
latency);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("requestMethod", requestMethod)
.add("requestUrl", requestUrl)
.add("requestSize", requestSize)
.add("status", status)
.add("responseSize", responseSize)
.add("userAgent", userAgent)
.add("remoteIp", remoteIp)
.add("serverIp", serverIp)
.add("referer", referer)
.add("cacheLookup", cacheLookup)
.add("cacheHit", cacheHit)
.add("cacheValidatedWithOriginServer", cacheValidatedWithOriginServer)
.add("cacheFillBytes", cacheFillBytes)
.add("latency", latency)
.toString();
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof HttpRequest)) {
return false;
}
HttpRequest other = (HttpRequest) obj;
return Objects.equals(requestMethod, other.requestMethod)
&& Objects.equals(requestUrl, other.requestUrl)
&& Objects.equals(requestSize, other.requestSize)
&& Objects.equals(status, other.status)
&& Objects.equals(responseSize, other.responseSize)
&& Objects.equals(userAgent, other.userAgent)
&& Objects.equals(remoteIp, other.remoteIp)
&& Objects.equals(serverIp, other.serverIp)
&& Objects.equals(referer, other.referer)
&& Objects.equals(latency, other.latency)
&& cacheLookup == other.cacheLookup
&& cacheHit == other.cacheHit
&& cacheValidatedWithOriginServer == other.cacheValidatedWithOriginServer
&& Objects.equals(cacheFillBytes, other.cacheFillBytes);
}
/** Returns a builder for this object. */
public Builder toBuilder() {
return new Builder(this);
}
com.google.logging.type.HttpRequest toPb() {
com.google.logging.type.HttpRequest.Builder builder =
com.google.logging.type.HttpRequest.newBuilder();
if (requestMethod != null) {
builder.setRequestMethod(requestMethod.name());
}
if (requestUrl != null) {
builder.setRequestUrl(requestUrl);
}
if (requestSize != null) {
builder.setRequestSize(requestSize);
}
if (status != null) {
builder.setStatus(status);
}
if (responseSize != null) {
builder.setResponseSize(responseSize);
}
if (userAgent != null) {
builder.setUserAgent(userAgent);
}
if (remoteIp != null) {
builder.setRemoteIp(remoteIp);
}
if (serverIp != null) {
builder.setServerIp(serverIp);
}
if (referer != null) {
builder.setReferer(referer);
}
builder.setCacheLookup(cacheLookup);
builder.setCacheHit(cacheHit);
builder.setCacheValidatedWithOriginServer(cacheValidatedWithOriginServer);
if (cacheFillBytes != null) {
builder.setCacheFillBytes(cacheFillBytes);
}
if (latency != null) {
// NOTE(pongad): Don't convert to nano; large durations overflow longs!
builder.setLatency(
com.google.protobuf.Duration.newBuilder()
.setSeconds(latency.getSeconds())
.setNanos(latency.getNano())
.build());
}
return builder.build();
}
/** Returns a builder for {@code HttpRequest} objects. */
public static Builder newBuilder() {
return new Builder();
}
static HttpRequest fromPb(com.google.logging.type.HttpRequest requestPb) {
Builder builder = newBuilder();
if (!Strings.isNullOrEmpty(requestPb.getRequestMethod())) {
builder.setRequestMethod(RequestMethod.valueOf(requestPb.getRequestMethod()));
}
if (!Strings.isNullOrEmpty(requestPb.getRequestUrl())) {
builder.setRequestUrl(requestPb.getRequestUrl());
}
if (requestPb.getRequestSize() != 0L) {
builder.setRequestSize(requestPb.getRequestSize());
}
if (requestPb.getStatus() != 0L) {
builder.setStatus(requestPb.getStatus());
}
if (requestPb.getResponseSize() != 0L) {
builder.setResponseSize(requestPb.getResponseSize());
}
if (!Strings.isNullOrEmpty(requestPb.getUserAgent())) {
builder.setUserAgent(requestPb.getUserAgent());
}
if (!Strings.isNullOrEmpty(requestPb.getServerIp())) {
builder.setServerIp(requestPb.getServerIp());
}
if (!Strings.isNullOrEmpty(requestPb.getRemoteIp())) {
builder.setRemoteIp(requestPb.getRemoteIp());
}
if (!Strings.isNullOrEmpty(requestPb.getReferer())) {
builder.setReferer(requestPb.getReferer());
}
builder.setCacheLookup(requestPb.getCacheLookup());
builder.setCacheHit(requestPb.getCacheHit());
builder.setCacheValidatedWithOriginServer(requestPb.getCacheValidatedWithOriginServer());
if (requestPb.getCacheFillBytes() != 0L) {
builder.setCacheFillBytes(requestPb.getCacheFillBytes());
}
if (requestPb.hasLatency()) {
// NOTE(pongad): Don't convert to nano; large durations overflow longs!
builder.setLatency(
Duration.ofSeconds(
requestPb.getLatency().getSeconds(), requestPb.getLatency().getNanos()));
}
return builder.build();
}
}