Search in sources :

Example 6 with RestClientException

use of org.platformlayer.rest.RestClientException in project platformlayer by platformlayer.

the class MetricClientImpl method getMetrics.

@Override
public MetricServiceData getMetrics(MetricQuery query) throws RestClientException {
    URI url = metricBaseUrl.resolve("api/metric/").resolve(project + "/");
    URIBuilder uriBuilder = new URIBuilder(url);
    if (query != null) {
        for (String filter : query.filters) {
            int firstEquals = filter.indexOf('=');
            if (firstEquals == -1) {
                uriBuilder.addParameter("has." + filter, "");
            } else {
                String key = filter.substring(0, firstEquals);
                String value = filter.substring(firstEquals + 1);
                uriBuilder.addParameter("filter." + key, value);
            }
        }
        for (String projection : query.projections) {
            int firstEquals = projection.indexOf('=');
            if (firstEquals == -1) {
                uriBuilder.addParameter("select." + projection, "");
            } else {
                throw new IllegalArgumentException();
            }
        }
        if (query.flatten) {
            uriBuilder.addParameter("flatten", "true");
        }
    }
    try {
        url = uriBuilder.build();
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("Error building URI", e);
    }
    HttpGet request = new HttpGet(url);
    HttpResponse response = null;
    try {
        response = httpClient.execute(request);
        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() != 200) {
            log.info("Error reading from metrics service: " + statusLine);
            throw new RestClientException("Error reading from metrics service", null, statusLine.getStatusCode());
        } else {
            MetricServiceData ret = new MetricServiceData(request, response);
            // Don't close yet
            response = null;
            return ret;
        }
    } catch (IOException e) {
        throw new RestClientException("Error reading from metrics service", e);
    } finally {
        if (response != null) {
            try {
                EntityUtils.consume(response.getEntity());
            } catch (IOException e) {
                log.warn("Error consuming response", e);
            }
        }
    }
}
Also used : StatusLine(org.apache.http.StatusLine) HttpGet(org.apache.http.client.methods.HttpGet) RestClientException(org.platformlayer.rest.RestClientException) HttpResponse(org.apache.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 7 with RestClientException

use of org.platformlayer.rest.RestClientException in project platformlayer by platformlayer.

the class PlatformLayerAuthenticationClient method authenticate.

public AuthenticateResponse authenticate(PasswordCredentials passwordCredentials) throws PlatformlayerAuthenticationClientException {
    Auth auth = new Auth();
    auth.setPasswordCredentials(passwordCredentials);
    AuthenticateRequest request = new AuthenticateRequest();
    request.setAuth(auth);
    AuthenticateResponse response;
    try {
        response = doSimpleXmlRequest(HttpMethod.POST, "api/tokens", request, AuthenticateResponse.class);
    } catch (RestClientException e) {
        Integer httpResponseCode = e.getHttpResponseCode();
        if (httpResponseCode != null && httpResponseCode == 401) {
            throw new PlatformlayerInvalidCredentialsException("Invalid credentials");
        }
        throw new PlatformlayerAuthenticationClientException("Error authenticating", e);
    }
    return response;
}
Also used : AuthenticateResponse(org.platformlayer.auth.v1.AuthenticateResponse) AuthenticateRequest(org.platformlayer.auth.v1.AuthenticateRequest) PlatformlayerInvalidCredentialsException(org.platformlayer.auth.PlatformlayerInvalidCredentialsException) Auth(org.platformlayer.auth.v1.Auth) RestClientException(org.platformlayer.rest.RestClientException) PlatformlayerAuthenticationClientException(org.platformlayer.auth.PlatformlayerAuthenticationClientException)

Aggregations

RestClientException (org.platformlayer.rest.RestClientException)7 PlatformlayerAuthenticationClientException (org.platformlayer.auth.PlatformlayerAuthenticationClientException)2 PlatformlayerAuthenticationToken (org.platformlayer.auth.PlatformlayerAuthenticationToken)2 Auth (org.platformlayer.auth.v1.Auth)2 AuthenticateRequest (org.platformlayer.auth.v1.AuthenticateRequest)2 AuthenticateResponse (org.platformlayer.auth.v1.AuthenticateResponse)2 ProjectValidation (org.platformlayer.auth.v1.ProjectValidation)2 UserValidation (org.platformlayer.auth.v1.UserValidation)2 ValidateAccess (org.platformlayer.auth.v1.ValidateAccess)2 ValidateTokenResponse (org.platformlayer.auth.v1.ValidateTokenResponse)2 SimpleClientCertificateKeyManager (com.fathomdb.crypto.SimpleClientCertificateKeyManager)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 X509Certificate (java.security.cert.X509Certificate)1 KeyManager (javax.net.ssl.KeyManager)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 HttpGet (org.apache.http.client.methods.HttpGet)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1