Search in sources :

Example 1 with AssetNotFoundException

use of org.broadleafcommerce.cms.common.AssetNotFoundException 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 2 with AssetNotFoundException

use of org.broadleafcommerce.cms.common.AssetNotFoundException in project BroadleafCommerce by BroadleafCommerce.

the class StaticAssetStorageServiceImpl method getCacheFileModel.

@Override
public Map<String, String> getCacheFileModel(String fullUrl, Map<String, String> parameterMap) throws Exception {
    StaticAsset staticAsset = findStaticAsset(fullUrl);
    if (staticAsset == null) {
        throw new AssetNotFoundException("Unable to find an asset for the url (" + fullUrl + ")");
    }
    String mimeType = staticAsset.getMimeType();
    // extract the values for any named parameters
    Map<String, String> convertedParameters = namedOperationManager.manageNamedParameters(parameterMap);
    String cachedFileName = constructCacheFileName(staticAsset, convertedParameters);
    // Look for a shared file (this represents a file that was based on a file originally in the classpath.
    File cacheFile = getFileFromLocalRepository(cachedFileName);
    if (cacheFile.exists()) {
        return buildModel(cacheFile.getAbsolutePath(), mimeType);
    }
    // Obtain the base file (that we may need to convert based on the parameters
    String baseCachedFileName = constructCacheFileName(staticAsset, null);
    File baseLocalFile = getFileFromLocalRepository(baseCachedFileName);
    if (!baseLocalFile.exists()) {
        if (broadleafFileService.checkForResourceOnClassPath(staticAsset.getFullUrl())) {
            cacheFile = broadleafFileService.getSharedLocalResource(cachedFileName);
            baseLocalFile = broadleafFileService.getSharedLocalResource(baseCachedFileName);
            createLocalFileFromClassPathResource(staticAsset, baseLocalFile);
        } else {
            baseLocalFile = lookupAssetAndCreateLocalFile(staticAsset, baseLocalFile);
        }
    }
    if (convertedParameters.isEmpty()) {
        return buildModel(baseLocalFile.getAbsolutePath(), mimeType);
    } else {
        FileInputStream assetStream = new FileInputStream(baseLocalFile);
        BufferedInputStream original = new BufferedInputStream(assetStream);
        original.mark(0);
        Operation[] operations = artifactService.buildOperations(convertedParameters, original, staticAsset.getMimeType());
        InputStream converted = artifactService.convert(original, operations, staticAsset.getMimeType());
        createLocalFileFromInputStream(converted, cacheFile);
        if ("image/gif".equals(mimeType)) {
            mimeType = "image/png";
        }
        return buildModel(cacheFile.getAbsolutePath(), mimeType);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) GloballySharedInputStream(org.broadleafcommerce.common.file.service.GloballySharedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StaticAsset(org.broadleafcommerce.cms.file.domain.StaticAsset) AssetNotFoundException(org.broadleafcommerce.cms.common.AssetNotFoundException) Operation(org.broadleafcommerce.openadmin.server.service.artifact.image.Operation) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileInputStream(java.io.FileInputStream)

Aggregations

AssetNotFoundException (org.broadleafcommerce.cms.common.AssetNotFoundException)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 StaticAsset (org.broadleafcommerce.cms.file.domain.StaticAsset)1 GloballySharedInputStream (org.broadleafcommerce.common.file.service.GloballySharedInputStream)1 BroadleafRequestContext (org.broadleafcommerce.common.web.BroadleafRequestContext)1 Operation (org.broadleafcommerce.openadmin.server.service.artifact.image.Operation)1 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1