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;
}
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));
}
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;
}
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);
}
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;
}
Aggregations