Search in sources :

Example 1 with UnauthenticatedException

use of co.cask.cdap.common.UnauthenticatedException in project cdap by caskdata.

the class StreamClient method getEvents.

/**
   * Reads events from a stream
   *
   * @param streamId ID of the stream
   * @param start Timestamp in milliseconds or now-xs format to start reading event from (inclusive)
   * @param end Timestamp in milliseconds or now-xs format for the last event to read (exclusive)
   * @param limit Maximum number of events to read
   * @param callback Callback to invoke for each stream event read. If the callback function returns {@code false}
   *                 upon invocation, it will stops the reading
   * @throws IOException If fails to read from stream
   * @throws StreamNotFoundException If the given stream does not exists
   */
public void getEvents(StreamId streamId, String start, String end, int limit, Function<? super StreamEvent, Boolean> callback) throws IOException, StreamNotFoundException, UnauthenticatedException {
    long startTime = TimeMathParser.parseTime(start, TimeUnit.MILLISECONDS);
    long endTime = TimeMathParser.parseTime(end, TimeUnit.MILLISECONDS);
    URL url = config.resolveNamespacedURLV3(streamId.getParent(), String.format("streams/%s/events?start=%d&end=%d&limit=%d", streamId.getStream(), startTime, endTime, limit));
    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    AccessToken accessToken = config.getAccessToken();
    if (accessToken != null) {
        urlConn.setRequestProperty(HttpHeaders.AUTHORIZATION, accessToken.getTokenType() + " " + accessToken.getValue());
    }
    if (urlConn instanceof HttpsURLConnection && !config.isVerifySSLCert()) {
        try {
            HttpRequests.disableCertCheck((HttpsURLConnection) urlConn);
        } catch (Exception e) {
        // TODO: Log "Got exception while disabling SSL certificate check for request.getURL()"
        }
    }
    try {
        if (urlConn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
            throw new UnauthenticatedException("Unauthorized status code received from the server.");
        }
        if (urlConn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
            throw new StreamNotFoundException(streamId);
        }
        if (urlConn.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) {
            return;
        }
        // The response is an array of stream event object
        InputStream inputStream = urlConn.getInputStream();
        JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream, Charsets.UTF_8));
        jsonReader.beginArray();
        while (jsonReader.peek() != JsonToken.END_ARRAY) {
            Boolean result = callback.apply(GSON.<StreamEvent>fromJson(jsonReader, StreamEvent.class));
            if (result == null || !result) {
                break;
            }
        }
        drain(inputStream);
    // No need to close reader, the urlConn.disconnect in finally will close all underlying streams
    } finally {
        urlConn.disconnect();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) StreamEvent(co.cask.cdap.api.flow.flowlet.StreamEvent) URL(java.net.URL) IOException(java.io.IOException) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) HttpURLConnection(java.net.HttpURLConnection) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) AccessToken(co.cask.cdap.security.authentication.client.AccessToken) StreamNotFoundException(co.cask.cdap.common.StreamNotFoundException) JsonReader(com.google.gson.stream.JsonReader) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 2 with UnauthenticatedException

use of co.cask.cdap.common.UnauthenticatedException in project cdap by caskdata.

the class RESTClient method execute.

private HttpResponse execute(HttpRequest request, int... allowedErrorCodes) throws IOException, UnauthenticatedException, DisconnectedException, UnauthorizedException {
    int currentTry = 0;
    HttpResponse response;
    int responseCode;
    boolean allowUnavailable = ArrayUtils.contains(allowedErrorCodes, HttpURLConnection.HTTP_UNAVAILABLE);
    do {
        onRequest(request, currentTry);
        response = HttpRequests.execute(request, clientConfig.getDefaultRequestConfig());
        responseCode = response.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_UNAVAILABLE || allowUnavailable) {
            // only retry if unavailable
            break;
        }
        currentTry++;
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            break;
        }
    } while (currentTry <= clientConfig.getUnavailableRetryLimit());
    onResponse(request, response, currentTry);
    if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
        throw new UnauthenticatedException("Unauthorized status code received from the server.");
    }
    if (responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
        throw new UnauthorizedException(response.getResponseBodyAsString());
    }
    if (!isSuccessful(responseCode) && !ArrayUtils.contains(allowedErrorCodes, responseCode)) {
        throw new IOException(responseCode + ": " + response.getResponseBodyAsString());
    }
    return response;
}
Also used : UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) HttpResponse(co.cask.common.http.HttpResponse) IOException(java.io.IOException)

Example 3 with UnauthenticatedException

use of co.cask.cdap.common.UnauthenticatedException in project cdap by caskdata.

the class RESTClient method upload.

public HttpResponse upload(HttpRequest request, AccessToken accessToken, int... allowedErrorCodes) throws IOException, UnauthenticatedException, DisconnectedException {
    HttpResponse response = HttpRequests.execute(HttpRequest.builder(request).addHeaders(getAuthHeaders(accessToken)).build(), clientConfig.getUploadRequestConfig());
    int responseCode = response.getResponseCode();
    if (!isSuccessful(responseCode) && !ArrayUtils.contains(allowedErrorCodes, responseCode)) {
        if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
            throw new UnauthenticatedException("Unauthorized status code received from the server.");
        }
        throw new IOException(response.getResponseBodyAsString());
    }
    return response;
}
Also used : UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) HttpResponse(co.cask.common.http.HttpResponse) IOException(java.io.IOException)

Example 4 with UnauthenticatedException

use of co.cask.cdap.common.UnauthenticatedException in project cdap by caskdata.

the class IntegrationTestBase method checkSystemServices.

protected void checkSystemServices() throws TimeoutException, InterruptedException {
    Callable<Boolean> cdapAvailable = new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            // first wait for all system services to be 'OK'
            if (!getMonitorClient().allSystemServicesOk()) {
                return false;
            }
            // For non-default namespaces, simply check that the dataset service is up with list().
            // If list() does not throw exception, which means the http request receives response
            // status HTTP_OK and dataset service is up, then check if default namespace exists, if so return true.
            List<NamespaceMeta> list = getNamespaceClient().list();
            if (!configuredNamespace.equals(NamespaceId.DEFAULT)) {
                return true;
            }
            // default namespace exists before integration test starts
            for (NamespaceMeta namespaceMeta : list) {
                if (namespaceMeta.getNamespaceId().equals(NamespaceId.DEFAULT)) {
                    return true;
                }
            }
            return false;
        }
    };
    String errorMessage = String.format("CDAP Services are not available. Retried for %s seconds.", SERVICE_CHECK_TIMEOUT_SECONDS);
    try {
        checkServicesWithRetry(cdapAvailable, errorMessage);
    } catch (Throwable e) {
        Throwable rootCause = Throwables.getRootCause(e);
        if (rootCause instanceof UnauthenticatedException) {
            // security is enabled, we need to get access token before checking system services
            try {
                accessToken = fetchAccessToken();
            } catch (IOException ex) {
                throw Throwables.propagate(ex);
            }
            checkServicesWithRetry(cdapAvailable, errorMessage);
        } else {
            throw Throwables.propagate(rootCause);
        }
    }
    LOG.info("CDAP Services are up and running!");
}
Also used : UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) IOException(java.io.IOException) Callable(java.util.concurrent.Callable)

Example 5 with UnauthenticatedException

use of co.cask.cdap.common.UnauthenticatedException in project cdap by caskdata.

the class ExploreDriver method connect.

@Override
public Connection connect(String url, Properties info) throws SQLException {
    if (!acceptsURL(url)) {
        return null;
    }
    ExploreConnectionParams params = ExploreConnectionParams.parseConnectionUrl(url);
    String authToken = getString(params, ExploreConnectionParams.Info.EXPLORE_AUTH_TOKEN, null);
    String namespace = getString(params, ExploreConnectionParams.Info.NAMESPACE, NamespaceId.DEFAULT.getNamespace());
    boolean sslEnabled = getBoolean(params, ExploreConnectionParams.Info.SSL_ENABLED, false);
    boolean verifySSLCert = getBoolean(params, ExploreConnectionParams.Info.VERIFY_SSL_CERT, true);
    ExploreClient exploreClient = new FixedAddressExploreClient(params.getHost(), params.getPort(), authToken, sslEnabled, verifySSLCert);
    try {
        exploreClient.ping();
    } catch (UnauthenticatedException e) {
        throw new SQLException("Cannot connect to " + url + ", not authenticated.");
    } catch (ServiceUnavailableException | ExploreException e) {
        throw new SQLException("Cannot connect to " + url + ", service not available.");
    }
    return new ExploreConnection(exploreClient, namespace, params);
}
Also used : ExploreClient(co.cask.cdap.explore.client.ExploreClient) FixedAddressExploreClient(co.cask.cdap.explore.client.FixedAddressExploreClient) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) SQLException(java.sql.SQLException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) FixedAddressExploreClient(co.cask.cdap.explore.client.FixedAddressExploreClient) ExploreException(co.cask.cdap.explore.service.ExploreException)

Aggregations

UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)5 IOException (java.io.IOException)4 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)2 HttpResponse (co.cask.common.http.HttpResponse)2 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)1 BadRequestException (co.cask.cdap.common.BadRequestException)1 ServiceUnavailableException (co.cask.cdap.common.ServiceUnavailableException)1 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)1 ExploreClient (co.cask.cdap.explore.client.ExploreClient)1 FixedAddressExploreClient (co.cask.cdap.explore.client.FixedAddressExploreClient)1 ExploreException (co.cask.cdap.explore.service.ExploreException)1 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)1 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)1 JsonReader (com.google.gson.stream.JsonReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 SQLException (java.sql.SQLException)1 Callable (java.util.concurrent.Callable)1