Search in sources :

Example 26 with HttpResponder

use of io.cdap.http.HttpResponder in project cdap by caskdata.

the class MonitorHandlerAuthorizationTest method testGetBootStatusAuthorization.

@Test
public void testGetBootStatusAuthorization() throws Exception {
    InstanceId instanceId = InstanceId.SELF;
    MonitorHandler handler = createMonitorHandler(Authorizable.fromEntityId(instanceId, EntityType.SYSTEM_SERVICE), Arrays.asList(StandardPermission.LIST));
    HttpRequest request = mock(HttpRequest.class);
    HttpResponder responder = mock(HttpResponder.class);
    AuthenticationTestContext.actAsPrincipal(UNPRIVILEGED_PRINCIPAL);
    try {
        handler.getBootStatus(request, responder);
    } catch (UnauthorizedException e) {
    // expected
    }
    AuthenticationTestContext.actAsPrincipal(MASTER_PRINCIPAL);
    handler.getBootStatus(request, responder);
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpResponder(io.cdap.http.HttpResponder) InstanceId(io.cdap.cdap.proto.id.InstanceId) MonitorHandler(io.cdap.cdap.gateway.handlers.MonitorHandler) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) Test(org.junit.Test)

Example 27 with HttpResponder

use of io.cdap.http.HttpResponder in project cdap by caskdata.

the class StoreHandler method publish.

@POST
@Path("/publish")
public BodyConsumer publish(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    return new SpillableBodyConsumer(Files.createTempFile(tempDir, "tms.publish", ".tmp"), bufferSize) {

        @Override
        protected void processInput(InputStream inputStream, HttpResponder responder) throws Exception {
            StoreRequest storeRequest = createStoreRequest(topicId, request, inputStream);
            // Empty payload is only allowed for transactional publish
            if (!storeRequest.isTransactional() && !storeRequest.hasPayload()) {
                throw new BadRequestException("Empty payload is only allowed for publishing transactional message. Topic: " + topicId);
            }
            // Publish the message and response with the rollback information
            RollbackDetail rollbackInfo = messagingService.publish(storeRequest);
            if (rollbackInfo == null) {
                // Non-tx publish doesn't have rollback info.
                responder.sendStatus(HttpResponseStatus.OK);
            } else {
                ByteBuf response = encodeRollbackDetail(rollbackInfo);
                responder.sendContent(HttpResponseStatus.OK, response, new DefaultHttpHeaders().set(HttpHeaderNames.CONTENT_TYPE, "avro/binary"));
            }
        }
    };
}
Also used : HttpResponder(io.cdap.http.HttpResponder) RollbackDetail(io.cdap.cdap.messaging.RollbackDetail) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) StoreRequest(io.cdap.cdap.messaging.StoreRequest) BadRequestException(io.cdap.cdap.common.BadRequestException) TopicId(io.cdap.cdap.proto.id.TopicId) SpillableBodyConsumer(io.cdap.cdap.common.http.SpillableBodyConsumer) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ByteBuf(io.netty.buffer.ByteBuf) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 28 with HttpResponder

use of io.cdap.http.HttpResponder in project cdap by caskdata.

the class StoreHandler method store.

@POST
@Path("/store")
public BodyConsumer store(HttpRequest request, HttpResponder responder, @PathParam("namespace") String namespace, @PathParam("topic") String topic) throws Exception {
    TopicId topicId = new NamespaceId(namespace).topic(topic);
    return new SpillableBodyConsumer(Files.createTempFile(tempDir, "tms.store", ".tmp"), bufferSize) {

        @Override
        protected void processInput(InputStream inputStream, HttpResponder responder) throws Exception {
            StoreRequest storeRequest = createStoreRequest(topicId, request, inputStream);
            // It must be transactional with payload for store request
            if (!storeRequest.isTransactional() || !storeRequest.hasPayload()) {
                throw new BadRequestException("Store request must be transactional with payload. Topic: " + topicId);
            }
            messagingService.storePayload(storeRequest);
            responder.sendStatus(HttpResponseStatus.OK);
        }
    };
}
Also used : HttpResponder(io.cdap.http.HttpResponder) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) StoreRequest(io.cdap.cdap.messaging.StoreRequest) BadRequestException(io.cdap.cdap.common.BadRequestException) TopicId(io.cdap.cdap.proto.id.TopicId) SpillableBodyConsumer(io.cdap.cdap.common.http.SpillableBodyConsumer) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

HttpResponder (io.cdap.http.HttpResponder)28 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)19 BadRequestException (io.cdap.cdap.common.BadRequestException)14 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)14 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 IOException (java.io.IOException)13 Path (javax.ws.rs.Path)13 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)12 HttpRequest (io.netty.handler.codec.http.HttpRequest)12 POST (javax.ws.rs.POST)12 Test (org.junit.Test)12 Gson (com.google.gson.Gson)11 JsonObject (com.google.gson.JsonObject)10 JsonSyntaxException (com.google.gson.JsonSyntaxException)10 ConflictException (io.cdap.cdap.common.ConflictException)10 NotFoundException (io.cdap.cdap.common.NotFoundException)10 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)10 NotImplementedException (io.cdap.cdap.common.NotImplementedException)9 MonitorHandler (io.cdap.cdap.gateway.handlers.MonitorHandler)9 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)9