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