Search in sources :

Example 1 with BroadleafRequestedCurrencyDto

use of org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafCurrencyResolverImpl method resolveCurrency.

@Override
public BroadleafRequestedCurrencyDto resolveCurrency(WebRequest request) {
    BroadleafCurrency desiredCurrency = null;
    // 1) Check request for currency
    desiredCurrency = (BroadleafCurrency) request.getAttribute(CURRENCY_VAR, WebRequest.SCOPE_REQUEST);
    // 2) Check for a request parameter
    if (desiredCurrency == null && BLCRequestUtils.getURLorHeaderParameter(request, CURRENCY_CODE_PARAM) != null) {
        String currencyCode = BLCRequestUtils.getURLorHeaderParameter(request, CURRENCY_CODE_PARAM);
        desiredCurrency = broadleafCurrencyService.findCurrencyByCode(currencyCode);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Attempt to find currency by param " + currencyCode + " resulted in " + desiredCurrency);
        }
    }
    // 3) Check session for currency
    if (desiredCurrency == null && BLCRequestUtils.isOKtoUseSession(request)) {
        desiredCurrency = (BroadleafCurrency) request.getAttribute(CURRENCY_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    // 4) Check locale for currency
    if (desiredCurrency == null) {
        Locale locale = (Locale) request.getAttribute(BroadleafLocaleResolverImpl.LOCALE_VAR, WebRequest.SCOPE_REQUEST);
        if (locale != null) {
            desiredCurrency = locale.getDefaultCurrency();
        }
    }
    // 5) Lookup default currency from DB
    BroadleafCurrency defaultCurrency = broadleafCurrencyService.findDefaultBroadleafCurrency();
    if (desiredCurrency == null) {
        desiredCurrency = defaultCurrency;
    }
    // For an out-of-box installation, only one currency is supported, so even though we have a
    // desired currency, we may not have any prices that support it.
    BroadleafCurrency currencyToUse = defaultCurrency;
    if (BLCRequestUtils.isOKtoUseSession(request)) {
        request.setAttribute(CURRENCY_VAR, currencyToUse, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    BroadleafRequestedCurrencyDto dto = new BroadleafRequestedCurrencyDto(currencyToUse, desiredCurrency);
    return dto;
}
Also used : Locale(org.broadleafcommerce.common.locale.domain.Locale) BroadleafCurrency(org.broadleafcommerce.common.currency.domain.BroadleafCurrency) BroadleafRequestedCurrencyDto(org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto)

Example 2 with BroadleafRequestedCurrencyDto

use of org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafRequestProcessor method process.

@Override
public void process(WebRequest request) {
    BroadleafRequestContext brc = new BroadleafRequestContext();
    brc.getAdditionalProperties().putAll(entityExtensionManagers);
    Site site = siteResolver.resolveSite(request);
    brc.setNonPersistentSite(site);
    brc.setWebRequest(request);
    if (site == null) {
        brc.setIgnoreSite(true);
    }
    brc.setAdmin(false);
    if (siteStrictValidateProductionChanges) {
        brc.setValidateProductionChangesState(ValidateProductionChangesState.SITE);
    } else {
        brc.setValidateProductionChangesState(ValidateProductionChangesState.UNDEFINED);
    }
    BroadleafRequestContext.setBroadleafRequestContext(brc);
    Locale locale = localeResolver.resolveLocale(request);
    brc.setLocale(locale);
    TimeZone timeZone = broadleafTimeZoneResolver.resolveTimeZone(request);
    BroadleafRequestedCurrencyDto currencyDto = currencyResolver.resolveCurrency(request);
    // Assumes BroadleafProcess
    RequestDTO requestDTO = (RequestDTO) request.getAttribute(REQUEST_DTO_PARAM_NAME, WebRequest.SCOPE_REQUEST);
    if (requestDTO == null) {
        requestDTO = new RequestDTOImpl(request);
    }
    SandBox currentSandbox = sandboxResolver.resolveSandBox(request, site);
    // When a user elects to switch his sandbox, we want to invalidate the current session. We'll then redirect the
    // user to the current URL so that the configured filters trigger again appropriately.
    Boolean reprocessRequest = (Boolean) request.getAttribute(BroadleafRequestProcessor.REPROCESS_PARAM_NAME, WebRequest.SCOPE_REQUEST);
    if (reprocessRequest != null && reprocessRequest) {
        LOG.debug("Reprocessing request");
        if (request instanceof ServletWebRequest) {
            HttpServletRequest hsr = ((ServletWebRequest) request).getRequest();
            clearBroadleafSessionAttrs(request);
            StringBuffer url = hsr.getRequestURL();
            HttpServletResponse response = ((ServletWebRequest) request).getResponse();
            try {
                if (!isUrlValid(url.toString())) {
                    LOG.error("SECURITY FAILURE Bad redirect location: " + StringUtil.sanitize(url.toString()));
                    response.sendError(403);
                    return;
                }
                String sandboxId = hsr.getParameter(SANDBOX_ID_PARAM);
                if (isSandboxIdValid(sandboxId)) {
                    String queryString = "?" + SANDBOX_ID_PARAM + "=" + sandboxId;
                    url.append(queryString);
                }
                response.sendRedirect(url.toString());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            throw new HaltFilterChainException("Reprocess required, redirecting user");
        }
    }
    if (!siteDisableSandboxPreview && currentSandbox != null) {
        SandBoxContext previewSandBoxContext = new SandBoxContext();
        previewSandBoxContext.setSandBoxId(currentSandbox.getId());
        previewSandBoxContext.setPreviewMode(true);
        SandBoxContext.setSandBoxContext(previewSandBoxContext);
    }
    if (currencyDto != null) {
        brc.setBroadleafCurrency(currencyDto.getCurrencyToUse());
        brc.setRequestedBroadleafCurrency(currencyDto.getRequestedCurrency());
    }
    // is used in a different session that it was initiated in. see QA#2576
    if (currentSandbox != null && currentSandbox.getChildSandBoxes() != null) {
        currentSandbox.getChildSandBoxes().size();
    }
    brc.setSandBox(currentSandbox);
    brc.setDeployBehavior(deployBehaviorUtil.isProductionSandBoxMode() ? DeployBehavior.CLONE_PARENT : DeployBehavior.OVERWRITE_PARENT);
    // Note that this must happen after the request context is set up as resolving a theme is dependent on site
    Theme theme = themeResolver.resolveTheme(request);
    brc.setTheme(theme);
    brc.setMessageSource(messageSource);
    brc.setTimeZone(timeZone);
    brc.setRequestDTO(requestDTO);
    Map<String, Object> ruleMap = (Map<String, Object>) request.getAttribute("blRuleMap", WebRequest.SCOPE_REQUEST);
    if (ruleMap == null) {
        LOG.trace("Creating ruleMap and adding in Locale.");
        ruleMap = new HashMap<String, Object>();
        request.setAttribute("blRuleMap", ruleMap, WebRequest.SCOPE_REQUEST);
    } else {
        LOG.trace("Using pre-existing ruleMap - added by non standard BLC process.");
    }
    ruleMap.put("locale", locale);
    String adminUserId = request.getParameter(BroadleafRequestFilter.ADMIN_USER_ID_PARAM_NAME);
    if (StringUtils.isNotBlank(adminUserId)) {
        // TODO: Add token logic to secure the admin user id
        brc.setAdminUserId(Long.parseLong(adminUserId));
    }
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) Locale(org.broadleafcommerce.common.locale.domain.Locale) SandBox(org.broadleafcommerce.common.sandbox.domain.SandBox) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BroadleafRequestedCurrencyDto(org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto) HttpServletRequest(javax.servlet.http.HttpServletRequest) TimeZone(java.util.TimeZone) RequestDTOImpl(org.broadleafcommerce.common.RequestDTOImpl) HaltFilterChainException(org.broadleafcommerce.common.web.exception.HaltFilterChainException) RequestDTO(org.broadleafcommerce.common.RequestDTO) Theme(org.broadleafcommerce.common.site.domain.Theme) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with BroadleafRequestedCurrencyDto

use of org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto in project BroadleafCommerce by BroadleafCommerce.

the class BroadleafAdminRequestProcessor method process.

@Override
public void process(WebRequest request) throws SiteNotFoundException {
    BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
    if (brc == null) {
        brc = new BroadleafRequestContext();
        BroadleafRequestContext.setBroadleafRequestContext(brc);
    }
    brc.getAdditionalProperties().putAll(entityExtensionManagers);
    if (brc.getSite() == null) {
        Site site = siteResolver.resolveSite(request);
        brc.setSite(site);
    }
    brc.setWebRequest(request);
    brc.setIgnoreSite(brc.getSite() == null);
    brc.setAdmin(true);
    if (adminStrictValidateProductionChanges) {
        brc.setValidateProductionChangesState(ValidateProductionChangesState.ADMIN);
    } else {
        brc.setValidateProductionChangesState(ValidateProductionChangesState.UNDEFINED);
    }
    Locale locale = localeResolver.resolveLocale(request);
    brc.setLocale(locale);
    brc.setMessageSource(messageSource);
    TimeZone timeZone = broadleafTimeZoneResolver.resolveTimeZone(request);
    brc.setTimeZone(timeZone);
    // Note: The currencyResolver will set the currency on the BroadleafRequestContext but
    // later modules (specifically PriceListRequestProcessor in BLC enterprise) may override based
    // on the desired currency.
    BroadleafRequestedCurrencyDto dto = currencyResolver.resolveCurrency(request);
    if (dto != null) {
        brc.setBroadleafCurrency(dto.getCurrencyToUse());
        brc.setRequestedBroadleafCurrency(dto.getRequestedCurrency());
    }
    AdminUser adminUser = adminRemoteSecurityService.getPersistentAdminUser();
    if (adminUser != null) {
        brc.setAdminUserId(adminUser.getId());
    }
    prepareSandBox(request, brc);
    prepareProfile(request, brc);
    prepareCatalog(request, brc);
    brc.getAdditionalProperties().put(staleStateProtectionService.getStateVersionTokenParameter(), staleStateProtectionService.getStateVersionToken());
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) Locale(org.broadleafcommerce.common.locale.domain.Locale) TimeZone(java.util.TimeZone) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) AdminUser(org.broadleafcommerce.openadmin.server.security.domain.AdminUser) BroadleafRequestedCurrencyDto(org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto)

Aggregations

BroadleafRequestedCurrencyDto (org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto)3 Locale (org.broadleafcommerce.common.locale.domain.Locale)3 TimeZone (java.util.TimeZone)2 Site (org.broadleafcommerce.common.site.domain.Site)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 RequestDTO (org.broadleafcommerce.common.RequestDTO)1 RequestDTOImpl (org.broadleafcommerce.common.RequestDTOImpl)1 BroadleafCurrency (org.broadleafcommerce.common.currency.domain.BroadleafCurrency)1 SandBox (org.broadleafcommerce.common.sandbox.domain.SandBox)1 Theme (org.broadleafcommerce.common.site.domain.Theme)1 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)1 HaltFilterChainException (org.broadleafcommerce.common.web.exception.HaltFilterChainException)1 AdminUser (org.broadleafcommerce.openadmin.server.security.domain.AdminUser)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1