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