Search in sources :

Example 91 with HttpResponse

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);
    }
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 92 with HttpResponse

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);
}
Also used : EntityTypeSimpleName(co.cask.cdap.proto.element.EntityTypeSimpleName) HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) URL(java.net.URL)

Example 93 with HttpResponse

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()));
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) IOException(java.io.IOException) NamespaceCannotBeDeletedException(co.cask.cdap.common.NamespaceCannotBeDeletedException) URL(java.net.URL) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException)

Example 94 with HttpResponse

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()));
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) IOException(java.io.IOException) NamespaceCannotBeDeletedException(co.cask.cdap.common.NamespaceCannotBeDeletedException) URL(java.net.URL) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException)

Example 95 with HttpResponse

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));
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceAlreadyExistsException(co.cask.cdap.common.NamespaceAlreadyExistsException) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

HttpResponse (co.cask.common.http.HttpResponse)204 URL (java.net.URL)145 HttpRequest (co.cask.common.http.HttpRequest)77 NotFoundException (co.cask.cdap.common.NotFoundException)41 TypeToken (com.google.common.reflect.TypeToken)26 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)24 Test (org.junit.Test)24 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)19 BadRequestException (co.cask.cdap.common.BadRequestException)17 IOException (java.io.IOException)14 ExploreException (co.cask.cdap.explore.service.ExploreException)12 ServiceManager (co.cask.cdap.test.ServiceManager)12 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)10 ApplicationManager (co.cask.cdap.test.ApplicationManager)10 StreamNotFoundException (co.cask.cdap.common.StreamNotFoundException)8 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)6 TypeToken (com.google.gson.reflect.TypeToken)6 List (java.util.List)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5 DatasetTypeNotFoundException (co.cask.cdap.common.DatasetTypeNotFoundException)5