1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.http.model;
17
18 import java.util.List;
19
20 import org.kuali.common.util.Assert;
21 import org.kuali.common.util.FormatUtils;
22
23 import com.google.common.base.Charsets;
24 import com.google.common.collect.ImmutableList;
25
26 public final class HttpContext {
27
28 private final String url;
29 private final String logMsgPrefix;
30 private final List<Integer> successCodes;
31 private final int requestTimeoutMillis;
32 private final int sleepIntervalMillis;
33 private final int overallTimeoutMillis;
34 private final String encoding;
35
36
37
38 private final List<Integer> continueWaitingCodes;
39
40 public static class Builder {
41
42 private static final int OK = 200;
43 private static final int SERVICE_UNAVAILABLE = 503;
44
45
46 private final String url;
47
48
49 private String logMsgPrefix = "Waiting for";
50 private List<Integer> successCodes = ImmutableList.of(OK);
51 private List<Integer> continueWaitingCodes = ImmutableList.of(SERVICE_UNAVAILABLE);
52 private int requestTimeoutMillis = getIntMillis("15s");
53 private int sleepIntervalMillis = getIntMillis("15s");
54 private int overallTimeoutMillis = getIntMillis("30m");
55 private String encoding = Charsets.UTF_8.name();
56
57 public Builder(String url) {
58 this.url = url;
59 }
60
61 public Builder logMsgPrefix(String logMsgPrefix) {
62 this.logMsgPrefix = logMsgPrefix;
63 return this;
64 }
65
66 public Builder encoding(String encoding) {
67 this.encoding = encoding;
68 return this;
69 }
70
71 public Builder successCodes(List<Integer> successCodes) {
72 this.successCodes = successCodes;
73 return this;
74 }
75
76 public Builder continueWaitingCodes(List<Integer> continueWaitingCodes) {
77 this.continueWaitingCodes = continueWaitingCodes;
78 return this;
79 }
80
81 public Builder requestTimeout(String requestTimeout) {
82 this.requestTimeoutMillis = getIntMillis(requestTimeout);
83 return this;
84 }
85
86 public Builder requestTimeoutMillis(int requestTimeoutMillis) {
87 this.requestTimeoutMillis = requestTimeoutMillis;
88 return this;
89 }
90
91 public Builder sleepInterval(String sleepInterval) {
92 this.sleepIntervalMillis = getIntMillis(sleepInterval);
93 return this;
94 }
95
96 public Builder sleepIntervalMillis(int sleepIntervalMillis) {
97 this.sleepIntervalMillis = sleepIntervalMillis;
98 return this;
99 }
100
101 public Builder overallTimeout(String overallTimeout) {
102 this.overallTimeoutMillis = getIntMillis(overallTimeout);
103 return this;
104 }
105
106 public Builder overallTimeoutMillis(int overallTimeoutMillis) {
107 this.overallTimeoutMillis = overallTimeoutMillis;
108 return this;
109 }
110
111 public HttpContext build() {
112 Assert.noBlanks(url, encoding);
113 Assert.noNulls(successCodes, continueWaitingCodes, logMsgPrefix);
114 Assert.isTrue(requestTimeoutMillis > 0, "requestTimeoutMillis must be a positive integer");
115 Assert.isTrue(overallTimeoutMillis > 0, "overallTimeoutMillis must be a positive integer");
116 Assert.isTrue(sleepIntervalMillis >= 0, "sleepIntervalMillis is negative");
117 this.successCodes = ImmutableList.copyOf(successCodes);
118 this.continueWaitingCodes = ImmutableList.copyOf(continueWaitingCodes);
119 return new HttpContext(this);
120 }
121
122 private static int getIntMillis(String time) {
123 return new Long(FormatUtils.getMillis(time)).intValue();
124 }
125
126 }
127
128 private HttpContext(Builder builder) {
129 this.url = builder.url;
130 this.encoding = builder.encoding;
131 this.logMsgPrefix = builder.logMsgPrefix;
132 this.successCodes = builder.successCodes;
133 this.continueWaitingCodes = builder.continueWaitingCodes;
134 this.requestTimeoutMillis = builder.requestTimeoutMillis;
135 this.sleepIntervalMillis = builder.sleepIntervalMillis;
136 this.overallTimeoutMillis = builder.overallTimeoutMillis;
137 }
138
139 public String getUrl() {
140 return url;
141 }
142
143 public List<Integer> getSuccessCodes() {
144 return successCodes;
145 }
146
147 public int getRequestTimeoutMillis() {
148 return requestTimeoutMillis;
149 }
150
151 public int getSleepIntervalMillis() {
152 return sleepIntervalMillis;
153 }
154
155 public int getOverallTimeoutMillis() {
156 return overallTimeoutMillis;
157 }
158
159 public List<Integer> getContinueWaitingCodes() {
160 return continueWaitingCodes;
161 }
162
163 public String getLogMsgPrefix() {
164 return logMsgPrefix;
165 }
166
167 public String getEncoding() {
168 return encoding;
169 }
170
171 }