/** * Get the current executor associated with the current HTTP context. * * Note that the returned executor is only valid for the current context. It should be used in a transient * fashion, long lived references to it should not be kept. * * @return An executor that will execute its tasks in the current HTTP context. */ public Executor current() { return HttpExecution.fromThread(delegate); } }
protected CompletionStage<Result> asyncBadRequest(final CompletionStage<Html> htmlCompletionStage) { return htmlCompletionStage.thenApplyAsync(Results::badRequest, HttpExecution.defaultContext()); }
protected CompletionStage<Result> asyncOk(final CompletionStage<Html> htmlCompletionStage) { return htmlCompletionStage.thenApplyAsync(Results::ok, HttpExecution.defaultContext()); }
protected CompletionStage<Result> asyncInternalServerError(final CompletionStage<Html> htmlCompletionStage) { return htmlCompletionStage.thenApplyAsync(Results::internalServerError, HttpExecution.defaultContext()); }
/** * Get the current executor associated with the current HTTP context. * * Note that the returned executor is only valid for the current context. It should be used in a transient * fashion, long lived references to it should not be kept. * * @return An executor that will execute its tasks in the current HTTP context. */ public Executor current() { return HttpExecution.fromThread(delegate); } }
@Override public <T extends Hook, R> CompletionStage<R> runActionHook(final Class<T> hookClass, final BiFunction<T, R, CompletionStage<R>> f, final R param) { hookRunnerLogger.debug("runActionHook {}", hookClass.getSimpleName()); CompletionStage<R> result = successful(param); final List<T> applicableHooks = controllerComponents.stream() .filter(x -> hookClass.isAssignableFrom(x.getClass())) .map(x -> (T) x) .collect(Collectors.toList()); for (final T hook : applicableHooks) { result = result.thenComposeAsync(res -> f.apply(hook, res), HttpExecution.defaultContext()); } return result; }
/** * Get the current executor associated with the current HTTP context. * * Note that the returned executor is only valid for the current context. It should be used in a transient * fashion, long lived references to it should not be kept. * * @return An executor that will execute its tasks in the current HTTP context. */ public Executor current() { return HttpExecution.fromThread(delegate); } }
protected final CompletionStage<Cart> getOrCreateCart() { return findCart() .thenComposeAsync(cartOptional -> cartOptional .map(cart -> (CompletionStage<Cart>) completedFuture(cart)) .orElseGet(this::createCart), defaultContext()); } }
/** * Searches for an existing cart the platform otherwise the stage contains a {@link CartNotFoundException}. * A cart will not be created if it does not exist. * @return stage with the cart in session, or {@link CartNotFoundException} */ protected CompletionStage<Cart> requireExistingCart() { return findCart() .thenApplyAsync(cartOptional -> cartOptional .orElseThrow(CartNotFoundException::new), defaultContext()); }
private CompletionStage<Optional<Cart>> fetchCart() { return buildQuery() .map(query -> CartQueryHook.runHook(hookContext, query)) .map(query -> sphereClient.execute(query) .thenApplyAsync(PagedResult::head, HttpExecution.defaultContext())) .orElseGet(() -> completedFuture(Optional.empty())); }
@Override public CompletionStage<?> onCustomerSignInResultLoaded(final CustomerSignInResult customerSignInResult) { return Optional.ofNullable(customerSignInResult.getCart()) .map(cart -> updateCartWithMissingInfoOnSignIn(cart, customerSignInResult.getCustomer())) .orElseGet(() -> completedFuture(null)) .thenAcceptAsync(this::overwriteCartInSession, HttpExecution.defaultContext()); }
default CompletionStage<Result> handleInvalidForm(final Form<? extends F> form, final T context) { return renderPage(form, context, null) .thenApplyAsync(Results::badRequest, HttpExecution.defaultContext()); }
@Override public CompletionStage<Optional<Customer>> findCustomer(final Void unused) { final CompletionStage<Optional<Customer>> customerStage = fetchCustomer(); customerStage.thenAcceptAsync(customerOpt -> customerOpt.ifPresent(customer -> CustomerLoadedHook.runHook(hookContext, customer)), HttpExecution.defaultContext()); return customerStage; }
@Override public CompletionStage<Optional<Cart>> findCart(final Void unused) { final CompletionStage<Optional<Cart>> cartStage = fetchCart(); cartStage.thenAcceptAsync(cartOpt -> cartOpt.ifPresent(cart -> CartLoadedHook.runHook(hookContext, cart)), HttpExecution.defaultContext()); return cartStage; }
@SunriseRoute("processChangeLineItemQuantityForm") public CompletionStage<Result> changeLineItemQuantity(final String languageTag) { return doRequest(() -> findCart() .thenComposeAsync(cartOptional -> cartOptional .map(this::validateForm) .orElseGet(this::redirectToCartDetail), HttpExecution.defaultContext())); }
@SunriseRoute("checkoutThankYouPageCall") public CompletionStage<Result> show(final String languageTag) { return doRequest(() -> findLastOrder(). thenComposeAsync(orderOpt -> orderOpt .map(this::handleFoundOrder) .orElseGet(this::handleNotFoundOrder), defaultContext())); }
@SunriseRoute("processDeleteLineItemForm") public CompletionStage<Result> removeLineItem(final String languageTag) { return doRequest(() -> { logger.debug("process remove line item form in locale={}", languageTag); return doRequest(() -> findCart() .thenComposeAsync(cartOptional -> cartOptional .map(this::validateForm) .orElseGet(this::redirectToCartDetail), HttpExecution.defaultContext())); }); }
@Override public CompletionStage<Html> renderPage(final Form<? extends CheckoutShippingFormData> form, final Cart cart, @Nullable final Cart updatedCart) { return getShippingMethods() .thenComposeAsync(shippingMethods -> { final Cart cartToRender = Optional.ofNullable(updatedCart).orElse(cart); final CheckoutShippingPageContentFactory pageContentFactory = injector().getInstance(CheckoutShippingPageContentFactory.class); final CheckoutShippingPageContent pageContent = pageContentFactory.create(form, cartToRender, shippingMethods); return renderPageWithTemplate(pageContent, getTemplateName()); }, HttpExecution.defaultContext()); }
@Override public CompletionStage<Html> renderPage(final Form<? extends CheckoutPaymentFormData> form, final Cart cart, @Nullable final Cart updatedCart) { return getPaymentMethodInfos() .thenComposeAsync(paymentMethods -> { final Cart cartToRender = Optional.ofNullable(updatedCart).orElse(cart); final CheckoutPaymentPageContentFactory pageContentFactory = injector().getInstance(CheckoutPaymentPageContentFactory.class); final CheckoutPaymentPageContent pageContent = pageContentFactory.create(form, cartToRender, paymentMethods); return renderPageWithTemplate(pageContent, getTemplateName()); }, defaultContext()); }
protected CompletionStage<Cart> requireNonEmptyCart() { return requireExistingCart() .thenApplyAsync(cart -> { if (cart.getLineItems().isEmpty()) { throw new CartEmptyException(cart); } else { return cart; } }, defaultContext()); }