Search in sources :

Example 1 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class ProgramClient method getWorkflowCurrent.

/**
   * Get the current run information for the Workflow based on the runid
   *
   * @param workflowId ID of the workflow
   * @param runId ID of the run for which the details are to be returned
   * @return list of {@link WorkflowActionNode} currently running for the given runid
   * @throws IOException if a network error occurred
   * @throws NotFoundException if the application, workflow, or runid could not be found
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public List<WorkflowActionNode> getWorkflowCurrent(WorkflowId workflowId, String runId) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
    String path = String.format("/apps/%s/workflows/%s/runs/%s/current", workflowId.getApplication(), workflowId.getProgram(), runId);
    URL url = config.resolveNamespacedURLV3(workflowId.getNamespaceId(), path);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(workflowId.run(runId));
    }
    ObjectResponse<List<WorkflowActionNode>> objectResponse = ObjectResponse.fromJsonBody(response, new TypeToken<List<WorkflowActionNode>>() {
    }.getType(), GSON);
    return objectResponse.getResponseObject();
}
Also used : TypeToken(com.google.common.reflect.TypeToken) WorkflowActionNode(co.cask.cdap.api.workflow.WorkflowActionNode) HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) List(java.util.List) URL(java.net.URL)

Example 2 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 3 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(NamespaceId 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.code());
    if (HttpResponseStatus.BAD_REQUEST.code() == 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 4 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class AbstractMetadataClient method doGetMetadata.

private Set<MetadataRecord> doGetMetadata(Id.NamespacedId namespacedId, String entityPath, @Nullable MetadataScope scope) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
    String path = String.format("%s/metadata", entityPath);
    path = scope == null ? path : String.format("%s?scope=%s", path, scope);
    HttpResponse response = makeRequest(namespacedId, path, HttpMethod.GET);
    return GSON.fromJson(response.getResponseBodyAsString(), SET_METADATA_RECORD_TYPE);
}
Also used : HttpResponse(co.cask.common.http.HttpResponse)

Example 5 with HttpResponse

use of co.cask.common.http.HttpResponse in project cdap by caskdata.

the class AbstractMetadataClient method getProperties.

private Map<String, String> getProperties(Id.NamespacedId namespacedId, String entityPath, @Nullable MetadataScope scope) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
    String path = String.format("%s/metadata/properties", entityPath);
    path = scope == null ? path : String.format("%s?scope=%s", path, scope);
    HttpResponse response = makeRequest(namespacedId, path, HttpMethod.GET);
    return GSON.fromJson(response.getResponseBodyAsString(), MAP_STRING_STRING_TYPE);
}
Also used : HttpResponse(co.cask.common.http.HttpResponse)

Aggregations

HttpResponse (co.cask.common.http.HttpResponse)216 URL (java.net.URL)147 HttpRequest (co.cask.common.http.HttpRequest)80 NotFoundException (co.cask.cdap.common.NotFoundException)42 TypeToken (com.google.common.reflect.TypeToken)26 Test (org.junit.Test)26 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)24 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)21 BadRequestException (co.cask.cdap.common.BadRequestException)20 IOException (java.io.IOException)16 ExploreException (co.cask.cdap.explore.service.ExploreException)13 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 HashMap (java.util.HashMap)7 List (java.util.List)7 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)6 TypeToken (com.google.gson.reflect.TypeToken)6 TopicNotFoundException (co.cask.cdap.api.messaging.TopicNotFoundException)5