/** * Judge the turning mode. * True means turn on, False is turn off. * If not present, Not supported. * Default implementation is check content is on/off for PUT method * * @param req HttpRequest */ protected CompletionStage<Optional<Boolean>> mode(@SuppressWarnings("unused") ServiceRequestContext ctx, HttpRequest req) { return req.aggregate() .thenApply(AggregatedHttpMessage::content) .thenApply(HttpData::toStringAscii) .thenApply(content -> { switch (Ascii.toUpperCase(content)) { case "ON": return Optional.of(true); case "OFF": return Optional.of(false); default: return Optional.empty(); } }); } }
@Override protected final HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) { return HttpResponse.from(req.aggregate() .thenApply(this::echo) .exceptionally(CompletionActions::log)); }
/** * Executes the service method in different ways regarding its return type and whether the request is * required to be aggregated. If the return type of the method is not a {@link CompletionStage} or * {@link HttpResponse}, it will be executed in the blocking task executor. */ private CompletionStage<HttpResponse> serve0(ServiceRequestContext ctx, HttpRequest req) { final CompletableFuture<AggregatedHttpMessage> f = aggregationRequired(aggregationStrategy, req) ? req.aggregate() : CompletableFuture.completedFuture(null); ctx.setAdditionalResponseHeaders(defaultHttpHeaders); ctx.setAdditionalResponseTrailers(defaultHttpTrailers); switch (responseType) { case HTTP_RESPONSE: return f.thenApply( msg -> new ExceptionFilteredHttpResponse(ctx, req, (HttpResponse) invoke(ctx, req, msg), exceptionHandler)); case COMPLETION_STAGE: return f.thenCompose(msg -> toCompletionStage(invoke(ctx, req, msg))) .handle((result, cause) -> cause == null ? convertResponse(ctx, req, null, result, HttpHeaders.EMPTY_HEADERS) : exceptionHandler.handleException(ctx, req, cause)); default: return f.thenApplyAsync(msg -> convertResponse(ctx, req, null, invoke(ctx, req, msg), HttpHeaders.EMPTY_HEADERS), ctx.blockingTaskExecutor()); } }
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final SamlServiceFunction func = serviceMap.get(req.path()); if (func == null) { return HttpResponse.of(HttpStatus.BAD_REQUEST); } final CompletionStage<AggregatedHttpMessage> f; if (portConfigHolder.isDone()) { f = req.aggregate(); } else { f = portConfigHolder.future().thenCompose(unused -> req.aggregate()); } return HttpResponse.from(f.handle((msg, cause) -> { if (cause != null) { return HttpResponse.of(HttpStatus.BAD_REQUEST); } final SamlPortConfig portConfig = portConfigHolder.config().get(); if (portConfig.scheme().isTls() != ctx.sessionProtocol().isTls()) { return HttpResponse.of(HttpStatus.BAD_REQUEST); } // Use user-specified hostname if it exists. // If there's no hostname set by a user, the default virtual hostname will be used. final String defaultHostname = firstNonNull(sp.hostname(), ctx.virtualHost().defaultHostname()); return func.serve(ctx, msg, defaultHostname, portConfig); })); }
req.aggregate().handle((aReq, cause) -> { try { if (cause != null) {
req.aggregate().handle((aReq, cause) -> { if (cause != null) { logger.warn("{} Failed to aggregate a request:", ctx, cause);
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.from(CompletableFuture.supplyAsync(() -> { final AggregatedHttpMessage msg = req.aggregate().join(); final String sessionId = AuthTokenExtractors.OAUTH2.apply(msg.headers()).accessToken(); if (!WRONG_SESSION_ID.equals(sessionId)) { logoutSessionPropagator.apply(sessionId).join(); } return HttpResponse.of(HttpStatus.OK); }, ctx.blockingTaskExecutor())); } }
@Override protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.from( req.aggregate() .thenApply(msg -> AuthTokenExtractors.OAUTH2.apply(msg.headers())) .thenCompose(token -> { final String sessionId = token.accessToken(); return executor.execute(Command.removeSession(sessionId)); }) .thenApply(unused -> HttpResponse.of(HttpStatus.OK)) .exceptionally(cause -> HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, cause))); } }
@Override protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.from( req.aggregate() .thenApply(msg -> AuthTokenExtractors.OAUTH2.apply(msg.headers())) .thenCompose(token -> { final String sessionId = token.accessToken(); return executor.execute(Command.removeSession(sessionId)); }) .thenApply(unused -> HttpResponse.of(HttpStatus.OK)) .exceptionally(cause -> HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, cause))); } }
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.from(CompletableFuture.supplyAsync(() -> { final AggregatedHttpMessage msg = req.aggregate().join(); final String username; final String password;
@Override protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) throws Exception { final CompletableFuture<HttpResponse> future = new CompletableFuture<>(); req.aggregate().thenAccept(aMsg -> { final OAuth2Token token = AuthTokenExtractors.OAUTH2.apply(aMsg.headers()); if (token == null) {
@Override public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { final SamlServiceFunction func = serviceMap.get(req.path()); if (func == null) { return HttpResponse.of(HttpStatus.BAD_REQUEST); } final CompletionStage<AggregatedHttpMessage> f; if (portConfigHolder.isDone()) { f = req.aggregate(); } else { f = portConfigHolder.future().thenCompose(unused -> req.aggregate()); } return HttpResponse.from(f.handle((msg, cause) -> { if (cause != null) { return HttpResponse.of(HttpStatus.BAD_REQUEST); } final SamlPortConfig portConfig = portConfigHolder.config().get(); if (portConfig.scheme().isTls() != ctx.sessionProtocol().isTls()) { return HttpResponse.of(HttpStatus.BAD_REQUEST); } // Use user-specified hostname if it exists. // If there's no hostname set by a user, the default virtual hostname will be used. final String defaultHostname = firstNonNull(sp.hostname(), ctx.virtualHost().defaultHostname()); return func.serve(ctx, msg, defaultHostname, portConfig); })); }
@Override protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) throws Exception { final CompletableFuture<HttpResponse> future = new CompletableFuture<>(); req.aggregate().thenAccept(aMsg -> { final UsernamePasswordToken usernamePassword; try {
@Override protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.from( req.aggregate() .thenApply(msg -> usernamePassword(ctx, msg)) .thenComposeAsync(usernamePassword -> {
@Override protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) throws Exception { return HttpResponse.from( req.aggregate().thenApply(msg -> AuthTokenExtractors.OAUTH2.apply(msg.headers())) .thenApplyAsync(token -> { if (token == null) {