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

inti.ws.spring.resource.WebResourceSet Maven / Gradle / Ivy

Go to download

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

There is a newer version: 1.6
Show 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;

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

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.apache.commons.codec.binary.Hex;

public class WebResourceSet extends WebResource {
    
    protected List files = new ArrayList();
    
    public WebResourceSet(ServletContext ctx, Minifier minifier, String... files) throws MalformedURLException,
            URISyntaxException {
        super(ctx, null, null, false);
        if (files != null && files.length > 0) {
            for (String file : files) {
                this.files.add(new WebResource(ctx, file, minifier, minifier != null));
            }
        }
    }
    
    public WebResourceSet(List files) throws URISyntaxException, MalformedURLException {
        super(null, null, null, false);
        this.files.addAll(files);
    }
    
    @Override
    public boolean hasChanged() throws IOException {
        for (WebResource file : files) {
            if (file.hasChanged()) {
                return true;
            }
        }
        return false;
    }
    
    @Override
    public void update() throws IOException, Exception {
        StringBuilder builder = new StringBuilder(2048);
        MessageDigest digest = digests.get();
        
        for (WebResource file : files) {
            if (file.hasChanged()) {
                file.update();
            }
            builder.append(file.getContent());
        }
        
        compressedFile = builder.toString();
        builder.delete(0, builder.length());
        
        digest.reset();
        builder.append(Hex.encodeHexString(digest.digest(compressedFile.getBytes(UTF8))));
        messageDigest = builder.toString();
        builder.delete(0, builder.length());
        
        DATE_FORMATTER.formatDate(lastModified, builder);
        lastModifiedString = builder.toString();
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy