use of org.broadleafcommerce.common.resource.GeneratedResource in project BroadleafCommerce by BroadleafCommerce.
the class BLCJSResourceResolver method convertResource.
protected Resource convertResource(Resource origResource, String resourceFileName) throws IOException {
byte[] bytes = FileCopyUtils.copyToByteArray(origResource.getInputStream());
String content = new String(bytes, DEFAULT_CHARSET);
String newContent = content;
if (!StringUtils.isEmpty(content)) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
newContent = newContent.replace("//BLC-SERVLET-CONTEXT", request.getContextPath());
String siteBaseUrl = urlResolver.getSiteBaseUrl();
if (!StringUtils.isEmpty(siteBaseUrl)) {
newContent = newContent.replace("//BLC-SITE-BASEURL", siteBaseUrl);
}
}
return new GeneratedResource(newContent.getBytes(), resourceFileName);
}
use of org.broadleafcommerce.common.resource.GeneratedResource in project BroadleafCommerce by BroadleafCommerce.
the class BLCSystemPropertyResourceResolver method convertResource.
protected Resource convertResource(Resource origResource, String resourceFileName) throws IOException {
byte[] bytes = FileCopyUtils.copyToByteArray(origResource.getInputStream());
String content = new String(bytes, DEFAULT_CHARSET);
String newContent = content;
if (!StringUtils.isEmpty(content)) {
String regexKey = "\\\"BLC_PROP:(.*)\\\"";
Pattern p = Pattern.compile(regexKey);
Matcher m = p.matcher(content);
while (m.find()) {
String matchedPlaceholder = m.group(0);
String propertyName = m.group(1);
String propVal = BLCSystemProperty.resolveSystemProperty(propertyName);
if (propVal == null) {
propVal = "";
}
newContent = newContent.replaceAll(matchedPlaceholder, '"' + propVal + '"');
}
}
return new GeneratedResource(newContent.getBytes(), resourceFileName);
}
use of org.broadleafcommerce.common.resource.GeneratedResource in project BroadleafCommerce by BroadleafCommerce.
the class ResourceMinificationServiceImpl method minify.
@Override
public Resource minify(Resource originalResource, String filename) {
if (!getEnabled()) {
LOG.trace("Minification is disabled, returning original resource");
return originalResource;
}
String type = getFileType(originalResource, filename);
if (type == null) {
LOG.info("Unsupported minification resource: " + filename);
return originalResource;
}
byte[] minifiedBytes = null;
try (BufferedReader in = new BufferedReader(new InputStreamReader(originalResource.getInputStream(), "utf-8"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(baos, "utf-8"))) {
minify(in, out, filename, type);
out.flush();
minifiedBytes = baos.toByteArray();
} catch (Exception e) {
LOG.warn("Could not minify resources, returned unminified bytes", e);
return originalResource;
}
return new GeneratedResource(minifiedBytes, filename);
}
use of org.broadleafcommerce.common.resource.GeneratedResource in project BroadleafCommerce by BroadleafCommerce.
the class AbstractGeneratedResourceHandler method getResource.
/**
* Attempts to retrive the requested resource from cache. If not cached, generates the resource, caches it,
* and then returns it
*
* @param request
* @param location
* @return the generated resource
*/
public Resource getResource(final String path, final List<Resource> locations) {
Element e = getGeneratedResourceCache().get(path);
Resource r = null;
if (e == null) {
statisticsService.addCacheStat(CacheStatType.GENERATED_RESOURCE_CACHE_HIT_RATE.toString(), false);
} else {
statisticsService.addCacheStat(CacheStatType.GENERATED_RESOURCE_CACHE_HIT_RATE.toString(), true);
}
boolean shouldGenerate = false;
if (e == null || e.getObjectValue() == null) {
shouldGenerate = true;
} else if (e.getObjectValue() instanceof GeneratedResource && isCachedResourceExpired((GeneratedResource) e.getObjectValue(), path, locations)) {
shouldGenerate = true;
} else {
r = (Resource) e.getObjectValue();
}
if (shouldGenerate) {
r = getFileContents(path, locations);
e = new Element(path, r);
getGeneratedResourceCache().put(e);
}
return r;
}
use of org.broadleafcommerce.common.resource.GeneratedResource in project BroadleafCommerce by BroadleafCommerce.
the class ResourceBundlingServiceImpl method createBundle.
protected Resource createBundle(String versionedBundleName, List<String> filePaths, ResourceResolverChain resolverChain, List<Resource> locations) {
HttpServletRequest req = BroadleafRequestContext.getBroadleafRequestContext().getRequest();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes = null;
// Join all of the resources for this bundle together into a byte[]
try {
for (String fileName : filePaths) {
Resource r = resolverChain.resolveResource(req, fileName, locations);
InputStream is = null;
if (r == null) {
LOG.warn(new StringBuilder().append("Could not resolve resource specified in bundle as [").append(fileName).append("]. Turn on trace logging to determine resolution failure. Skipping file.").toString());
} else {
try {
is = r.getInputStream();
StreamUtils.copy(is, baos);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(is);
}
// file to ensure it won't fail to compile.
if (versionedBundleName.endsWith(".js")) {
baos.write(";".getBytes());
}
baos.write(System.getProperty("line.separator").getBytes());
}
}
bytes = baos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(baos);
}
// Create our GenerateResource that holds our combined bundle
GeneratedResource r = new GeneratedResource(bytes, versionedBundleName);
return r;
}
Aggregations