Java Code Examples for io.netty.util.AsciiString#of()

The following examples show how to use io.netty.util.AsciiString#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InboundHeadersBenchmark.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
private static void setupRequestHeaders() {
  requestHeaders = new AsciiString[18];
  int i = 0;
  requestHeaders[i++] = AsciiString.of(":method");
  requestHeaders[i++] = AsciiString.of("POST");
  requestHeaders[i++] = AsciiString.of(":scheme");
  requestHeaders[i++] = AsciiString.of("http");
  requestHeaders[i++] = AsciiString.of(":path");
  requestHeaders[i++] = AsciiString.of("/google.pubsub.v2.PublisherService/CreateTopic");
  requestHeaders[i++] = AsciiString.of(":authority");
  requestHeaders[i++] = AsciiString.of("pubsub.googleapis.com");
  requestHeaders[i++] = AsciiString.of("te");
  requestHeaders[i++] = AsciiString.of("trailers");
  requestHeaders[i++] = AsciiString.of("grpc-timeout");
  requestHeaders[i++] = AsciiString.of("1S");
  requestHeaders[i++] = AsciiString.of("content-type");
  requestHeaders[i++] = AsciiString.of("application/grpc+proto");
  requestHeaders[i++] = AsciiString.of("grpc-encoding");
  requestHeaders[i++] = AsciiString.of("gzip");
  requestHeaders[i++] = AsciiString.of("authorization");
  requestHeaders[i] = AsciiString.of("Bearer y235.wef315yfh138vh31hv93hv8h3v");
}
 
Example 2
Source File: NettyClientStreamTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Override
protected NettyClientStream createStream() {
  when(handler.getWriteQueue()).thenReturn(writeQueue);
  NettyClientStream stream = new NettyClientStream(
      new TransportStateImpl(handler, DEFAULT_MAX_MESSAGE_SIZE),
      methodDescriptor,
      new Metadata(),
      channel,
      AsciiString.of("localhost"),
      AsciiString.of("http"),
      AsciiString.of("agent"),
      StatsTraceContext.NOOP,
      transportTracer,
      CallOptions.DEFAULT,
      false);
  stream.start(listener);
  stream.transportState().setHttp2Stream(http2Stream);
  reset(listener);
  return stream;
}
 
Example 3
Source File: NettyClientStreamTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void setHttp2StreamShouldNotifyReady() {
  listener = mock(ClientStreamListener.class);

  stream = new NettyClientStream(new TransportStateImpl(handler, DEFAULT_MAX_MESSAGE_SIZE),
      methodDescriptor,
      new Metadata(),
      channel,
      AsciiString.of("localhost"),
      AsciiString.of("http"),
      AsciiString.of("agent"),
      StatsTraceContext.NOOP,
      transportTracer,
      CallOptions.DEFAULT,
      false);
  stream.start(listener);
  stream().transportState().setId(STREAM_ID);
  verify(listener, never()).onReady();
  assertFalse(stream.isReady());
  stream().transportState().setHttp2Stream(http2Stream);
  verify(listener).onReady();
  assertTrue(stream.isReady());
}
 
Example 4
Source File: InboundHeadersBenchmark.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
private static void setupRequestHeaders() {
  requestHeaders = new AsciiString[18];
  int i = 0;
  requestHeaders[i++] = AsciiString.of(":method");
  requestHeaders[i++] = AsciiString.of("POST");
  requestHeaders[i++] = AsciiString.of(":scheme");
  requestHeaders[i++] = AsciiString.of("http");
  requestHeaders[i++] = AsciiString.of(":path");
  requestHeaders[i++] = AsciiString.of("/google.pubsub.v2.PublisherService/CreateTopic");
  requestHeaders[i++] = AsciiString.of(":authority");
  requestHeaders[i++] = AsciiString.of("pubsub.googleapis.com");
  requestHeaders[i++] = AsciiString.of("te");
  requestHeaders[i++] = AsciiString.of("trailers");
  requestHeaders[i++] = AsciiString.of("grpc-timeout");
  requestHeaders[i++] = AsciiString.of("1S");
  requestHeaders[i++] = AsciiString.of("content-type");
  requestHeaders[i++] = AsciiString.of("application/grpc+proto");
  requestHeaders[i++] = AsciiString.of("grpc-encoding");
  requestHeaders[i++] = AsciiString.of("gzip");
  requestHeaders[i++] = AsciiString.of("authorization");
  requestHeaders[i] = AsciiString.of("Bearer y235.wef315yfh138vh31hv93hv8h3v");
}
 
Example 5
Source File: ServerHandshakePacket.java    From spring-boot-protocol with Apache License 2.0 6 votes vote down vote up
private ServerHandshakePacket(Builder builder) {
	super(builder.authPluginData);
	if (builder.authPluginData.readableBytes() < Constants.AUTH_PLUGIN_DATA_PART1_LEN) {
		throw new IllegalArgumentException("authPluginData can not contain less than " + Constants.AUTH_PLUGIN_DATA_PART1_LEN + " bytes.");
	}
	protocolVersion = builder.protocolVersion;
	if (builder.serverVersion == null) {
		throw new NullPointerException("serverVersion can not be null");
	}
	sequenceId = builder.sequenceId;
	serverVersion = AsciiString.of(builder.serverVersion);
	connectionId = builder.connectionId;
	capabilities = Collections.unmodifiableSet(builder.capabilities);
	characterSet = builder.characterSet;
	serverStatus = Collections.unmodifiableSet(builder.serverStatus);
	authPluginName = builder.authPluginName;
}
 
Example 6
Source File: NettyClientStreamTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Override
protected NettyClientStream createStream() {
  when(handler.getWriteQueue()).thenReturn(writeQueue);
  NettyClientStream stream = new NettyClientStream(
      new TransportStateImpl(handler, DEFAULT_MAX_MESSAGE_SIZE),
      methodDescriptor,
      new Metadata(),
      channel,
      AsciiString.of("localhost"),
      AsciiString.of("http"),
      AsciiString.of("agent"),
      StatsTraceContext.NOOP,
      transportTracer,
      CallOptions.DEFAULT);
  stream.start(listener);
  stream.transportState().setId(STREAM_ID);
  stream.transportState().setHttp2Stream(http2Stream);
  reset(listener);
  return stream;
}
 
Example 7
Source File: NettyClientStreamTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void setHttp2StreamShouldNotifyReady() {
  listener = mock(ClientStreamListener.class);

  stream = new NettyClientStream(new TransportStateImpl(handler, DEFAULT_MAX_MESSAGE_SIZE),
      methodDescriptor,
      new Metadata(),
      channel,
      AsciiString.of("localhost"),
      AsciiString.of("http"),
      AsciiString.of("agent"),
      StatsTraceContext.NOOP,
      transportTracer,
      CallOptions.DEFAULT);
  stream.start(listener);
  stream().transportState().setId(STREAM_ID);
  verify(listener, never()).onReady();
  assertFalse(stream.isReady());
  stream().transportState().setHttp2Stream(http2Stream);
  verify(listener).onReady();
  assertTrue(stream.isReady());
}
 
Example 8
Source File: NettyClientStreamTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void getRequestSentThroughHeader() {
  // Creating a GET method
  MethodDescriptor<?, ?> descriptor = MethodDescriptor.<Void, Void>newBuilder()
      .setType(MethodDescriptor.MethodType.UNARY)
      .setFullMethodName("testService/test")
      .setRequestMarshaller(marshaller)
      .setResponseMarshaller(marshaller)
      .setIdempotent(true)
      .setSafe(true)
      .build();
  NettyClientStream stream = new NettyClientStream(
      new TransportStateImpl(handler, DEFAULT_MAX_MESSAGE_SIZE),
      descriptor,
      new Metadata(),
      channel,
      AsciiString.of("localhost"),
      AsciiString.of("http"),
      AsciiString.of("agent"),
      StatsTraceContext.NOOP,
      transportTracer,
      CallOptions.DEFAULT);
  stream.start(listener);
  stream.transportState().setId(STREAM_ID);
  stream.transportState().setHttp2Stream(http2Stream);

  byte[] msg = smallMessage();
  stream.writeMessage(new ByteArrayInputStream(msg));
  stream.flush();
  stream.halfClose();
  ArgumentCaptor<CreateStreamCommand> cmdCap = ArgumentCaptor.forClass(CreateStreamCommand.class);
  verify(writeQueue).enqueue(cmdCap.capture(), eq(true));
  ImmutableListMultimap<CharSequence, CharSequence> headers =
      ImmutableListMultimap.copyOf(cmdCap.getValue().headers());
  assertThat(headers).containsEntry(AsciiString.of(":method"), Utils.HTTP_GET_METHOD);
  assertThat(headers)
      .containsEntry(
          AsciiString.of(":path"),
          AsciiString.of("/testService/test?" + BaseEncoding.base64().encode(msg)));
}
 
Example 9
Source File: NettyClientStreamTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@Test
public void removeUserAgentFromApplicationHeaders() {
  Metadata metadata = new Metadata();
  metadata.put(GrpcUtil.USER_AGENT_KEY, "bad agent");
  listener = mock(ClientStreamListener.class);
  Mockito.reset(writeQueue);
  ChannelPromise completedPromise = new DefaultChannelPromise(channel)
      .setSuccess();
  when(writeQueue.enqueue(any(QueuedCommand.class), any(boolean.class)))
      .thenReturn(completedPromise);

  stream = new NettyClientStream(
      new TransportStateImpl(handler, DEFAULT_MAX_MESSAGE_SIZE),
      methodDescriptor,
      new Metadata(),
      channel,
      AsciiString.of("localhost"),
      AsciiString.of("http"),
      AsciiString.of("good agent"),
      StatsTraceContext.NOOP,
      transportTracer,
      CallOptions.DEFAULT);
  stream.start(listener);

  ArgumentCaptor<CreateStreamCommand> cmdCap = ArgumentCaptor.forClass(CreateStreamCommand.class);
  verify(writeQueue).enqueue(cmdCap.capture(), eq(false));
  assertThat(ImmutableListMultimap.copyOf(cmdCap.getValue().headers()))
      .containsEntry(Utils.USER_AGENT, AsciiString.of("good agent"));
}
 
Example 10
Source File: HttpHeaderNamesTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void testOfAsciiString() {
    // Should produce a lower-cased AsciiString.
    final AsciiString mixedCased = AsciiString.of("Foo");
    assertThat((Object) HttpHeaderNames.of(mixedCased)).isNotSameAs(mixedCased);
    assertThat(HttpHeaderNames.of(mixedCased).toString()).isEqualTo("foo");

    // Should not produce a new instance for an AsciiString that's already lower-cased.
    final AsciiString lowerCased = AsciiString.of("foo");
    assertThat((Object) HttpHeaderNames.of(lowerCased)).isSameAs(lowerCased);

    // Should reuse known header name instances.
    assertThat((Object) HttpHeaderNames.of(AsciiString.of("date"))).isSameAs(HttpHeaderNames.DATE);
}
 
Example 11
Source File: InboundHeadersBenchmark.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private static void setupResponseHeaders() {
  responseHeaders = new AsciiString[4];
  int i = 0;
  responseHeaders[i++] = AsciiString.of(":status");
  responseHeaders[i++] = AsciiString.of("200");
  responseHeaders[i++] = AsciiString.of("grpc-encoding");
  responseHeaders[i] = AsciiString.of("gzip");
}
 
Example 12
Source File: InboundHeadersBenchmark.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private static void setupResponseHeaders() {
  responseHeaders = new AsciiString[4];
  int i = 0;
  responseHeaders[i++] = AsciiString.of(":status");
  responseHeaders[i++] = AsciiString.of("200");
  responseHeaders[i++] = AsciiString.of("grpc-encoding");
  responseHeaders[i] = AsciiString.of("gzip");
}
 
Example 13
Source File: NettyClientStreamTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void removeUserAgentFromApplicationHeaders() {
  Metadata metadata = new Metadata();
  metadata.put(GrpcUtil.USER_AGENT_KEY, "bad agent");
  listener = mock(ClientStreamListener.class);
  Mockito.reset(writeQueue);
  ChannelPromise completedPromise = new DefaultChannelPromise(channel)
      .setSuccess();
  when(writeQueue.enqueue(any(QueuedCommand.class), anyBoolean())).thenReturn(completedPromise);

  stream = new NettyClientStream(
      new TransportStateImpl(handler, DEFAULT_MAX_MESSAGE_SIZE),
      methodDescriptor,
      new Metadata(),
      channel,
      AsciiString.of("localhost"),
      AsciiString.of("http"),
      AsciiString.of("good agent"),
      StatsTraceContext.NOOP,
      transportTracer,
      CallOptions.DEFAULT,
      false);
  stream.start(listener);

  ArgumentCaptor<CreateStreamCommand> cmdCap = ArgumentCaptor.forClass(CreateStreamCommand.class);
  verify(writeQueue).enqueue(cmdCap.capture(), eq(false));
  assertThat(ImmutableListMultimap.copyOf(cmdCap.getValue().headers()))
      .containsEntry(Utils.USER_AGENT, AsciiString.of("good agent"));
}
 
Example 14
Source File: HttpConversionUtil.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
public static void toHttp2Headers(HttpHeaders inHeaders, Http2Headers out) {
    Iterator<Entry<CharSequence, CharSequence>> iter = inHeaders.iteratorCharSequence();
    // Choose 8 as a default size because it is unlikely we will see more than 4 Connection headers values, but
    // still allowing for "enough" space in the map to reduce the chance of hash code collision.
    CharSequenceMap<AsciiString> connectionBlacklist =
        toLowercaseMap(inHeaders.valueCharSequenceIterator(CONNECTION), 8);
    while (iter.hasNext()) {
        Entry<CharSequence, CharSequence> entry = iter.next();
        final AsciiString aName = AsciiString.of(entry.getKey()).toLowerCase();
        if (!HTTP_TO_HTTP2_HEADER_BLACKLIST.contains(aName) && !connectionBlacklist.contains(aName)) {
            // https://tools.ietf.org/html/rfc7540#section-8.1.2.2 makes a special exception for TE
            if (aName.contentEqualsIgnoreCase(TE)) {
                toHttp2HeadersFilterTE(entry, out);
            } else if (aName.contentEqualsIgnoreCase(COOKIE)) {
                AsciiString value = AsciiString.of(entry.getValue());
                // split up cookies to allow for better compression
                // https://tools.ietf.org/html/rfc7540#section-8.1.2.5
                try {
                    int index = value.forEachByte(FIND_SEMI_COLON);
                    if (index != -1) {
                        int start = 0;
                        do {
                            out.add(COOKIE, value.subSequence(start, index, false));
                            // skip 2 characters "; " (see https://tools.ietf.org/html/rfc6265#section-4.2.1)
                            start = index + 2;
                        } while (start < value.length() &&
                                (index = value.forEachByte(start, value.length() - start, FIND_SEMI_COLON)) != -1);
                        if (start >= value.length()) {
                            throw new IllegalArgumentException("cookie value is of unexpected format: " + value);
                        }
                        out.add(COOKIE, value.subSequence(start, value.length(), false));
                    } else {
                        out.add(COOKIE, value);
                    }
                } catch (Exception e) {
                    // This is not expect to happen because FIND_SEMI_COLON never throws but must be caught
                    // because of the ByteProcessor interface.
                    throw new IllegalStateException(e);
                }
            } else {
                out.add(aName, entry.getValue());
            }
        }
    }
}
 
Example 15
Source File: SmtpCommand.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the {@link SmtpCommand} for the given command name.
 */
public static SmtpCommand valueOf(CharSequence commandName) {
    ObjectUtil.checkNotNull(commandName, "commandName");
    SmtpCommand command = COMMANDS.get(commandName.toString());
    return command != null ? command : new SmtpCommand(AsciiString.of(commandName));
}
 
Example 16
Source File: ViaHeaderAppendingInterceptor.java    From styx with Apache License 2.0 4 votes vote down vote up
public ViaHeaderAppendingInterceptor(final String via) {
    final String value = isBlank(via) ? DEFAULT_VIA : via;
    via10 = AsciiString.of("1.0 " + value);
    via11 = AsciiString.of("1.1 " + value);
}
 
Example 17
Source File: GrpcServiceServerTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@BeforeAll
static void createLargePayload() {
    LARGE_PAYLOAD = AsciiString.of(Strings.repeat("a", MAX_MESSAGE_SIZE + 1));
}
 
Example 18
Source File: NettyClientStream.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setAuthority(String authority) {
  this.authority = AsciiString.of(checkNotNull(authority, "authority"));
}
 
Example 19
Source File: NettyClientStreamTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@Test
public void getRequestSentThroughHeader() {
  // Creating a GET method
  MethodDescriptor<?, ?> descriptor = MethodDescriptor.<Void, Void>newBuilder()
      .setType(MethodDescriptor.MethodType.UNARY)
      .setFullMethodName("testService/test")
      .setRequestMarshaller(marshaller)
      .setResponseMarshaller(marshaller)
      .setIdempotent(true)
      .setSafe(true)
      .build();
  NettyClientStream stream = new NettyClientStream(
      new TransportStateImpl(handler, DEFAULT_MAX_MESSAGE_SIZE),
      descriptor,
      new Metadata(),
      channel,
      AsciiString.of("localhost"),
      AsciiString.of("http"),
      AsciiString.of("agent"),
      StatsTraceContext.NOOP,
      transportTracer,
      CallOptions.DEFAULT,
      true);
  stream.start(listener);
  stream.transportState().setId(STREAM_ID);
  stream.transportState().setHttp2Stream(http2Stream);

  byte[] msg = smallMessage();
  stream.writeMessage(new ByteArrayInputStream(msg));
  stream.flush();
  stream.halfClose();
  ArgumentCaptor<CreateStreamCommand> cmdCap = ArgumentCaptor.forClass(CreateStreamCommand.class);
  verify(writeQueue).enqueue(cmdCap.capture(), eq(true));
  ImmutableListMultimap<CharSequence, CharSequence> headers =
      ImmutableListMultimap.copyOf(cmdCap.getValue().headers());
  assertThat(headers).containsEntry(AsciiString.of(":method"), Utils.HTTP_GET_METHOD);
  assertThat(headers)
      .containsEntry(
          AsciiString.of(":path"),
          AsciiString.of("/testService/test?" + BaseEncoding.base64().encode(msg)));
}
 
Example 20
Source File: NettyClientStream.java    From grpc-nebula-java with Apache License 2.0 4 votes vote down vote up
@Override
public void setAuthority(String authority) {
  this.authority = AsciiString.of(checkNotNull(authority, "authority"));
}