1 | |
package org.kuali.maven.plugins; |
2 | |
|
3 | |
import java.io.IOException; |
4 | |
import java.util.ArrayList; |
5 | |
import java.util.List; |
6 | |
|
7 | |
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; |
8 | |
import org.apache.commons.httpclient.HttpClient; |
9 | |
import org.apache.commons.httpclient.HttpMethod; |
10 | |
import org.apache.commons.httpclient.HttpMethodRetryHandler; |
11 | |
import org.apache.commons.httpclient.methods.GetMethod; |
12 | |
import org.apache.commons.httpclient.params.HttpClientParams; |
13 | |
import org.apache.commons.httpclient.params.HttpMethodParams; |
14 | |
import org.slf4j.Logger; |
15 | |
import org.slf4j.LoggerFactory; |
16 | |
|
17 | 0 | public class HttpInspector { |
18 | 0 | private final Logger logger = LoggerFactory.getLogger(HttpInspector.class); |
19 | 0 | List<Integer> successCodes = new ArrayList<Integer>(); |
20 | 0 | int requestTimeout = 3000; |
21 | 0 | int sleepInterval = 3000; |
22 | 0 | int timeout = 180; |
23 | |
|
24 | |
protected boolean isSuccess(int resultCode) { |
25 | 0 | for (int successCode : successCodes) { |
26 | 0 | if (resultCode == successCode) { |
27 | 0 | return true; |
28 | |
} |
29 | |
} |
30 | 0 | return false; |
31 | |
} |
32 | |
|
33 | |
protected String getMsg(String msg) { |
34 | 0 | return getMsg(msg, -1); |
35 | |
} |
36 | |
|
37 | |
protected String getMsg(String msg, long l) { |
38 | 0 | StringBuilder sb = new StringBuilder(); |
39 | 0 | sb.append(msg); |
40 | 0 | if (l == -1) { |
41 | 0 | return sb.toString(); |
42 | |
} |
43 | 0 | sb.append(" - (Timeout in " + l + "s)"); |
44 | 0 | return sb.toString(); |
45 | |
} |
46 | |
|
47 | |
public Result wait(String url) { |
48 | 0 | HttpClient client = getHttpClient(); |
49 | 0 | long now = System.currentTimeMillis(); |
50 | 0 | long end = now + (timeout * 1000); |
51 | 0 | logger.info(getMsg("Determining status for '" + url + "'")); |
52 | |
for (;;) { |
53 | 0 | long secondsRemaining = (long) Math.ceil((end - System.currentTimeMillis()) / 1000D); |
54 | 0 | Result result = doRequest(client, url, secondsRemaining); |
55 | 0 | if (result.equals(Result.SUCCESS)) { |
56 | 0 | return result; |
57 | 0 | } else if (result.equals(Result.INVALID_HTTP_STATUS_CODE)) { |
58 | 0 | logger.info("Invalid http status code. Expected " + successCodes); |
59 | 0 | return result; |
60 | |
} |
61 | 0 | sleep(sleepInterval); |
62 | 0 | if (System.currentTimeMillis() > end) { |
63 | 0 | logger.info("Timed out waiting for response from '" + url + "'"); |
64 | 0 | return Result.TIMEOUT; |
65 | |
} |
66 | 0 | } |
67 | |
} |
68 | |
|
69 | |
protected HttpClient getHttpClient() { |
70 | 0 | HttpClient client = new HttpClient(); |
71 | 0 | HttpClientParams clientParams = client.getParams(); |
72 | 0 | HttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(0, false); |
73 | 0 | clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); |
74 | 0 | clientParams.setParameter(HttpMethodParams.SO_TIMEOUT, requestTimeout); |
75 | 0 | return client; |
76 | |
} |
77 | |
|
78 | |
protected Result doRequest(HttpClient client, String url, long secondsRemaining) { |
79 | |
try { |
80 | 0 | HttpMethod method = new GetMethod(url); |
81 | 0 | client.executeMethod(method); |
82 | 0 | int statusCode = method.getStatusCode(); |
83 | 0 | String statusText = method.getStatusText(); |
84 | 0 | boolean success = isSuccess(statusCode); |
85 | 0 | if (success) { |
86 | 0 | logger.info(getMsg("Status for '" + url + "' is '" + statusCode + ":" + statusText + "'")); |
87 | 0 | return Result.SUCCESS; |
88 | |
} else { |
89 | 0 | logger.info(getMsg("Status for '" + url + "' is '" + statusCode + ":" + statusText + "'", |
90 | |
secondsRemaining)); |
91 | 0 | return Result.INVALID_HTTP_STATUS_CODE; |
92 | |
} |
93 | 0 | } catch (IOException e) { |
94 | 0 | logger.info(getMsg("Status for '" + url + "' is '" + e.getMessage() + "'", secondsRemaining)); |
95 | 0 | return Result.IO_EXCEPTION; |
96 | |
} |
97 | |
} |
98 | |
|
99 | |
protected void sleep(long millis) { |
100 | |
try { |
101 | 0 | Thread.sleep(millis); |
102 | 0 | } catch (InterruptedException e) { |
103 | 0 | throw new RuntimeException(e); |
104 | 0 | } |
105 | 0 | } |
106 | |
|
107 | |
public int getRequestTimeout() { |
108 | 0 | return requestTimeout; |
109 | |
} |
110 | |
|
111 | |
public void setRequestTimeout(int requestTimeout) { |
112 | 0 | this.requestTimeout = requestTimeout; |
113 | 0 | } |
114 | |
|
115 | |
public int getSleepInterval() { |
116 | 0 | return sleepInterval; |
117 | |
} |
118 | |
|
119 | |
public void setSleepInterval(int sleepInterval) { |
120 | 0 | this.sleepInterval = sleepInterval; |
121 | 0 | } |
122 | |
|
123 | |
public int getTimeout() { |
124 | 0 | return timeout; |
125 | |
} |
126 | |
|
127 | |
public void setTimeout(int waitTimeout) { |
128 | 0 | this.timeout = waitTimeout; |
129 | 0 | } |
130 | |
|
131 | |
public Logger getLogger() { |
132 | 0 | return logger; |
133 | |
} |
134 | |
|
135 | |
public List<Integer> getSuccessCodes() { |
136 | 0 | return successCodes; |
137 | |
} |
138 | |
|
139 | |
public void setSuccessCodes(List<Integer> successCodes) { |
140 | 0 | this.successCodes = successCodes; |
141 | 0 | } |
142 | |
} |