Search in sources :

Example 1 with NotRetryableClientProtocolException

use of io.hops.hopsworks.common.proxies.client.NotRetryableClientProtocolException in project hopsworks by logicalclocks.

the class KibanaClient method execute.

private JSONObject execute(HttpMethod method, KibanaType type, String id, String data, Users user, Project project, boolean overwrite, boolean runAsDataOwner) throws ElasticException {
    String url = settings.getKibanaUri() + "/api/saved_objects";
    if (type != KibanaType.All) {
        url += "/" + type.toString();
    }
    if (id != null) {
        url += "/" + id;
    }
    if (overwrite) {
        url += "?overwrite=true";
    }
    try {
        final HttpUriRequest httpRequest = buildHttpRequest(method, url, data);
        httpRequest.setHeader("kbn-xsrf", "required");
        httpRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
        // authorization
        if (settings.isElasticOpenDistroSecurityEnabled()) {
            if (settings.isElasticJWTEnabled() && project != null && (user != null || runAsDataOwner)) {
                String token = runAsDataOwner ? elasticJWTController.createTokenForELKAsDataOwner(project) : elasticJWTController.createTokenForELK(user, project);
                httpRequest.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
            } else {
                String userPass = settings.getElasticAdminUser() + ":" + settings.getElasticAdminPassword();
                httpRequest.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString(userPass.getBytes()));
            }
        }
        HttpRetryableAction<JSONObject> retryableAction = new HttpRetryableAction<JSONObject>(backOffPolicy) {

            @Override
            public JSONObject performAction() throws ClientProtocolException, IOException {
                return client.execute(httpRequest, httpResponse -> {
                    int statusCode = httpResponse.getStatusLine().getStatusCode();
                    if (statusCode / 100 == 2) {
                        String response = EntityUtils.toString(httpResponse.getEntity());
                        return Strings.isNullOrEmpty(response) ? new JSONObject() : new JSONObject(response);
                    } else if (statusCode / 100 == 4) {
                        if (statusCode == 404) {
                            throw new NotFoundClientProtocolException(httpResponse.toString());
                        } else {
                            throw new NotRetryableClientProtocolException(httpResponse.toString());
                        }
                    } else {
                        // Retry
                        throw new ClientProtocolException();
                    }
                });
            }
        };
        return retryableAction.tryAction();
    } catch (IOException e) {
        throw new ElasticException(RESTCodes.ElasticErrorCode.KIBANA_REQ_ERROR, Level.INFO, "Failed to execute a Kibana request. Reason: " + e.getMessage(), "url:" + url, e);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ElasticException(io.hops.hopsworks.exceptions.ElasticException) JSONObject(org.json.JSONObject) NotRetryableClientProtocolException(io.hops.hopsworks.common.proxies.client.NotRetryableClientProtocolException) HttpRetryableAction(io.hops.hopsworks.common.proxies.client.HttpRetryableAction) IOException(java.io.IOException) NotFoundClientProtocolException(io.hops.hopsworks.common.proxies.client.NotFoundClientProtocolException) ClientProtocolException(org.apache.http.client.ClientProtocolException) NotFoundClientProtocolException(io.hops.hopsworks.common.proxies.client.NotFoundClientProtocolException) NotRetryableClientProtocolException(io.hops.hopsworks.common.proxies.client.NotRetryableClientProtocolException)

Aggregations

HttpRetryableAction (io.hops.hopsworks.common.proxies.client.HttpRetryableAction)1 NotFoundClientProtocolException (io.hops.hopsworks.common.proxies.client.NotFoundClientProtocolException)1 NotRetryableClientProtocolException (io.hops.hopsworks.common.proxies.client.NotRetryableClientProtocolException)1 ElasticException (io.hops.hopsworks.exceptions.ElasticException)1 IOException (java.io.IOException)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 JSONObject (org.json.JSONObject)1