KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > server > util > HttpUtil


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.InputStreamReader JavaDoc;
18 import java.net.ConnectException JavaDoc;
19 import java.net.URL JavaDoc;
20
21 /**
22  * This utility is meant to be expanded. It delegates to the Apache Commons Http package.
23  */

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     // cannot instantiate
33
}
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 JavaDoc url, HttpClient client) throws ConnectException JavaDoc, IOException JavaDoc {
43     return Boolean.valueOf(getResponseBody(url, client)).booleanValue();
44   }
45
46   public static boolean[] getBooleanValues(URL JavaDoc url, HttpClient client) throws ConnectException JavaDoc, IOException JavaDoc {
47     String JavaDoc responseBody = getResponseBody(url, client);
48     String JavaDoc[] 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 JavaDoc getResponseBody(URL JavaDoc url, HttpClient client) throws ConnectException JavaDoc, IOException JavaDoc {
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     // this disables the automatic request retry junk in HttpClient
65
get.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, NoRetryHandler.INSTANCE);
66
67     try {
68       int status = client.executeMethod(get);
69       if (status != HttpStatus.SC_OK) {
70         // make formatter sane
71
throw new ConnectException JavaDoc("The http client has encountered a status code other than ok for the url: " + url
72                                    + " status: " + HttpStatus.getStatusText(status));
73       }
74       StringBuffer JavaDoc response = new StringBuffer JavaDoc(100);
75       BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(get.getResponseBodyAsStream()));
76       String JavaDoc 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 JavaDoc url, HttpClient client) throws ConnectException JavaDoc, IOException JavaDoc {
88     return Integer.valueOf(getResponseBody(url, client)).intValue();
89   }
90
91   public static int[] getIntValues(URL JavaDoc url, HttpClient client) throws ConnectException JavaDoc, IOException JavaDoc {
92     String JavaDoc responseBody = getResponseBody(url, client);
93     String JavaDoc[] 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 JavaDoc 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 JavaDoc ioexception, int i) {
112       return false;
113     }
114
115   }
116
117 }
Popular Tags