Search in sources :

Example 31 with BroadleafRequestContext

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

Example 32 with BroadleafRequestContext

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

the class AbstractCacheMissAware method buildKey.

/**
 * Build the key representing this missed cache item. Will include sandbox and/or site information
 * if appropriate.
 *
 * @param params the appropriate params comprising a unique key for this cache item
 * @return the completed key
 */
protected String buildKey(String... params) {
    BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
    SandBox sandBox = null;
    if (context != null) {
        sandBox = context.getSandBox();
    }
    String key = StringUtils.join(params, '_');
    if (sandBox != null) {
        key = sandBox.getId() + "_" + key;
    }
    Site site = context.getNonPersistentSite();
    if (site != null) {
        key = key + "_" + site.getId();
    }
    return key;
}
Also used : SandBox(org.broadleafcommerce.common.sandbox.domain.SandBox) Site(org.broadleafcommerce.common.site.domain.Site) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext)

Example 33 with BroadleafRequestContext

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

the class AbstractCacheMissAware method getCachedObject.

/**
 * This is the main entry point for retrieving an object from this cache.
 *
 * @see org.broadleafcommerce.common.cache.StatisticsService
 * @param responseClass the class representing the type of the cache item
 * @param cacheName the name of the cache - the ehcache region name
 * @param statisticsName the name to use for cache hit statistics
 * @param retrieval the block of code to execute if a cache miss is not found in this cache
 * @param params the appropriate params comprising a unique key for this cache item
 * @param <T> the type of the cache item
 * @return The object retrieved from the executiom of the PersistentRetrieval, or null if a cache miss was found in this cache
 */
protected <T> T getCachedObject(Class<T> responseClass, String cacheName, String statisticsName, PersistentRetrieval<T> retrieval, String... params) {
    T nullResponse = getNullObject(responseClass);
    BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
    String key = buildKey(params);
    T response = null;
    boolean allowL2Cache = false;
    if (context != null) {
        allowL2Cache = context.isProductionSandBox() || (context.getAdditionalProperties().containsKey("allowLevel2Cache") && (Boolean) context.getAdditionalProperties().get("allowLevel2Cache"));
    }
    if (allowL2Cache) {
        response = getObjectFromCache(key, cacheName);
    }
    if (response == null) {
        response = retrieval.retrievePersistentObject();
        if (response == null) {
            response = nullResponse;
        }
        // only handle null, non-hits. Otherwise, let level 2 cache handle it
        if (allowL2Cache && response.equals(nullResponse)) {
            statisticsService.addCacheStat(statisticsName, false);
            getCache(cacheName).put(new Element(key, response));
            if (getLogger().isTraceEnabled()) {
                getLogger().trace("Caching [" + key + "] as null in the [" + cacheName + "] cache.");
            }
        }
    } else {
        statisticsService.addCacheStat(statisticsName, true);
    }
    if (response.equals(nullResponse)) {
        return null;
    }
    return response;
}
Also used : BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext) Element(net.sf.ehcache.Element)

Example 34 with BroadleafRequestContext

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

the class AuditableListener method setAuditValueAgent.

@Override
protected void setAuditValueAgent(Field field, Object entity) throws IllegalArgumentException, IllegalAccessException {
    try {
        BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
        if (context != null && context.getAdmin() && context.getAdminUserId() != null) {
            field.setAccessible(true);
            field.set(entity, context.getAdminUserId());
        } else if (context != null && context.getWebRequest() != null) {
            Long customerId = 0L;
            Object customer = BroadleafRequestCustomerResolverImpl.getRequestCustomerResolver().getCustomer();
            if (customer != null) {
                Class<?> customerClass = customer.getClass();
                Field userNameField = BLCFieldUtils.getSingleField(customerClass, "username");
                userNameField.setAccessible(true);
                String username = (String) userNameField.get(customer);
                if (username != null) {
                    // the customer has been persisted
                    Field idField = BLCFieldUtils.getSingleField(customerClass, "id");
                    idField.setAccessible(true);
                    customerId = (Long) idField.get(customer);
                }
            }
            field.setAccessible(true);
            field.set(entity, customerId);
        }
    } catch (Exception e) {
        LOG.error("Error setting audit field.", e);
    }
}
Also used : Field(java.lang.reflect.Field) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext)

Example 35 with BroadleafRequestContext

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

the class FileSystemFileServiceProvider method getSiteDirectory.

/**
 * Creates a unique directory on the file system for each site.
 * Each site may be in one of 255 base directories.   This model efficiently supports up to 65,000 sites
 * served from a single file system based on most OS systems ability to quickly access files as long
 * as there are not more than 255 directories.
 *
 * @param The starting directory for local files which must end with a '/';
 */
protected String getSiteDirectory(String baseDirectory) {
    BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext();
    if (brc != null) {
        Site site = brc.getSite();
        if (site != null) {
            String siteDirectory = "site-" + site.getId();
            String siteHash = DigestUtils.md5Hex(siteDirectory);
            String sitePath = FilenameUtils.concat(siteHash.substring(0, 2), siteDirectory);
            return FilenameUtils.concat(baseDirectory, sitePath);
        }
    }
    return baseDirectory;
}
Also used : Site(org.broadleafcommerce.common.site.domain.Site) BroadleafRequestContext(org.broadleafcommerce.common.web.BroadleafRequestContext)

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