Example usage for io.netty.util AsciiString AsciiString

List of usage examples for io.netty.util AsciiString AsciiString

Introduction

In this page you can find the example usage for io.netty.util AsciiString AsciiString.

Prototype

public AsciiString(CharSequence value, Charset charset) 

Source Link

Document

Create a copy of value into this instance using the encoding type of charset .

Usage

From source file:at.yawk.dbus.protocol.object.ObjectPathObject.java

public CharSequence getSequence() {
    return new AsciiString(bytes, false);
}

From source file:io.grpc.netty.GrpcHpackDecoder.java

License:Apache License

private CharSequence readStringLiteral(ByteBuf in, int length, boolean huffmanEncoded) throws Http2Exception {
    if (huffmanEncoded) {
        return hpackHuffmanDecoder.decode(in, length);
    }//from w  w  w. ja  v  a 2  s .com
    byte[] buf = new byte[length];
    in.readBytes(buf);
    return new AsciiString(buf, false);
}

From source file:io.grpc.netty.GrpcHttp2OutboundHeaders.java

License:Apache License

private GrpcHttp2OutboundHeaders(AsciiString[] preHeaders, byte[][] serializedMetadata) {
    normalHeaders = new AsciiString[serializedMetadata.length];
    for (int i = 0; i < normalHeaders.length; i++) {
        normalHeaders[i] = new AsciiString(serializedMetadata[i], false);
    }//from   ww  w . ja  v  a 2 s  .  com
    this.preHeaders = preHeaders;
}

From source file:io.grpc.netty.MethodDescriptorBenchmark.java

License:Apache License

/** Foo bar. */
@Benchmark/*www .  ja  v  a2s .  c  o  m*/
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public AsciiString direct() {
    return new AsciiString(directBytes, false);
}

From source file:io.grpc.netty.NettyClientStreamTest.java

License:Apache License

@Test
public void invalidInboundContentTypeShouldCancelStream() {
    // Set stream id to indicate it has been created
    stream().transportState().setId(STREAM_ID);
    Http2Headers headers = new DefaultHttp2Headers().status(STATUS_OK).set(CONTENT_TYPE_HEADER,
            new AsciiString("application/bad", UTF_8));
    stream().transportState().transportHeadersReceived(headers, false);
    Http2Headers trailers = new DefaultHttp2Headers().set(new AsciiString("grpc-status", UTF_8),
            new AsciiString("0", UTF_8));
    stream().transportState().transportHeadersReceived(trailers, true);
    ArgumentCaptor<Status> captor = ArgumentCaptor.forClass(Status.class);
    ArgumentCaptor<Metadata> metadataCaptor = ArgumentCaptor.forClass(Metadata.class);
    verify(listener).closed(captor.capture(), same(PROCESSED), metadataCaptor.capture());
    Status status = captor.getValue();/*  w  w  w  .j a  v a 2 s. c  om*/
    assertEquals(Status.Code.UNKNOWN, status.getCode());
    assertTrue(status.getDescription().contains("content-type"));
    assertEquals("application/bad",
            metadataCaptor.getValue().get(Metadata.Key.of("Content-Type", Metadata.ASCII_STRING_MARSHALLER)));
}

From source file:io.grpc.netty.NettyServerHandler.java

License:Apache License

private void respondWithHttpError(ChannelHandlerContext ctx, int streamId, int code, Status.Code statusCode,
        String msg) {/*from  w  ww  .j  a  va 2 s  . com*/
    Metadata metadata = new Metadata();
    metadata.put(InternalStatus.CODE_KEY, statusCode.toStatus());
    metadata.put(InternalStatus.MESSAGE_KEY, msg);
    byte[][] serialized = InternalMetadata.serialize(metadata);

    Http2Headers headers = new DefaultHttp2Headers(true, serialized.length / 2).status("" + code)
            .set(CONTENT_TYPE_HEADER, "text/plain; encoding=utf-8");
    for (int i = 0; i < serialized.length; i += 2) {
        headers.add(new AsciiString(serialized[i], false), new AsciiString(serialized[i + 1], false));
    }
    encoder().writeHeaders(ctx, streamId, headers, 0, false, ctx.newPromise());
    ByteBuf msgBuf = ByteBufUtil.writeUtf8(ctx.alloc(), msg);
    encoder().writeData(ctx, streamId, msgBuf, 0, true, ctx.newPromise());
}

From source file:io.grpc.netty.NettyServerHandlerTest.java

License:Apache License

@Test
public void headersWithInvalidContentTypeShouldFail() throws Exception {
    manualSetUp();/*ww w.j av  a  2  s .c  o m*/
    Http2Headers headers = new DefaultHttp2Headers().method(HTTP_METHOD)
            .set(CONTENT_TYPE_HEADER, new AsciiString("application/bad", UTF_8)).set(TE_HEADER, TE_TRAILERS)
            .path(new AsciiString("/foo/bar"));
    ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
    channelRead(headersFrame);
    Http2Headers responseHeaders = new DefaultHttp2Headers()
            .set(InternalStatus.CODE_KEY.name(), String.valueOf(Code.INTERNAL.value()))
            .set(InternalStatus.MESSAGE_KEY.name(), "Content-Type 'application/bad' is not supported")
            .status("" + 415).set(CONTENT_TYPE_HEADER, "text/plain; encoding=utf-8");

    verifyWrite().writeHeaders(eq(ctx()), eq(STREAM_ID), eq(responseHeaders), eq(0),
            eq(DEFAULT_PRIORITY_WEIGHT), eq(false), eq(0), eq(false), any(ChannelPromise.class));
}

From source file:io.grpc.netty.NettyServerHandlerTest.java

License:Apache License

@Test
public void headersSupportExtensionContentType() throws Exception {
    manualSetUp();//  w  w w .  j av  a 2s . co m
    Http2Headers headers = new DefaultHttp2Headers().method(HTTP_METHOD)
            .set(CONTENT_TYPE_HEADER, new AsciiString("application/grpc+json", UTF_8))
            .set(TE_HEADER, TE_TRAILERS).path(new AsciiString("/foo/bar"));
    ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
    channelRead(headersFrame);

    ArgumentCaptor<NettyServerStream> streamCaptor = ArgumentCaptor.forClass(NettyServerStream.class);
    ArgumentCaptor<String> methodCaptor = ArgumentCaptor.forClass(String.class);
    verify(transportListener).streamCreated(streamCaptor.capture(), methodCaptor.capture(),
            any(Metadata.class));
    stream = streamCaptor.getValue();
}