Search in sources :

Example 1 with Decompressor

use of io.grpc.Decompressor in project grpc-java by grpc.

the class ForwardingClientStreamTest method setDecompressorRegistryTest.

@Test
public void setDecompressorRegistryTest() {
    DecompressorRegistry decompressor = DecompressorRegistry.emptyInstance().with(new Decompressor() {

        @Override
        public String getMessageEncoding() {
            return "some-encoding";
        }

        @Override
        public InputStream decompress(InputStream is) throws IOException {
            return is;
        }
    }, true);
    forward.setDecompressorRegistry(decompressor);
    verify(mock).setDecompressorRegistry(same(decompressor));
}
Also used : Decompressor(io.grpc.Decompressor) InputStream(java.io.InputStream) DecompressorRegistry(io.grpc.DecompressorRegistry) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with Decompressor

use of io.grpc.Decompressor in project vertx-grpc by eclipse-vertx.

the class VertxClientCall method start.

@Override
public void start(Listener<ResponseT> responseListener, Metadata headers) {
    listener = responseListener;
    fut = client.request(server, methodDescriptor);
    fut.onComplete(ar1 -> {
        if (ar1.succeeded()) {
            request = ar1.result();
            Utils.writeMetadata(headers, request.headers());
            if (encoding != null) {
                request.encoding(encoding);
            }
            Future<GrpcClientResponse<RequestT, ResponseT>> responseFuture = request.response();
            responseFuture.onComplete(ar2 -> {
                if (ar2.succeeded()) {
                    grpcResponse = ar2.result();
                    String respEncoding = grpcResponse.encoding();
                    Decompressor decompressor = DecompressorRegistry.getDefaultInstance().lookupDecompressor(respEncoding);
                    BridgeMessageDecoder<ResponseT> decoder = new BridgeMessageDecoder<>(methodDescriptor.getResponseMarshaller(), decompressor);
                    Metadata responseHeaders = Utils.readMetadata(grpcResponse.headers());
                    if (exec == null) {
                        responseListener.onHeaders(responseHeaders);
                    } else {
                        exec.execute(() -> {
                            responseListener.onHeaders(responseHeaders);
                        });
                    }
                    readAdapter.init(grpcResponse, decoder);
                } else {
                    Throwable err = ar2.cause();
                    if (err instanceof StreamResetException) {
                        StreamResetException reset = (StreamResetException) err;
                        switch((int) reset.getCode()) {
                            case 8:
                                doClose(Status.CANCELLED, new Metadata());
                                break;
                            default:
                                System.out.println("handle me");
                                break;
                        }
                    } else {
                        System.out.println("handle me");
                    }
                }
            });
            writeAdapter.init(request, new BridgeMessageEncoder<>(methodDescriptor.getRequestMarshaller(), compressor));
        }
    });
}
Also used : BridgeMessageDecoder(io.vertx.grpc.common.impl.BridgeMessageDecoder) Decompressor(io.grpc.Decompressor) StreamResetException(io.vertx.core.http.StreamResetException) Metadata(io.grpc.Metadata)

Example 3 with Decompressor

use of io.grpc.Decompressor in project grpc-java by grpc.

the class ClientCallImplTest method prepareHeaders_acceptedMessageEncodingsAdded.

@Test
public void prepareHeaders_acceptedMessageEncodingsAdded() {
    Metadata m = new Metadata();
    DecompressorRegistry customRegistry = DecompressorRegistry.emptyInstance().with(new Decompressor() {

        @Override
        public String getMessageEncoding() {
            return "a";
        }

        @Override
        public InputStream decompress(InputStream is) throws IOException {
            return null;
        }
    }, true).with(new Decompressor() {

        @Override
        public String getMessageEncoding() {
            return "b";
        }

        @Override
        public InputStream decompress(InputStream is) throws IOException {
            return null;
        }
    }, true).with(new Decompressor() {

        @Override
        public String getMessageEncoding() {
            return "c";
        }

        @Override
        public InputStream decompress(InputStream is) throws IOException {
            return null;
        }
    }, // not advertised
    false);
    ClientCallImpl.prepareHeaders(m, customRegistry, Codec.Identity.NONE, false);
    Iterable<String> acceptedEncodings = ACCEPT_ENCODING_SPLITTER.split(new String(m.get(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY), GrpcUtil.US_ASCII));
    // Order may be different, since decoder priorities have not yet been implemented.
    assertEquals(ImmutableSet.of("b", "a"), ImmutableSet.copyOf(acceptedEncodings));
}
Also used : Decompressor(io.grpc.Decompressor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Metadata(io.grpc.Metadata) DecompressorRegistry(io.grpc.DecompressorRegistry) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with Decompressor

use of io.grpc.Decompressor in project grpc-java by grpc.

the class ClientCallImplTest method prepareHeaders_acceptedMessageAndContentEncodingsAdded.

@Test
public void prepareHeaders_acceptedMessageAndContentEncodingsAdded() {
    Metadata m = new Metadata();
    DecompressorRegistry customRegistry = DecompressorRegistry.emptyInstance().with(new Decompressor() {

        @Override
        public String getMessageEncoding() {
            return "a";
        }

        @Override
        public InputStream decompress(InputStream is) throws IOException {
            return null;
        }
    }, true).with(new Decompressor() {

        @Override
        public String getMessageEncoding() {
            return "b";
        }

        @Override
        public InputStream decompress(InputStream is) throws IOException {
            return null;
        }
    }, true).with(new Decompressor() {

        @Override
        public String getMessageEncoding() {
            return "c";
        }

        @Override
        public InputStream decompress(InputStream is) throws IOException {
            return null;
        }
    }, // not advertised
    false);
    ClientCallImpl.prepareHeaders(m, customRegistry, Codec.Identity.NONE, true);
    Iterable<String> acceptedMessageEncodings = ACCEPT_ENCODING_SPLITTER.split(new String(m.get(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY), GrpcUtil.US_ASCII));
    // Order may be different, since decoder priorities have not yet been implemented.
    assertEquals(ImmutableSet.of("b", "a"), ImmutableSet.copyOf(acceptedMessageEncodings));
    Iterable<String> acceptedContentEncodings = ACCEPT_ENCODING_SPLITTER.split(new String(m.get(GrpcUtil.CONTENT_ACCEPT_ENCODING_KEY), GrpcUtil.US_ASCII));
    assertEquals(ImmutableSet.of("gzip"), ImmutableSet.copyOf(acceptedContentEncodings));
}
Also used : Decompressor(io.grpc.Decompressor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Metadata(io.grpc.Metadata) DecompressorRegistry(io.grpc.DecompressorRegistry) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with Decompressor

use of io.grpc.Decompressor in project grpc-java by grpc.

the class ClientCallImplTest method prepareHeaders_acceptedEncodingsAdded.

@Test
public void prepareHeaders_acceptedEncodingsAdded() {
    Metadata m = new Metadata();
    DecompressorRegistry customRegistry = DecompressorRegistry.emptyInstance().with(new Decompressor() {

        @Override
        public String getMessageEncoding() {
            return "a";
        }

        @Override
        public InputStream decompress(InputStream is) throws IOException {
            return null;
        }
    }, true).with(new Decompressor() {

        @Override
        public String getMessageEncoding() {
            return "b";
        }

        @Override
        public InputStream decompress(InputStream is) throws IOException {
            return null;
        }
    }, true).with(new Decompressor() {

        @Override
        public String getMessageEncoding() {
            return "c";
        }

        @Override
        public InputStream decompress(InputStream is) throws IOException {
            return null;
        }
    }, // not advertised
    false);
    ClientCallImpl.prepareHeaders(m, customRegistry, Codec.Identity.NONE, statsTraceCtx);
    Iterable<String> acceptedEncodings = ACCEPT_ENCODING_SPLITTER.split(new String(m.get(GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY), GrpcUtil.US_ASCII));
    // Order may be different, since decoder priorities have not yet been implemented.
    assertEquals(ImmutableSet.of("b", "a"), ImmutableSet.copyOf(acceptedEncodings));
}
Also used : Decompressor(io.grpc.Decompressor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Metadata(io.grpc.Metadata) DecompressorRegistry(io.grpc.DecompressorRegistry) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Decompressor (io.grpc.Decompressor)6 DecompressorRegistry (io.grpc.DecompressorRegistry)5 IOException (java.io.IOException)5 Metadata (io.grpc.Metadata)4 InputStream (java.io.InputStream)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Compressor (io.grpc.Compressor)1 CompressorRegistry (io.grpc.CompressorRegistry)1 Server (io.grpc.Server)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 NettyServerBuilder (io.grpc.netty.NettyServerBuilder)1 StreamObserver (io.grpc.stub.StreamObserver)1 DeliberateException (io.servicetalk.concurrent.internal.DeliberateException)1 GrpcStatusException (io.servicetalk.grpc.api.GrpcStatusException)1 CompatRequest (io.servicetalk.grpc.netty.CompatProto.RequestContainer.CompatRequest)1 CompatResponse (io.servicetalk.grpc.netty.CompatProto.ResponseContainer.CompatResponse)1 StreamResetException (io.vertx.core.http.StreamResetException)1 BridgeMessageDecoder (io.vertx.grpc.common.impl.BridgeMessageDecoder)1