1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.maven.plugins.dnsme;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20
21 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
22 import org.apache.commons.httpclient.Header;
23 import org.apache.commons.httpclient.HttpClient;
24 import org.apache.commons.httpclient.HttpMethod;
25 import org.apache.commons.httpclient.HttpMethodRetryHandler;
26 import org.apache.commons.httpclient.methods.GetMethod;
27 import org.apache.commons.httpclient.params.HttpClientParams;
28 import org.apache.commons.httpclient.params.HttpMethodParams;
29 import org.apache.commons.io.IOUtils;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class HttpUtil {
34 private final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
35 int requestTimeout = 8000;
36 int sleepInterval = 3000;
37 int timeout = 300;
38
39 protected String getTimeout(long l) {
40 if (l == -1) {
41 return "";
42 } else {
43 return " - (Timeout in " + l + "s)";
44 }
45 }
46
47 public void log(String url, HttpRequestResult result, int secondsRemaining) {
48 StringBuilder sb = new StringBuilder();
49 sb.append("Status for '" + url + "' is '" + getMsg(result) + "'");
50 sb.append(getTimeout(secondsRemaining));
51 logger.info(sb.toString());
52 }
53
54 protected String getMsg(HttpRequestResult result) {
55 switch (result.getType()) {
56 case EXCEPTION:
57 Exception exception = result.getException();
58 return exception.getMessage();
59 case COMPLETED:
60 int statusCode = result.getStatusCode();
61 String statusText = result.getStatusText();
62 return statusCode + ":" + statusText;
63 case TIMEOUT:
64 return "Timeout exceeded";
65 default:
66 throw new IllegalArgumentException(result.getType() + " is an unknown type");
67 }
68 }
69
70 protected int getSecondsRemaining(long endMillis) {
71 long currentMillis = System.currentTimeMillis();
72 long millisRemaining = endMillis - currentMillis;
73 double secondsRemaining = millisRemaining / 1000D;
74 return (int) Math.ceil(secondsRemaining);
75 }
76
77 public HttpRequestResult doWait(String url) {
78 HttpClient client = getHttpClient();
79 long now = System.currentTimeMillis();
80 long timeoutMillis = timeout * 1000;
81 long end = now + timeoutMillis;
82 logger.info("Determining status for '" + url + "'");
83 for (;;) {
84 HttpRequestResult result = executeMethod(client, url);
85 int secondsRemaining = getSecondsRemaining(end);
86 log(url, result, secondsRemaining);
87 if (HttpRequestResultType.COMPLETED.equals(result.getType())) {
88 return result;
89 }
90 if (System.currentTimeMillis() > end) {
91 result.setType(HttpRequestResultType.TIMEOUT);
92 log(url, result, -1);
93 return result;
94 }
95 sleep(sleepInterval);
96 }
97 }
98
99 public HttpClient getHttpClient() {
100 HttpClient client = new HttpClient();
101 HttpClientParams clientParams = client.getParams();
102 HttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(0, false);
103 clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);
104 clientParams.setParameter(HttpMethodParams.SO_TIMEOUT, requestTimeout);
105 return client;
106 }
107
108 protected String getResponseBody(HttpMethod method) throws IOException {
109 InputStream in = null;
110 try {
111 in = method.getResponseBodyAsStream();
112 return IOUtils.toString(in);
113 } finally {
114 IOUtils.closeQuietly(in);
115 }
116 }
117
118 public HttpRequestResult executeMethod(HttpMethod method) {
119 return executeMethod(getHttpClient(), method);
120 }
121
122 public HttpRequestResult executeMethod(HttpClient client, HttpMethod method) {
123 HttpRequestResult result = new HttpRequestResult();
124 try {
125 client.executeMethod(method);
126 int statusCode = method.getStatusCode();
127 String statusText = method.getStatusText();
128 String responseBody = getResponseBody(method);
129 Header[] responseHeaders = method.getResponseHeaders();
130 method.releaseConnection();
131 result.setStatusCode(statusCode);
132 result.setStatusText(statusText);
133 result.setResponseBody(responseBody);
134 result.setResponseHeaders(responseHeaders);
135 result.setType(HttpRequestResultType.COMPLETED);
136 } catch (Exception e) {
137 result.setType(HttpRequestResultType.EXCEPTION);
138 result.setException(e);
139 }
140 return result;
141 }
142
143 public HttpRequestResult executeMethod(HttpClient client, String url) {
144 HttpMethod method = new GetMethod(url);
145 return executeMethod(client, method);
146 }
147
148 protected void sleep(long millis) {
149 try {
150 Thread.sleep(millis);
151 } catch (InterruptedException e) {
152 throw new RuntimeException(e);
153 }
154 }
155
156 public int getRequestTimeout() {
157 return requestTimeout;
158 }
159
160 public void setRequestTimeout(int requestTimeout) {
161 this.requestTimeout = requestTimeout;
162 }
163
164 public int getSleepInterval() {
165 return sleepInterval;
166 }
167
168 public void setSleepInterval(int sleepInterval) {
169 this.sleepInterval = sleepInterval;
170 }
171
172 public int getTimeout() {
173 return timeout;
174 }
175
176 public void setTimeout(int waitTimeout) {
177 this.timeout = waitTimeout;
178 }
179
180 public Logger getLogger() {
181 return logger;
182 }
183 }