本文整理匯總了Java中com.linecorp.armeria.common.HttpStatus類的典型用法代碼示例。如果您正苦於以下問題:Java HttpStatus類的具體用法?Java HttpStatus怎麽用?Java HttpStatus使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
HttpStatus類屬於com.linecorp.armeria.common包,在下文中一共展示了HttpStatus類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createToken0
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
private CompletionStage<Token> createToken0(Revision revision, Token newToken) {
return getTokens(revision).thenCompose(entries -> {
final boolean exist = entries.values().stream().anyMatch(
token -> token.appId().equals(newToken.appId()));
if (exist) {
// TODO(hyangtack) Would send 409 conflict when the following PR is merged.
// https://github.com/line/armeria/pull/746
throw HttpStatusException.of(HttpStatus.CONFLICT);
}
entries.put(newToken.secret(), newToken);
final User user = AuthenticationUtil.currentUser();
final String summary = user.name() + " generates a token: " + newToken.appId();
return updateTokens(revision, entries, summary).thenApply(unused -> newToken);
});
}
示例2: serve
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
@Override
public HttpResponse serve(Service<HttpRequest, HttpResponse> delegate,
ServiceRequestContext ctx,
HttpRequest req) throws Exception {
final String projectName = ctx.pathParam("projectName");
checkArgument(!isNullOrEmpty(projectName),
"No project name is specified");
final Function<String, ProjectRole> map = ctx.attr(RoleResolvingDecorator.ROLE_MAP).get();
final ProjectRole projectRole = map != null ? map.apply(projectName) : null;
final User user = AuthenticationUtil.currentUser(ctx);
if (!isAllowedRole(user, projectRole)) {
throw HttpStatusException.of(HttpStatus.UNAUTHORIZED);
}
return delegate.serve(ctx, req);
}
示例3: convertResponse
import com.linecorp.armeria.common.HttpStatus; //導入依賴的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);
}
}
示例4: convertResponse
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, Object resObj) throws Exception {
try {
final HttpRequest request = RequestContext.current().request();
if (HttpMethod.DELETE == request.method() ||
(resObj instanceof Iterable && Iterables.size((Iterable) resObj) == 0)) {
return HttpResponse.of(HttpStatus.NO_CONTENT);
}
final HttpData httpData = HttpData.of(Jackson.writeValueAsBytes(resObj));
return HttpResponse.of(HttpStatus.OK, MediaType.JSON_UTF_8, httpData);
} catch (JsonProcessingException e) {
logger.debug("Failed to convert a response:", e);
return HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
示例5: handleException
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
@Override
public HttpResponse handleException(RequestContext ctx, HttpRequest req, Throwable cause) {
if (cause instanceof IllegalArgumentException) {
if (cause.getMessage() != null) {
return newResponseWithErrorMessage(HttpStatus.BAD_REQUEST, cause.getMessage());
}
return HttpResponse.of(HttpStatus.BAD_REQUEST);
}
if (cause instanceof StorageException) {
// Use precomputed map if the cause is instance of StorageException to access in a faster way.
final ExceptionHandlerFunction func = storageExceptionHandlers.get(cause.getClass());
if (func != null) {
return func.handleException(ctx, req, cause);
}
}
return ExceptionHandlerFunction.fallthrough();
}
示例6: unremoveRepository
import com.linecorp.armeria.common.HttpStatus; //導入依賴的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);
}
示例7: unremoveProject
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
@Test
public void unremoveProject() throws IOException {
createProject("bar");
final String projectPath = PROJECTS_PREFIX + "/bar";
httpClient.delete(projectPath).aggregate().join();
final HttpHeaders headers = HttpHeaders.of(HttpMethod.PATCH, projectPath)
.contentType(MediaType.JSON_PATCH);
final String unremovePatch = "[{\"op\":\"replace\",\"path\":\"/status\",\"value\":\"active\"}]";
final AggregatedHttpMessage aRes = httpClient.execute(headers, unremovePatch).aggregate().join();
System.err.println(aRes.content().toStringUtf8());
assertThat(aRes.headers().status()).isEqualTo(HttpStatus.OK);
final String expectedJson =
'{' +
" \"name\": \"bar\"," +
" \"creator\": {" +
" \"name\": \"System\"," +
" \"email\": \"[email protected]\"" +
" }," +
" \"url\": \"/api/v1/projects/bar\"," +
" \"createdAt\": \"${json-unit.ignore}\"" +
'}';
final String actualJson = aRes.content().toStringUtf8();
assertThatJson(actualJson).isEqualTo(expectedJson);
}
示例8: deleteFile
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
@Test
public void deleteFile() throws IOException {
addFooJson();
addBarTxt();
final String body =
'{' +
" \"path\": \"/foo.json\"," +
" \"type\": \"REMOVE\"," +
" \"commitMessage\" : {" +
" \"summary\" : \"Delete foo.json\"" +
" }" +
'}';
final HttpHeaders headers = HttpHeaders.of(HttpMethod.POST, CONTENTS_PREFIX)
.contentType(MediaType.JSON);
final AggregatedHttpMessage res1 = httpClient.execute(headers, body).aggregate().join();
assertThat(res1.headers().status()).isEqualTo(HttpStatus.NO_CONTENT);
final AggregatedHttpMessage res2 = httpClient.get(CONTENTS_PREFIX + "/**").aggregate().join();
// only /a/bar.txt file is left
assertThat(Jackson.readTree(res2.content().toStringUtf8()).size()).isOne();
}
示例9: deleteFileInvalidRevision
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
@Test
public void deleteFileInvalidRevision() {
addFooJson();
addBarTxt();
final String body =
'{' +
" \"path\": \"/foo.json\"," +
" \"type\": \"REMOVE\"," +
" \"commitMessage\" : {" +
" \"summary\" : \"Delete foo.json\"" +
" }" +
'}';
final HttpHeaders headers = HttpHeaders.of(HttpMethod.POST, CONTENTS_PREFIX + "?revision=2")
.contentType(MediaType.JSON);
final AggregatedHttpMessage res = httpClient.execute(headers, body).aggregate().join();
assertThat(res.headers().status()).isEqualTo(HttpStatus.CONFLICT);
}
示例10: testThriftServiceRegistrationBean
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
@Test
public void testThriftServiceRegistrationBean() throws Exception {
HelloService.Iface client = Clients.newClient(newUrl("tbinary+h1c") + "/thrift",
HelloService.Iface.class);
assertThat(client.hello("world")).isEqualTo("hello world");
HttpClient httpClient = HttpClient.of(newUrl("h1c"));
HttpResponse response = httpClient.get("/internal/docs/specification.json");
AggregatedHttpMessage msg = response.aggregate().get();
assertThat(msg.status()).isEqualTo(HttpStatus.OK);
assertThatJson(msg.content().toStringUtf8()).matches(
jsonPartMatches("services[0].exampleHttpHeaders[0].x-additional-header",
is("headerVal")));
}
示例11: statusToTrailers
import com.linecorp.armeria.common.HttpStatus; //導入依賴的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;
}
示例12: testStrings_acceptEncodingGzip_largeFixedContent
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
@Test(timeout = 10000)
public void testStrings_acceptEncodingGzip_largeFixedContent() throws Exception {
final HttpHeaders req = HttpHeaders.of(HttpMethod.GET, "/large")
.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);
}
assertThat(new String(decoded, StandardCharsets.UTF_8), is(Strings.repeat("a", 1024)));
}
示例13: missingMethod
import com.linecorp.armeria.common.HttpStatus; //導入依賴的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[] {})));
}
示例14: write
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
@Override
public boolean write(HttpObject o) {
if (o instanceof HttpHeaders) {
// NB: It's safe to call logBuilder.start() multiple times.
// See AbstractMessageLog.start() for more information.
logBuilder.startResponse();
final HttpHeaders headers = (HttpHeaders) o;
final HttpStatus status = headers.status();
if (status != null && status.codeClass() != HttpStatusClass.INFORMATIONAL) {
logBuilder.responseHeaders(headers);
}
} else if (o instanceof HttpData) {
logBuilder.increaseResponseLength(((HttpData) o).length());
}
return delegate.write(o);
}
示例15: testHeaders
import com.linecorp.armeria.common.HttpStatus; //導入依賴的package包/類
@Test(timeout = 10000)
public void testHeaders() throws Exception {
final HttpHeaders req = HttpHeaders.of(HttpMethod.GET, "/headers");
final CompletableFuture<AggregatedHttpMessage> f = client().execute(req).aggregate();
final AggregatedHttpMessage res = f.get();
assertThat(res.status(), is(HttpStatus.OK));
// Verify all header names are in lowercase
for (AsciiString headerName : res.headers().names()) {
headerName.chars().filter(Character::isAlphabetic)
.forEach(c -> assertTrue(Character.isLowerCase(c)));
}
assertThat(res.headers().get(AsciiString.of("x-custom-header1")), is("custom1"));
assertThat(res.headers().get(AsciiString.of("x-custom-header2")), is("custom2"));
assertThat(res.content().toStringUtf8(), is("headers"));
}