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();
}
}
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);
}
}
Aggregations