View Javadoc

1   /**
2    * Copyright 2013-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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; // Url to contact
29  	private final String logMsgPrefix; // Gets printed as a prefix to any log messages emitted by the HttpService
30  	private final List<Integer> successCodes; // HTTP codes signifying success
31  	private final int requestTimeoutMillis; // Millis to wait before an individual http request times out (15 seconds)
32  	private final int sleepIntervalMillis; // Millis to wait in between http requests (15 seconds)
33  	private final int overallTimeoutMillis; // Total number of millis to wait before timing out (30 minutes)
34  	private final String encoding;
35  
36  	// If Tomcat is fronted by an Apache web server, and Apache is up and running but Tomcat is still starting, http 503 is returned by Apache
37  	// We don't want to fail if we get a 503, just continue waiting
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  		// Required
46  		private final String url;
47  
48  		// Optional
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"); // 15 seconds
53  		private int sleepIntervalMillis = getIntMillis("15s"); // 15 seconds
54  		private int overallTimeoutMillis = getIntMillis("30m"); // 30 minutes
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 }