use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class DatasetModuleClient method add.
/**
* Adds a new dataset module.
*
* @param module the new dataset module
* @param className name of the dataset module class within the moduleJarFile
* @param moduleJarFile Jar file containing the dataset module class and dependencies
* @throws BadRequestException if the moduleJarFile does not exist
* @throws AlreadyExistsException if a dataset module with the same name already exists
* @throws IOException if a network error occurred
*/
public void add(DatasetModuleId module, String className, File moduleJarFile) throws BadRequestException, AlreadyExistsException, IOException, UnauthenticatedException {
URL url = config.resolveNamespacedURLV3(module.getParent(), String.format("data/modules/%s", module.getModule()));
Map<String, String> headers = ImmutableMap.of("X-Class-Name", className);
HttpRequest request = HttpRequest.put(url).addHeaders(headers).withBody(moduleJarFile).build();
HttpResponse response = restClient.upload(request, config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_CONFLICT);
if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(String.format("Module jar file does not exist: %s", moduleJarFile));
} else if (response.getResponseCode() == HttpURLConnection.HTTP_CONFLICT) {
throw new DatasetModuleAlreadyExistsException(module);
}
}
use of co.cask.common.http.HttpRequest 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.HttpRequest 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.HttpRequest in project cdap by caskdata.
the class AuthorizationClient method removeRoleFromPrincipal.
@Override
public void removeRoleFromPrincipal(Role role, Principal principal) throws IOException, FeatureDisabledException, UnauthenticatedException, UnauthorizedException, RoleNotFoundException, NotFoundException {
URL url = config.resolveURLV3(String.format(AUTHORIZATION_BASE + "%s/%s/roles/%s", principal.getType(), principal.getName(), role.getName()));
HttpRequest request = HttpRequest.delete(url).build();
executeExistingRolesRequest(role, request);
}
use of co.cask.common.http.HttpRequest in project cdap by caskdata.
the class DatasetClient method getProperties.
/**
* Retrieve the properties with which a dataset was created or updated.
* @param instance the dataset instance
* @return the properties as a map
*/
public Map<String, String> getProperties(DatasetId instance) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(instance.getParent(), String.format("data/datasets/%s/properties", instance.getDataset()));
HttpRequest request = HttpRequest.get(url).build();
HttpResponse response = restClient.execute(request, config.getAccessToken());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(instance);
}
return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() {
}).getResponseObject();
}
Aggregations