CASC-180: Fix the issue with casting to http url connections and provide a default instance for the http url factory.

This commit is contained in:
Misagh Moayyed 2013-03-04 14:17:55 -07:00
parent d102c50779
commit c34ff785dd
4 changed files with 15 additions and 11 deletions

View File

@ -2,6 +2,7 @@ package org.jasig.cas.client.ssl;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URLConnection;
import java.security.KeyStore;
import java.util.Properties;
@ -30,6 +31,8 @@ public final class HttpsURLConnectionFactory implements URLConnectionFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpsURLConnectionFactory.class);
public static final HttpsURLConnectionFactory INSTANCE = new HttpsURLConnectionFactory();
/**
* Hostname verifier used when making an SSL request to the CAS server.
* Defaults to {@link HttpsURLConnection#getDefaultHostnameVerifier()}
@ -63,7 +66,7 @@ public final class HttpsURLConnectionFactory implements URLConnectionFactory {
this.hostnameVerifier = verifier;
}
public URLConnection getURLConnection(final URLConnection url) {
public HttpURLConnection buildHttpURLConnection(final URLConnection url) {
return this.configureHttpsConnectionIfNeeded(url);
}
@ -74,7 +77,7 @@ public final class HttpsURLConnectionFactory implements URLConnectionFactory {
*
* @param conn the http connection
*/
private URLConnection configureHttpsConnectionIfNeeded(final URLConnection conn) {
private HttpURLConnection configureHttpsConnectionIfNeeded(final URLConnection conn) {
if (conn instanceof HttpsURLConnection) {
final HttpsURLConnection httpsConnection = (HttpsURLConnection) conn;
final SSLSocketFactory socketFactory = this.createSSLSocketFactory();
@ -86,7 +89,7 @@ public final class HttpsURLConnectionFactory implements URLConnectionFactory {
httpsConnection.setHostnameVerifier(this.hostnameVerifier);
}
}
return conn;
return (HttpURLConnection)conn;
}
/**

View File

@ -18,6 +18,7 @@
*/
package org.jasig.cas.client.ssl;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
@ -36,9 +37,9 @@ public interface URLConnectionFactory {
* to accommodate method chaining.
*
* @param url The url connection that needs to be configured
* @return The configured {@link URLConnection} instance
* @return The configured {@link HttpURLConnection} instance
*
* @see {@link HttpsURLConnectionFactory}
*/
URLConnection getURLConnection(final URLConnection url);
HttpURLConnection buildHttpURLConnection(final URLConnection url);
}

View File

@ -331,9 +331,9 @@ public final class CommonUtils {
*/
public static String getResponseFromServer(final URL constructedUrl, final URLConnectionFactory factory, final String encoding) {
URLConnection conn = null;
HttpURLConnection conn = null;
try {
conn = factory.getURLConnection(constructedUrl.openConnection());
conn = factory.buildHttpURLConnection(constructedUrl.openConnection());
final BufferedReader in;
@ -355,8 +355,8 @@ public final class CommonUtils {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
if (conn != null && conn instanceof HttpURLConnection) {
((HttpURLConnection)conn).disconnect();
if (conn != null) {
conn.disconnect();
}
}
@ -371,7 +371,7 @@ public final class CommonUtils {
*/
public static String getResponseFromServer(final String url, String encoding) {
try {
return getResponseFromServer(new URL(url), new HttpsURLConnectionFactory(), encoding);
return getResponseFromServer(new URL(url), HttpsURLConnectionFactory.INSTANCE, encoding);
} catch (final MalformedURLException e) {
throw new IllegalArgumentException(e);
}

View File

@ -233,7 +233,7 @@ public final class Saml11TicketValidator extends AbstractUrlBasedTicketValidator
BufferedReader in = null;
try {
conn = (HttpURLConnection) this.getURLConnectionFactory().getURLConnection(validationUrl.openConnection());
conn = this.getURLConnectionFactory().buildHttpURLConnection(validationUrl.openConnection());
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml");
conn.setRequestProperty("Content-Length", Integer.toString(MESSAGE_TO_SEND.length()));