Coverage Report - org.kuali.maven.plugins.HttpInspector
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpInspector
0%
0/67
0%
0/12
1.875
 
 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 boolean 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
                         boolean success = doRequest(client, url, secondsRemaining);
 55  0
                         if (success) {
 56  0
                                 return true;
 57  
                         }
 58  0
                         sleep(sleepInterval);
 59  0
                         if (System.currentTimeMillis() > end) {
 60  0
                                 logger.info("Timed out waiting for response from '" + url + "'");
 61  0
                                 return false;
 62  
                         }
 63  0
                 }
 64  
         }
 65  
 
 66  
         protected HttpClient getHttpClient() {
 67  0
                 HttpClient client = new HttpClient();
 68  0
                 HttpClientParams clientParams = client.getParams();
 69  0
                 HttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(0, false);
 70  0
                 clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);
 71  0
                 clientParams.setParameter(HttpMethodParams.SO_TIMEOUT, requestTimeout);
 72  0
                 return client;
 73  
         }
 74  
 
 75  
         protected boolean doRequest(HttpClient client, String url, long secondsRemaining) {
 76  
                 try {
 77  0
                         HttpMethod method = new GetMethod(url);
 78  0
                         client.executeMethod(method);
 79  0
                         int statusCode = method.getStatusCode();
 80  0
                         String statusText = method.getStatusText();
 81  0
                         boolean success = isSuccess(statusCode);
 82  0
                         if (success) {
 83  0
                                 logger.info(getMsg("Status for '" + url + "' is '" + statusCode + ":" + statusText + "'"));
 84  0
                                 return true;
 85  
                         } else {
 86  0
                                 logger.info(getMsg("Status for '" + url + "' is '" + statusCode + ":" + statusText + "'",
 87  
                                                 secondsRemaining));
 88  
                         }
 89  0
                 } catch (IOException e) {
 90  0
                         logger.info(getMsg("Status for '" + url + "' is '" + e.getMessage() + "'", secondsRemaining));
 91  0
                 }
 92  0
                 return false;
 93  
         }
 94  
 
 95  
         protected void sleep(long millis) {
 96  
                 try {
 97  0
                         Thread.sleep(millis);
 98  0
                 } catch (InterruptedException e) {
 99  0
                         throw new RuntimeException(e);
 100  0
                 }
 101  0
         }
 102  
 
 103  
         public int getRequestTimeout() {
 104  0
                 return requestTimeout;
 105  
         }
 106  
 
 107  
         public void setRequestTimeout(int requestTimeout) {
 108  0
                 this.requestTimeout = requestTimeout;
 109  0
         }
 110  
 
 111  
         public int getSleepInterval() {
 112  0
                 return sleepInterval;
 113  
         }
 114  
 
 115  
         public void setSleepInterval(int sleepInterval) {
 116  0
                 this.sleepInterval = sleepInterval;
 117  0
         }
 118  
 
 119  
         public int getTimeout() {
 120  0
                 return timeout;
 121  
         }
 122  
 
 123  
         public void setTimeout(int waitTimeout) {
 124  0
                 this.timeout = waitTimeout;
 125  0
         }
 126  
 
 127  
         public Logger getLogger() {
 128  0
                 return logger;
 129  
         }
 130  
 
 131  
         public List<Integer> getSuccessCodes() {
 132  0
                 return successCodes;
 133  
         }
 134  
 
 135  
         public void setSuccessCodes(List<Integer> successCodes) {
 136  0
                 this.successCodes = successCodes;
 137  0
         }
 138  
 }