use of co.cask.cdap.common.BadRequestException 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.cdap.common.BadRequestException in project cdap by caskdata.
the class AbstractMetadataClient method searchMetadata.
/**
* Searches entities in the specified namespace whose metadata matches the specified query.
*
* @param namespace the namespace to search in
* @param query the query string with which to search
* @param targets {@link EntityTypeSimpleName}s to search. If empty, all possible types will be searched
* @param sort specifies sort field and sort order. If {@code null}, the sort order is by relevance
* @param offset the index to start with in the search results. To return results from the beginning, pass {@code 0}
* @param limit the number of results to return, starting from #offset. To return all, pass {@link Integer#MAX_VALUE}
* @param numCursors the number of cursors to return in the response. A cursor identifies the first index of the
* next page for pagination purposes
* @param cursor the cursor that acts as the starting index for the requested page. This is only applicable when
* #sortInfo is not default. If offset is also specified, it is applied starting at
* the cursor. If {@code null}, the first row is used as the cursor
* @param showHidden boolean which specifies whether to display hidden entities (entity whose name start with "_")
* or not.
* @return A set of {@link MetadataSearchResultRecord} for the given query.
*/
public MetadataSearchResponse searchMetadata(Id.Namespace namespace, String query, Set<EntityTypeSimpleName> targets, @Nullable String sort, int offset, int limit, int numCursors, @Nullable String cursor, boolean showHidden) throws IOException, UnauthenticatedException, UnauthorizedException, BadRequestException {
String path = String.format("metadata/search?query=%s", query);
for (EntityTypeSimpleName t : targets) {
path += "&target=" + t;
}
if (sort != null) {
path += "&sort=" + URLEncoder.encode(sort, "UTF-8");
}
path += "&offset=" + offset;
path += "&limit=" + limit;
path += "&numCursors=" + numCursors;
if (cursor != null) {
path += "&cursor=" + cursor;
}
if (showHidden) {
path += "&showHidden=" + true;
}
URL searchURL = resolve(namespace, path);
HttpResponse response = execute(HttpRequest.get(searchURL).build(), HttpResponseStatus.BAD_REQUEST.getCode());
if (HttpResponseStatus.BAD_REQUEST.getCode() == response.getResponseCode()) {
throw new BadRequestException(response.getResponseBodyAsString());
}
return GSON.fromJson(response.getResponseBodyAsString(), MetadataSearchResponse.class);
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class AbstractNamespaceClient method create.
@Override
public void create(NamespaceMeta metadata) throws Exception {
URL url = resolve(String.format("namespaces/%s", metadata.getName()));
HttpResponse response = execute(HttpRequest.put(url).withBody(GSON.toJson(metadata)).build());
String responseBody = response.getResponseBodyAsString();
if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
if (responseBody.equals(String.format("Namespace '%s' already exists.", metadata.getName()))) {
throw new NamespaceAlreadyExistsException(metadata.getNamespaceId());
}
return;
}
if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException("Bad request: " + responseBody);
}
throw new IOException(String.format("Cannot create namespace %s. Reason: %s", metadata.getName(), responseBody));
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class AbstractNamespaceClient method updateProperties.
@Override
public void updateProperties(NamespaceId namespaceId, NamespaceMeta metadata) throws Exception {
URL url = resolve(String.format("namespaces/%s/properties", namespaceId.getNamespace()));
HttpResponse response = execute(HttpRequest.put(url).withBody(GSON.toJson(metadata)).build());
String responseBody = response.getResponseBodyAsString();
if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
return;
}
if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException("Bad request: " + responseBody);
}
throw new IOException(String.format("Cannot update namespace %s. Reason: %s", namespaceId, responseBody));
}
use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.
the class AbstractMetadataClient method makeRequest.
// makes a request and throws BadRequestException or NotFoundException, as appropriate
private HttpResponse makeRequest(Id.NamespacedId namespacedId, String path, HttpMethod httpMethod, @Nullable String body) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
URL url = resolve(namespacedId.getNamespace(), path);
HttpRequest.Builder builder = HttpRequest.builder(httpMethod, url);
if (body != null) {
builder.withBody(body);
}
HttpResponse response = execute(builder.build(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
throw new BadRequestException(response.getResponseBodyAsString());
}
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException(namespacedId.toEntityId());
}
return response;
}
Aggregations