本文整理汇总了Java中io.netty.util.AsciiString类的典型用法代码示例。如果您正苦于以下问题:Java AsciiString类的具体用法?Java AsciiString怎么用?Java AsciiString使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AsciiString类属于io.netty.util包,在下文中一共展示了AsciiString类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertRequest
import io.netty.util.AsciiString; //导入依赖的package包/类
/**
* Converts the specified {@code request} to {@link Optional} which contains {@link WatchRequest} when
* the request has {@link HttpHeaderNames#IF_NONE_MATCH}. {@link Optional#EMPTY} otherwise.
*/
@Override
public Object convertRequest(ServiceRequestContext ctx, AggregatedHttpMessage request,
Class<?> expectedResultType) throws Exception {
final String ifNoneMatch = request.headers().get(AsciiString.of("if-none-match"));
if (!isNullOrEmpty(ifNoneMatch)) {
final Revision lastKnownRevision = new Revision(ifNoneMatch);
final String prefer = request.headers().get(AsciiString.of("prefer"));
final long timeoutMillis;
if (!isNullOrEmpty(prefer)) {
timeoutMillis = getTimeoutMillis(prefer);
} else {
timeoutMillis = DEFAULT_TIMEOUT_MILLIS;
}
return Optional.of(new WatchRequest(lastKnownRevision, timeoutMillis));
}
return Optional.empty();
}
示例2: turnOff
import io.netty.util.AsciiString; //导入依赖的package包/类
@Test
public void turnOff() throws Exception {
service.serverHealth.setHealthy(true);
AggregatedHttpMessage res = service.serve(context, HC_TURN_OFF_REQ).aggregate().get();
assertThat(res.status(), is(HttpStatus.OK));
assertThat(res.headers().get(AsciiString.of(HttpHeaders.CONTENT_TYPE)),
is(MediaType.PLAIN_TEXT_UTF_8.toString()));
assertThat(res.content().toStringUtf8(), is("Set unhealthy."));
res = service.serve(context, HC_REQ).aggregate().get();
assertThat(res.status(), is(HttpStatus.SERVICE_UNAVAILABLE));
assertThat(res.headers().get(AsciiString.of(HttpHeaders.CONTENT_TYPE)),
is(MediaType.PLAIN_TEXT_UTF_8.toString()));
}
示例3: serializeTrailersAsMessage
import io.netty.util.AsciiString; //导入依赖的package包/类
private HttpData serializeTrailersAsMessage(HttpHeaders trailers) {
ByteBuf serialized = ctx.alloc().buffer();
boolean success = false;
try {
serialized.writeByte(TRAILERS_FRAME_HEADER);
// Skip, we'll set this after serializing the headers.
serialized.writeInt(0);
for (Map.Entry<AsciiString, String> trailer : trailers) {
encodeHeader(trailer.getKey(), trailer.getValue(), serialized);
}
int messageSize = serialized.readableBytes() - 5;
serialized.setInt(1, messageSize);
success = true;
} finally {
if (!success) {
serialized.release();
}
}
return new ByteBufHttpData(serialized, true);
}
示例4: missingMethod
import io.netty.util.AsciiString; //导入依赖的package包/类
@Test
public void missingMethod() throws Exception {
when(ctx.mappedPath()).thenReturn("/grpc.testing.TestService/FooCall");
HttpResponse response = grpcService.doPost(
ctx,
HttpRequest.of(HttpHeaders.of(HttpMethod.POST, "/grpc.testing.TestService/FooCall")
.set(HttpHeaderNames.CONTENT_TYPE, "application/grpc+proto")));
assertThat(response.aggregate().get()).isEqualTo(AggregatedHttpMessage.of(
HttpHeaders.of(HttpStatus.OK)
.set(HttpHeaderNames.CONTENT_TYPE, "application/grpc+proto")
.set(AsciiString.of("grpc-status"), "12")
.set(AsciiString.of("grpc-message"),
"Method not found: grpc.testing.TestService/FooCall")
.set(HttpHeaderNames.CONTENT_LENGTH, "0"),
HttpData.of(new byte[] {})));
}
示例5: testDerivedClient
import io.netty.util.AsciiString; //导入依赖的package包/类
@Test(timeout = 10000)
public void testDerivedClient() throws Exception {
final String AUTHORIZATION = "authorization";
final String NO_TOKEN = "";
final String TOKEN_A = "token 1234";
final String TOKEN_B = "token 5678";
final HeaderService.Iface client = Clients.newClient(clientFactory(), getURI(Handlers.HEADER),
Handlers.HEADER.iface(), clientOptions);
assertThat(client.header(AUTHORIZATION)).isEqualTo(NO_TOKEN);
final HeaderService.Iface clientA =
Clients.newDerivedClient(client, newHttpHeaderOption(AsciiString.of(AUTHORIZATION), TOKEN_A));
final HeaderService.Iface clientB =
Clients.newDerivedClient(client, newHttpHeaderOption(AsciiString.of(AUTHORIZATION), TOKEN_B));
assertThat(clientA.header(AUTHORIZATION)).isEqualTo(TOKEN_A);
assertThat(clientB.header(AUTHORIZATION)).isEqualTo(TOKEN_B);
// Ensure that the parent client's HTTP_HEADERS option did not change:
assertThat(client.header(AUTHORIZATION)).isEqualTo(NO_TOKEN);
}
示例6: configureHttp1WithUpgrade
import io.netty.util.AsciiString; //导入依赖的package包/类
private void configureHttp1WithUpgrade(ChannelHandlerContext ctx) {
final ChannelPipeline p = ctx.pipeline();
final HttpServerCodec http1codec = new HttpServerCodec(
config.defaultMaxHttp1InitialLineLength(),
config.defaultMaxHttp1HeaderSize(),
config.defaultMaxHttp1ChunkSize());
String baseName = name;
baseName = addAfter(p, baseName, http1codec);
baseName = addAfter(p, baseName, new HttpServerUpgradeHandler(
http1codec,
protocol -> {
if (!AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) {
return null;
}
return new Http2ServerUpgradeCodec(
newHttp2ConnectionHandler(p));
},
UPGRADE_REQUEST_MAX_LENGTH));
addAfter(p, baseName, new Http1RequestDecoder(config, ctx.channel(), SCHEME_HTTP));
}
示例7: preflightResponseHeaders
import io.netty.util.AsciiString; //导入依赖的package包/类
/**
* Returns HTTP response headers that should be added to a CORS preflight response.
*
* @return {@link HttpHeaders} the HTTP response headers to be added.
*
* @throws IllegalStateException if CORS support is not enabled
*/
public HttpHeaders preflightResponseHeaders() {
ensureEnabled();
if (preflightResponseHeaders.isEmpty()) {
return HttpHeaders.EMPTY_HEADERS;
}
final HttpHeaders preflightHeaders = new DefaultHttpHeaders(false);
for (Entry<AsciiString, Supplier<?>> entry : preflightResponseHeaders.entrySet()) {
final Object value = getValue(entry.getValue());
if (value instanceof Iterable) {
preflightHeaders.addObject(entry.getKey(), (Iterable<?>) value);
} else {
preflightHeaders.addObject(entry.getKey(), value);
}
}
return preflightHeaders;
}
示例8: toArmeria
import io.netty.util.AsciiString; //导入依赖的package包/类
/**
* Converts the specified Netty HTTP/2 into Armeria HTTP/2 headers.
*/
public static HttpHeaders toArmeria(Http2Headers headers) {
final HttpHeaders converted = new DefaultHttpHeaders(false, headers.size());
StringJoiner cookieJoiner = null;
for (Entry<CharSequence, CharSequence> e : headers) {
final AsciiString name = AsciiString.of(e.getKey());
final CharSequence value = e.getValue();
// Cookies must be concatenated into a single octet string.
// https://tools.ietf.org/html/rfc7540#section-8.1.2.5
if (name.equals(HttpHeaderNames.COOKIE)) {
if (cookieJoiner == null) {
cookieJoiner = new StringJoiner(COOKIE_SEPARATOR);
}
COOKIE_SPLITTER.split(value).forEach(cookieJoiner::add);
} else {
converted.add(name, value.toString());
}
}
if (cookieJoiner != null && cookieJoiner.length() != 0) {
converted.add(HttpHeaderNames.COOKIE, cookieJoiner.toString());
}
return converted;
}
示例9: toHttp2HeadersFilterTE
import io.netty.util.AsciiString; //导入依赖的package包/类
/**
* Filter the {@link HttpHeaderNames#TE} header according to the
* <a href="https://tools.ietf.org/html/rfc7540#section-8.1.2.2">special rules in the HTTP/2 RFC</a>.
* @param entry An entry whose name is {@link HttpHeaderNames#TE}.
* @param out the resulting HTTP/2 headers.
*/
private static void toHttp2HeadersFilterTE(Entry<CharSequence, CharSequence> entry,
HttpHeaders out) {
if (AsciiString.indexOf(entry.getValue(), ',', 0) == -1) {
if (AsciiString.contentEqualsIgnoreCase(AsciiString.trim(entry.getValue()),
HttpHeaderValues.TRAILERS)) {
out.add(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS.toString());
}
} else {
List<CharSequence> teValues = StringUtil.unescapeCsvFields(entry.getValue());
for (CharSequence teValue : teValues) {
if (AsciiString.contentEqualsIgnoreCase(AsciiString.trim(teValue),
HttpHeaderValues.TRAILERS)) {
out.add(HttpHeaderNames.TE, HttpHeaderValues.TRAILERS.toString());
break;
}
}
}
}
示例10: notSupported
import io.netty.util.AsciiString; //导入依赖的package包/类
@Test
public void notSupported() throws Exception {
HttpRequestWriter noopRequest = HttpRequest.streaming(HttpMethod.PUT, "/");
noopRequest.write(() -> HttpData.ofAscii("noop"));
noopRequest.close();
AggregatedHttpMessage res = service.serve(context, noopRequest).aggregate().get();
assertThat(res.status(), is(HttpStatus.BAD_REQUEST));
assertThat(res.headers().get(AsciiString.of(HttpHeaders.CONTENT_TYPE)),
is(MediaType.PLAIN_TEXT_UTF_8.toString()));
assertThat(res.content().toStringUtf8(), is("Not supported."));
service.serverHealth.setHealthy(true);
noopRequest = HttpRequest.streaming(HttpMethod.PUT, "/");
noopRequest.write(() -> HttpData.ofAscii("noop"));
noopRequest.close();
res = service.serve(context, noopRequest).aggregate().get();
assertThat(res.status(), is(HttpStatus.BAD_REQUEST));
assertThat(res.headers().get(AsciiString.of(HttpHeaders.CONTENT_TYPE)),
is(MediaType.PLAIN_TEXT_UTF_8.toString()));
assertThat(res.content().toStringUtf8(), is("Not supported."));
}
示例11: test400
import io.netty.util.AsciiString; //导入依赖的package包/类
@Test
public void test400(final TestContext testContext) throws Exception {
final HttpServerRequest serverRequest = mock(HttpServerRequest.class);
when(serverRequest.absoluteURI()).thenReturn("http://test.trajano.net/api/hello/400");
when(serverRequest.path()).thenReturn("/api/hello/400");
when(serverRequest.uri()).thenReturn("/api/hello/400");
when(serverRequest.isEnded()).thenReturn(true);
when(serverRequest.method()).thenReturn(HttpMethod.GET);
final HttpServerResponse response = mock(HttpServerResponse.class);
when(response.putHeader(anyString(), anyString())).thenReturn(response);
when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response);
when(response.headers()).thenReturn(new VertxHttpHeaders());
final Async async = testContext.async();
when(response.setStatusCode(Matchers.any(Integer.class))).then(invocation -> {
try {
return response;
} finally {
async.complete();
}
});
when(serverRequest.response()).thenReturn(response);
router.accept(serverRequest);
async.awaitSuccess();
verify(response, times(1)).setStatusCode(400);
}
示例12: test404
import io.netty.util.AsciiString; //导入依赖的package包/类
@Test
public void test404(final TestContext testContext) throws Exception {
final Router router = Router.router(rule.vertx());
final JaxRsRouter jaxRsRouter = new JaxRsRouter();
final SpringJaxRsHandler handler = new SpringJaxRsHandler(MyApp.class);
jaxRsRouter.register(MyApp.class, router, handler, handler);
final HttpServerRequest serverRequest = mock(HttpServerRequest.class);
when(serverRequest.absoluteURI()).thenReturn("http://test.trajano.net/api/nothello");
when(serverRequest.path()).thenReturn("/api/nothello");
when(serverRequest.uri()).thenReturn("/api/nothello");
when(serverRequest.isEnded()).thenReturn(true);
when(serverRequest.method()).thenReturn(HttpMethod.GET);
final HttpServerResponse response = mock(HttpServerResponse.class);
when(response.putHeader(anyString(), anyString())).thenReturn(response);
when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response);
when(response.headers()).thenReturn(new VertxHttpHeaders());
final Async async = testContext.async();
Mockito.doAnswer(invocation -> {
async.complete();
return null;
}).when(response).end(anyString());
when(serverRequest.response()).thenReturn(response);
router.accept(serverRequest);
async.await();
verify(response, times(1)).setStatusCode(404);
verify(response, times(1)).end(anyString());
}
示例13: test404Internal
import io.netty.util.AsciiString; //导入依赖的package包/类
@Test
public void test404Internal(final TestContext testContext) throws Exception {
final Router router = Router.router(rule.vertx());
final JaxRsRouter jaxRsRouter = new JaxRsRouter();
final SpringJaxRsHandler handler = new SpringJaxRsHandler(MyApp.class);
jaxRsRouter.register(MyApp.class, router, handler, handler);
final HttpServerRequest serverRequest = mock(HttpServerRequest.class);
when(serverRequest.absoluteURI()).thenReturn("http://test.trajano.net/api/hello/404");
when(serverRequest.path()).thenReturn("/api/hello/404");
when(serverRequest.uri()).thenReturn("/api/hello/404");
when(serverRequest.isEnded()).thenReturn(true);
when(serverRequest.method()).thenReturn(HttpMethod.GET);
final HttpServerResponse response = mock(HttpServerResponse.class);
when(response.putHeader(anyString(), anyString())).thenReturn(response);
when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response);
when(response.headers()).thenReturn(new VertxHttpHeaders());
final Async async = testContext.async();
when(response.setStatusCode(Matchers.any(Integer.class))).then(invocation -> {
try {
return response;
} finally {
async.complete();
}
});
when(serverRequest.response()).thenReturn(response);
router.accept(serverRequest);
async.await();
verify(response, times(1)).setStatusCode(404);
}
示例14: test500
import io.netty.util.AsciiString; //导入依赖的package包/类
@Test
public void test500(final TestContext testContext) throws Exception {
final Router router = Router.router(rule.vertx());
final JaxRsRouter jaxRsRouter = new JaxRsRouter();
final SpringJaxRsHandler handler = new SpringJaxRsHandler(MyApp.class);
jaxRsRouter.register(MyApp.class, router, handler, handler);
final HttpServerRequest serverRequest = mock(HttpServerRequest.class);
when(serverRequest.absoluteURI()).thenReturn("http://test.trajano.net/api/hello/cough");
when(serverRequest.path()).thenReturn("/api/hello/cough");
when(serverRequest.uri()).thenReturn("/api/hello/cough");
when(serverRequest.isEnded()).thenReturn(true);
when(serverRequest.method()).thenReturn(HttpMethod.GET);
final HttpServerResponse response = mock(HttpServerResponse.class);
when(response.putHeader(anyString(), anyString())).thenReturn(response);
when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response);
when(response.headers()).thenReturn(new VertxHttpHeaders());
final Async async = testContext.async();
when(response.write(Matchers.any(Buffer.class))).thenReturn(response);
doAnswer(i -> {
async.complete();
return null;
}).when(response).end();
when(serverRequest.response()).thenReturn(response);
router.accept(serverRequest);
async.awaitSuccess();
final ArgumentCaptor<Buffer> captor = ArgumentCaptor.forClass(Buffer.class);
verify(response, times(1)).setStatusCode(500);
verify(response, atLeastOnce()).write(captor.capture());
final String errorMessage = String.join("", captor.getAllValues().stream().map(Buffer::toString).collect(Collectors.toList()));
assertTrue(errorMessage.contains("server_error"));
}
示例15: testFailure
import io.netty.util.AsciiString; //导入依赖的package包/类
@Test
public void testFailure(final TestContext testContext) throws Exception {
final HttpServerRequest serverRequest = mock(HttpServerRequest.class);
when(serverRequest.absoluteURI()).thenThrow(new RuntimeException("boom"));
when(serverRequest.path()).thenReturn("/api/hello/400");
when(serverRequest.uri()).thenReturn("/api/hello/400");
when(serverRequest.isEnded()).thenReturn(true);
when(serverRequest.method()).thenReturn(HttpMethod.GET);
final HttpServerResponse response = mock(HttpServerResponse.class);
when(response.putHeader(anyString(), anyString())).thenReturn(response);
when(response.putHeader(any(AsciiString.class), anyString())).thenReturn(response);
when(response.headers()).thenReturn(new VertxHttpHeaders());
final Async async = testContext.async();
when(response.setStatusCode(Matchers.any(Integer.class))).then(invocation -> {
try {
return response;
} finally {
async.complete();
}
});
when(serverRequest.response()).thenReturn(response);
router.accept(serverRequest);
async.await();
verify(response, times(1)).setStatusCode(500);
}