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