inti.ws.spring.resource.FilteredWebResource.java Source code

Java tutorial

Introduction

Here is the source code for inti.ws.spring.resource.FilteredWebResource.java

Source

/**
 * 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;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
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 de.odysseus.el.util.SimpleContext;

public class FilteredWebResource extends WebResource {

    protected SimpleContext context;
    protected ValueExpression content;
    protected Map<String, Object> parameters = new HashMap<String, Object>();
    protected List<WebResource> dependencies = new ArrayList<WebResource>();

    public FilteredWebResource(ServletContext ctx, String location, String route)
            throws URISyntaxException, MalformedURLException {
        super(ctx, location, route, null, false);
        context = new SimpleContext();
    }

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

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

        for (WebResource dependency : dependencies) {
            dependency.updateIfNeeded();
        }

        super.update();

        factory = ExpressionFactory.newInstance();
        content = factory.createValueExpression(context, compressedFile, String.class);
        for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
            var = factory.createValueExpression(context, "${" + parameter.getKey() + '}', String.class);
            val = parameter.getValue();
            if (val instanceof WebResource) {
                ((WebResource) val).updateIfNeeded();
                val = ((WebResource) val).getContent().hashCode();
            }
            var.setValue(context, val);
        }
        compressedFile = (String) content.getValue(context);

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

    public List<WebResource> getDependencies() {
        return dependencies;
    }

    public Map<String, Object> getParameters() {
        return parameters;
    }

}