本文整理汇总了Java中com.linecorp.armeria.common.HttpResponse.of方法的典型用法代码示例。如果您正苦于以下问题:Java HttpResponse.of方法的具体用法?Java HttpResponse.of怎么用?Java HttpResponse.of使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.linecorp.armeria.common.HttpResponse
的用法示例。
在下文中一共展示了HttpResponse.of方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convertResponse
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的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: convertResponse
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的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);
}
}
示例3: handleException
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的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();
}
示例4: usersMe
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* GET /users/me
* Returns a login {@link User} if the user is authorized. Otherwise, {@code 401 Unauthorized} HTTP
* response is sent.
*/
@Get("/users/me")
public HttpResponse usersMe() throws Exception {
final User user = AuthenticationUtil.currentUser();
return HttpResponse.of(HttpStatus.OK, MediaType.JSON_UTF_8,
HttpData.of(Jackson.writeValueAsBytes(user)));
}
示例5: convertResponse
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, Object resObj) throws Exception {
try {
final HttpRequest request = RequestContext.current().request();
final HttpData httpData =
resObj.getClass() == Object.class ? EMPTY_RESULT
: HttpData.of(Jackson.writeValueAsBytes(resObj));
return HttpResponse.of(HttpMethod.POST == request.method() ? HttpStatus.CREATED
: HttpStatus.OK,
MediaType.JSON_UTF_8,
httpData);
} catch (JsonProcessingException e) {
return HttpResponse.of(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
示例6: handleWatchFailure
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
private Object handleWatchFailure(Throwable thrown) {
if (Throwables.getRootCause(thrown) instanceof CancellationException &&
!watchService.isServerStopping()) {
// timeout happens
return HttpResponse.of(HttpStatus.NOT_MODIFIED);
}
return Exceptions.throwUnsafely(thrown);
}
示例7: newResponseWithErrorMessage
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Returns a newly created {@link HttpResponse} with the specified {@link HttpStatus} and {@code message}.
*/
static HttpResponse newResponseWithErrorMessage(HttpStatus status, String message) {
// TODO(minwoox) refine the error message
final ObjectNode content = JsonNodeFactory.instance.objectNode().put("message", message);
try {
return HttpResponse.of(status, MediaType.JSON_UTF_8, Jackson.writeValueAsBytes(content));
} catch (JsonProcessingException e) {
// should not reach here
throw new Error(e);
}
}
示例8: doPost
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) {
final SerializationFormat serializationFormat = determineSerializationFormat(req);
if (serializationFormat == null) {
return HttpResponse.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE,
MediaType.PLAIN_TEXT_UTF_8, PROTOCOL_NOT_SUPPORTED);
}
if (!validateAcceptHeaders(req, serializationFormat)) {
return HttpResponse.of(HttpStatus.NOT_ACCEPTABLE,
MediaType.PLAIN_TEXT_UTF_8, ACCEPT_THRIFT_PROTOCOL_MUST_MATCH_CONTENT_TYPE);
}
CompletableFuture<HttpResponse> responseFuture = new CompletableFuture<>();
HttpResponse res = HttpResponse.from(responseFuture);
ctx.logBuilder().serializationFormat(serializationFormat);
ctx.logBuilder().deferRequestContent();
req.aggregate().handle(voidFunction((aReq, cause) -> {
if (cause != null) {
responseFuture.complete(
HttpResponse.of(
HttpStatus.INTERNAL_SERVER_ERROR,
MediaType.PLAIN_TEXT_UTF_8, Throwables.getStackTraceAsString(cause)));
return;
}
decodeAndInvoke(ctx, aReq, serializationFormat, responseFuture);
})).exceptionally(CompletionActions::log);
return res;
}
示例9: testRemoteInvocation
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
private static void testRemoteInvocation(Tracing tracing, String remoteServiceName)
throws Exception {
// prepare parameters
final HttpRequest req = HttpRequest.of(HttpMethod.POST, "/hello/armeria");
final RpcRequest rpcReq = RpcRequest.of(HelloService.Iface.class, "hello", "Armeria");
final HttpResponse res = HttpResponse.of(HttpStatus.OK);
final RpcResponse rpcRes = RpcResponse.of("Hello, Armeria!");
final ClientRequestContext ctx = new DefaultClientRequestContext(
new DefaultEventLoop(), NoopMeterRegistry.get(), H2C, Endpoint.of("localhost", 8080),
HttpMethod.POST, "/hello/armeria", null, null, ClientOptions.DEFAULT, req);
ctx.logBuilder().startRequest(mock(Channel.class), H2C, "localhost");
ctx.logBuilder().requestContent(rpcReq, req);
ctx.logBuilder().endRequest();
@SuppressWarnings("unchecked")
Client<HttpRequest, HttpResponse> delegate = mock(Client.class);
when(delegate.execute(any(), any())).thenReturn(res);
HttpTracingClient stub = new HttpTracingClient(delegate, tracing, remoteServiceName);
// do invoke
HttpResponse actualRes = stub.execute(ctx, req);
assertThat(actualRes).isEqualTo(res);
verify(delegate, times(1)).execute(ctx, req);
ctx.logBuilder().responseHeaders(HttpHeaders.of(HttpStatus.OK));
ctx.logBuilder().responseContent(rpcRes, res);
ctx.logBuilder().endResponse();
}
示例10: serve
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse serve(Service<HttpRequest, HttpResponse> delegate,
ServiceRequestContext ctx,
HttpRequest req) throws Exception {
validateContextAndRequest(ctx, req);
return HttpResponse.of(HttpStatus.LOCKED);
}
示例11: doGet
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req)
throws Exception {
ByteBuf buf = ctx.alloc().buffer();
buf.writeCharSequence("pooled content", StandardCharsets.UTF_8);
releasedByteBuf.set(buf);
return HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, new ByteBufHttpData(buf, false));
}
示例12: convertResponse
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse convertResponse(ServiceRequestContext ctx, Object result) throws Exception {
if (result instanceof String && "hello foo".equals(result)) {
assertThat(responseCounter.getAndIncrement()).isEqualTo(2);
return HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, HttpData.ofUtf8(
(String) result));
}
return ResponseConverterFunction.fallthrough();
}
示例13: doGet
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) throws Exception {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try (OutputStreamWriter writer = new OutputStreamWriter(stream)) {
TextFormat.write004(writer, collectorRegistry.metricFamilySamples());
}
return HttpResponse.of(HttpStatus.OK, CONTENT_TYPE_004, stream.toByteArray());
}
示例14: requestConverterOrder
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Post("/requestConverterOrder")
@RequestConverter(MethodLevelRequestConverter.class)
public HttpResponse requestConverterOrder(
@RequestObject(ParameterLevelRequestConverter.class) JsonNode node) {
assertThat(node).isNotNull();
return HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, HttpData.ofUtf8(node.toString()));
}
示例15: handleCorsPreflight
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Handles CORS preflight by setting the appropriate headers.
*
* @param req the decoded HTTP request
*/
private HttpResponse handleCorsPreflight(HttpRequest req) {
HttpHeaders headers = HttpHeaders.of(HttpStatus.OK);
if (setCorsOrigin(req, headers)) {
setCorsAllowMethods(headers);
setCorsAllowHeaders(headers);
setCorsAllowCredentials(headers);
setCorsMaxAge(headers);
setPreflightHeaders(headers);
}
return HttpResponse.of(headers);
}