// Post the CAS info
String casServiceUrl = uriBuilder.setPath(casUrl).addParameter("service", url).build().toString();
casPostMethod = new PostMethod(casServiceUrl);
casPostMethod.setFollowRedirects(false);
// Prepare login parameters
NameValuePair lt = new NameValuePair("lt", ltTagValue);
NameValuePair execution = new NameValuePair("execution", executionTagValue);
NameValuePair userid = new NameValuePair("username", getCasUser());
NameValuePair password = new NameValuePair("password", getCasPassword());
NameValuePair eventId = new NameValuePair("_eventId", "submit");
NameValuePair submit = new NameValuePair("submit", "LOGIN");
casPostMethod.setRequestBody(new NameValuePair[] { lt, execution, userid, password, eventId, submit });
casPostMethod.setRequestHeader(new Header("Cookie", sessionCookie.toExternalForm()));
client.executeMethod(casPostMethod);
casPostMethod.releaseConnection();
int statuscode = casPostMethod.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
Header locationHeader = casPostMethod.getResponseHeader("location");
if (locationHeader != null) {
String serviceRedirectUri = locationHeader.getValue();
if ((serviceRedirectUri == null) || (serviceRedirectUri.equals(""))) {
serviceRedirectUri = "/";
}
GetMethod serviceRedirectGetMethod = new GetMethod(serviceRedirectUri);
client.executeMethod(serviceRedirectGetMethod);
inputStream = serviceRedirectGetMethod.getResponseBodyAsStream();
String jsonResponse = IOUtils.toString(inputStream, "UTF-8");
answer = jsonResponse;
serviceRedirectGetMethod.releaseConnection();
}
}
} else {
// Should be the the service response
// Read the response body.
answer = possibleLoginHtml;
}
}
} catch (HttpException e) {
logger.error("Fatal protocol violation: ", e);
} catch (IOException e) {
logger.error("Fatal transport error: ", e);
} catch (URISyntaxException e) {
logger.error("Syntax error in URL", e);
}
return answer;
}