use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class WorkflowClient method getWorkflowNodeStates.
/**
* Get node states associated with the Workflow run.
* @param workflowRunId run id for the Workflow
* @return the map of node id to the {@link WorkflowNodeStateDetail}
* @throws IOException if the error occurred during executing the http request
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
* @throws NotFoundException if the workflow with given runid not found
*/
public Map<String, WorkflowNodeStateDetail> getWorkflowNodeStates(ProgramRunId workflowRunId) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
String path = String.format("apps/%s/workflows/%s/runs/%s/nodes/state", workflowRunId.getApplication(), workflowRunId.getProgram(), workflowRunId.getRun());
NamespaceId namespaceId = workflowRunId.getNamespaceId();
URL urlPath = config.resolveNamespacedURLV3(namespaceId, path);
HttpResponse response = restClient.execute(HttpMethod.GET, urlPath, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(workflowRunId);
}
return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, WorkflowNodeStateDetail>>() {
}).getResponseObject();
}
use of co.cask.common.http.HttpResponse 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.common.http.HttpResponse 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.common.http.HttpResponse in project cdap by caskdata.
the class StreamClient method truncate.
/**
* Truncates a stream, deleting all stream events belonging to the stream.
*
* @param stream ID of the stream to truncate
* @throws IOException if a network error occurred
* @throws StreamNotFoundException if the stream with the specified name was not found
*/
public void truncate(StreamId stream) throws IOException, StreamNotFoundException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(stream.getParent(), String.format("streams/%s/truncate", stream.getStream()));
HttpResponse response = restClient.execute(HttpMethod.POST, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new StreamNotFoundException(stream);
}
}
use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class StreamClient method setStreamProperties.
/**
* Sets properties of a stream.
*
* @param stream ID of the stream
* @param properties properties to set
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the client is unauthorized
* @throws BadRequestException if the request is bad
* @throws StreamNotFoundException if the stream was not found
*/
public void setStreamProperties(StreamId stream, StreamProperties properties) throws IOException, UnauthenticatedException, BadRequestException, StreamNotFoundException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(stream.getParent(), String.format("streams/%s/properties", stream.getStream()));
HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(properties)).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException("Bad request: " + response.getResponseBodyAsString());
}
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new StreamNotFoundException(stream);
}
}
Aggregations