use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class ProgramClient method setRuntimeArgs.
/**
* Sets the runtime args of a program.
*
* @param program the program
* @param runtimeArgs args of the program
* @throws IOException if a network error occurred
* @throws ProgramNotFoundException if the application or program could not be found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public void setRuntimeArgs(ProgramId program, Map<String, String> runtimeArgs) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
String path = String.format("apps/%s/versions/%s/%s/%s/runtimeargs", program.getApplication(), program.getVersion(), program.getType().getCategoryName(), program.getProgram());
URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(runtimeArgs)).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ProgramNotFoundException(program);
}
}
use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class ProgramClient method getProgramLogs.
/**
* Gets the logs of a program.
*
* @param program the program
* @param start start time of the time range of desired logs
* @param stop end time of the time range of desired logs
* @return the logs of the program
* @throws IOException if a network error occurred
* @throws NotFoundException if the application or program could not be found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public String getProgramLogs(ProgramId program, long start, long stop) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
String path = String.format("apps/%s/%s/%s/logs?start=%d&stop=%d&escape=false", program.getApplication(), program.getType().getCategoryName(), program.getProgram(), start, stop);
URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new ProgramNotFoundException(program);
}
return new String(response.getResponseBody(), Charsets.UTF_8);
}
use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class ProgramClient method setServiceInstances.
/**
* Sets the number of instances of a service.
*
* @param service the service
* @param instances number of instances for the service
* @throws IOException if a network error occurred
* @throws NotFoundException if the application or service could not be found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public void setServiceInstances(ServiceId service, int instances) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(service.getNamespaceId(), String.format("apps/%s/services/%s/instances", service.getApplication(), service.getProgram()));
HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(service);
}
}
use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class ProgramClient method getWorkerInstances.
/**
* Gets the number of instances that a worker is currently running on.
*
* @param worker the worker
* @return number of instances that the worker is currently running on
* @throws IOException if a network error occurred
* @throws NotFoundException if the application or worker could not be found
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
*/
public int getWorkerInstances(ProgramId worker) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(worker.getNamespaceId(), String.format("apps/%s/workers/%s/instances", worker.getApplication(), worker.getProgram()));
HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(worker);
}
return ObjectResponse.fromJsonBody(response, Instances.class).getResponseObject().getInstances();
}
use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class AuthorizationClient method doExecuteRequest.
private HttpResponse doExecuteRequest(HttpRequest request, int... additionalAllowedErrorCodes) throws IOException, UnauthenticatedException, FeatureDisabledException, UnauthorizedException {
int[] allowedErrorCodes = new int[additionalAllowedErrorCodes.length + 2];
System.arraycopy(additionalAllowedErrorCodes, 0, allowedErrorCodes, 0, additionalAllowedErrorCodes.length);
allowedErrorCodes[additionalAllowedErrorCodes.length] = HttpURLConnection.HTTP_NOT_IMPLEMENTED;
HttpResponse response = restClient.execute(request, config.getAccessToken(), allowedErrorCodes);
if (HttpURLConnection.HTTP_NOT_IMPLEMENTED == response.getResponseCode()) {
FeatureDisabledException.Feature feature = FeatureDisabledException.Feature.AUTHORIZATION;
String enableConfig = Constants.Security.Authorization.ENABLED;
if (response.getResponseBodyAsString().toLowerCase().contains("authentication")) {
feature = FeatureDisabledException.Feature.AUTHENTICATION;
enableConfig = Constants.Security.ENABLED;
}
throw new FeatureDisabledException(feature, FeatureDisabledException.CDAP_SITE, enableConfig, "true");
}
return response;
}
Aggregations