use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.
the class PreviewHttpHandler method getTracersData.
@POST
@Path("/previews/{preview-id}/tracers")
public void getTracersData(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("preview-id") String previewId) throws Exception {
ApplicationId application = validateAndGetAppId(namespaceId, previewId);
Map<String, List<String>> previewRequest;
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.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.getData(application, tracerName));
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result));
}
use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.
the class PreviewHttpHandler method start.
@POST
@Path("/previews")
public void start(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
NamespaceId namespace = new NamespaceId(namespaceId);
try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
AppRequest<?> appRequest = GSON.fromJson(reader, AppRequest.class);
responder.sendString(HttpResponseStatus.OK, GSON.toJson(previewManager.start(namespace, appRequest)));
} catch (JsonSyntaxException e) {
throw new BadRequestException("Request body is invalid json: " + e.getMessage());
} catch (IllegalArgumentException e) {
throw new BadRequestException(e.getMessage());
}
}
use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.
the class DefaultPreviewManager method stopPreview.
@Override
public void stopPreview(@Name("applicationId") ApplicationId applicationId) throws Exception {
accessEnforcer.enforce(applicationId, authenticationContext.getPrincipal(), ApplicationPermission.PREVIEW);
PreviewStatus status = getStatus(applicationId);
if (status.getStatus().isEndState()) {
throw new BadRequestException(String.format("Preview run cannot be stopped. It is already in %s state.", status.getStatus().name()));
}
if (status.getStatus() == PreviewStatus.Status.WAITING) {
previewStore.setPreviewStatus(applicationId, new PreviewStatus(PreviewStatus.Status.KILLED, status.getSubmitTime(), null, null, null));
return;
}
previewRunStopper.stop(applicationId);
}
use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.
the class RuntimeServiceRoutingHandler method openConnection.
/**
* Opens a {@link HttpURLConnection} to the given service for the given program run.
*
* @throws BadRequestException if the request for service routing is not valid
*/
private HttpURLConnection openConnection(HttpRequest request, String namespace, String app, String version, String programType, String program, String run, String service) throws BadRequestException {
ApplicationId appId = new NamespaceId(namespace).app(app, version);
ProgramRunId programRunId = new ProgramRunId(appId, ProgramType.valueOfCategoryName(programType, BadRequestException::new), program, run);
requestValidator.getProgramRunStatus(programRunId, request);
Discoverable discoverable = endpointStrategyLoadingCache.getUnchecked(service).pick(2, TimeUnit.SECONDS);
if (discoverable == null) {
throw new ServiceUnavailableException(service);
}
String prefix = String.format("%s/runtime/namespaces/%s/apps/%s/versions/%s/%s/%s/runs/%s/services/%s", Constants.Gateway.INTERNAL_API_VERSION_3, namespace, app, version, programType, program, run, service);
URI uri = URIScheme.createURI(discoverable, request.uri().substring(prefix.length()));
try {
URL url = uri.toURL();
HttpURLConnection urlConn;
try {
urlConn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
// If fail to open the connection, treat it as service unavailable so that the client can retry
throw new ServiceUnavailableException(service);
}
if (urlConn instanceof HttpsURLConnection) {
new HttpsEnabler().setTrustAll(true).enable((HttpsURLConnection) urlConn);
}
for (Map.Entry<String, String> header : request.headers().entries()) {
urlConn.setRequestProperty(header.getKey(), header.getValue());
}
urlConn.setRequestMethod(request.method().name());
urlConn.setDoInput(true);
return urlConn;
} catch (MalformedURLException | ProtocolException e) {
// This can only happen if the incoming request is bad
throw new BadRequestException("Invalid request due to " + e.getMessage(), e);
}
}
use of io.cdap.cdap.common.BadRequestException in project cdap by cdapio.
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