use of it.unibo.arces.wot.sepa.commons.response.JWTResponse in project SEPA by arces-wot.
the class DefaultAuthenticationService method requestToken.
public Response requestToken(String authorization, int timeout) {
logger.log(Level.getLevel("oauth"), "TOKEN_REQUEST: " + authorization);
CloseableHttpResponse response = null;
long start = Timings.getTime();
try {
URI uri = new URI(oauthProperties.getTokenRequestUrl());
HttpPost httpRequest = new HttpPost(uri);
httpRequest.setHeader("Content-Type", "application/json");
httpRequest.setHeader("Accept", "application/json");
httpRequest.setHeader("Authorization", authorization);
// Set timeout
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build();
httpRequest.setConfig(requestConfig);
try {
response = httpClient.execute(httpRequest);
// break;
} catch (IOException e) {
logger.error("HTTP EXECUTE: " + e.getMessage());
return new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "HttpExecute", e.getMessage());
}
logger.log(Level.getLevel("oauth"), "Response: " + response);
HttpEntity entity = response.getEntity();
String jsonResponse = EntityUtils.toString(entity, Charset.forName("UTF-8"));
EntityUtils.consume(entity);
// Parse response
JsonObject json = new JsonParser().parse(jsonResponse).getAsJsonObject();
if (json.has("error")) {
Timings.log("TOKEN_REQUEST", start, Timings.getTime());
ErrorResponse error = new ErrorResponse(json.get("status_code").getAsInt(), json.get("error").getAsString(), json.get("error_description").getAsString());
return error;
}
return new JWTResponse(json);
} catch (Exception e) {
logger.error(e.getMessage());
Timings.log("TOKEN_REQUEST", start, Timings.getTime());
return new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Exception", e.getMessage());
} finally {
try {
if (response != null)
response.close();
} catch (IOException e) {
logger.error(e.getMessage());
Timings.log("TOKEN_REQUEST", start, Timings.getTime());
return new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, "IOException", e.getMessage());
}
}
}
Aggregations