From: <do...@hy...> - 2007-08-11 03:59:45
|
Author: dougm Date: 2007-08-10 20:59:41 -0700 (Fri, 10 Aug 2007) New Revision: 5547 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=5547 Modified: trunk/src/org/hyperic/hq/product/servlet/client/JMXProtocolHttp.java trunk/src/org/hyperic/hq/product/servlet/client/JMXProtocolHttps.java Log: [HHQ-930] Use httpclient instead of URL.openConnection Modified: trunk/src/org/hyperic/hq/product/servlet/client/JMXProtocolHttp.java =================================================================== --- trunk/src/org/hyperic/hq/product/servlet/client/JMXProtocolHttp.java 2007-08-11 00:54:05 UTC (rev 5546) +++ trunk/src/org/hyperic/hq/product/servlet/client/JMXProtocolHttp.java 2007-08-11 03:59:41 UTC (rev 5547) @@ -26,10 +26,13 @@ package org.hyperic.hq.product.servlet.client; import java.net.URL; -import java.net.HttpURLConnection; import java.io.InputStream; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.UsernamePasswordCredentials; +import org.apache.commons.httpclient.methods.GetMethod; + /** * Talk to jmx servlet over http. */ @@ -38,6 +41,10 @@ public void shutdown() { } + protected boolean isSSL() { + return false; + } + public InputStream openStream(String host, int port, String user, String pass, String path, String query) @@ -47,20 +54,29 @@ path=path + "?" + query; } - //XXX cache - URL url=new URL("http", host, port, path); + String scheme = isSSL() ? "https" : "http"; + URL url = new URL(scheme, host, port, path); - HttpURLConnection conn=(HttpURLConnection)url.openConnection(); + HttpClient client = new HttpClient(); + final int timeout = 1 * 1000; //1min + client.setTimeout(timeout); + client.setConnectionTimeout(timeout); + GetMethod get = new GetMethod(url.toString()); + if (user != null) { - String auth = user + ":" + pass; - - // Easiest - but may not be very portable. - auth = new sun.misc.BASE64Encoder().encode(auth.getBytes()); + UsernamePasswordCredentials credentials = + new UsernamePasswordCredentials(user, pass); - conn.setRequestProperty("Authorization", "Basic " + auth); + client.getState().setCredentials(host, + host, + credentials); + + get.setDoAuthentication(true); } - - return conn.getInputStream(); + + client.executeMethod(get); + + return get.getResponseBodyAsStream(); } } Modified: trunk/src/org/hyperic/hq/product/servlet/client/JMXProtocolHttps.java =================================================================== --- trunk/src/org/hyperic/hq/product/servlet/client/JMXProtocolHttps.java 2007-08-11 00:54:05 UTC (rev 5546) +++ trunk/src/org/hyperic/hq/product/servlet/client/JMXProtocolHttps.java 2007-08-11 03:59:41 UTC (rev 5547) @@ -25,57 +25,13 @@ package org.hyperic.hq.product.servlet.client; -import java.net.URL; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLSession; - -import java.io.InputStream; - /** * Talk to HQ JMXServlet over https. */ public class JMXProtocolHttps - extends JMXProtocolRequest - implements HostnameVerifier { + extends JMXProtocolHttp { - public void shutdown() { - } - - public boolean verify(String hostname, SSLSession session) { + protected boolean isSSL() { return true; } - - public InputStream openStream(String host, int port, - String user, String pass, - String path, String query) - throws Exception - { - if (query != null) { - path=path + "?" + query; - } - - //XXX cache - - URL url = new URL("https", host, port, path); - - HttpsURLConnection conn = (HttpsURLConnection)url.openConnection(); - - // HQ certificate signed with hyperic.net. Install - // a bogus HostnameVerifier to allow identification mismatch - // as no customers will have that hostname. - conn.setHostnameVerifier(this); - - if (user != null) { - String auth = user + ":" + pass; - - // Easiest - but may not be very portable. - auth = new sun.misc.BASE64Encoder().encode(auth.getBytes()); - - conn.setRequestProperty("Authorization", "Basic " + auth); - } - - return conn.getInputStream(); - } } |