1 4 package com.tc.test.server.util; 5 6 import org.apache.commons.httpclient.Cookie; 7 import org.apache.commons.httpclient.HttpClient; 8 import org.apache.commons.httpclient.HttpMethod; 9 import org.apache.commons.httpclient.HttpMethodRetryHandler; 10 import org.apache.commons.httpclient.HttpStatus; 11 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 12 import org.apache.commons.httpclient.methods.GetMethod; 13 import org.apache.commons.httpclient.params.HttpMethodParams; 14 15 import java.io.BufferedReader ; 16 import java.io.IOException ; 17 import java.io.InputStreamReader ; 18 import java.net.ConnectException ; 19 import java.net.URL ; 20 21 24 public final class HttpUtil { 25 26 private static final int DEFAULT_TIMEOUT = 60 * 1000; 27 private static final int DEFAULT_MAX_CONN = 1000; 28 29 private static final boolean DEBUG = false; 30 31 private HttpUtil() { 32 } 34 35 public static HttpClient createHttpClient() { 36 HttpClient client = new HttpClient(new MultiThreadedHttpConnectionManager()); 37 client.getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_TIMEOUT); 38 client.getHttpConnectionManager().getParams().setMaxTotalConnections(DEFAULT_MAX_CONN); 39 return client; 40 } 41 42 public static boolean getBoolean(URL url, HttpClient client) throws ConnectException , IOException { 43 return Boolean.valueOf(getResponseBody(url, client)).booleanValue(); 44 } 45 46 public static boolean[] getBooleanValues(URL url, HttpClient client) throws ConnectException , IOException { 47 String responseBody = getResponseBody(url, client); 48 String [] lines = responseBody.split("\n"); 49 boolean[] values = new boolean[lines.length]; 50 for (int i = 0; i < lines.length; i++) { 51 values[i] = Boolean.valueOf(lines[i].trim()).booleanValue(); 52 } 53 return values; 54 } 55 56 public static String getResponseBody(URL url, HttpClient client) throws ConnectException , IOException { 57 Cookie[] cookies = client.getState().getCookies(); 58 for (int i = 0; i < cookies.length; i++) { 59 debugPrint("localClient... cookie " + i + ": " + cookies[i].toString()); 60 } 61 62 GetMethod get = new GetMethod(url.toString()); 63 64 get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, NoRetryHandler.INSTANCE); 66 67 try { 68 int status = client.executeMethod(get); 69 if (status != HttpStatus.SC_OK) { 70 throw new ConnectException ("The http client has encountered a status code other than ok for the url: " + url 72 + " status: " + HttpStatus.getStatusText(status)); 73 } 74 StringBuffer response = new StringBuffer (100); 75 BufferedReader reader = new BufferedReader (new InputStreamReader (get.getResponseBodyAsStream())); 76 String line; 77 while ((line = reader.readLine()) != null) { 78 response.append(line).append("\n"); 79 } 80 reader.close(); 81 return response.toString().trim(); 82 } finally { 83 get.releaseConnection(); 84 } 85 } 86 87 public static int getInt(URL url, HttpClient client) throws ConnectException , IOException { 88 return Integer.valueOf(getResponseBody(url, client)).intValue(); 89 } 90 91 public static int[] getIntValues(URL url, HttpClient client) throws ConnectException , IOException { 92 String responseBody = getResponseBody(url, client); 93 String [] lines = responseBody.split("\n"); 94 int[] values = new int[lines.length]; 95 for (int i = 0; i < lines.length; i++) { 96 values[i] = Integer.valueOf(lines[i].trim()).intValue(); 97 } 98 return values; 99 } 100 101 private static void debugPrint(String s) { 102 if (DEBUG) { 103 System.out.println("XXXXX " + s); 104 } 105 } 106 107 private static class NoRetryHandler implements HttpMethodRetryHandler { 108 109 static final NoRetryHandler INSTANCE = new NoRetryHandler(); 110 111 public boolean retryMethod(HttpMethod httpmethod, IOException ioexception, int i) { 112 return false; 113 } 114 115 } 116 117 } | Popular Tags |