Search in sources :

Example 1 with RestClientException

use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.

the class GcpClientTest method setZonesUnknownException.

@Test(expected = Exception.class)
public void setZonesUnknownException() {
    // given
    GcpConfig gcpConfig = GcpConfig.builder().build();
    RestClientException exception = new RestClientException("unknown", 500);
    given(gcpComputeApi.zones(any(), any(), any())).willThrow(exception);
    // when
    new GcpClient(gcpMetadataApi, gcpComputeApi, gcpAuthenticator, gcpConfig);
// then
// throws exception
}
Also used : RestClientException(com.hazelcast.spi.exception.RestClientException) Test(org.junit.Test)

Example 2 with RestClientException

use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.

the class GcpClientTest method getAddressesUnauthorized.

@Test
public void getAddressesUnauthorized() {
    // given
    Label label = null;
    String forbiddenMessage = "\"reason\":\"Request had insufficient authentication scopes\"";
    RestClientException exception = new RestClientException(forbiddenMessage, 401);
    given(gcpComputeApi.instances(CURRENT_PROJECT, CURRENT_ZONE, label, ACCESS_TOKEN)).willThrow(exception);
    GcpConfig gcpConfig = GcpConfig.builder().setLabel(label).build();
    GcpClient gcpClient = new GcpClient(gcpMetadataApi, gcpComputeApi, gcpAuthenticator, gcpConfig);
    // when
    List<GcpAddress> result = gcpClient.getAddresses();
    // then
    assertEquals(emptyList(), result);
}
Also used : RestClientException(com.hazelcast.spi.exception.RestClientException) Test(org.junit.Test)

Example 3 with RestClientException

use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.

the class GcpClientTest method getAddressesForbidden.

@Test
public void getAddressesForbidden() {
    // given
    Label label = null;
    String forbiddenMessage = "\"reason\":\"Request had insufficient authentication scopes\"";
    RestClientException exception = new RestClientException(forbiddenMessage, 403);
    given(gcpComputeApi.instances(CURRENT_PROJECT, CURRENT_ZONE, label, ACCESS_TOKEN)).willThrow(exception);
    GcpConfig gcpConfig = GcpConfig.builder().setLabel(label).build();
    GcpClient gcpClient = new GcpClient(gcpMetadataApi, gcpComputeApi, gcpAuthenticator, gcpConfig);
    // when
    List<GcpAddress> result = gcpClient.getAddresses();
    // then
    assertEquals(emptyList(), result);
}
Also used : RestClientException(com.hazelcast.spi.exception.RestClientException) Test(org.junit.Test)

Example 4 with RestClientException

use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.

the class AwsDiscoveryStrategy method discoverNodes.

@Override
public Iterable<DiscoveryNode> discoverNodes() {
    try {
        Map<String, String> addresses = awsClient.getAddresses();
        logResult(addresses);
        List<DiscoveryNode> result = new ArrayList<>();
        for (Map.Entry<String, String> entry : addresses.entrySet()) {
            for (int port = portRange.getFromPort(); port <= portRange.getToPort(); port++) {
                Address privateAddress = new Address(entry.getKey(), port);
                Address publicAddress = new Address(entry.getValue(), port);
                result.add(new SimpleDiscoveryNode(privateAddress, publicAddress));
            }
        }
        return result;
    } catch (NoCredentialsException e) {
        if (!isKnownExceptionAlreadyLogged) {
            LOGGER.warning("No AWS credentials found! Starting standalone. To use Hazelcast AWS discovery, configure" + " properties (access-key, secret-key) or assign the required IAM Role to your EC2 instance");
            LOGGER.finest(e);
            isKnownExceptionAlreadyLogged = true;
        }
    } catch (RestClientException e) {
        if (e.getHttpErrorCode() == HTTP_FORBIDDEN) {
            if (!isKnownExceptionAlreadyLogged) {
                LOGGER.warning("AWS IAM Role Policy missing 'ec2:DescribeInstances' Action! Starting standalone.");
                isKnownExceptionAlreadyLogged = true;
            }
            LOGGER.finest(e);
        } else {
            LOGGER.warning("Cannot discover nodes. Starting standalone.", e);
        }
    } catch (Exception e) {
        LOGGER.warning("Cannot discover nodes. Starting standalone.", e);
    }
    return Collections.emptyList();
}
Also used : DiscoveryNode(com.hazelcast.spi.discovery.DiscoveryNode) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode) Address(com.hazelcast.cluster.Address) NoCredentialsException(com.hazelcast.spi.exception.NoCredentialsException) ArrayList(java.util.ArrayList) NoCredentialsException(com.hazelcast.spi.exception.NoCredentialsException) InvalidConfigurationException(com.hazelcast.config.InvalidConfigurationException) RestClientException(com.hazelcast.spi.exception.RestClientException) RestClientException(com.hazelcast.spi.exception.RestClientException) SimpleDiscoveryNode(com.hazelcast.spi.discovery.SimpleDiscoveryNode) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with RestClientException

use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.

the class RestClient method call.

private Response call(String method) {
    HttpURLConnection connection = null;
    try {
        URL urlToConnect = new URL(url);
        connection = (HttpURLConnection) urlToConnect.openConnection();
        if (connection instanceof HttpsURLConnection && caCertificate != null) {
            ((HttpsURLConnection) connection).setSSLSocketFactory(buildSslSocketFactory());
        }
        connection.setReadTimeout((int) TimeUnit.SECONDS.toMillis(readTimeoutSeconds));
        connection.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(connectTimeoutSeconds));
        connection.setRequestMethod(method);
        for (Parameter header : headers) {
            connection.setRequestProperty(header.getKey(), header.getValue());
        }
        if (body != null) {
            byte[] bodyData = body.getBytes(StandardCharsets.UTF_8);
            connection.setDoOutput(true);
            connection.setRequestProperty("charset", "utf-8");
            connection.setRequestProperty("Content-Length", Integer.toString(bodyData.length));
            try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                outputStream.write(bodyData);
                outputStream.flush();
            }
        }
        checkResponseCode(method, connection);
        return new Response(connection.getResponseCode(), read(connection));
    } catch (IOException e) {
        throw new RestClientException("Failure in executing REST call", e);
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) DataOutputStream(java.io.DataOutputStream) RestClientException(com.hazelcast.spi.exception.RestClientException) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

RestClientException (com.hazelcast.spi.exception.RestClientException)10 Test (org.junit.Test)7 IOException (java.io.IOException)2 Address (com.hazelcast.cluster.Address)1 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)1 DiscoveryNode (com.hazelcast.spi.discovery.DiscoveryNode)1 SimpleDiscoveryNode (com.hazelcast.spi.discovery.SimpleDiscoveryNode)1 NoCredentialsException (com.hazelcast.spi.exception.NoCredentialsException)1 DataOutputStream (java.io.DataOutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 KeyStore (java.security.KeyStore)1 Certificate (java.security.cert.Certificate)1 CertificateException (java.security.cert.CertificateException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)1