Search in sources :

Example 1 with RevokedJWTTokenDTO

use of org.wso2.carbon.apimgt.gateway.dto.RevokedJWTTokenDTO in project carbon-apimgt by wso2.

the class RevokedJWTTokensRetriever method retrieveRevokedJWTTokensData.

/**
 * This method will retrieve revoked JWT tokens by calling a web service.
 *
 * @return List of RevokedJWTTokensDTOs.
 */
private RevokedJWTTokenDTO[] retrieveRevokedJWTTokensData() {
    try {
        // The resource resides in the throttle web app. Hence reading throttle configs
        String url = getEventHubConfiguration().getServiceUrl().concat(APIConstants.INTERNAL_WEB_APP_EP).concat("/revokedjwt");
        HttpGet method = new HttpGet(url);
        byte[] credentials = Base64.encodeBase64((getEventHubConfiguration().getUsername() + ":" + getEventHubConfiguration().getPassword()).getBytes(StandardCharsets.UTF_8));
        method.setHeader("Authorization", "Basic " + new String(credentials, StandardCharsets.UTF_8));
        URL keyMgtURL = new URL(url);
        int keyMgtPort = keyMgtURL.getPort();
        String keyMgtProtocol = keyMgtURL.getProtocol();
        HttpClient httpClient = APIUtil.getHttpClient(keyMgtPort, keyMgtProtocol);
        HttpResponse httpResponse = null;
        int retryCount = 0;
        boolean retry;
        do {
            try {
                httpResponse = httpClient.execute(method);
                retry = false;
            } catch (IOException ex) {
                retryCount++;
                if (retryCount < revokedJWTTokensRetrievalRetries) {
                    retry = true;
                    log.warn("Failed retrieving revoked JWT token signatures from remote endpoint: " + ex.getMessage() + ". Retrying after " + revokedJWTTokensRetrievalTimeoutInSeconds + " seconds...");
                    Thread.sleep(revokedJWTTokensRetrievalTimeoutInSeconds * 1000);
                } else {
                    throw ex;
                }
            }
        } while (retry);
        String responseString = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
        if (responseString != null && !responseString.isEmpty()) {
            return new Gson().fromJson(responseString, RevokedJWTTokenDTO[].class);
        }
    } catch (IOException | InterruptedException e) {
        log.error("Exception when retrieving revoked JWT tokens from remote endpoint ", e);
    }
    return null;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) RevokedJWTTokenDTO(org.wso2.carbon.apimgt.gateway.dto.RevokedJWTTokenDTO) HttpClient(org.apache.http.client.HttpClient)

Aggregations

Gson (com.google.gson.Gson)1 IOException (java.io.IOException)1 URL (java.net.URL)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 RevokedJWTTokenDTO (org.wso2.carbon.apimgt.gateway.dto.RevokedJWTTokenDTO)1