Search in sources :

Example 61 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class AuthorizationHandler method verifyAuthRequest.

private void verifyAuthRequest(AuthorizationRequest request) throws BadRequestException, NotFoundException {
    if (request == null) {
        throw new BadRequestException("Missing request body");
    }
    EntityId entity = request.getEntity();
    entityExistenceVerifier.ensureExists(entity);
}
Also used : EntityId(co.cask.cdap.proto.id.EntityId) BadRequestException(co.cask.cdap.common.BadRequestException)

Example 62 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class LineageClient method getLineage.

private LineageRecord getLineage(NamespacedEntityId namespacedId, String path) throws IOException, UnauthenticatedException, NotFoundException, BadRequestException, UnauthorizedException {
    URL lineageURL = config.resolveNamespacedURLV3(new NamespaceId(namespacedId.getNamespace()), path);
    HttpResponse response = restClient.execute(HttpRequest.get(lineageURL).build(), config.getAccessToken(), HttpURLConnection.HTTP_BAD_REQUEST, HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(response.getResponseBodyAsString());
    }
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(response.getResponseBodyAsString());
    }
    return GSON.fromJson(response.getResponseBodyAsString(), LineageRecord.class);
}
Also used : HttpResponse(co.cask.common.http.HttpResponse) BadRequestException(co.cask.cdap.common.BadRequestException) NotFoundException(co.cask.cdap.common.NotFoundException) NamespaceId(co.cask.cdap.proto.id.NamespaceId) URL(java.net.URL)

Example 63 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class MonitorClient method setSystemServiceInstances.

/**
   * Sets the number of instances the system service is running on.
   *
   * @param serviceName name of the system service
   * @param instances number of instances the system service is running on
   * @throws IOException if a network error occurred
   * @throws NotFoundException if the system service with the specified name was not found
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public void setSystemServiceInstances(String serviceName, int instances) throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveURL(String.format("system/services/%s/instances", serviceName));
    HttpRequest request = HttpRequest.put(url).withBody(GSON.toJson(new Instances(instances))).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(new SystemServiceId(serviceName));
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(new String(response.getResponseBody()));
    }
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) Instances(co.cask.cdap.proto.Instances) SystemServiceId(co.cask.cdap.proto.id.SystemServiceId) HttpResponse(co.cask.common.http.HttpResponse) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) URL(java.net.URL)

Example 64 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class MonitorClient method getSystemServiceStatus.

/**
   * Gets the status of a system service.
   *
   * @param serviceName Name of the system service
   * @return status of the system service
   * @throws IOException if a network error occurred
   * @throws NotFoundException if the system service with the specified name could not be found
   * @throws BadRequestException if the operation was not valid for the system service
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public String getSystemServiceStatus(String serviceName) throws IOException, NotFoundException, BadRequestException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveURL(String.format("system/services/%s/status", serviceName));
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND, HttpURLConnection.HTTP_BAD_REQUEST);
    String responseBody = new String(response.getResponseBody());
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new NotFoundException(new SystemServiceId(serviceName));
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new BadRequestException(responseBody);
    }
    Map<String, String> status = GSON.fromJson(responseBody, new TypeToken<Map<String, String>>() {
    }.getType());
    return status.get("status");
}
Also used : SystemServiceId(co.cask.cdap.proto.id.SystemServiceId) TypeToken(com.google.common.reflect.TypeToken) HttpResponse(co.cask.common.http.HttpResponse) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) URL(java.net.URL)

Example 65 with BadRequestException

use of co.cask.cdap.common.BadRequestException in project cdap by caskdata.

the class PreviewHttpHandler method getTracersData.

@POST
@Path("/previews/{preview-id}/tracers")
public void getTracersData(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("preview-id") String previewId) throws Exception {
    NamespaceId namespace = new NamespaceId(namespaceId);
    ApplicationId application = namespace.app(previewId);
    Map<String, List<String>> previewRequest;
    try (Reader reader = new InputStreamReader(new ChannelBufferInputStream(request.getContent()), Charsets.UTF_8)) {
        previewRequest = GSON.fromJson(reader, STRING_LIST_MAP_TYPE);
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Request body is invalid json: " + e.getMessage());
    }
    if (previewRequest == null) {
        throw new BadRequestException("The body is not provided.");
    }
    List<String> tracerNames = previewRequest.get("tracers");
    if (tracerNames == null || tracerNames.isEmpty()) {
        throw new BadRequestException("Tracer names cannot be empty.");
    }
    Map<String, Map<String, List<JsonElement>>> result = new HashMap<>();
    for (String tracerName : tracerNames) {
        result.put(tracerName, previewManager.getRunner(application).getData(tracerName));
    }
    responder.sendString(HttpResponseStatus.OK, GSON.toJson(result));
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) LogReader(co.cask.cdap.logging.read.LogReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) BadRequestException(co.cask.cdap.common.BadRequestException) List(java.util.List) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) ApplicationId(co.cask.cdap.proto.id.ApplicationId) HashMap(java.util.HashMap) Map(java.util.Map) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

BadRequestException (co.cask.cdap.common.BadRequestException)82 Path (javax.ws.rs.Path)29 NotFoundException (co.cask.cdap.common.NotFoundException)25 HttpResponse (co.cask.common.http.HttpResponse)17 JsonSyntaxException (com.google.gson.JsonSyntaxException)17 URL (java.net.URL)17 POST (javax.ws.rs.POST)17 NamespaceId (co.cask.cdap.proto.id.NamespaceId)16 IOException (java.io.IOException)15 ChannelBufferInputStream (org.jboss.netty.buffer.ChannelBufferInputStream)13 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)12 HttpRequest (co.cask.common.http.HttpRequest)12 InputStreamReader (java.io.InputStreamReader)11 Reader (java.io.Reader)11 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)9 ApplicationId (co.cask.cdap.proto.id.ApplicationId)8 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)7 ProgramType (co.cask.cdap.proto.ProgramType)7 ProgramId (co.cask.cdap.proto.id.ProgramId)6 Test (org.junit.Test)6