Search in sources :

Example 56 with BadRequestException

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));
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) Reader(java.io.Reader) LogReader(io.cdap.cdap.logging.read.LogReader) InputStreamReader(java.io.InputStreamReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) BadRequestException(io.cdap.cdap.common.BadRequestException) List(java.util.List) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Map(java.util.Map) HashMap(java.util.HashMap) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 57 with BadRequestException

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());
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) LogReader(io.cdap.cdap.logging.read.LogReader) InputStreamReader(java.io.InputStreamReader) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 58 with BadRequestException

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);
}
Also used : PreviewStatus(io.cdap.cdap.app.preview.PreviewStatus) BadRequestException(io.cdap.cdap.common.BadRequestException)

Example 59 with BadRequestException

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);
    }
}
Also used : ProtocolException(java.net.ProtocolException) Discoverable(org.apache.twill.discovery.Discoverable) MalformedURLException(java.net.MalformedURLException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) URI(java.net.URI) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) BadRequestException(io.cdap.cdap.common.BadRequestException) HttpsEnabler(io.cdap.cdap.common.security.HttpsEnabler) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Map(java.util.Map) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 60 with BadRequestException

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;
}
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)188 Path (javax.ws.rs.Path)68 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)54 IOException (java.io.IOException)46 JsonSyntaxException (com.google.gson.JsonSyntaxException)44 NotFoundException (io.cdap.cdap.common.NotFoundException)42 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)42 POST (javax.ws.rs.POST)42 HttpResponse (io.cdap.common.http.HttpResponse)36 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)34 URL (java.net.URL)34 ProgramType (io.cdap.cdap.proto.ProgramType)30 InputStreamReader (java.io.InputStreamReader)28 Reader (java.io.Reader)28 ArrayList (java.util.ArrayList)28 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)26 ProgramId (io.cdap.cdap.proto.id.ProgramId)26 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)24 GET (javax.ws.rs.GET)24 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)22