Example usage for org.springframework.web.context.request WebRequest removeAttribute

List of usage examples for org.springframework.web.context.request WebRequest removeAttribute

Introduction

In this page you can find the example usage for org.springframework.web.context.request WebRequest removeAttribute.

Prototype

void removeAttribute(String name, int scope);

Source Link

Document

Remove the scoped attribute of the given name, if it exists.

Usage

From source file:org.broadleafcommerce.core.web.order.security.CartStateRequestProcessor.java

/**
 * Looks up the anonymous customer and merges that cart with the cart from the given logged in <b>customer</b>. This
 * will also remove the customer from session after it has finished since it is no longer needed
 *///from  w w  w.jav  a 2s  . c o  m
public Order mergeCart(Customer customer, WebRequest request) {
    Customer anonymousCustomer = customerStateRequestProcessor.getAnonymousCustomer(request);
    MergeCartResponse mergeCartResponse;
    try {
        Order cart = orderService.findCartForCustomer(anonymousCustomer);
        mergeCartResponse = mergeCartService.mergeCart(customer, cart);
    } catch (PricingException e) {
        throw new RuntimeException(e);
    } catch (RemoveFromCartException e) {
        throw new RuntimeException(e);
    }

    if (BLCRequestUtils.isOKtoUseSession(request)) {
        // The anonymous customer from session is no longer needed; it can be safely removed
        request.removeAttribute(CustomerStateRequestProcessor.getAnonymousCustomerSessionAttributeName(),
                WebRequest.SCOPE_GLOBAL_SESSION);
        request.removeAttribute(CustomerStateRequestProcessor.getAnonymousCustomerIdSessionAttributeName(),
                WebRequest.SCOPE_GLOBAL_SESSION);

        request.setAttribute(mergeCartResponseKey, mergeCartResponse, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    return mergeCartResponse.getOrder();
}

From source file:org.broadleafcommerce.openadmin.web.filter.BroadleafAdminRequestProcessor.java

protected void prepareProfile(WebRequest request, BroadleafRequestContext brc) {
    AdminUser adminUser = adminRemoteSecurityService.getPersistentAdminUser();
    if (adminUser == null) {
        //clear any profile
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            request.removeAttribute(PROFILE_REQ_PARAM, WebRequest.SCOPE_GLOBAL_SESSION);
        }// w  w w  .  j  a v a 2s .  c  o m
    } else {
        Site profile = null;
        if (StringUtils.isNotBlank(request.getParameter(PROFILE_REQ_PARAM))) {
            Long profileId = Long.parseLong(request.getParameter(PROFILE_REQ_PARAM));
            profile = siteService.retrievePersistentSiteById(profileId);
            if (profile == null) {
                throw new IllegalArgumentException(
                        String.format("Unable to find the requested profile: %s", profileId));
            }
        }

        if (profile == null) {
            Long previouslySetProfileId = null;
            if (BLCRequestUtils.isOKtoUseSession(request)) {
                previouslySetProfileId = (Long) request.getAttribute(PROFILE_REQ_PARAM,
                        WebRequest.SCOPE_GLOBAL_SESSION);
            }
            if (previouslySetProfileId != null) {
                profile = siteService.retrievePersistentSiteById(previouslySetProfileId);
            }
        }

        if (profile == null) {
            List<Site> profiles = new ArrayList<Site>();
            if (brc.getNonPersistentSite() != null) {
                Site currentSite = siteService.retrievePersistentSiteById(brc.getNonPersistentSite().getId());
                if (extensionManager != null) {
                    ExtensionResultHolder<Set<Site>> profilesResult = new ExtensionResultHolder<Set<Site>>();
                    extensionManager.getProxy().retrieveProfiles(currentSite, profilesResult);
                    if (!CollectionUtils.isEmpty(profilesResult.getResult())) {
                        profiles.addAll(profilesResult.getResult());
                    }
                }
            }
            if (profiles.size() == 1) {
                profile = profiles.get(0);
            }
        }

        if (profile != null) {
            if (BLCRequestUtils.isOKtoUseSession(request)) {
                request.setAttribute(PROFILE_REQ_PARAM, profile.getId(), WebRequest.SCOPE_GLOBAL_SESSION);
            }
            brc.setCurrentProfile(profile);
        }
    }
}

From source file:org.broadleafcommerce.openadmin.web.filter.BroadleafAdminRequestProcessor.java

protected void prepareCatalog(WebRequest request, BroadleafRequestContext brc) {
    AdminUser adminUser = adminRemoteSecurityService.getPersistentAdminUser();
    if (adminUser == null) {
        //clear any catalog
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            request.removeAttribute(CATALOG_REQ_PARAM, WebRequest.SCOPE_GLOBAL_SESSION);
        }/*from www.j  a  v  a  2  s.com*/
    } else {
        Catalog catalog = null;
        if (StringUtils.isNotBlank(request.getParameter(CATALOG_REQ_PARAM))) {
            Long catalogId = Long.parseLong(request.getParameter(CATALOG_REQ_PARAM));
            catalog = siteService.findCatalogById(catalogId);
            if (catalog == null) {
                throw new IllegalArgumentException(
                        String.format("Unable to find the requested catalog: %s", catalogId));
            }
        }

        if (catalog == null) {
            Long previouslySetCatalogId = null;
            if (BLCRequestUtils.isOKtoUseSession(request)) {
                previouslySetCatalogId = (Long) request.getAttribute(CATALOG_REQ_PARAM,
                        WebRequest.SCOPE_GLOBAL_SESSION);
            }
            if (previouslySetCatalogId != null) {
                catalog = siteService.findCatalogById(previouslySetCatalogId);
            }
        }

        if (catalog == null) {
            List<Catalog> catalogs = new ArrayList<Catalog>();
            if (brc.getNonPersistentSite() != null) {
                Site currentSite = siteService.retrievePersistentSiteById(brc.getNonPersistentSite().getId());
                if (extensionManager != null) {
                    ExtensionResultHolder<Set<Catalog>> catalogResult = new ExtensionResultHolder<Set<Catalog>>();
                    extensionManager.getProxy().retrieveCatalogs(currentSite, catalogResult);
                    if (!CollectionUtils.isEmpty(catalogResult.getResult())) {
                        catalogs.addAll(catalogResult.getResult());
                    }
                }
            }
            if (catalogs.size() == 1) {
                catalog = catalogs.get(0);
            }
        }

        if (catalog != null) {
            if (BLCRequestUtils.isOKtoUseSession(request)) {
                request.setAttribute(CATALOG_REQ_PARAM, catalog.getId(), WebRequest.SCOPE_GLOBAL_SESSION);
            }
            brc.setCurrentCatalog(catalog);
        }
    }
}

From source file:org.broadleafcommerce.openadmin.web.filter.BroadleafAdminRequestProcessor.java

protected void prepareSandBox(WebRequest request, BroadleafRequestContext brc) {
    AdminUser adminUser = adminRemoteSecurityService.getPersistentAdminUser();
    if (adminUser == null) {
        //clear any sandbox
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            request.removeAttribute(BroadleafSandBoxResolver.SANDBOX_ID_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
        }/*from w  w w. ja va  2 s  .c  om*/
    } else {
        SandBox sandBox = null;
        if (StringUtils.isNotBlank(request.getParameter(SANDBOX_REQ_PARAM))) {
            Long sandBoxId = Long.parseLong(request.getParameter(SANDBOX_REQ_PARAM));
            sandBox = sandBoxService.retrieveUserSandBoxForParent(adminUser.getId(), sandBoxId);
            if (sandBox == null) {
                SandBox approvalOrUserSandBox = sandBoxService.retrieveSandBoxManagementById(sandBoxId);
                if (approvalOrUserSandBox != null) {
                    if (approvalOrUserSandBox.getSandBoxType().equals(SandBoxType.USER)) {
                        sandBox = approvalOrUserSandBox;
                    } else {
                        sandBox = sandBoxService.createUserSandBox(adminUser.getId(), approvalOrUserSandBox);
                    }
                }
            }
        }

        if (sandBox == null) {
            Long previouslySetSandBoxId = null;
            if (BLCRequestUtils.isOKtoUseSession(request)) {
                previouslySetSandBoxId = (Long) request.getAttribute(BroadleafSandBoxResolver.SANDBOX_ID_VAR,
                        WebRequest.SCOPE_GLOBAL_SESSION);
            }
            if (previouslySetSandBoxId != null) {
                sandBox = sandBoxService.retrieveSandBoxManagementById(previouslySetSandBoxId);
            }
        }

        if (sandBox == null) {
            List<SandBox> defaultSandBoxes = sandBoxService.retrieveSandBoxesByType(SandBoxType.DEFAULT);
            if (defaultSandBoxes.size() > 1) {
                throw new IllegalStateException("Only one sandbox should be configured as default");
            }

            SandBox defaultSandBox;
            if (defaultSandBoxes.size() == 1) {
                defaultSandBox = defaultSandBoxes.get(0);
            } else {
                defaultSandBox = sandBoxService.createDefaultSandBox();
            }

            sandBox = sandBoxService.retrieveUserSandBoxForParent(adminUser.getId(), defaultSandBox.getId());
            if (sandBox == null) {
                sandBox = sandBoxService.createUserSandBox(adminUser.getId(), defaultSandBox);
            }
        }

        // If the user just changed sandboxes, we want to update the database record.
        Long previouslySetSandBoxId = null;
        if (BLCRequestUtils.isOKtoUseSession(request)) {
            previouslySetSandBoxId = (Long) request.getAttribute(BroadleafSandBoxResolver.SANDBOX_ID_VAR,
                    WebRequest.SCOPE_GLOBAL_SESSION);
        }
        if (previouslySetSandBoxId != null && !sandBox.getId().equals(previouslySetSandBoxId)) {
            adminUser.setLastUsedSandBoxId(sandBox.getId());
            adminUser = adminSecurityService.saveAdminUser(adminUser);
        }

        if (BLCRequestUtils.isOKtoUseSession(request)) {
            request.setAttribute(BroadleafSandBoxResolver.SANDBOX_ID_VAR, sandBox.getId(),
                    WebRequest.SCOPE_GLOBAL_SESSION);
        }
        brc.setSandBox(sandBox);
        brc.setDeployBehavior(deployBehaviorUtil.isProductionSandBoxMode() ? DeployBehavior.CLONE_PARENT
                : DeployBehavior.OVERWRITE_PARENT);
        brc.getAdditionalProperties().put("adminUser", adminUser);
    }
}

From source file:org.encuestame.oauth1.support.OAuth1RequestFlow.java

/**
 * Extract request token.//from  w  w  w .j ava 2s.c  om
 * @param request
 * @return
 */
private OAuth1Token extractCachedRequestToken(WebRequest request) {
    OAuth1Token requestToken = (OAuth1Token) request.getAttribute(OAuthUtils.OAUTH_TOKEN_ATTRIBUTE,
            WebRequest.SCOPE_SESSION);
    request.removeAttribute(OAuthUtils.OAUTH_TOKEN_ATTRIBUTE, WebRequest.SCOPE_SESSION);
    log.debug("requestToken " + requestToken.toString());
    return requestToken;
}

From source file:org.openmrs.web.controller.LoginController.java

/**
 * Generates an appropriate alert message and send it to users with the system developer role
 *
 * @param webRequest the {@link WebRequest} object
 * @param model the {@link ModelMap} object
 * @return the view name//from   www .ja  v  a2s  .c  o m
 */
@RequestMapping(LOGIN_FORM)
public String handleRequest(WebRequest webRequest, ModelMap model) {
    boolean failedPrivilegeCheck = false;
    Object attributeValue = webRequest.getAttribute(WebConstants.INSUFFICIENT_PRIVILEGES,
            WebRequest.SCOPE_SESSION);
    if (attributeValue != null) {
        if (Boolean.valueOf(attributeValue.toString().trim())) {
            failedPrivilegeCheck = true;
        }
        webRequest.removeAttribute(WebConstants.INSUFFICIENT_PRIVILEGES, WebRequest.SCOPE_SESSION);
    }

    //If there is a currently logged in user and they failed a privilege check, else go to login in page
    if (Context.getAuthenticatedUser() != null && failedPrivilegeCheck) {
        model.addAttribute("foundMissingPrivileges", true);
        webRequest.setAttribute(WebConstants.OPENMRS_ERROR_ATTR, "error.insufficientPrivileges",
                WebRequest.SCOPE_SESSION);

        String deniedPage = null;
        String requiredPrivileges = null;
        String exceptionMsg = null;
        String refererUrl = null;
        if (webRequest.getAttribute(WebConstants.DENIED_PAGE, WebRequest.SCOPE_SESSION) != null) {
            String deniedPageTemp = webRequest.getAttribute(WebConstants.DENIED_PAGE, WebRequest.SCOPE_SESSION)
                    .toString();
            webRequest.removeAttribute(WebConstants.DENIED_PAGE, WebRequest.SCOPE_SESSION);
            if (StringUtils.isNotBlank(deniedPageTemp)) {
                deniedPage = deniedPageTemp;
            }

        }
        if (webRequest.getAttribute(WebConstants.REQUIRED_PRIVILEGES, WebRequest.SCOPE_SESSION) != null) {
            String requiredPrivilegesTemp = webRequest
                    .getAttribute(WebConstants.REQUIRED_PRIVILEGES, WebRequest.SCOPE_SESSION).toString();
            webRequest.removeAttribute(WebConstants.REQUIRED_PRIVILEGES, WebRequest.SCOPE_SESSION);
            if (StringUtils.isNotBlank(requiredPrivilegesTemp)) {
                requiredPrivileges = requiredPrivilegesTemp;
            }
        }
        if (webRequest.getAttribute(WebConstants.UNCAUGHT_EXCEPTION_MESSAGE,
                WebRequest.SCOPE_SESSION) != null) {
            String exceptionMsgTemp = webRequest
                    .getAttribute(WebConstants.UNCAUGHT_EXCEPTION_MESSAGE, WebRequest.SCOPE_SESSION).toString();
            webRequest.removeAttribute(WebConstants.UNCAUGHT_EXCEPTION_MESSAGE, WebRequest.SCOPE_SESSION);
            if (StringUtils.isNotBlank(exceptionMsgTemp)) {
                exceptionMsg = exceptionMsgTemp;
            }
        }
        if (webRequest.getAttribute(WebConstants.REFERER_URL, WebRequest.SCOPE_SESSION) != null) {
            String refererUrlTemp = webRequest.getAttribute(WebConstants.REFERER_URL, WebRequest.SCOPE_SESSION)
                    .toString();
            webRequest.removeAttribute(WebConstants.REFERER_URL, WebRequest.SCOPE_SESSION);
            if (StringUtils.isNotBlank(refererUrlTemp) && !refererUrlTemp.contains("login.")) {
                refererUrl = refererUrlTemp;
            }
        }

        String alertMessage = null;
        if (requiredPrivileges != null && deniedPage != null) {
            alertMessage = Context.getMessageSourceService()
                    .getMessage("general.alert.requestPrivilegesForPage", new String[] {
                            Context.getAuthenticatedUser().getUsername(), requiredPrivileges, deniedPage },
                            null);
        } else if (exceptionMsg != null && deniedPage != null) {
            alertMessage = Context.getMessageSourceService().getMessage(
                    "general.alert.privilegesForPageOnException",
                    new String[] { exceptionMsg, Context.getAuthenticatedUser().getUsername(), deniedPage },
                    null);
        } else if (deniedPage != null) {
            alertMessage = Context.getMessageSourceService().getMessage(
                    "general.alert.requestUnKnownPrivilegesForPage",
                    new String[] { Context.getAuthenticatedUser().getUsername(), deniedPage }, null);
        } else if (requiredPrivileges != null) {
            alertMessage = Context.getMessageSourceService().getMessage("general.alert.requestPrivileges",
                    new String[] { Context.getAuthenticatedUser().getUsername(), requiredPrivileges }, null);
        } else if (exceptionMsg != null) {
            alertMessage = Context.getMessageSourceService().getMessage("general.alert.requestPrivileges",
                    new String[] { Context.getAuthenticatedUser().getUsername(), exceptionMsg }, null);
        }

        String reason = null;
        if (requiredPrivileges != null) {
            reason = Context.getMessageSourceService().getMessage("error.privilegesRequired",
                    new Object[] { requiredPrivileges }, null);
        } else if (exceptionMsg != null) {
            reason = exceptionMsg;
        } else {
            reason = Context.getMessageSourceService().getMessage("error.extraPrivilegesRequired");
        }

        //else we don't know both the page and privileges required, and there 
        //was no exception message that might contain the required privilege

        //will be sending the alert via ajax, so we need to escape js special chars
        model.put("alertMessage", JavaScriptUtils.javaScriptEscape(alertMessage));
        model.put("reason", reason);
        model.put("refererUrl", refererUrl);
    }

    return "/module/legacyui" + LOGIN_FORM;
}

From source file:org.sparkcommerce.core.web.order.security.CartStateRequestProcessor.java

/**
 * Looks up the anonymous customer and merges that cart with the cart from the given logged in <b>customer</b>. This
 * will also remove the customer from session after it has finished since it is no longer needed
 */// www.j  a  v a 2s.co  m
public Order mergeCart(Customer customer, WebRequest request) {
    Customer anonymousCustomer = customerStateRequestProcessor.getAnonymousCustomer(request);
    MergeCartResponse mergeCartResponse;
    try {
        Order cart = orderService.findCartForCustomer(anonymousCustomer);
        mergeCartResponse = mergeCartService.mergeCart(customer, cart);
    } catch (PricingException e) {
        throw new RuntimeException(e);
    } catch (RemoveFromCartException e) {
        throw new RuntimeException(e);
    }

    if (SCRequestUtils.isOKtoUseSession(request)) {
        // The anonymous customer from session is no longer needed; it can be safely removed
        request.removeAttribute(CustomerStateRequestProcessor.getAnonymousCustomerSessionAttributeName(),
                WebRequest.SCOPE_GLOBAL_SESSION);
        request.removeAttribute(CustomerStateRequestProcessor.getAnonymousCustomerIdSessionAttributeName(),
                WebRequest.SCOPE_GLOBAL_SESSION);

        request.setAttribute(mergeCartResponseKey, mergeCartResponse, WebRequest.SCOPE_GLOBAL_SESSION);
    }
    return mergeCartResponse.getOrder();
}

From source file:org.sparkcommerce.openadmin.web.filter.SparkAdminRequestProcessor.java

protected void prepareSandBox(WebRequest request, SparkRequestContext brc) {
    AdminUser adminUser = adminRemoteSecurityService.getPersistentAdminUser();
    if (adminUser == null) {
        //clear any sandbox
        if (SCRequestUtils.isOKtoUseSession(request)) {
            request.removeAttribute(SparkSandBoxResolver.SANDBOX_ID_VAR, WebRequest.SCOPE_GLOBAL_SESSION);
        }/*from  w w w.j a  v a 2s. com*/
    } else {
        SandBox sandBox = null;
        if (StringUtils.isNotBlank(request.getParameter(SANDBOX_REQ_PARAM))) {
            Long sandBoxId = Long.parseLong(request.getParameter(SANDBOX_REQ_PARAM));
            sandBox = sandBoxService.retrieveUserSandBoxForParent(adminUser.getId(), sandBoxId);
            if (sandBox == null) {
                SandBox approvalOrUserSandBox = sandBoxService.retrieveSandBoxById(sandBoxId);
                if (approvalOrUserSandBox.getSandBoxType().equals(SandBoxType.USER)) {
                    sandBox = approvalOrUserSandBox;
                } else {
                    sandBox = sandBoxService.createUserSandBox(adminUser.getId(), approvalOrUserSandBox);
                }
            }
        }

        if (sandBox == null) {
            Long previouslySetSandBoxId = null;
            if (SCRequestUtils.isOKtoUseSession(request)) {
                previouslySetSandBoxId = (Long) request.getAttribute(SparkSandBoxResolver.SANDBOX_ID_VAR,
                        WebRequest.SCOPE_GLOBAL_SESSION);
            }
            if (previouslySetSandBoxId != null) {
                sandBox = sandBoxService.retrieveSandBoxById(previouslySetSandBoxId);
            }
        }

        if (sandBox == null) {
            List<SandBox> defaultSandBoxes = sandBoxService.retrieveSandBoxesByType(SandBoxType.DEFAULT);
            if (defaultSandBoxes.size() > 1) {
                throw new IllegalStateException("Only one sandbox should be configured as default");
            }

            SandBox defaultSandBox;
            if (defaultSandBoxes.size() == 1) {
                defaultSandBox = defaultSandBoxes.get(0);
            } else {
                defaultSandBox = sandBoxService.createDefaultSandBox();
            }

            sandBox = sandBoxService.retrieveUserSandBoxForParent(adminUser.getId(), defaultSandBox.getId());
            if (sandBox == null) {
                sandBox = sandBoxService.createUserSandBox(adminUser.getId(), defaultSandBox);
            }
        }

        // If the user just changed sandboxes, we want to update the database record.
        Long previouslySetSandBoxId = null;
        if (SCRequestUtils.isOKtoUseSession(request)) {
            previouslySetSandBoxId = (Long) request.getAttribute(SparkSandBoxResolver.SANDBOX_ID_VAR,
                    WebRequest.SCOPE_GLOBAL_SESSION);
        }
        if (previouslySetSandBoxId != null && !sandBox.getId().equals(previouslySetSandBoxId)) {
            adminUser.setLastUsedSandBoxId(sandBox.getId());
            adminUser = adminSecurityService.saveAdminUser(adminUser);
        }

        if (SCRequestUtils.isOKtoUseSession(request)) {
            request.setAttribute(SparkSandBoxResolver.SANDBOX_ID_VAR, sandBox.getId(),
                    WebRequest.SCOPE_GLOBAL_SESSION);
        }
        brc.setSandBox(sandBox);
        brc.getAdditionalProperties().put("adminUser", adminUser);
    }
}

From source file:org.springframework.data.neo4j.web.support.OpenSessionInViewInterceptor.java

private boolean decrementParticipateCount(WebRequest request) {
    String participateAttributeName = getParticipateAttributeName();
    Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
    if (count == null) {
        return false;
    }/* w  w w .  j  a  va 2  s.c o m*/
    // Do not modify the Session: just clear the marker.
    if (count > 1) {
        request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST);
    } else {
        request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
    }
    return true;
}

From source file:org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor.java

@Override
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException {
    String participateAttributeName = getParticipateAttributeName();
    Integer count = (Integer) request.getAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
    if (count != null) {
        // Do not modify the PersistenceManager: just clear the marker.
        if (count > 1) {
            request.setAttribute(participateAttributeName, count - 1, WebRequest.SCOPE_REQUEST);
        } else {/*from  w  w w  .j  av  a2  s  .co  m*/
            request.removeAttribute(participateAttributeName, WebRequest.SCOPE_REQUEST);
        }
    } else {
        PersistenceManagerHolder pmHolder = (PersistenceManagerHolder) TransactionSynchronizationManager
                .unbindResource(getPersistenceManagerFactory());
        logger.debug("Closing JDO PersistenceManager in OpenPersistenceManagerInViewInterceptor");
        PersistenceManagerFactoryUtils.releasePersistenceManager(pmHolder.getPersistenceManager(),
                getPersistenceManagerFactory());
    }
}