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);
}
}
}
}
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;
}
Aggregations