1 package org.kuali.common.httplib.api.model;
2
3 import static com.google.common.collect.Lists.newArrayList;
4 import static org.kuali.common.httplib.api.model.HttpStatusCodeType.SUCCESS;
5 import static org.kuali.common.jute.reflect.Reflection.checkNoNulls;
6
7 import java.util.List;
8
9 import org.kuali.common.jute.base.RunningStopwatch;
10 import org.kuali.common.jute.base.TimedInterval;
11
12 import com.fasterxml.jackson.annotation.JsonIgnore;
13 import com.fasterxml.jackson.annotation.JsonSetter;
14 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
15 import com.google.common.collect.ImmutableList;
16
17 @JsonDeserialize(builder = HttpResult.Builder.class)
18 public final class HttpResult {
19
20 private final ResponseContainer response;
21 private final ImmutableList<ResponseContainer> failedAttempts;
22 private final TimedInterval timing;
23
24 private HttpResult(Builder builder) {
25 this.response = builder.response;
26 this.failedAttempts = ImmutableList.copyOf(builder.failedAttempts);
27 this.timing = builder.timing;
28 }
29
30 public static Builder builder() {
31 return new Builder();
32 }
33
34 public static class Builder implements org.apache.commons.lang3.builder.Builder<HttpResult> {
35
36 private ResponseContainer response;
37 private List<ResponseContainer> failedAttempts = newArrayList();
38 private TimedInterval timing;
39
40 public Builder withResponse(ResponseContainer response) {
41 this.response = response;
42 return this;
43 }
44
45 public Builder withFailedAttempts(List<ResponseContainer> failedAttempts) {
46 this.failedAttempts = failedAttempts;
47 return this;
48 }
49
50 @JsonSetter
51 public Builder withTiming(TimedInterval timing) {
52 this.timing = timing;
53 return this;
54 }
55
56 public Builder withTiming(RunningStopwatch sw) {
57 return withTiming(TimedInterval.build(sw));
58 }
59
60 @Override
61 public HttpResult build() {
62 return checkNoNulls(new HttpResult(this));
63 }
64 }
65
66
67
68
69 @JsonIgnore
70 public boolean isSuccess() {
71 if (response.getException().isPresent()) {
72 return false;
73 }
74 if (!response.getMetadata().isPresent()) {
75 return false;
76 }
77 ResponseMetadata meta = response.getMetadata().get();
78 int code = meta.getStatus().getCode();
79 return SUCCESS.getRange().contains(code);
80 }
81
82 public ResponseContainer getResponse() {
83 return response;
84 }
85
86 public List<ResponseContainer> getFailedAttempts() {
87 return failedAttempts;
88 }
89
90 public TimedInterval getTiming() {
91 return timing;
92 }
93
94 }