Search in sources :

Example 1 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.

the class DirectRuntimeRequestValidator method getProgramRunStatus.

@Override
public ProgramRunInfo getProgramRunStatus(ProgramRunId programRunId, HttpRequest request) throws BadRequestException {
    accessEnforcer.enforce(programRunId, authenticationContext.getPrincipal(), StandardPermission.GET);
    ProgramRunInfo programRunInfo;
    try {
        programRunInfo = programRunsCache.get(programRunId).orElseThrow(() -> new BadRequestException("Program run " + programRunId + " is not valid"));
    } catch (BadRequestException e) {
        throw e;
    } catch (Exception e) {
        throw new ServiceUnavailableException(Constants.Service.RUNTIME, e);
    }
    return programRunInfo;
}
Also used : BadRequestException(io.cdap.cdap.common.BadRequestException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) NotFoundException(io.cdap.cdap.common.NotFoundException)

Example 2 with BadRequestException

use of io.cdap.cdap.common.BadRequestException 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(io.cdap.common.http.HttpResponse) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceAlreadyExistsException(io.cdap.cdap.common.NamespaceAlreadyExistsException) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.

the class LineageHTTPHandler method getCollapseTypes.

private static Set<CollapseType> getCollapseTypes(@Nullable List<String> collapse) throws BadRequestException {
    if (collapse == null) {
        return Collections.emptySet();
    }
    Set<CollapseType> collapseTypes = new HashSet<>();
    for (String c : collapse) {
        try {
            CollapseType type = CollapseType.valueOf(c.toUpperCase());
            collapseTypes.add(type);
        } catch (IllegalArgumentException e) {
            throw new BadRequestException(String.format("Invalid collapse type %s", c));
        }
    }
    return collapseTypes;
}
Also used : CollapseType(io.cdap.cdap.proto.metadata.lineage.CollapseType) BadRequestException(io.cdap.cdap.common.BadRequestException) HashSet(java.util.HashSet)

Example 4 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.

the class LineageHTTPHandler method parseRange.

// TODO: CDAP-3715 This is a fairly common operation useful in various handlers
private TimeRange parseRange(String startStr, String endStr) throws BadRequestException {
    if (startStr == null) {
        throw new BadRequestException("Start time is required.");
    }
    if (endStr == null) {
        throw new BadRequestException("End time is required.");
    }
    long now = TimeMathParser.nowInSeconds();
    long start;
    long end;
    try {
        // start and end are specified in seconds from HTTP request,
        // but specified in milliseconds to the LineageGenerator
        start = TimeUnit.SECONDS.toMillis(TimeMathParser.parseTimeInSeconds(now, startStr));
        end = TimeUnit.SECONDS.toMillis(TimeMathParser.parseTimeInSeconds(now, endStr));
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
    }
    if (start < 0) {
        throw new BadRequestException(String.format("Invalid start time (%s -> %d), should be >= 0.", startStr, start));
    }
    if (end < 0) {
        throw new BadRequestException(String.format("Invalid end time (%s -> %d), should be >= 0.", endStr, end));
    }
    if (start > end) {
        throw new BadRequestException(String.format("Start time (%s -> %d) should be lesser than end time (%s -> %d).", startStr, start, endStr, end));
    }
    return new TimeRange(start, end);
}
Also used : BadRequestException(io.cdap.cdap.common.BadRequestException)

Example 5 with BadRequestException

use of io.cdap.cdap.common.BadRequestException in project cdap by caskdata.

the class MetadataHttpHandler method readProperties.

private Map<String, String> readProperties(FullHttpRequest request) throws BadRequestException {
    ByteBuf content = request.content();
    if (!content.isReadable()) {
        throw new BadRequestException("Unable to read metadata properties from the request.");
    }
    Map<String, String> metadata;
    try (Reader reader = new InputStreamReader(new ByteBufInputStream(content), StandardCharsets.UTF_8)) {
        metadata = GSON.fromJson(reader, MAP_STRING_STRING_TYPE);
    } catch (IOException e) {
        throw new BadRequestException("Unable to read metadata properties from the request.", e);
    }
    if (metadata == null) {
        throw new BadRequestException("Null metadata was read from the request");
    }
    return metadata;
}
Also used : InputStreamReader(java.io.InputStreamReader) BadRequestException(io.cdap.cdap.common.BadRequestException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

BadRequestException (io.cdap.cdap.common.BadRequestException)94 Path (javax.ws.rs.Path)34 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)27 IOException (java.io.IOException)23 JsonSyntaxException (com.google.gson.JsonSyntaxException)22 NotFoundException (io.cdap.cdap.common.NotFoundException)21 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)21 POST (javax.ws.rs.POST)21 HttpResponse (io.cdap.common.http.HttpResponse)18 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)17 URL (java.net.URL)17 ProgramType (io.cdap.cdap.proto.ProgramType)15 InputStreamReader (java.io.InputStreamReader)14 Reader (java.io.Reader)14 ArrayList (java.util.ArrayList)14 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)13 ProgramId (io.cdap.cdap.proto.id.ProgramId)13 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)12 GET (javax.ws.rs.GET)12 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)11