本文整理汇总了Java中com.linecorp.armeria.common.HttpHeaderNames类的典型用法代码示例。如果您正苦于以下问题:Java HttpHeaderNames类的具体用法?Java HttpHeaderNames怎么用?Java HttpHeaderNames使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpHeaderNames类属于com.linecorp.armeria.common包,在下文中一共展示了HttpHeaderNames类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertResponse
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, Object resObj) throws Exception {
try {
final JsonNode jsonNode = Jackson.valueToTree(resObj);
final String url = jsonNode.get("url").asText();
// Remove the url field and send it with the LOCATION header.
((ObjectNode) jsonNode).remove("url");
final HttpHeaders headers = HttpHeaders.of(HttpStatus.CREATED)
.add(HttpHeaderNames.LOCATION, url)
.contentType(MediaType.JSON_UTF_8);
return HttpResponse.of(headers, HttpData.of(Jackson.writeValueAsBytes(jsonNode)));
} catch (JsonProcessingException e) {
logger.debug("Failed to convert a response:", e);
return HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
示例2: serve
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
final String authorization = req.headers().get(HttpHeaderNames.AUTHORIZATION);
if (authorization == null || !PATTERN.matcher(authorization).matches()) {
final InetSocketAddress raddr = ctx.remoteAddress();
final String ip = raddr.getAddress().getHostAddress();
final Instant now = Instant.now(clock);
final Instant lastReport = reportedAddresses.putIfAbsent(ip, now);
final boolean report;
if (lastReport == null) {
report = true;
} else if (ChronoUnit.DAYS.between(lastReport, now) >= 1) {
report = reportedAddresses.replace(ip, lastReport, now);
} else {
report = false;
}
if (report) {
report(raddr.getHostString(), ip);
}
}
return delegate().serve(ctx, req);
}
示例3: init
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
@Before
public void init() {
final InetSocketAddress serverAddress = dogma.dogma().activePort().get().localAddress();
final String serverUri = "http://" + serverAddress.getHostString() + ':' + serverAddress.getPort();
httpClient = new HttpClientBuilder(serverUri)
.addHttpHeader(HttpHeaderNames.AUTHORIZATION, "bearer anonymous").build();
// the default project used for unit tests
HttpHeaders headers = HttpHeaders.of(HttpMethod.POST, "/api/v1/projects").contentType(MediaType.JSON);
String body = "{\"name\": \"myPro\"}";
httpClient.execute(headers, body).aggregate().join();
// the default repository used for unit tests
headers = HttpHeaders.of(HttpMethod.POST, "/api/v1/projects/myPro/repos").contentType(MediaType.JSON);
body = "{\"name\": \"myRepo\"}";
httpClient.execute(headers, body).aggregate().join();
// default files used for unit tests
addFooFile();
}
示例4: unremoveRepository
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
@Test
public void unremoveRepository() {
createRepository("foo");
httpClient.delete(REPOS_PREFIX + "/foo").aggregate().join();
final HttpHeaders reqHeaders = HttpHeaders.of(HttpMethod.PATCH, REPOS_PREFIX + "/foo")
.add(HttpHeaderNames.CONTENT_TYPE,
"application/json-patch+json");
final String unremovePatch = "[{\"op\":\"replace\",\"path\":\"/status\",\"value\":\"active\"}]";
final AggregatedHttpMessage aRes = httpClient.execute(reqHeaders, unremovePatch).aggregate().join();
final HttpHeaders headers = aRes.headers();
assertThat(headers.status()).isEqualTo(HttpStatus.OK);
final String expectedJson =
'{' +
" \"name\": \"foo\"," +
" \"creator\": {" +
" \"name\": \"System\"," +
" \"email\": \"[email protected]\"" +
" }," +
" \"headRevision\": 1," +
" \"url\": \"/api/v1/projects/myPro/repos/foo\"," +
" \"createdAt\": \"${json-unit.ignore}\"" +
'}';
final String actualJson = aRes.content().toStringUtf8();
assertThatJson(actualJson).isEqualTo(expectedJson);
}
示例5: init
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
@Before
public void init() {
final InetSocketAddress serverAddress = dogma.dogma().activePort().get().localAddress();
final String serverUri = "http://" + serverAddress.getHostString() + ':' + serverAddress.getPort();
httpClient = new HttpClientBuilder(serverUri)
.addHttpHeader(HttpHeaderNames.AUTHORIZATION, "bearer anonymous").build();
// the default project used for unit tests
// the default project used for unit tests
HttpHeaders headers = HttpHeaders.of(HttpMethod.POST, "/api/v1/projects").contentType(MediaType.JSON);
String body = "{\"name\": \"myPro\"}";
httpClient.execute(headers, body).aggregate().join();
// the default repository used for unit tests
headers = HttpHeaders.of(HttpMethod.POST, "/api/v1/projects/myPro/repos").contentType(MediaType.JSON);
body = "{\"name\": \"myRepo\"}";
httpClient.execute(headers, body).aggregate().join();
// default files used for unit tests
}
示例6: yummlyApi
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
@Provides
@Singleton
static YummlyApi yummlyApi(YummlyConfig config) {
return new ArmeriaRetrofitBuilder()
.baseUrl("http://api.yummly.com/v1/api/")
.addCallAdapterFactory(GuavaCallAdapterFactory.create())
.addConverterFactory(JacksonConverterFactory.create(OBJECT_MAPPER))
.withClientOptions(
(unused, options) ->
options
.addHttpHeader(HttpHeaderNames.of("X-Yummly-App-ID"), config.getApiId())
.addHttpHeader(HttpHeaderNames.of("X-Yummly-App-Key"), config.getApiKey())
.decorator(HttpRequest.class, HttpResponse.class, LoggingClient.newDecorator()))
.build()
.create(YummlyApi.class);
}
示例7: statusToTrailers
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
static HttpHeaders statusToTrailers(Status status, boolean headersSent) {
final HttpHeaders trailers;
if (headersSent) {
// Normal trailers.
trailers = new DefaultHttpHeaders();
} else {
// Trailers only response
trailers = new DefaultHttpHeaders(true, 3, true)
.status(HttpStatus.OK)
.set(HttpHeaderNames.CONTENT_TYPE, "application/grpc+proto");
}
trailers.add(GrpcHeaderNames.GRPC_STATUS, Integer.toString(status.getCode().value()));
if (status.getDescription() != null) {
trailers.add(GrpcHeaderNames.GRPC_MESSAGE,
StatusMessageEscaper.escape(status.getDescription()));
}
return trailers;
}
示例8: grpcWeb
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
@Test
public void grpcWeb() throws Exception {
HttpClient client = HttpClient.of(server.httpUri("/"));
AggregatedHttpMessage response = client.execute(
HttpHeaders.of(HttpMethod.POST,
UnitTestServiceGrpc.METHOD_STATIC_UNARY_CALL.getFullMethodName())
.set(HttpHeaderNames.CONTENT_TYPE, "application/grpc-web"),
GrpcTestUtil.uncompressedFrame(GrpcTestUtil.requestByteBuf())).aggregate().get();
byte[] serializedStatusHeader = "grpc-status: 0\r\n".getBytes(StandardCharsets.US_ASCII);
byte[] serializedTrailers = Bytes.concat(
new byte[] { ArmeriaServerCall.TRAILERS_FRAME_HEADER },
Ints.toByteArray(serializedStatusHeader.length),
serializedStatusHeader);
assertThat(response.content().array()).containsExactly(
Bytes.concat(
GrpcTestUtil.uncompressedFrame(
GrpcTestUtil.protoByteBuf(RESPONSE_MESSAGE)),
serializedTrailers));
}
示例9: json
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
@Test
public void json() throws Exception {
AtomicReference<HttpHeaders> requestHeaders = new AtomicReference<>();
UnitTestServiceBlockingStub jsonStub =
new ClientBuilder(server.httpUri(GrpcSerializationFormats.JSON, "/"))
.decorator(HttpRequest.class, HttpResponse.class,
client -> new SimpleDecoratingClient<HttpRequest, HttpResponse>(client) {
@Override
public HttpResponse execute(ClientRequestContext ctx, HttpRequest req)
throws Exception {
requestHeaders.set(req.headers());
return delegate().execute(ctx, req);
}
})
.build(UnitTestServiceBlockingStub.class);
SimpleResponse response = jsonStub.staticUnaryCall(REQUEST_MESSAGE);
assertThat(response).isEqualTo(RESPONSE_MESSAGE);
assertThat(requestHeaders.get().get(HttpHeaderNames.CONTENT_TYPE)).isEqualTo("application/grpc+json");
}
示例10: missingMethod
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的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[] {})));
}
示例11: toArmeria
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的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;
}
示例12: toHttp2HeadersFilterTE
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的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;
}
}
}
}
示例13: toNettyHttp2
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
/**
* Converts the specified Armeria HTTP/2 headers into Netty HTTP/2 headers.
*/
public static Http2Headers toNettyHttp2(HttpHeaders in) {
final Http2Headers out = new DefaultHttp2Headers(false, in.size());
out.set(in);
out.remove(HttpHeaderNames.CONNECTION);
out.remove(HttpHeaderNames.TRANSFER_ENCODING);
out.remove(HttpHeaderNames.TRAILER);
if (!out.contains(HttpHeaderNames.COOKIE)) {
return out;
}
// Split up cookies to allow for better compression.
// https://tools.ietf.org/html/rfc7540#section-8.1.2.5
final List<CharSequence> cookies = out.getAllAndRemove(HttpHeaderNames.COOKIE);
for (CharSequence c : cookies) {
out.add(HttpHeaderNames.COOKIE, COOKIE_SPLITTER.split(c));
}
return out;
}
示例14: testUserAgentOverridableByRequestHeader
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
@Test
public void testUserAgentOverridableByRequestHeader() throws Exception {
HttpHeaders headers = HttpHeaders.of(HttpHeaderNames.USER_AGENT, TEST_USER_AGENT_NAME);
ClientOptions options = ClientOptions.of(ClientOption.HTTP_HEADERS.newValue(headers));
HttpClient client = HttpClient.of(server.uri("/"), options);
final String OVERIDDEN_USER_AGENT_NAME = "Overridden";
AggregatedHttpMessage response =
client.execute(HttpHeaders.of(HttpMethod.GET, "/useragent")
.add(HttpHeaderNames.USER_AGENT, OVERIDDEN_USER_AGENT_NAME))
.aggregate().get();
assertEquals(OVERIDDEN_USER_AGENT_NAME, response.content().toStringUtf8());
}
示例15: testStrings_acceptEncodingGzip
import com.linecorp.armeria.common.HttpHeaderNames; //导入依赖的package包/类
@Test(timeout = 10000)
public void testStrings_acceptEncodingGzip() throws Exception {
final HttpHeaders req = HttpHeaders.of(HttpMethod.GET, "/strings")
.set(HttpHeaderNames.ACCEPT_ENCODING, "gzip");
final CompletableFuture<AggregatedHttpMessage> f = client().execute(req).aggregate();
final AggregatedHttpMessage res = f.get();
assertThat(res.status(), is(HttpStatus.OK));
assertThat(res.headers().get(HttpHeaderNames.CONTENT_ENCODING), is("gzip"));
assertThat(res.headers().get(HttpHeaderNames.VARY), is("accept-encoding"));
byte[] decoded;
try (GZIPInputStream unzipper = new GZIPInputStream(new ByteArrayInputStream(res.content().array()))) {
decoded = ByteStreams.toByteArray(unzipper);
} catch (EOFException e) {
throw new IllegalArgumentException(e);
}
assertThat(new String(decoded, StandardCharsets.UTF_8), is("Armeria is awesome!"));
}