Coverage Report - org.kuali.maven.plugins.dnsme.HttpUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
HttpUtil
67%
58/86
40%
4/10
2
HttpUtil$1
100%
1/1
N/A
2
 
 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  3
         if (l == -1) {
 26  2
             return "";
 27  
         } else {
 28  1
             return " - (Timeout in " + l + "s)";
 29  
         }
 30  
     }
 31  
 
 32  
     public void log(String url, HttpRequestResult result, int secondsRemaining) {
 33  3
         StringBuilder sb = new StringBuilder();
 34  3
         sb.append("Status for '" + url + "' is '" + getMsg(result) + "'");
 35  3
         sb.append(getTimeout(secondsRemaining));
 36  3
         logger.info(sb.toString());
 37  3
     }
 38  
 
 39  
     protected String getMsg(HttpRequestResult result) {
 40  3
         switch (result.getType()) {
 41  
         case EXCEPTION:
 42  0
             Exception exception = result.getException();
 43  0
             return exception.getMessage();
 44  
         case COMPLETED:
 45  3
             int statusCode = result.getStatusCode();
 46  3
             String statusText = result.getStatusText();
 47  3
             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  4
         HttpClient client = new HttpClient();
 86  4
         HttpClientParams clientParams = client.getParams();
 87  4
         HttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(0, false);
 88  4
         clientParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler);
 89  4
         clientParams.setParameter(HttpMethodParams.SO_TIMEOUT, requestTimeout);
 90  4
         return client;
 91  
     }
 92  
 
 93  
     protected String getResponseBody(HttpMethod method) throws IOException {
 94  4
         InputStream in = null;
 95  
         try {
 96  4
             in = method.getResponseBodyAsStream();
 97  4
             return IOUtils.toString(in);
 98  
         } finally {
 99  4
             IOUtils.closeQuietly(in);
 100  
         }
 101  
     }
 102  
 
 103  
     public HttpRequestResult executeMethod(HttpMethod method) {
 104  2
         return executeMethod(getHttpClient(), method);
 105  
     }
 106  
 
 107  
     public HttpRequestResult executeMethod(HttpClient client, HttpMethod method) {
 108  4
         HttpRequestResult result = new HttpRequestResult();
 109  
         try {
 110  4
             client.executeMethod(method);
 111  4
             int statusCode = method.getStatusCode();
 112  4
             String statusText = method.getStatusText();
 113  4
             String responseBody = getResponseBody(method);
 114  4
             Header[] responseHeaders = method.getResponseHeaders();
 115  4
             method.releaseConnection();
 116  4
             result.setStatusCode(statusCode);
 117  4
             result.setStatusText(statusText);
 118  4
             result.setResponseBody(responseBody);
 119  4
             result.setResponseHeaders(responseHeaders);
 120  4
             result.setType(HttpRequestResultType.COMPLETED);
 121  0
         } catch (Exception e) {
 122  0
             result.setType(HttpRequestResultType.EXCEPTION);
 123  0
             result.setException(e);
 124  4
         }
 125  4
         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  
 }