Search in sources :

Example 46 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project cdap by caskdata.

the class StreamViewHttpHandler method createOrUpdate.

@PUT
@Path("/streams/{stream}/views/{view}")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void createOrUpdate(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("stream") String stream, @PathParam("view") String view) throws Exception {
    StreamViewId viewId;
    try {
        viewId = new StreamViewId(namespace, stream, view);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(e);
    }
    try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
        ViewSpecification spec = GSON.fromJson(reader, ViewSpecification.class);
        if (spec == null) {
            throw new BadRequestException("Missing ViewSpecification in request body");
        }
        boolean created = admin.createOrUpdateView(viewId, spec);
        responder.sendStatus(created ? HttpResponseStatus.CREATED : HttpResponseStatus.OK);
    } catch (JsonSyntaxException e) {
        responder.sendString(HttpResponseStatus.BAD_REQUEST, "Couldn't decode body as view config JSON");
    } catch (IOException e) {
        LOG.warn("Error closing InputStreamReader", e);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ViewSpecification(co.cask.cdap.proto.ViewSpecification) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) StreamViewId(co.cask.cdap.proto.id.StreamViewId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 47 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project cdap by caskdata.

the class ExploreExecutorHttpHandler method enableStream.

@POST
@Path("streams/{stream}/tables/{table}/enable")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void enableStream(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @PathParam("stream") String streamName, @PathParam("table") final String tableName) throws Exception {
    final StreamId streamId = new StreamId(namespace, streamName);
    try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()))) {
        final FormatSpecification format = GSON.fromJson(reader, FormatSpecification.class);
        if (format == null) {
            throw new BadRequestException("Expected format in the body");
        }
        QueryHandle handle = impersonator.doAs(streamId, new Callable<QueryHandle>() {

            @Override
            public QueryHandle call() throws Exception {
                return exploreTableManager.enableStream(tableName, streamId, format);
            }
        });
        JsonObject json = new JsonObject();
        json.addProperty("handle", handle.getHandle());
        responder.sendJson(HttpResponseStatus.OK, json.toString());
    } catch (UnsupportedTypeException e) {
        LOG.error("Exception while generating create statement for stream {}", streamName, e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    }
}
Also used : StreamId(co.cask.cdap.proto.id.StreamId) InputStreamReader(java.io.InputStreamReader) FormatSpecification(co.cask.cdap.api.data.format.FormatSpecification) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BadRequestException(co.cask.cdap.common.BadRequestException) JsonObject(com.google.gson.JsonObject) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) QueryHandle(co.cask.cdap.proto.QueryHandle) BadRequestException(co.cask.cdap.common.BadRequestException) ExploreException(co.cask.cdap.explore.service.ExploreException) SQLException(java.sql.SQLException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) JsonSyntaxException(com.google.gson.JsonSyntaxException) UnsupportedTypeException(co.cask.cdap.api.data.schema.UnsupportedTypeException) IOException(java.io.IOException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 48 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project cdap by caskdata.

the class StoreHandler method rollback.

@POST
@Path("/rollback")
public void rollback(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ByteBufInputStream(request.content()), null);
    DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.PublishResponse.SCHEMA);
    messagingService.rollback(topicId, new GenericRecordRollbackDetail(datumReader.read(null, decoder)));
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Decoder(org.apache.avro.io.Decoder) GenericRecord(org.apache.avro.generic.GenericRecord) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 49 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project cdap by caskdata.

the class FetchHandler method poll.

@POST
@Path("poll")
public void poll(FullHttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    // Currently only support avro
    if (!"avro/binary".equals(request.headers().get(HttpHeaderNames.CONTENT_TYPE))) {
        throw new BadRequestException("Only avro/binary content type is supported.");
    }
    // Decode the poll request
    Decoder decoder = DecoderFactory.get().directBinaryDecoder(new ByteBufInputStream(request.content()), null);
    DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(Schemas.V1.ConsumeRequest.SCHEMA);
    // Fetch the messages
    CloseableIterator<RawMessage> iterator = fetchMessages(datumReader.read(null, decoder), topicId);
    try {
        responder.sendContent(HttpResponseStatus.OK, new MessagesBodyProducer(iterator, messageChunkSize), new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary"));
    } catch (Throwable t) {
        iterator.close();
        throw t;
    }
}
Also used : GenericDatumReader(org.apache.avro.generic.GenericDatumReader) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) Decoder(org.apache.avro.io.Decoder) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) BadRequestException(co.cask.cdap.common.BadRequestException) TopicId(co.cask.cdap.proto.id.TopicId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) GenericRecord(org.apache.avro.generic.GenericRecord) RawMessage(co.cask.cdap.messaging.data.RawMessage) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 50 with ByteBufInputStream

use of org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream in project cdap by caskdata.

the class AppLifecycleHttpHandler method updateApp.

/**
 * Updates an existing application.
 */
@POST
@Path("/apps/{app-id}/update")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void updateApp(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") final String namespaceId, @PathParam("app-id") final String appName) throws NotFoundException, BadRequestException, UnauthorizedException, IOException {
    ApplicationId appId = validateApplicationId(namespaceId, appName);
    AppRequest appRequest;
    try (Reader reader = new InputStreamReader(new ByteBufInputStream(request.content()), StandardCharsets.UTF_8)) {
        appRequest = DECODE_GSON.fromJson(reader, AppRequest.class);
    } catch (IOException e) {
        LOG.error("Error reading request to update app {} in namespace {}.", appName, namespaceId, e);
        throw new IOException("Error reading request body.");
    } catch (JsonSyntaxException e) {
        throw new BadRequestException("Request body is invalid json: " + e.getMessage());
    }
    try {
        applicationLifecycleService.updateApp(appId, appRequest, createProgramTerminator());
        responder.sendString(HttpResponseStatus.OK, "Update complete.");
    } catch (InvalidArtifactException e) {
        throw new BadRequestException(e.getMessage());
    } catch (ConflictException e) {
        responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage());
    } catch (NotFoundException | UnauthorizedException e) {
        throw e;
    } catch (Exception e) {
        // this is the same behavior as deploy app pipeline, but this is bad behavior. Error handling needs improvement.
        LOG.error("Deploy failure", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) ConflictException(co.cask.cdap.common.ConflictException) WriteConflictException(co.cask.cdap.internal.app.runtime.artifact.WriteConflictException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileReader(java.io.FileReader) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) ArtifactAlreadyExistsException(co.cask.cdap.common.ArtifactAlreadyExistsException) ConflictException(co.cask.cdap.common.ConflictException) BadRequestException(co.cask.cdap.common.BadRequestException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) WriteConflictException(co.cask.cdap.internal.app.runtime.artifact.WriteConflictException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) ExecutionException(java.util.concurrent.ExecutionException) NotFoundException(co.cask.cdap.common.NotFoundException) AppRequest(co.cask.cdap.proto.artifact.AppRequest) JsonSyntaxException(com.google.gson.JsonSyntaxException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) BadRequestException(co.cask.cdap.common.BadRequestException) ApplicationId(co.cask.cdap.proto.id.ApplicationId) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)69 ByteBuf (io.netty.buffer.ByteBuf)22 IOException (java.io.IOException)22 InputStreamReader (java.io.InputStreamReader)18 BadRequestException (co.cask.cdap.common.BadRequestException)16 Reader (java.io.Reader)16 JsonSyntaxException (com.google.gson.JsonSyntaxException)11 InputStream (java.io.InputStream)10 Path (javax.ws.rs.Path)9 ObjectInputStream (java.io.ObjectInputStream)8 NamespaceId (co.cask.cdap.proto.id.NamespaceId)6 POST (javax.ws.rs.POST)6 Test (org.junit.jupiter.api.Test)5 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)4 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)4 RpcException (org.apache.drill.exec.rpc.RpcException)4 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)3 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)3 ObjectOutputStream (java.io.ObjectOutputStream)3