001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.test.server.util;
005:
006: import org.apache.commons.httpclient.Cookie;
007: import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
008: import org.apache.commons.httpclient.HttpClient;
009: import org.apache.commons.httpclient.HttpException;
010: import org.apache.commons.httpclient.HttpMethod;
011: import org.apache.commons.httpclient.HttpMethodRetryHandler;
012: import org.apache.commons.httpclient.HttpStatus;
013: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
014: import org.apache.commons.httpclient.methods.GetMethod;
015: import org.apache.commons.httpclient.params.HttpMethodParams;
016:
017: import java.io.BufferedReader;
018: import java.io.IOException;
019: import java.io.InputStreamReader;
020: import java.net.ConnectException;
021: import java.net.URL;
022:
023: /**
024: * This utility is meant to be expanded. It delegates to the Apache Commons Http package.
025: */
026: public final class HttpUtil {
027:
028: private static final int DEFAULT_TIMEOUT = 60 * 1000;
029: private static final int DEFAULT_MAX_CONN = 1000;
030:
031: private static final boolean DEBUG = false;
032:
033: private HttpUtil() {
034: // cannot instantiate
035: }
036:
037: public static HttpClient createHttpClient() {
038: HttpClient client = new HttpClient(
039: new MultiThreadedHttpConnectionManager());
040: client.getHttpConnectionManager().getParams()
041: .setConnectionTimeout(DEFAULT_TIMEOUT);
042: client.getHttpConnectionManager().getParams()
043: .setMaxTotalConnections(DEFAULT_MAX_CONN);
044: return client;
045: }
046:
047: public static boolean getBoolean(URL url, HttpClient client)
048: throws ConnectException, IOException {
049: return Boolean.valueOf(getResponseBody(url, client))
050: .booleanValue();
051: }
052:
053: public static boolean[] getBooleanValues(URL url, HttpClient client)
054: throws ConnectException, IOException {
055: String responseBody = getResponseBody(url, client);
056: String[] lines = responseBody.split("\n");
057: boolean[] values = new boolean[lines.length];
058: for (int i = 0; i < lines.length; i++) {
059: values[i] = Boolean.valueOf(lines[i].trim()).booleanValue();
060: }
061: return values;
062: }
063:
064: public static String getResponseBody(URL url, HttpClient client)
065: throws HttpException, IOException {
066: return getResponseBody(url, client, false);
067: }
068:
069: public static String getResponseBody(URL url, HttpClient client,
070: boolean retryIfFail) throws HttpException, IOException {
071: StringBuffer response = new StringBuffer(100);
072: Cookie[] cookies = client.getState().getCookies();
073: for (int i = 0; i < cookies.length; i++) {
074: debugPrint("localClient... cookie " + i + ": "
075: + cookies[i].toString());
076: }
077:
078: GetMethod get = new GetMethod(url.toString());
079:
080: if (retryIfFail) {
081: // retries failed request 3 times
082: get.getParams().setParameter(
083: HttpMethodParams.RETRY_HANDLER,
084: new DefaultHttpMethodRetryHandler());
085: } else {
086: // this disables the automatic request retry junk in HttpClient
087: get.getParams().setParameter(
088: HttpMethodParams.RETRY_HANDLER,
089: NoRetryHandler.INSTANCE);
090: }
091:
092: BufferedReader reader = null;
093: try {
094: int status = client.executeMethod(get);
095: if (status != HttpStatus.SC_OK) {
096: // make formatter sane
097: throw new HttpException(
098: "The http client has encountered a status code other than ok for the url: "
099: + url + " status: "
100: + HttpStatus.getStatusText(status));
101: }
102: reader = new BufferedReader(new InputStreamReader(get
103: .getResponseBodyAsStream()));
104: String line;
105: while ((line = reader.readLine()) != null) {
106: response.append(line).append("\n");
107: }
108: } finally {
109: if (reader != null)
110: reader.close();
111: get.releaseConnection();
112: }
113: return response.toString().trim();
114: }
115:
116: public static int getInt(URL url, HttpClient client)
117: throws ConnectException, IOException {
118: return Integer.valueOf(getResponseBody(url, client)).intValue();
119: }
120:
121: public static int[] getIntValues(URL url, HttpClient client)
122: throws ConnectException, IOException {
123: String responseBody = getResponseBody(url, client);
124: String[] lines = responseBody.split("\n");
125: int[] values = new int[lines.length];
126: for (int i = 0; i < lines.length; i++) {
127: values[i] = Integer.valueOf(lines[i].trim()).intValue();
128: }
129: return values;
130: }
131:
132: private static void debugPrint(String s) {
133: if (DEBUG) {
134: System.out.println("XXXXX " + s);
135: }
136: }
137:
138: private static class NoRetryHandler implements
139: HttpMethodRetryHandler {
140:
141: static final NoRetryHandler INSTANCE = new NoRetryHandler();
142:
143: public boolean retryMethod(HttpMethod httpmethod,
144: IOException ioexception, int i) {
145: return false;
146: }
147:
148: }
149:
150: }
|