Search in sources :

Example 16 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project BroadleafCommerce by BroadleafCommerce.

the class CookieRuleRequestProcessor method process.

@Override
public void process(WebRequest request) {
    if (request instanceof ServletWebRequest) {
        ServletWebRequest servletWebRequest = (ServletWebRequest) request;
        Map proxy = (Map) BLCRequestUtils.getSessionAttributeIfOk(request, COOKIE_ATTRIBUTE_NAME);
        if (proxy == null) {
            proxy = getVals(servletWebRequest);
            BLCRequestUtils.setSessionAttributeIfOk(request, COOKIE_ATTRIBUTE_NAME, proxy);
        }
        BroadleafRequestContext.getBroadleafRequestContext().getAdditionalProperties().put(COOKIE_ATTRIBUTE_NAME, proxy);
        Map<String, Object> ruleMap = getRuleMapFromRequest(request);
        ruleMap.put(COOKIE_ATTRIBUTE_NAME, proxy);
        request.setAttribute(BLC_RULE_MAP_PARAM, ruleMap, WebRequest.SCOPE_REQUEST);
    }
}
Also used : ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Map(java.util.Map) HashMap(java.util.HashMap)

Example 17 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project BroadleafCommerce by BroadleafCommerce.

the class GeolocationRequestProcessor method process.

@Override
public void process(WebRequest request) {
    if (isGeolocationEnabled()) {
        if (request instanceof ServletWebRequest) {
            ServletWebRequest servletWebRequest = (ServletWebRequest) request;
            GeolocationDTO location = (GeolocationDTO) BLCRequestUtils.getSessionAttributeIfOk(request, GEOLOCATON_ATTRIBUTE_NAME);
            if (location == null) {
                String ipAddress = getIPAddress(servletWebRequest);
                location = geolocationService.getLocationData(ipAddress);
                BLCRequestUtils.setSessionAttributeIfOk(request, GEOLOCATON_ATTRIBUTE_NAME, location);
            }
            BroadleafRequestContext.getBroadleafRequestContext().getAdditionalProperties().put(GEOLOCATON_ATTRIBUTE_NAME, location);
            Map<String, Object> ruleMap = getRuleMapFromRequest(request);
            ruleMap.put(GEOLOCATON_ATTRIBUTE_NAME, location);
            request.setAttribute(BLC_RULE_MAP_PARAM, ruleMap, WebRequest.SCOPE_REQUEST);
        }
    }
}
Also used : ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) GeolocationDTO(org.broadleafcommerce.core.geolocation.GeolocationDTO)

Example 18 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project BroadleafCommerce by BroadleafCommerce.

the class StaleStateProtectionServiceImpl method invalidateState.

@Override
public void invalidateState(boolean notify) {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    if (BLCRequestUtils.isOKtoUseSession(new ServletWebRequest(request))) {
        HttpSession session = request.getSession();
        session.removeAttribute(STATEVERSIONTOKEN);
        if (notify) {
            getStateVersionToken();
            request.setAttribute(STATECHANGENOTIFICATIONTOKEN, "true");
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest)

Example 19 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest 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 20 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project pivotal-cla by pivotalsoftware.

the class ImportedSignaturesSessionAttrResolverTests method resolveArgumentFalse.

@Test
public void resolveArgumentFalse() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    NativeWebRequest webRequest = new ServletWebRequest(request);
    MethodParameter parameter = new MethodParameter(method, 0);
    ImportedSignaturesSessionAttr resolved = (ImportedSignaturesSessionAttr) resolver.resolveArgument(parameter, null, webRequest, null);
    assertThat(resolved.getValue()).isFalse();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Aggregations

ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)224 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)80 Test (org.junit.jupiter.api.Test)79 MethodParameter (org.springframework.core.MethodParameter)50 BeforeEach (org.junit.jupiter.api.BeforeEach)41 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)32 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)30 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)28 Method (java.lang.reflect.Method)21 Test (org.junit.Test)21 MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)21 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)18 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)14 TestBean (org.springframework.beans.testfixture.beans.TestBean)14 RequestParam (org.springframework.web.bind.annotation.RequestParam)14 MockPart (org.springframework.web.testfixture.servlet.MockPart)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 MultipartFile (org.springframework.web.multipart.MultipartFile)13 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)11 IOException (java.io.IOException)10