public SessionInfo getSession(String version, Credential credential) throws ClientException, ServiceException {
String authMethodUrn = guessAuthenticationMethodUrn(credential);
String authnMethod = authMethodUrn == null ? WASAuthenticationMethod.URN : authMethodUrn;
String encodedCredential = getCredentialFactory().encode(authnMethod, credential);
HTTPRequest httpRequest = httpClientFactory.create(getWssURLString()).post()
.contentType("application/x-www-form-urlencoded", "UTF-8")
.parameter("VERSION", version, "CREDENTIALS", encodedCredential, "SERVICE", "WSS", "REQUEST", "GetSession", "AUTHMETHOD", "authnMethod");
HTTPResponse<InputStream> httpResponse = httpRequest.asStream().start();
if (httpResponse.isError()) {
LOG.error(httpResponse.getError().getMessage(), httpResponse.getError());
throw new ClientException("I/O exception during GETSESSION request to Security Service:\n" + httpResponse.getError(), ClientException.CONNECTION_FAILED);
}
String contentType = httpResponse.getContentType();
if (contentType.startsWith("text/xml")) {
return SessionInfo.createFromXML(httpResponse.getContent());
} else if (contentType.startsWith("application/vnd.ogc.se_xml")) {
throw ServiceException.createFromXML(new InputSource(httpResponse.getContent()));
} else {
LOG.error("GetSession response contained no session information. Response is: " + StringUtils.streamToString(httpResponse.getContent(), "UTF-8"));
throw new ClientException("Security Service response specifies unknown/unexpected MIME type: " + contentType + ". Session information unavailable.", ClientException.UNEXPECTED_MIME_TYPE);
}
}