diff --git a/cas-client-core/pom.xml b/cas-client-core/pom.xml index bdc4d34..20b9f72 100644 --- a/cas-client-core/pom.xml +++ b/cas-client-core/pom.xml @@ -2,7 +2,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> org.jasig.cas - 3.1.8 + 3.1.9-SNAPSHOT cas-client 4.0.0 diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/proxy/Cas20ProxyRetriever.java b/cas-client-core/src/main/java/org/jasig/cas/client/proxy/Cas20ProxyRetriever.java index 1dc986e..59e3fec 100644 --- a/cas-client-core/src/main/java/org/jasig/cas/client/proxy/Cas20ProxyRetriever.java +++ b/cas-client-core/src/main/java/org/jasig/cas/client/proxy/Cas20ProxyRetriever.java @@ -60,44 +60,18 @@ public final class Cas20ProxyRetriever implements ProxyRetriever { final String targetService) { final String url = constructUrl(proxyGrantingTicketId, targetService); - HttpURLConnection conn = null; - try { - final URL constructedUrl = new URL(url); - conn = (HttpURLConnection) constructedUrl.openConnection(); + final String response = CommonUtils.getResponseFromServer(url); + final String error = XmlUtils.getTextForElement(response, "proxyFailure"); - final BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); - - String line; - final StringBuffer stringBuffer = new StringBuffer(255); - final String response; - - synchronized (stringBuffer) { - while ((line = in.readLine()) != null) { - stringBuffer.append(line); - } - response = stringBuffer.toString(); - } - - final String error = XmlUtils.getTextForElement(response, - "proxyFailure"); - - if (CommonUtils.isNotEmpty(error)) { - log.debug(error); - return null; - } - - return XmlUtils.getTextForElement(response, "proxyTicket"); - } catch (final Exception e) { - throw new RuntimeException(e); - } finally { - if (conn != null) { - conn.disconnect(); - } + if (CommonUtils.isNotEmpty(error)) { + log.debug(error); + return null; } + + return XmlUtils.getTextForElement(response, "proxyTicket"); } - private String constructUrl(final String proxyGrantingTicketId, - final String targetService) { + private String constructUrl(final String proxyGrantingTicketId, final String targetService) { try { return this.casServerUrl + (this.casServerUrl.endsWith("/") ? "" : "/") + "proxy" + "?pgt=" + proxyGrantingTicketId + "&targetService=" diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/util/CommonUtils.java b/cas-client-core/src/main/java/org/jasig/cas/client/util/CommonUtils.java index 5b7598d..200ab59 100644 --- a/cas-client-core/src/main/java/org/jasig/cas/client/util/CommonUtils.java +++ b/cas-client-core/src/main/java/org/jasig/cas/client/util/CommonUtils.java @@ -15,7 +15,12 @@ import javax.servlet.ServletRequest; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.net.URLEncoder; +import java.net.URL; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.util.Collection; /** @@ -137,7 +142,7 @@ public final class CommonUtils { * @param gateway where we should send gateway or not. * @return the fully constructed redirect url. */ - public static final String constructRedirectUrl(final String casServerLoginUrl, final String serviceParameterName, final String serviceUrl, final boolean renew, final boolean gateway) { + public static String constructRedirectUrl(final String casServerLoginUrl, final String serviceParameterName, final String serviceUrl, final boolean renew, final boolean gateway) { try { return casServerLoginUrl + (casServerLoginUrl.indexOf("?") != -1 ? "&" : "?") + serviceParameterName + "=" + URLEncoder.encode(serviceUrl, "UTF-8") @@ -181,6 +186,10 @@ public final class CommonUtils { * * @param request the HttpServletRequest * @param response the HttpServletResponse + * @param service the configured service url (this will be used if not null) + * @param serverName the server name to use to constuct the service url if the service param is empty + * @param artifactParameterName the artifact parameter name to remove (i.e. ticket) + * @param encode whether to encode the url or not (i.e. Jsession). * @return the service url to use. */ public static String constructServiceUrl(final HttpServletRequest request, @@ -257,4 +266,52 @@ public final class CommonUtils { } return request.getQueryString() == null || request.getQueryString().indexOf(parameter) == -1 ? null : request.getParameter(parameter); } + + /** + * Contacts the remote URL and returns the response. + * + * @param constructedUrl the url to contact. + * @return the response. + */ + public static String getResponseFromServer(final URL constructedUrl) { + HttpURLConnection conn = null; + try { + conn = (HttpURLConnection) constructedUrl.openConnection(); + + final BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); + + String line; + final StringBuffer stringBuffer = new StringBuffer(255); + + synchronized (stringBuffer) { + while ((line = in.readLine()) != null) { + stringBuffer.append(line); + stringBuffer.append("\n"); + } + return stringBuffer.toString(); + } + } catch (final Exception e) { + LOG.error(e.getMessage(), e); + throw new RuntimeException(e); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + + } + + /** + * Contacts the remote URL and returns the response. + * + * @param url the url to contact. + * @return the response. + */ + public static String getResponseFromServer(final String url) { + try { + return getResponseFromServer(new URL(url)); + } catch (final MalformedURLException e) { + throw new IllegalArgumentException(e); + } + } } diff --git a/cas-client-core/src/main/java/org/jasig/cas/client/validation/AbstractCasProtocolUrlBasedTicketValidator.java b/cas-client-core/src/main/java/org/jasig/cas/client/validation/AbstractCasProtocolUrlBasedTicketValidator.java index edc10c5..2046fdf 100644 --- a/cas-client-core/src/main/java/org/jasig/cas/client/validation/AbstractCasProtocolUrlBasedTicketValidator.java +++ b/cas-client-core/src/main/java/org/jasig/cas/client/validation/AbstractCasProtocolUrlBasedTicketValidator.java @@ -5,6 +5,8 @@ */ package org.jasig.cas.client.validation; +import org.jasig.cas.client.util.CommonUtils; + import java.net.URL; import java.net.HttpURLConnection; import java.io.BufferedReader; @@ -28,30 +30,6 @@ public abstract class AbstractCasProtocolUrlBasedTicketValidator extends Abstrac * Retrieves the response from the server by opening a connection and merely reading the response. */ protected final String retrieveResponseFromServer(final URL validationUrl, final String ticket) { - HttpURLConnection connection = null; - - try { - connection = (HttpURLConnection) validationUrl.openConnection(); - final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); - - String line; - final StringBuffer stringBuffer = new StringBuffer(255); - - synchronized (stringBuffer) { - while ((line = in.readLine()) != null) { - stringBuffer.append(line); - stringBuffer.append("\n"); - } - return stringBuffer.toString(); - } - - } catch (final IOException e) { - log.error(e,e); - return null; - } finally { - if (connection != null) { - connection.disconnect(); - } - } + return CommonUtils.getResponseFromServer(validationUrl); } } diff --git a/cas-client-integration-atlassian/pom.xml b/cas-client-integration-atlassian/pom.xml index 9ebe77e..ff1421c 100644 --- a/cas-client-integration-atlassian/pom.xml +++ b/cas-client-integration-atlassian/pom.xml @@ -2,7 +2,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> org.jasig.cas - 3.1.8 + 3.1.9-SNAPSHOT cas-client 4.0.0 diff --git a/pom.xml b/pom.xml index 0164410..b95a910 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ 4.0.0 org.jasig.cas - 3.1.8 + 3.1.9-SNAPSHOT cas-client pom JA-SIG CAS Client for Java