Search in sources :

Example 1 with GeneratedResource

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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) GeneratedResource(org.broadleafcommerce.common.resource.GeneratedResource)

Example 2 with GeneratedResource

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);
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) GeneratedResource(org.broadleafcommerce.common.resource.GeneratedResource)

Example 3 with GeneratedResource

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);
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GeneratedResource(org.broadleafcommerce.common.resource.GeneratedResource) EvaluatorException(org.mozilla.javascript.EvaluatorException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 4 with GeneratedResource

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;
}
Also used : Element(net.sf.ehcache.Element) GeneratedResource(org.broadleafcommerce.common.resource.GeneratedResource) Resource(org.springframework.core.io.Resource) GeneratedResource(org.broadleafcommerce.common.resource.GeneratedResource)

Example 5 with GeneratedResource

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;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) GeneratedResource(org.broadleafcommerce.common.resource.GeneratedResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GeneratedResource(org.broadleafcommerce.common.resource.GeneratedResource)

Aggregations

GeneratedResource (org.broadleafcommerce.common.resource.GeneratedResource)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 Resource (org.springframework.core.io.Resource)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Element (net.sf.ehcache.Element)1 EvaluatorException (org.mozilla.javascript.EvaluatorException)1 FileSystemResource (org.springframework.core.io.FileSystemResource)1 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)1