Search in sources :

Example 6 with Site

use of org.broadleafcommerce.common.site.domain.Site in project BroadleafCommerce by BroadleafCommerce.

the class SiteServiceImpl method retrieveSiteByIdentifier.

protected Site retrieveSiteByIdentifier(final String identifier, final boolean persistentResult) {
    // only run the operation under a transaction if there is not already an entity manager in the view
    if (identifier == null) {
        return null;
    }
    final Site[] response = new Site[1];
    transUtil.runOptionalTransactionalOperation(new StreamCapableTransactionalOperationAdapter() {

        @Override
        public void execute() throws Throwable {
            Site site = siteDao.retrieveSiteByIdentifier(identifier);
            if (persistentResult) {
                response[0] = site;
            } else {
                response[0] = getNonPersistentSite(site);
            }
        }
    }, RuntimeException.class, !TransactionSynchronizationManager.hasResource(((JpaTransactionManager) transUtil.getTransactionManager()).getEntityManagerFactory()));
    return response[0];
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) StreamCapableTransactionalOperationAdapter(org.broadleafcommerce.common.util.StreamCapableTransactionalOperationAdapter)

Example 7 with Site

use of org.broadleafcommerce.common.site.domain.Site in project BroadleafCommerce by BroadleafCommerce.

the class EntityDuplicatorImpl method copy.

@Override
public <T> T copy(Class<T> entityClass, Long id, Map<String, String> copyHints, EntityDuplicateModifier... modifiers) {
    genericEntityService.flush();
    genericEntityService.clear();
    Object entity = genericEntityService.readGenericEntity(entityClass, id);
    if (!(entity instanceof MultiTenantCloneable)) {
        IllegalArgumentException e = new IllegalArgumentException("Copying is only supported for classes implementing MultiTenantCloneable");
        LOG.error(String.format("Unable to duplicate entity %s:%s", entityClass.getName(), id), e);
        throw e;
    }
    boolean isValid = validate(entity);
    T dup;
    if (isValid) {
        try {
            Site currentSite = BroadleafRequestContext.getBroadleafRequestContext().getNonPersistentSite();
            MultiTenantCopyContext context = new MultiTenantCopyContext(null, null, currentSite, currentSite, genericEntityService, mtCopierExtensionManager);
            if (extensionManager != null) {
                ExtensionResultHolder<MultiTenantCopyContext> contextResponse = new ExtensionResultHolder<MultiTenantCopyContext>();
                extensionManager.setupDuplicate(entity, contextResponse);
                if (contextResponse.getResult() != null) {
                    context = contextResponse.getResult();
                }
            }
            dup = performCopy(context, (MultiTenantCloneable<T>) entity, copyHints, modifiers);
        } catch (Exception e) {
            LOG.error(String.format("Unable to duplicate entity %s:%s", entityClass.getName(), id), e);
            throw ExceptionHelper.refineException(e);
        } finally {
            if (extensionManager != null) {
                extensionManager.tearDownDuplicate();
            }
        }
    } else {
        LOG.error(String.format("Entity not valid for duplication - %s:%s", entityClass.getName(), id));
        throw new IllegalArgumentException(String.format("Entity not valid for duplication - %s:%s", entityClass.getName(), id));
    }
    return dup;
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) MultiTenantCloneable(org.broadleafcommerce.common.copy.MultiTenantCloneable) MultiTenantCopyContext(org.broadleafcommerce.common.copy.MultiTenantCopyContext) ExtensionResultHolder(org.broadleafcommerce.common.extension.ExtensionResultHolder)

Example 8 with Site

use of org.broadleafcommerce.common.site.domain.Site 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 9 with Site

use of org.broadleafcommerce.common.site.domain.Site in project BroadleafCommerce by BroadleafCommerce.

the class IdentityExecutionUtils method runOperationByIdentifier.

public static <T, G extends Throwable> T runOperationByIdentifier(IdentityOperation<T, G> operation, Site site, Site profile, Catalog catalog, PlatformTransactionManager transactionManager) throws G {
    IdentityUtilContext context = new IdentityUtilContext();
    context.setIdentifier(site);
    IdentityUtilContext.setUtilContext(context);
    BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
    Site previousSite = brc.getSite();
    Catalog previousCatalog = brc.getCurrentCatalog();
    Site previousProfile = brc.getCurrentProfile();
    boolean isNew = initRequestContext(site, profile, catalog);
    activateSession();
    TransactionContainer container = null;
    if (transactionManager != null) {
        container = establishTransaction(transactionManager);
    }
    boolean isError = false;
    try {
        return operation.execute();
    } catch (RuntimeException e) {
        isError = true;
        throw e;
    } finally {
        if (container != null) {
            finalizeTransaction(transactionManager, container, isError);
        }
        IdentityUtilContext.setUtilContext(null);
        if (isNew) {
            BroadleafRequestContext.setBroadleafRequestContext(null);
        }
        BroadleafRequestContext.getBroadleafRequestContext().setSite(previousSite);
        BroadleafRequestContext.getBroadleafRequestContext().setCurrentCatalog(previousCatalog);
        BroadleafRequestContext.getBroadleafRequestContext().setCurrentProfile(previousProfile);
    }
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) Catalog(org.broadleafcommerce.common.site.domain.Catalog)

Example 10 with Site

use of org.broadleafcommerce.common.site.domain.Site in project BroadleafCommerce by BroadleafCommerce.

the class IdentityExecutionUtils method runOperationAndIgnoreIdentifier.

public static <T, G extends Throwable> T runOperationAndIgnoreIdentifier(IdentityOperation<T, G> operation, PlatformTransactionManager transactionManager) throws G {
    BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
    Site previousSite = brc.getSite();
    Catalog previousCatalog = brc.getCurrentCatalog();
    Site previousProfile = brc.getCurrentProfile();
    boolean isNew = initRequestContext(null, null, null);
    boolean isIgnoringSite = BroadleafRequestContext.getBroadleafRequestContext().getIgnoreSite();
    BroadleafRequestContext.getBroadleafRequestContext().setIgnoreSite(true);
    activateSession();
    TransactionContainer container = null;
    if (transactionManager != null) {
        container = establishTransaction(transactionManager);
    }
    boolean isError = false;
    try {
        return operation.execute();
    } catch (RuntimeException e) {
        isError = true;
        throw e;
    } finally {
        if (container != null) {
            finalizeTransaction(transactionManager, container, isError);
        }
        if (isNew) {
            BroadleafRequestContext.setBroadleafRequestContext(null);
        }
        BroadleafRequestContext.getBroadleafRequestContext().setIgnoreSite(isIgnoringSite);
        BroadleafRequestContext.getBroadleafRequestContext().setSite(previousSite);
        BroadleafRequestContext.getBroadleafRequestContext().setCurrentCatalog(previousCatalog);
        BroadleafRequestContext.getBroadleafRequestContext().setCurrentProfile(previousProfile);
    }
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) Catalog(org.broadleafcommerce.common.site.domain.Catalog)

Aggregations

Site (org.broadleafcommerce.common.site.domain.Site)27 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)16 ArrayList (java.util.ArrayList)5 Locale (org.broadleafcommerce.common.locale.domain.Locale)4 SandBox (org.broadleafcommerce.common.sandbox.domain.SandBox)4 SiteImpl (org.broadleafcommerce.common.site.domain.SiteImpl)4 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)3 Catalog (org.broadleafcommerce.common.site.domain.Catalog)3 AdminUser (org.broadleafcommerce.openadmin.server.security.domain.AdminUser)3 File (java.io.File)2 Set (java.util.Set)2 TimeZone (java.util.TimeZone)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Element (net.sf.ehcache.Element)2 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)2 RequestDTOImpl (org.broadleafcommerce.common.RequestDTOImpl)2 BroadleafRequestedCurrencyDto (org.broadleafcommerce.common.currency.domain.BroadleafRequestedCurrencyDto)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1