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


Java HttpResponse.from方法代码示例

本文整理汇总了Java中com.linecorp.armeria.common.HttpResponse.from方法的典型用法代码示例。如果您正苦于以下问题:Java HttpResponse.from方法的具体用法?Java HttpResponse.from怎么用?Java HttpResponse.from使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.linecorp.armeria.common.HttpResponse的用法示例。


在下文中一共展示了HttpResponse.from方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: serve

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
 * Resolves all {@link ProjectRole}s of the current user and puts them into {@link RequestContext}
 * attributes.
 */
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {

    final User user = AuthenticationUtil.currentUser(ctx);

    final CompletionStage<Map<String, ProjectRole>> future;
    if (user instanceof UserWithToken) {
        future = mds.findRole(((UserWithToken) user).secret());
    } else {
        future = mds.findRoles(user);
    }

    return HttpResponse.from(future.thenApplyAsync(map -> {
        try {
            ctx.attr(ROLE_MAP).set(map::get);
            return delegate().serve(ctx, req);
        } catch (Exception e) {
            return Exceptions.throwUnsafely(e);
        }
    }, ctx.contextAwareEventLoop()));
}
 
开发者ID:line,项目名称:centraldogma,代码行数:26,代码来源:RoleResolvingDecorator.java

示例2: doPost

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) {
    CompletableFuture<HttpResponse> responseFuture = new CompletableFuture<>();
    HttpResponse res = HttpResponse.from(responseFuture);
    req.subscribe(new StreamConsumer(ctx.eventLoop(), slow) {
        @Override
        public void onError(Throwable cause) {
            responseFuture.complete(
                    HttpResponse.of(
                            HttpStatus.INTERNAL_SERVER_ERROR,
                            MediaType.PLAIN_TEXT_UTF_8,
                            Throwables.getStackTraceAsString(cause)));
        }

        @Override
        public void onComplete() {
            responseFuture.complete(
                    HttpResponse.of(
                            HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, "%d", numReceivedBytes()));
        }
    });
    return res;
}
 
开发者ID:line,项目名称:armeria,代码行数:24,代码来源:HttpServerTest.java

示例3: 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

示例4: doExecute

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
protected HttpResponse doExecute(ClientRequestContext ctx, HttpRequest req) throws Exception {
    final CompletableFuture<HttpResponse> responseFuture = new CompletableFuture<>();
    final HttpResponse res = HttpResponse.from(responseFuture);
    final HttpRequestDuplicator reqDuplicator = new HttpRequestDuplicator(req, 0, ctx.eventLoop());
    doExecute0(ctx, reqDuplicator, responseFuture);
    return res;
}
 
开发者ID:line,项目名称:armeria,代码行数:9,代码来源:RetryingHttpClient.java

示例5: serve

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
    return HttpResponse.from(authorize(req, ctx).handleAsync((result, t) -> {
        try {
            if (t != null || !result) {
                return onFailure(ctx, req, t);
            } else {
                return onSuccess(ctx, req);
            }
        } catch (Exception e) {
            return Exceptions.throwUnsafely(e);
        }
    }, ctx.contextAwareEventLoop()));
}
 
开发者ID:line,项目名称:armeria,代码行数:15,代码来源:HttpAuthService.java

示例6: doPost

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
protected final HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) {
    return HttpResponse.from(req.aggregate()
       .thenApply(this::echo)
       .exceptionally(CompletionActions::log));
}
 
开发者ID:line,项目名称:armeria,代码行数:7,代码来源:ZooKeeperRegistrationTest.java

示例7: serve

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
    final HttpHeaders clientHeaders = req.headers();
    final MediaType contentType = clientHeaders.contentType();
    if (contentType == null) {
        // All gRPC requests, whether framed or non-framed, must have content-type. If it's not sent, let
        // the delegate return its usual error message.
        return delegate().serve(ctx, req);
    }

    for (SerializationFormat format : GrpcSerializationFormats.values()) {
        if (format.isAccepted(contentType)) {
            // Framed request, so just delegate.
            return delegate().serve(ctx, req);
        }
    }

    String methodName = GrpcRequestUtil.determineMethod(ctx);
    MethodDescriptor<?, ?> method = methodName != null ? methodsByName.get(methodName) : null;
    if (method == null) {
        // Unknown method, let the delegate return a usual error.
        return delegate().serve(ctx, req);
    }

    if (method.getType() != MethodType.UNARY) {
        return HttpResponse.of(HttpStatus.BAD_REQUEST,
                               MediaType.PLAIN_TEXT_UTF_8,
                               "Only unary methods can be used with non-framed requests.");
    }

    HttpHeaders grpcHeaders = HttpHeaders.copyOf(clientHeaders);

    final MediaType framedContentType;
    if (contentType.is(MediaType.PROTOBUF)) {
        framedContentType = GrpcSerializationFormats.PROTO.mediaType();
    } else if (contentType.is(MediaType.JSON_UTF_8)) {
        framedContentType = GrpcSerializationFormats.JSON.mediaType();
    } else {
        return HttpResponse.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE,
                               MediaType.PLAIN_TEXT_UTF_8,
                               "Unsupported media type. Only application/protobuf is supported.");
    }
    grpcHeaders.contentType(framedContentType);

    if (grpcHeaders.get(GrpcHeaderNames.GRPC_ENCODING) != null) {
        return HttpResponse.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE,
                               MediaType.PLAIN_TEXT_UTF_8,
                               "gRPC encoding is not supported for non-framed requests.");
    }

    // All clients support no encoding, and we don't support gRPC encoding for non-framed requests, so just
    // clear the header if it's present.
    grpcHeaders.remove(GrpcHeaderNames.GRPC_ACCEPT_ENCODING);

    final CompletableFuture<HttpResponse> responseFuture = new CompletableFuture<>();
    final HttpResponse res = HttpResponse.from(responseFuture);
    req.aggregate().whenCompleteAsync(
            (clientRequest, t) -> {
                if (t != null) {
                    responseFuture.completeExceptionally(t);
                } else {
                    frameAndServe(ctx, grpcHeaders, clientRequest, responseFuture);
                }
            },
            ctx.eventLoop());
    return res;
}
 
开发者ID:line,项目名称:armeria,代码行数:68,代码来源:UnframedGrpcService.java

示例8: doPut

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
protected HttpResponse doPut(ServiceRequestContext ctx, HttpRequest req) throws Exception {
    return HttpResponse.from(
            updateHealthStatus(ctx, req).thenApply(HttpResponse::of)
                                        .exceptionally(HttpResponse::ofFailure));
}
 
开发者ID:line,项目名称:armeria,代码行数:7,代码来源:ManagedHttpHealthCheckService.java

示例9: serve

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
    return HttpResponse.from(delegate.serve(ctx, req));
}
 
开发者ID:line,项目名称:armeria,代码行数:5,代码来源:AnnotatedHttpService.java

示例10: httpResponse

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Get("/resp1")
public HttpResponse httpResponse(ServiceRequestContext ctx, HttpRequest req) {
    return HttpResponse.from(raiseExceptionImmediately());
}
 
开发者ID:line,项目名称:armeria,代码行数:5,代码来源:AnnotatedHttpServiceExceptionHandlerTest.java

示例11: asyncHttpResponse

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Get("/resp2")
@ExceptionHandler(NoExceptionHandler.class)
@ExceptionHandler(AnticipatedExceptionHandler2.class)
public HttpResponse asyncHttpResponse(ServiceRequestContext ctx, HttpRequest req) {
    return HttpResponse.from(completeExceptionallyLater(ctx));
}
 
开发者ID:line,项目名称:armeria,代码行数:7,代码来源:AnnotatedHttpServiceExceptionHandlerTest.java

示例12: bad1

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Get("/bad1")
public HttpResponse bad1(ServiceRequestContext ctx, HttpRequest req) {
    return HttpResponse.from(completeExceptionallyLater(ctx));
}
 
开发者ID:line,项目名称:armeria,代码行数:5,代码来源:AnnotatedHttpServiceExceptionHandlerTest.java

示例13: bad2

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Get("/bad2")
@ExceptionHandler(BadExceptionHandler2.class)
public HttpResponse bad2(ServiceRequestContext ctx, HttpRequest req) {
    return HttpResponse.from(completeExceptionallyLater(ctx));
}
 
开发者ID:line,项目名称:armeria,代码行数:6,代码来源:AnnotatedHttpServiceExceptionHandlerTest.java

示例14: handler3

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Get("/handler3")
public HttpResponse handler3(ServiceRequestContext ctx, HttpRequest req) {
    return HttpResponse.from(completeExceptionallyLater(ctx));
}
 
开发者ID:line,项目名称:armeria,代码行数:5,代码来源:AnnotatedHttpServiceExceptionHandlerTest.java

示例15: doPost

import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
protected final HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) {
    return HttpResponse.from(req.aggregate()
                                .thenApply(this::echo)
                                .exceptionally(CompletionActions::log));
}
 
开发者ID:line,项目名称:armeria,代码行数:7,代码来源:ServerTest.java


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