private String internalReadURL(String urlString) throws Exception {
log.debug("Creating URL from string: " + urlString);
URL url = new URL(urlString);
log.debug("Created URL object from string, protocol="
+ url.getProtocol());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
/*
* Override the host verifier so we can use a test server cert with a
* hostname that may not match the https url hostname.
*/
System.setProperty("org.jboss.security.ignoreHttpsHost", "true");
Util.configureHttpsHostVerifier(conn);
// if SSL is used we must point to the test truststore.
if (conn instanceof HttpsURLConnection) {
// Setup the test keystore (truststore).
URL keyStoreURL = Thread.currentThread().getContextClassLoader()
.getResource("META-INF/tst.keystore");
InputStream stream = keyStoreURL.openStream();
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(stream, "unit-tests".toCharArray());
// Setup the test TrustManagerFactory.
TrustManagerFactory trustMgr = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustMgr.init(keyStore);
// Setup the test SSLSocketFactory.
SSLContext sslCtx = SSLContext.getInstance("TLS");
sslCtx.init(null, trustMgr.getTrustManagers(), null);
((HttpsURLConnection) conn).setSSLSocketFactory(sslCtx.getSocketFactory());
}
// Connect to the remote HTTP server.
log.debug("Connecting to URL: " + url);
byte[] buffer = new byte[1024];
int length = conn.getContentLength();
log.debug("ContentLength: " + length);
InputStream is = conn.getInputStream();
StringBuffer reply = new StringBuffer();
while ((length = is.read(buffer)) > 0)
reply.append(new String(buffer, 0, length));
log.debug("Done, closing streams");
is.close();