Search in sources :

Example 76 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty by netty.

the class AbstractMemoryHttpDataTest method testSetContentFromStream.

/**
 * Provide content into HTTP data with input stream.
 *
 * @throws Exception In case of any exception.
 */
@Test
public void testSetContentFromStream() throws Exception {
    // definedSize=0
    TestHttpData test = new TestHttpData("test", UTF_8, 0);
    String contentStr = "foo_test";
    ByteBuf buf = Unpooled.wrappedBuffer(contentStr.getBytes(UTF_8));
    buf.markReaderIndex();
    ByteBufInputStream is = new ByteBufInputStream(buf);
    try {
        test.setContent(is);
        assertFalse(buf.isReadable());
        assertEquals(test.getString(UTF_8), contentStr);
        buf.resetReaderIndex();
        assertTrue(ByteBufUtil.equals(buf, test.getByteBuf()));
    } finally {
        is.close();
    }
    Random random = new SecureRandom();
    for (int i = 0; i < 20; i++) {
        // Generate input data bytes.
        int size = random.nextInt(Short.MAX_VALUE);
        byte[] bytes = new byte[size];
        random.nextBytes(bytes);
        // Generate parsed HTTP data block.
        TestHttpData data = new TestHttpData("name", UTF_8, 0);
        data.setContent(new ByteArrayInputStream(bytes));
        // Validate stored data.
        ByteBuf buffer = data.getByteBuf();
        assertEquals(0, buffer.readerIndex());
        assertEquals(bytes.length, buffer.writerIndex());
        assertArrayEquals(bytes, Arrays.copyOf(buffer.array(), bytes.length));
        assertArrayEquals(bytes, data.get());
    }
}
Also used : Random(java.util.Random) SecureRandom(java.security.SecureRandom) ByteArrayInputStream(java.io.ByteArrayInputStream) SecureRandom(java.security.SecureRandom) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 77 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty by netty.

the class DiskFileUploadTest method testSetContentFromInputStream.

@Test
public void testSetContentFromInputStream() throws Exception {
    String json = "{\"hello\":\"world\",\"foo\":\"bar\"}";
    DiskFileUpload f1 = new DiskFileUpload("file3", "file3", "application/json", null, null, 0);
    try {
        byte[] bytes = json.getBytes(CharsetUtil.UTF_8);
        ByteBuf buf = Unpooled.wrappedBuffer(bytes);
        InputStream is = new ByteBufInputStream(buf);
        try {
            f1.setContent(is);
            assertEquals(json, f1.getString());
            assertArrayEquals(bytes, f1.get());
            File file = f1.getFile();
            assertEquals((long) bytes.length, file.length());
            assertArrayEquals(bytes, doReadFile(file, bytes.length));
        } finally {
            buf.release();
            is.close();
        }
    } finally {
        f1.delete();
    }
}
Also used : FileInputStream(java.io.FileInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 78 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project netty by netty.

the class ObjectDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }
    ObjectInputStream ois = new CompactObjectInputStream(new ByteBufInputStream(frame, true), classResolver);
    try {
        return ois.readObject();
    } finally {
        ois.close();
    }
}
Also used : ByteBufInputStream(io.netty.buffer.ByteBufInputStream) ByteBuf(io.netty.buffer.ByteBuf) ObjectInputStream(java.io.ObjectInputStream)

Example 79 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project flink by apache.

the class AbstractHandler method respondAsLeader.

@Override
protected void respondAsLeader(ChannelHandlerContext ctx, RoutedRequest routedRequest, T gateway) {
    HttpRequest httpRequest = routedRequest.getRequest();
    if (log.isTraceEnabled()) {
        log.trace("Received request " + httpRequest.uri() + '.');
    }
    FileUploads uploadedFiles = null;
    try {
        if (!inFlightRequestTracker.registerRequest()) {
            log.debug("The handler instance for {} had already been closed.", untypedResponseMessageHeaders.getTargetRestEndpointURL());
            ctx.channel().close();
            return;
        }
        if (!(httpRequest instanceof FullHttpRequest)) {
            // The RestServerEndpoint defines a HttpObjectAggregator in the pipeline that always
            // returns
            // FullHttpRequests.
            log.error("Implementation error: Received a request that wasn't a FullHttpRequest.");
            throw new RestHandlerException("Bad request received.", HttpResponseStatus.BAD_REQUEST);
        }
        final ByteBuf msgContent = ((FullHttpRequest) httpRequest).content();
        uploadedFiles = FileUploadHandler.getMultipartFileUploads(ctx);
        if (!untypedResponseMessageHeaders.acceptsFileUploads() && !uploadedFiles.getUploadedFiles().isEmpty()) {
            throw new RestHandlerException("File uploads not allowed.", HttpResponseStatus.BAD_REQUEST);
        }
        R request;
        if (msgContent.capacity() == 0) {
            try {
                request = MAPPER.readValue("{}", untypedResponseMessageHeaders.getRequestClass());
            } catch (JsonParseException | JsonMappingException je) {
                throw new RestHandlerException("Bad request received. Request did not conform to expected format.", HttpResponseStatus.BAD_REQUEST, je);
            }
        } else {
            try {
                InputStream in = new ByteBufInputStream(msgContent);
                request = MAPPER.readValue(in, untypedResponseMessageHeaders.getRequestClass());
            } catch (JsonParseException | JsonMappingException je) {
                throw new RestHandlerException(String.format("Request did not match expected format %s.", untypedResponseMessageHeaders.getRequestClass().getSimpleName()), HttpResponseStatus.BAD_REQUEST, je);
            }
        }
        final HandlerRequest<R> handlerRequest;
        try {
            handlerRequest = HandlerRequest.resolveParametersAndCreate(request, untypedResponseMessageHeaders.getUnresolvedMessageParameters(), routedRequest.getRouteResult().pathParams(), routedRequest.getRouteResult().queryParams(), uploadedFiles.getUploadedFiles());
        } catch (HandlerRequestException hre) {
            log.error("Could not create the handler request.", hre);
            throw new RestHandlerException(String.format("Bad request, could not parse parameters: %s", hre.getMessage()), HttpResponseStatus.BAD_REQUEST, hre);
        }
        log.trace("Starting request processing.");
        CompletableFuture<Void> requestProcessingFuture = respondToRequest(ctx, httpRequest, handlerRequest, gateway);
        final FileUploads finalUploadedFiles = uploadedFiles;
        requestProcessingFuture.handle((Void ignored, Throwable throwable) -> {
            if (throwable != null) {
                return handleException(ExceptionUtils.stripCompletionException(throwable), ctx, httpRequest);
            }
            return CompletableFuture.<Void>completedFuture(null);
        }).thenCompose(Function.identity()).whenComplete((Void ignored, Throwable throwable) -> {
            if (throwable != null) {
                log.warn("An exception occurred while handling another exception.", throwable);
            }
            finalizeRequestProcessing(finalUploadedFiles);
        });
    } catch (Throwable e) {
        final FileUploads finalUploadedFiles = uploadedFiles;
        handleException(e, ctx, httpRequest).whenComplete((Void ignored, Throwable throwable) -> finalizeRequestProcessing(finalUploadedFiles));
    }
}
Also used : HttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.HttpRequest) FullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest) FullHttpRequest(org.apache.flink.shaded.netty4.io.netty.handler.codec.http.FullHttpRequest) ByteBufInputStream(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufInputStream) ByteBuf(org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf) JsonParseException(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonParseException) JsonMappingException(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JsonMappingException)

Example 80 with ByteBufInputStream

use of io.netty.buffer.ByteBufInputStream in project drill by apache.

the class ServerAuthenticationHandler method handle.

@Override
public void handle(S connection, int rpcType, ByteBuf pBody, ByteBuf dBody, ResponseSender sender) throws RpcException {
    final String remoteAddress = connection.getRemoteAddress().toString();
    // exchange involves server "challenges" and client "responses" (initiated by client)
    if (saslRequestTypeValue == rpcType) {
        final SaslMessage saslResponse;
        try {
            saslResponse = SaslMessage.PARSER.parseFrom(new ByteBufInputStream(pBody));
        } catch (final InvalidProtocolBufferException e) {
            handleAuthFailure(connection, sender, e, saslResponseType);
            return;
        }
        logger.trace("Received SASL message {} from {}", saslResponse.getStatus(), remoteAddress);
        final SaslResponseProcessor processor = RESPONSE_PROCESSORS.get(saslResponse.getStatus());
        if (processor == null) {
            logger.info("Unknown message type from client from {}. Will stop authentication.", remoteAddress);
            handleAuthFailure(connection, sender, new SaslException("Received unexpected message"), saslResponseType);
            return;
        }
        final SaslResponseContext<S, T> context = new SaslResponseContext<>(saslResponse, connection, sender, requestHandler, saslResponseType);
        try {
            processor.process(context);
        } catch (final Exception e) {
            handleAuthFailure(connection, sender, e, saslResponseType);
        }
    } else {
        // drop connection
        throw new RpcException(String.format("Request of type %d is not allowed without authentication. Client on %s must authenticate " + "before making requests. Connection dropped. [Details: %s]", rpcType, remoteAddress, connection.getEncryptionCtxtString()));
    }
}
Also used : RpcException(org.apache.drill.exec.rpc.RpcException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) SaslMessage(org.apache.drill.exec.proto.UserBitShared.SaslMessage) ByteString(com.google.protobuf.ByteString) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) SaslException(javax.security.sasl.SaslException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) RpcException(org.apache.drill.exec.rpc.RpcException) IOException(java.io.IOException) SaslException(javax.security.sasl.SaslException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException)

Aggregations

ByteBufInputStream (io.netty.buffer.ByteBufInputStream)78 IOException (java.io.IOException)28 ByteBuf (io.netty.buffer.ByteBuf)27 InputStreamReader (java.io.InputStreamReader)18 BadRequestException (co.cask.cdap.common.BadRequestException)16 Reader (java.io.Reader)16 InputStream (java.io.InputStream)15 JsonSyntaxException (com.google.gson.JsonSyntaxException)11 Path (javax.ws.rs.Path)9 ObjectInputStream (java.io.ObjectInputStream)8 DataInputStream (java.io.DataInputStream)7 POST (javax.ws.rs.POST)6 NamespaceId (co.cask.cdap.proto.id.NamespaceId)5 ByteBuffer (java.nio.ByteBuffer)5 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