当前位置: 首页>>代码示例>>Java>>正文


Java HttpResponse.of方法代码示例

本文整理汇总了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);
    }
}
 
开发者ID:line,项目名称:centraldogma,代码行数:19,代码来源:CreateApiResponseConverter.java

示例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);
    }
}
 
开发者ID:line,项目名称:centraldogma,代码行数:17,代码来源:HttpApiResponseConverter.java

示例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();
}
 
开发者ID:line,项目名称:centraldogma,代码行数:21,代码来源:HttpApiExceptionHandler.java

示例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)));
}
 
开发者ID:line,项目名称:centraldogma,代码行数:12,代码来源:UserService.java

示例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);
    }
}
 
开发者ID:line,项目名称:centraldogma,代码行数:16,代码来源:RestfulJsonResponseConverter.java

示例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);
}
 
开发者ID:line,项目名称:centraldogma,代码行数:10,代码来源:ContentServiceV1.java

示例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);
    }
}
 
开发者ID:line,项目名称:centraldogma,代码行数:14,代码来源:HttpApiUtil.java

示例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;
}
 
开发者ID:line,项目名称:armeria,代码行数:32,代码来源:THttpService.java

示例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();
}
 
开发者ID:line,项目名称:armeria,代码行数:34,代码来源:HttpTracingClientTest.java

示例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);
}
 
开发者ID:line,项目名称:armeria,代码行数:8,代码来源:AnnotatedHttpServiceDecorationTest.java

示例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));
}
 
开发者ID:line,项目名称:armeria,代码行数:9,代码来源:HttpClientIntegrationTest.java

示例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();
}
 
开发者ID:line,项目名称:armeria,代码行数:10,代码来源:AnnotatedHttpServiceHandlersOrderTest.java

示例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());
}
 
开发者ID:line,项目名称:armeria,代码行数:9,代码来源:PrometheusExpositionService.java

示例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()));
}
 
开发者ID:line,项目名称:armeria,代码行数:8,代码来源:AnnotatedHttpServiceHandlersOrderTest.java

示例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);
}
 
开发者ID:line,项目名称:armeria,代码行数:18,代码来源:CorsService.java


注:本文中的com.linecorp.armeria.common.HttpResponse.of方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。