use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class PreferencesClient method setProgramPreferences.
/**
* Sets Preferences at the Program Level.
*
* @param program Program Id
* @param preferences map of key-value pairs
* @throws IOException if a network error occurred
* @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
* @throws ProgramNotFoundException if the requested program is not found
*/
public void setProgramPreferences(ProgramId program, Map<String, String> preferences) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), String.format("/apps/%s/%s/%s/preferences", program.getApplication(), program.getType().getCategoryName(), program.getProgram()));
HttpResponse response = restClient.execute(HttpMethod.PUT, url, GSON.toJson(preferences), null, 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 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.common.http.HttpResponse in project cdap by caskdata.
the class AbstractNamespaceClient method delete.
@Override
public void delete(NamespaceId namespaceId) throws Exception {
URL url = resolve(String.format("unrecoverable/namespaces/%s", namespaceId.getNamespace()));
HttpResponse response = execute(HttpRequest.delete(url).build());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NamespaceNotFoundException(namespaceId);
} else if (HttpURLConnection.HTTP_FORBIDDEN == response.getResponseCode()) {
throw new NamespaceCannotBeDeletedException(namespaceId, response.getResponseBodyAsString());
} else if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
return;
}
throw new IOException(String.format("Cannot delete namespace %s. Reason: %s", namespaceId, response.getResponseBodyAsString()));
}
use of co.cask.common.http.HttpResponse in project cdap by caskdata.
the class AbstractNamespaceClient method deleteDatasets.
@Override
public void deleteDatasets(NamespaceId namespaceId) throws Exception {
URL url = resolve(String.format("unrecoverable/namespaces/%s/datasets", namespaceId.getNamespace()));
HttpResponse response = execute(HttpRequest.delete(url).build());
if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NamespaceNotFoundException(namespaceId);
} else if (HttpURLConnection.HTTP_FORBIDDEN == response.getResponseCode()) {
String msg = String.format("Datasets in the namespace '%s' cannot be deleted. Reason: '%s'.", namespaceId, response.getResponseBodyAsString());
throw new NamespaceCannotBeDeletedException(namespaceId, msg);
} else if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
return;
}
throw new IOException(String.format("Cannot delete datasets in namespace %s. Reason: %s", namespaceId, response.getResponseBodyAsString()));
}
use of co.cask.common.http.HttpResponse 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));
}
Aggregations