Search in sources :

Example 71 with HttpResponse

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

the class ProgramClient method setRuntimeArgs.

/**
   * Sets the runtime args of a program.
   *
   * @param program the program
   * @param runtimeArgs args of the program
   * @throws IOException if a network error occurred
   * @throws ProgramNotFoundException if the application or program could not be found
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public void setRuntimeArgs(ProgramId program, Map<String, String> runtimeArgs) throws IOException, UnauthenticatedException, ProgramNotFoundException, UnauthorizedException {
    String path = String.format("apps/%s/versions/%s/%s/%s/runtimeargs", program.getApplication(), program.getVersion(), program.getType().getCategoryName(), program.getProgram());
    URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(runtimeArgs)).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(program);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 72 with HttpResponse

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

the class ProgramClient method getProgramLogs.

/**
   * Gets the logs of a program.
   *
   * @param program the program
   * @param start start time of the time range of desired logs
   * @param stop end time of the time range of desired logs
   * @return the logs of the program
   * @throws IOException if a network error occurred
   * @throws NotFoundException if the application or program could not be found
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public String getProgramLogs(ProgramId program, long start, long stop) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
    String path = String.format("apps/%s/%s/%s/logs?start=%d&stop=%d&escape=false", program.getApplication(), program.getType().getCategoryName(), program.getProgram(), start, stop);
    URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(program);
    }
    return new String(response.getResponseBody(), Charsets.UTF_8);
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 73 with HttpResponse

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

the class ProgramClient method setServiceInstances.

/**
   * Sets the number of instances of a service.
   *
   * @param service the service
   * @param instances number of instances for the service
   * @throws IOException if a network error occurred
   * @throws NotFoundException if the application or service could not be found
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public void setServiceInstances(ServiceId service, int instances) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(service.getNamespaceId(), String.format("apps/%s/services/%s/instances", service.getApplication(), service.getProgram()));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(service);
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) Instances(co.cask.cdap.proto.Instances) HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) URL(java.net.URL)

Example 74 with HttpResponse

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

the class ProgramClient method getWorkerInstances.

/**
   * Gets the number of instances that a worker is currently running on.
   *
   * @param worker the worker
   * @return number of instances that the worker is currently running on
   * @throws IOException if a network error occurred
   * @throws NotFoundException if the application or worker could not be found
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public int getWorkerInstances(ProgramId worker) throws IOException, NotFoundException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(worker.getNamespaceId(), String.format("apps/%s/workers/%s/instances", worker.getApplication(), worker.getProgram()));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(worker);
    }
    return ObjectResponse.fromJsonBody(response, Instances.class).getResponseObject().getInstances();
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) URL(java.net.URL)

Example 75 with HttpResponse

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

the class AuthorizationClient method doExecuteRequest.

private HttpResponse doExecuteRequest(HttpRequest request, int... additionalAllowedErrorCodes) throws IOException, UnauthenticatedException, FeatureDisabledException, UnauthorizedException {
    int[] allowedErrorCodes = new int[additionalAllowedErrorCodes.length + 2];
    System.arraycopy(additionalAllowedErrorCodes, 0, allowedErrorCodes, 0, additionalAllowedErrorCodes.length);
    allowedErrorCodes[additionalAllowedErrorCodes.length] = HttpURLConnection.HTTP_NOT_IMPLEMENTED;
    HttpResponse response = restClient.execute(request, config.getAccessToken(), allowedErrorCodes);
    if (HttpURLConnection.HTTP_NOT_IMPLEMENTED == response.getResponseCode()) {
        FeatureDisabledException.Feature feature = FeatureDisabledException.Feature.AUTHORIZATION;
        String enableConfig = Constants.Security.Authorization.ENABLED;
        if (response.getResponseBodyAsString().toLowerCase().contains("authentication")) {
            feature = FeatureDisabledException.Feature.AUTHENTICATION;
            enableConfig = Constants.Security.ENABLED;
        }
        throw new FeatureDisabledException(feature, FeatureDisabledException.CDAP_SITE, enableConfig, "true");
    }
    return response;
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) FeatureDisabledException(co.cask.cdap.common.FeatureDisabledException)

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