use of org.ovirt.engine.sdk4.Error in project ovirt-engine-sdk-java by oVirt.
the class ConnectionBuilder42 method createConnectionSocketFactoryRegistry.
private SchemeRegistry createConnectionSocketFactoryRegistry() {
SchemeRegistry schemeRegistry = new SchemeRegistry();
SSLSocketFactory sf;
// Create SSL/TLS or plain connection:
if (HTTP_PROTOCOL.equals(getProtocol())) {
schemeRegistry.register(new Scheme(HTTP_PROTOCOL, getPort(), PlainSocketFactory.getSocketFactory()));
} else if (HTTPS_PROTOCOL.equals(getProtocol())) {
try {
if (this.insecure) {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[] { noCaTrustManager }, null);
sf = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
} else {
KeyStore truststore = null;
InputStream in = null;
if (this.trustStoreFile != null) {
truststore = KeyStore.getInstance(KeyStore.getDefaultType());
try {
in = new FileInputStream(this.trustStoreFile);
truststore.load(in, this.trustStorePassword != null ? this.trustStorePassword.toCharArray() : null);
} finally {
if (in != null) {
in.close();
}
}
}
sf = new SSLSocketFactory(SSLSocketFactory.TLS, null, null, truststore, null, null, SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
}
schemeRegistry.register(new Scheme(HTTPS_PROTOCOL, getPort(), sf));
} catch (NoSuchAlgorithmException e) {
throw new Error(NO_TLS_ERROR, e);
} catch (KeyManagementException e) {
throw new Error(BAD_KEY_ERROR, e);
} catch (KeyStoreException e) {
throw new Error(KEY_STORE_ERROR, e);
} catch (FileNotFoundException e) {
throw new Error(KEY_STORE_FILE_NOT_FOUND_ERROR, e);
} catch (CertificateException e) {
throw new Error(CERTIFICATE_ERROR, e);
} catch (IOException e) {
throw new Error(IO_ERROR, e);
} catch (UnrecoverableKeyException e) {
throw new Error(UNRECOVERABLE_KEY_ERROR, e);
}
}
return schemeRegistry;
}
use of org.ovirt.engine.sdk4.Error in project ovirt-engine-sdk-java by oVirt.
the class HttpConnection method checkContentType.
/**
* Checks the given content type and raises an exception if it isn't the
* expected one.
*
* @param pattern The regular expression used to check the expected content type.
* @param expectedName The name of the expected content type.
* @param actual The actual value of the `Content-Type` header.
* @throws MalformedURLException When URL isn't correct.
*/
private void checkContentType(Pattern pattern, String expectedName, String actual) throws MalformedURLException {
if (!pattern.matcher(actual).matches()) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("The response content type '%1$s' isn't the expected %2$s", actual, expectedName));
URL url = new URL(getUrl());
if (!url.getPath().equals(TYPICAL_PATH)) {
sb.append(String.format(". Is the path '%1$s' included in the 'url' parameter correct?", url.getPath()));
sb.append(String.format(" The typical one is '%1$s'", TYPICAL_PATH));
}
throw new Error(sb.toString());
}
}
use of org.ovirt.engine.sdk4.Error in project ovirt-engine-sdk-java by oVirt.
the class HttpConnection method getSsoResponse.
private JsonNode getSsoResponse(URI uri, List<NameValuePair> params) {
HttpResponse response = null;
try {
// Send request and parse token:
HttpPost requestToken = new HttpPost(uri);
requestToken.addHeader("User-Agent", "JavaSDK");
requestToken.addHeader("Accept", "application/json");
requestToken.setEntity(new UrlEncodedFormEntity(params));
response = client.execute(requestToken);
checkContentType(JSON_CONTENT_TYPE_RE, "JSON", response.getFirstHeader("content-type").getValue());
ObjectMapper mapper = new ObjectMapper();
return mapper.readTree(response.getEntity().getContent());
} catch (IOException ex) {
throw new Error("Failed to parse JSON response", ex);
} catch (Exception ex) {
throw new Error("Failed to send SSO request", ex);
} finally {
if (response != null) {
EntityUtils.consumeQuietly(response.getEntity());
}
}
}
use of org.ovirt.engine.sdk4.Error in project ovirt-engine-sdk-java by oVirt.
the class SsoUtils method buildSsoUrlKerberos.
/**
* Construct SSO URL to obtain token from kerberos authentication.
*
* @param url oVirt engine URL
* @return URI to be used to obtain token
*/
public static URI buildSsoUrlKerberos(String url) {
try {
URI uri = new URI(url);
URIBuilder uriBuilder = new URIBuilder(String.format("%1$s://%2$s/ovirt-engine/sso/oauth/%3$s", uri.getScheme(), uri.getAuthority(), ENTRY_POINT_HTTP));
return uriBuilder.build();
} catch (URISyntaxException ex) {
throw new Error("Failed to build SSO authentication URL", ex);
}
}
use of org.ovirt.engine.sdk4.Error in project ovirt-engine-sdk-java by oVirt.
the class SsoUtils method buildSsoRevokeUrl.
/**
* Construct SSO URL to revoke SSO token
*
* @param url oVirt engine URL
* @return URI to be used to revoke token
*/
public static URI buildSsoRevokeUrl(String url) {
try {
URI uri = new URI(url);
URIBuilder uriBuilder = new URIBuilder(String.format("%1$s://%2$s/ovirt-engine/services/sso-logout", uri.getScheme(), uri.getAuthority()));
return uriBuilder.build();
} catch (URISyntaxException ex) {
throw new Error("Failed to build SSO revoke URL", ex);
}
}
Aggregations