Search in sources :

Example 86 with HttpResponse

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

the class ProgramClient method stop.

/**
   * Stops a batch of programs in the same call.
   *
   * @param namespace the namespace of the programs
   * @param programs the programs to stop
   * @return the result of stopping each program
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public List<BatchProgramResult> stop(NamespaceId namespace, List<BatchProgram> programs) throws IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespace, "stop");
    HttpRequest request = HttpRequest.builder(HttpMethod.POST, url).withBody(GSON.toJson(programs), Charsets.UTF_8).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken());
    return ObjectResponse.<List<BatchProgramResult>>fromJsonBody(response, BATCH_RESULTS_TYPE, GSON).getResponseObject();
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) BatchProgramResult(co.cask.cdap.proto.BatchProgramResult) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Example 87 with HttpResponse

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

the class ScheduleClient method getStatus.

public String getStatus(ScheduleId scheduleId) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    String path = String.format("apps/%s/versions/%s/schedules/%s/status", scheduleId.getApplication(), scheduleId.getVersion(), scheduleId.getSchedule());
    URL url = config.resolveNamespacedURLV3(scheduleId.getParent().getParent(), path);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (HttpURLConnection.HTTP_NOT_FOUND == response.getResponseCode()) {
        throw new NotFoundException(scheduleId);
    }
    Map<String, String> responseObject = ObjectResponse.<Map<String, String>>fromJsonBody(response, MAP_STRING_STRING_TYPE, GSON).getResponseObject();
    return responseObject.get("status");
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) NotFoundException(co.cask.cdap.common.NotFoundException) URL(java.net.URL)

Example 88 with HttpResponse

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

the class ScheduleClient method resume.

public void resume(ScheduleId scheduleId) throws IOException, UnauthenticatedException, NotFoundException, UnauthorizedException {
    String path = String.format("apps/%s/versions/%s/schedules/%s/resume", scheduleId.getApplication(), scheduleId.getVersion(), scheduleId.getSchedule());
    URL url = config.resolveNamespacedURLV3(scheduleId.toId().getNamespace(), path);
    HttpResponse response = restClient.execute(HttpMethod.POST, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (HttpURLConnection.HTTP_NOT_FOUND == response.getResponseCode()) {
        throw new NotFoundException(scheduleId);
    }
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) NotFoundException(co.cask.cdap.common.NotFoundException) URL(java.net.URL)

Example 89 with HttpResponse

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

the class PreferencesClient method deleteProgramPreferences.

/**
   * Deletes Preferences at the Program Level.
   *
   * @param program Program Id
   * @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 deleteProgramPreferences(ProgramId program) 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.DELETE, url, 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 90 with HttpResponse

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

the class PreferencesClient method getInstancePreferences.

/**
   * Returns the Preferences stored at the Instance Level.
   *
   * @return 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
   */
public Map<String, String> getInstancePreferences() throws IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveURLV3("preferences");
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken());
    return ObjectResponse.fromJsonBody(response, new TypeToken<Map<String, String>>() {
    }).getResponseObject();
}
Also used : TypeToken(com.google.common.reflect.TypeToken) HttpResponse(co.cask.common.http.HttpResponse) 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