Search in sources :

Example 51 with BroadleafRequestContext

use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.

the class AdminNavigationServiceImpl method buildAuthorizedSectionsList.

protected List<AdminSection> buildAuthorizedSectionsList(AdminUser adminUser, AdminModule module) {
    List<AdminSection> authorizedSections = new ArrayList<AdminSection>();
    BroadleafRequestContext broadleafRequestContext = BroadleafRequestContext.getBroadleafRequestContext();
    Site site = broadleafRequestContext.getNonPersistentSite();
    Long siteId = site == null ? null : site.getId();
    for (AdminSection section : module.getSections()) {
        if (isUserAuthorizedToViewSection(adminUser, section)) {
            if (section instanceof SiteDiscriminator) {
                Long sectionSiteId = ((SiteDiscriminator) section).getSiteDiscriminator();
                if (sectionSiteId == null || sectionSiteId.equals(siteId)) {
                    authorizedSections.add(section);
                }
            } else {
                authorizedSections.add(section);
            }
        }
    }
    Collections.sort(authorizedSections, SECTION_COMPARATOR);
    return authorizedSections;
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) SiteDiscriminator(org.broadleafcommerce.common.extensibility.jpa.SiteDiscriminator) AdminSection(org.broadleafcommerce.openadmin.server.security.domain.AdminSection) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) ArrayList(java.util.ArrayList)

Example 52 with BroadleafRequestContext

use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.

the class IdentityExecutionUtils method initRequestContext.

private static boolean initRequestContext(Site site, Site profile, Catalog catalog) {
    boolean isNew = false;
    BroadleafRequestContext requestContext = BroadleafRequestContext.getBroadleafRequestContext();
    if (requestContext == null) {
        requestContext = new BroadleafRequestContext();
        BroadleafRequestContext.setBroadleafRequestContext(requestContext);
        isNew = true;
    }
    requestContext.setSite(site);
    requestContext.setCurrentCatalog(catalog);
    requestContext.setCurrentProfile(profile);
    if (site != null) {
        requestContext.setIgnoreSite(false);
    }
    return isNew;
}
Also used : BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext)

Example 53 with BroadleafRequestContext

use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.

the class StaticAssetViewController method handleRequestInternal.

/**
 * Process the static asset request by determining the asset name.
 * Checks the current sandbox for a matching asset.   If not found, checks the
 * production sandbox.
 *
 * The view portion will be handled by a component with the name "blStaticAssetView" This is
 * intended to be the specific class StaticAssetView.
 *
 * @see StaticAssetView
 *
 * @see #handleRequest
 */
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
    String fullUrl = removeAssetPrefix(request.getRequestURI());
    // Static Assets don't typically go through the Spring Security pipeline but they may need access
    // to the site
    BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
    context.setNonPersistentSite(siteResolver.resolveSite(new ServletWebRequest(request, response)));
    try {
        Map<String, String> model = staticAssetStorageService.getCacheFileModel(fullUrl, convertParameterMap(request.getParameterMap()));
        return new ModelAndView(viewResolverName, model);
    } catch (AssetNotFoundException e) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return null;
    } catch (FileNotFoundException e) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        LOG.error("Could not retrieve asset request " + fullUrl + " from the StaticAssetStorage. The underlying file path checked was " + e.getMessage());
        return null;
    } catch (Exception e) {
        LOG.error("Unable to retrieve static asset", e);
        throw new RuntimeException(e);
    } finally {
        ThreadLocalManager.remove();
    }
}
Also used : BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) ModelAndView(org.springframework.web.servlet.ModelAndView) FileNotFoundException(java.io.FileNotFoundException) AssetNotFoundException(org.broadleafcommerce.cms.common.AssetNotFoundException) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) AssetNotFoundException(org.broadleafcommerce.cms.common.AssetNotFoundException) FileNotFoundException(java.io.FileNotFoundException)

Example 54 with BroadleafRequestContext

use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.

the class ContentProcessor method populateModelVariables.

@Override
public Map<String, Object> populateModelVariables(String tagName, Map<String, String> tagAttributes, BroadleafTemplateContext context) {
    String contentType = tagAttributes.get("contentType");
    String contentName = tagAttributes.get("contentName");
    String maxResultsStr = tagAttributes.get("maxResults");
    if (StringUtils.isEmpty(contentType) && StringUtils.isEmpty(contentName)) {
        throw new IllegalArgumentException("The content processor must have a non-empty attribute value for 'contentType' or 'contentName'");
    }
    Integer maxResults = null;
    if (maxResultsStr != null) {
        maxResults = Ints.tryParse(maxResultsStr);
    }
    if (maxResults == null) {
        maxResults = Integer.MAX_VALUE;
    }
    String contentListVar = getAttributeValue(tagAttributes, "contentListVar", "contentList");
    String contentItemVar = getAttributeValue(tagAttributes, "contentItemVar", "contentItem");
    String numResultsVar = getAttributeValue(tagAttributes, "numResultsVar", "numResults");
    String fieldFilters = tagAttributes.get("fieldFilters");
    final String sorts = tagAttributes.get("sorts");
    BroadleafRequestContext blcContext = BroadleafRequestContext.getBroadleafRequestContext();
    HttpServletRequest request = blcContext.getRequest();
    Map<String, Object> mvelParameters = buildMvelParameters(blcContext.getRequest(), tagAttributes, context);
    SandBox currentSandbox = blcContext.getSandBox();
    List<StructuredContentDTO> contentItems;
    StructuredContentType structuredContentType = null;
    if (contentType != null) {
        structuredContentType = structuredContentService.findStructuredContentTypeByName(contentType);
    }
    Locale locale = blcContext.getLocale();
    Map<String, Object> newModelVars = new HashMap<>();
    contentItems = getContentItems(contentName, maxResults, request, mvelParameters, currentSandbox, structuredContentType, locale, tagName, tagAttributes, newModelVars, context);
    if (contentItems.size() > 0) {
        // sort the resulting list by the configured property sorts on the tag
        if (StringUtils.isNotEmpty(sorts)) {
            final BroadleafTemplateContext finalContext = context;
            // In order to use the context in a comparator it needs to be final
            Collections.sort(contentItems, new Comparator<StructuredContentDTO>() {

                @Override
                public int compare(StructuredContentDTO o1, StructuredContentDTO o2) {
                    List<BroadleafAssignation> sortAssignments = finalContext.getAssignationSequence(sorts, false);
                    CompareToBuilder compareBuilder = new CompareToBuilder();
                    for (BroadleafAssignation sortAssignment : sortAssignments) {
                        String property = sortAssignment.getLeftStringRepresentation(finalContext);
                        Object val1 = o1.getPropertyValue(property);
                        Object val2 = o2.getPropertyValue(property);
                        if (sortAssignment.parseRight(finalContext).equals("ASCENDING")) {
                            compareBuilder.append(val1, val2);
                        } else {
                            compareBuilder.append(val2, val1);
                        }
                    }
                    return compareBuilder.toComparison();
                }
            });
        }
        List<Map<String, Object>> contentItemFields = new ArrayList<>();
        for (StructuredContentDTO item : contentItems) {
            if (StringUtils.isNotEmpty(fieldFilters)) {
                List<BroadleafAssignation> assignments = context.getAssignationSequence(fieldFilters, false);
                boolean valid = true;
                for (BroadleafAssignation assignment : assignments) {
                    if (ObjectUtils.notEqual(assignment.parseRight(context), item.getValues().get(assignment.getLeftStringRepresentation(context)))) {
                        LOG.info("Excluding content " + item.getId() + " based on the property value of " + assignment.getLeftStringRepresentation(context));
                        valid = false;
                        break;
                    }
                }
                if (valid) {
                    contentItemFields.add(item.getValues());
                }
            } else {
                contentItemFields.add(item.getValues());
            }
        }
        Map<String, Object> contentItem = null;
        if (contentItemFields.size() > 0) {
            contentItem = contentItemFields.get(0);
        }
        newModelVars.put(contentItemVar, contentItem);
        newModelVars.put(contentListVar, contentItemFields);
        newModelVars.put(numResultsVar, contentItems.size());
    } else {
        if (LOG.isInfoEnabled()) {
            LOG.info("**************************The contentItems is null*************************");
        }
        newModelVars.put(contentItemVar, null);
        newModelVars.put(contentListVar, null);
        newModelVars.put(numResultsVar, 0);
    }
    String deepLinksVar = tagAttributes.get("deepLinks");
    if (StringUtils.isNotBlank(deepLinksVar) && contentItems.size() > 0) {
        List<DeepLink> links = contentDeepLinkService.getLinks(contentItems.get(0));
        extensionManager.getProxy().addExtensionFieldDeepLink(links, tagName, tagAttributes, context);
        extensionManager.getProxy().postProcessDeepLinks(links);
        newModelVars.put(deepLinksVar, links);
    }
    return newModelVars;
}
Also used : Locale(org.broadleafcommerce.common.locale.domain.Locale) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) List(java.util.List) CompareToBuilder(org.apache.commons.lang3.builder.CompareToBuilder) BroadleafTemplateContext(org.broadleafcommerce.presentation.model.BroadleafTemplateContext) SandBox(org.broadleafcommerce.common.sandbox.domain.SandBox) StructuredContentDTO(org.broadleafcommerce.common.structure.dto.StructuredContentDTO) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) DeepLink(org.broadleafcommerce.common.web.deeplink.DeepLink) StructuredContentType(org.broadleafcommerce.cms.structure.domain.StructuredContentType) BroadleafAssignation(org.broadleafcommerce.presentation.model.BroadleafAssignation) HashMap(java.util.HashMap) Map(java.util.Map)

Example 55 with BroadleafRequestContext

use of org.broadleafcommerce.common.web.BroadleafRequestContext in project BroadleafCommerce by BroadleafCommerce.

the class PageHandlerMapping method getHandlerInternal.

@Override
protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
    BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
    if (context != null && context.getRequestURIWithoutContext() != null) {
        String requestUri = URLDecoder.decode(context.getRequestURIWithoutContext(), charEncoding);
        Boolean internalValidateFindPreviouslySet = false;
        PageDTO page;
        try {
            if (!BroadleafRequestContext.getBroadleafRequestContext().getInternalValidateFind()) {
                BroadleafRequestContext.getBroadleafRequestContext().setInternalValidateFind(true);
                internalValidateFindPreviouslySet = true;
            }
            page = pageService.findPageByURI(context.getLocale(), requestUri, buildMvelParameters(request), context.isSecure());
        } finally {
            if (internalValidateFindPreviouslySet) {
                BroadleafRequestContext.getBroadleafRequestContext().setInternalValidateFind(false);
            }
        }
        if (page != null && !(page instanceof NullPageDTO)) {
            context.getRequest().setAttribute(PAGE_ATTRIBUTE_NAME, page);
            return controllerName;
        }
    }
    return null;
}
Also used : NullPageDTO(org.broadleafcommerce.common.page.dto.NullPageDTO) PageDTO(org.broadleafcommerce.common.page.dto.PageDTO) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) NullPageDTO(org.broadleafcommerce.common.page.dto.NullPageDTO)

Aggregations

BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)78 Site (org.broadleafcommerce.common.site.domain.Site)16 HashMap (java.util.HashMap)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 Locale (org.broadleafcommerce.common.locale.domain.Locale)8 SandBox (org.broadleafcommerce.common.sandbox.domain.SandBox)5 StructuredContentDTO (org.broadleafcommerce.common.structure.dto.StructuredContentDTO)5 MessageSource (org.springframework.context.MessageSource)5 List (java.util.List)4 StructuredContent (org.broadleafcommerce.cms.structure.domain.StructuredContent)4 Field (java.lang.reflect.Field)3 HashSet (java.util.HashSet)3 Locale (java.util.Locale)3 Catalog (org.broadleafcommerce.common.site.domain.Catalog)3 BroadleafAttributeModifier (org.broadleafcommerce.presentation.model.BroadleafAttributeModifier)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2