All Downloads are FREE. Search and download functionalities are using the official Maven repository.

inti.ws.spring.resource.template.TemplateResource Maven / Gradle / Ivy

Go to download

An utility library for spring-based web-services for compressing / optimizing JS and CSS resources.

The newest version!
/**
 * Copyright 2012 the project-owners
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package inti.ws.spring.resource.template;

import inti.ws.spring.resource.WebResource;
import inti.ws.spring.resource.minify.Minifier;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.el.ExpressionFactory;
import javax.el.ValueExpression;
import javax.servlet.ServletContext;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.IOUtils;
import org.springframework.web.context.support.ServletContextResource;

import de.odysseus.el.util.SimpleContext;

public class TemplateResource extends WebResource {

    protected String template;
    protected Map parameters;
    protected String moduleName;
    protected List files = new ArrayList();
    SimpleContext context = new SimpleContext();
    ValueExpression content;

    public TemplateResource(ServletContext ctx, String moduleName, String containerLocation, String templateLocation,
                    List files, Map parameters, Minifier minifier, boolean minify)
                    throws Exception {
        super(ctx, containerLocation, null, minifier, minify);

        ServletContextResource resource;
        InputStream inputStream;

        this.moduleName = moduleName;
        this.parameters = parameters;
        if (files != null) {
            this.files.addAll(files);
        }

        if (templateLocation != null) {
            resource = new ServletContextResource(ctx, templateLocation);
            inputStream = resource.getInputStream();

            try {
                template = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
            } finally {
                inputStream.close();
            }
        }
    }

    @Override
    public boolean hasChanged() throws IOException {
        for (WebResource file : files) {
            if (file.hasChanged()) {
                return true;
            }
        }
        return super.hasChanged();
    }

    @Override
    public void update() throws Exception {
        ExpressionFactory factory;
        ValueExpression var;
        Object val;
        StringBuilder builder = new StringBuilder(2048);
        MessageDigest digest = DIGESTS.get();

        factory = ExpressionFactory.newInstance();

        for (WebResource file : files) {
            if (file.hasChanged()) {
                file.update();
            }
            builder.append(applyTemplate(factory, file.getName(), file.getContent().replaceAll("\\s+", " ")));
            builder.append(',');
        }
        builder.delete(builder.length() - 1, builder.length());

        super.update();

        content = factory.createValueExpression(context, compressedFile, String.class);

        var = factory.createValueExpression(context, "${files}", String.class);
        var.setValue(context, builder.toString());

        if (parameters != null) {

            for (Map.Entry parameter : parameters.entrySet()) {
                var = factory.createValueExpression(context, "${" + parameter.getKey() + '}', String.class);
                val = parameter.getValue();

                if ("$filename".equals(val)) {
                    val = resource.getFile();
                } else if ("$modulename".equals(val)) {
                    val = moduleName;
                }

                var.setValue(context, val);
            }

        }
        compressedFile = (String) content.getValue(context);
        builder.delete(0, builder.length());

        digest.reset();
        builder.append(Hex.encodeHexString(digest.digest(compressedFile.getBytes(StandardCharsets.UTF_8))));
        messageDigest = builder.toString();
        builder.delete(0, builder.length());

        DATE_FORMATTER.formatDate(lastModified, builder);
        lastModifiedString = builder.toString();
    }

    protected String applyTemplate(ExpressionFactory factory, String templateName, String templateFile) {
        ValueExpression var;
        Object val;
        SimpleContext templateContext = new SimpleContext();
        ValueExpression templateContent;

        templateContent = factory.createValueExpression(templateContext, template, String.class);
        var = factory.createValueExpression(templateContext, "${content}", String.class);
        var.setValue(templateContext, templateFile.replaceAll("\"", "\\\\\""));

        for (Map.Entry parameter : parameters.entrySet()) {
            var = factory.createValueExpression(templateContext, "${" + parameter.getKey() + '}', String.class);
            val = parameter.getValue();

            if ("$filename".equals(val)) {
                val = templateName;
            } else if ("$modulename".equals(val)) {
                val = moduleName;
            }

            var.setValue(templateContext, val);
        }

        return (String) templateContent.getValue(templateContext);
    }

    public Map getParameters() {
        return parameters;
    }

    public void setParameters(Map parameters) {
        this.parameters = parameters;
    }

    public void setTemplate(String template) {
        this.template = template;
    }

    public String getTemplate() {
        return template;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy