Search in sources :

Example 1 with RequestDTO

use of org.broadleafcommerce.common.RequestDTO in project BroadleafCommerce by BroadleafCommerce.

the class MvelHelperTest method testRequestMapProperty.

/**
 * Tests MVEL syntax for accessing request property map values.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testRequestMapProperty() {
    BroadleafRequestContext.setBroadleafRequestContext(new BroadleafRequestContext());
    RequestDTO dto = new RequestDTOImpl();
    dto.getProperties().put("blcSearchTerm", "hot");
    Map parameters = new HashMap();
    parameters.put("request", dto);
    // If the "key" property doesn't contain an underscore, the expression returns true
    boolean result = MvelHelper.evaluateRule("request.properties['blcSearchTerm'] == 'hot'", parameters);
    assertTrue(result);
}
Also used : HashMap(java.util.HashMap) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) RequestDTOImpl(org.broadleafcommerce.common.RequestDTOImpl) RequestDTO(org.broadleafcommerce.common.RequestDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with RequestDTO

use of org.broadleafcommerce.common.RequestDTO in project BroadleafCommerce by BroadleafCommerce.

the class PageHandlerMapping method buildMvelParameters.

/**
 * MVEL is used to process the content targeting rules.
 *
 * @param request
 * @return
 */
private Map<String, Object> buildMvelParameters(HttpServletRequest request) {
    TimeDTO timeDto = new TimeDTO(SystemTime.asCalendar());
    RequestDTO requestDto = (RequestDTO) request.getAttribute(REQUEST_DTO);
    Map<String, Object> mvelParameters = new HashMap<String, Object>();
    mvelParameters.put("time", timeDto);
    mvelParameters.put("request", requestDto);
    Map<String, Object> blcRuleMap = (Map<String, Object>) request.getAttribute(BLC_RULE_MAP_PARAM);
    if (blcRuleMap != null) {
        for (String mapKey : blcRuleMap.keySet()) {
            mvelParameters.put(mapKey, blcRuleMap.get(mapKey));
        }
    }
    return mvelParameters;
}
Also used : TimeDTO(org.broadleafcommerce.common.TimeDTO) HashMap(java.util.HashMap) RequestDTO(org.broadleafcommerce.common.RequestDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with RequestDTO

use of org.broadleafcommerce.common.RequestDTO in project BroadleafCommerce by BroadleafCommerce.

the class AbstractBaseProcessor method removeInvalidRequestOffers.

protected List<Offer> removeInvalidRequestOffers(List<Offer> offers) {
    RequestDTO requestDTO = null;
    if (BroadleafRequestContext.getBroadleafRequestContext() != null) {
        requestDTO = BroadleafRequestContext.getBroadleafRequestContext().getRequestDTO();
    }
    List<Offer> offersToRemove = new ArrayList<Offer>();
    for (Offer offer : offers) {
        if (!couldOfferApplyToRequestDTO(offer, requestDTO)) {
            offersToRemove.add(offer);
        }
    }
    // remove all offers in the offersToRemove list from original offers list
    for (Offer offer : offersToRemove) {
        offers.remove(offer);
    }
    return offers;
}
Also used : Offer(org.broadleafcommerce.core.offer.domain.Offer) RequestDTO(org.broadleafcommerce.common.RequestDTO) ArrayList(java.util.ArrayList)

Example 4 with RequestDTO

use of org.broadleafcommerce.common.RequestDTO in project BroadleafCommerce by BroadleafCommerce.

the class MvelHelper method buildMvelParameters.

/**
 * Builds parameters using time, request, customer, and cart.
 *
 * Should be called from within a valid web request.
 *
 * @param request
 * @return
 */
public static Map<String, Object> buildMvelParameters() {
    Map<String, Object> mvelParameters = new HashMap<String, Object>();
    BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
    if (brc != null && brc.getRequest() != null) {
        TimeDTO timeDto = new TimeDTO(SystemTime.asCalendar());
        HttpServletRequest request = brc.getRequest();
        RequestDTO requestDto = brc.getRequestDTO();
        mvelParameters.put("time", timeDto);
        mvelParameters.put("request", requestDto);
        Map<String, Object> blcRuleMap = (Map<String, Object>) request.getAttribute(BLC_RULE_MAP_PARAM);
        if (blcRuleMap != null) {
            for (String mapKey : blcRuleMap.keySet()) {
                mvelParameters.put(mapKey, blcRuleMap.get(mapKey));
            }
        }
    }
    return mvelParameters;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) TimeDTO(org.broadleafcommerce.common.TimeDTO) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) RequestDTO(org.broadleafcommerce.common.RequestDTO) HashMap(java.util.HashMap) EfficientLRUMap(org.broadleafcommerce.common.util.EfficientLRUMap) Map(java.util.Map)

Example 5 with RequestDTO

use of org.broadleafcommerce.common.RequestDTO 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)

Aggregations

RequestDTO (org.broadleafcommerce.common.RequestDTO)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 TimeDTO (org.broadleafcommerce.common.TimeDTO)4 TimeZone (java.util.TimeZone)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 RequestDTOImpl (org.broadleafcommerce.common.RequestDTOImpl)2 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 BroadleafRequestedCurrencyDto (org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto)1 Locale (org.broadleafcommerce.common.locale.domain.Locale)1 SandBox (org.broadleafcommerce.common.sandbox.domain.SandBox)1 Site (org.broadleafcommerce.common.site.domain.Site)1 Theme (org.broadleafcommerce.common.site.domain.Theme)1 EfficientLRUMap (org.broadleafcommerce.common.util.EfficientLRUMap)1 HaltFilterChainException (org.broadleafcommerce.common.web.exception.HaltFilterChainException)1 Offer (org.broadleafcommerce.core.offer.domain.Offer)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1