inti.ws.spring.resource.WebResourceSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ws-spring-resource Show documentation
Show all versions of ws-spring-resource Show documentation
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;
import inti.ws.spring.resource.minify.Minifier;
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.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 route, String... files)
throws MalformedURLException, URISyntaxException {
super(ctx, null, route, null, false);
if (files != null && files.length > 0) {
for (String file : files) {
this.files.add(new WebResource(ctx, file, null, minifier, minifier != null));
}
}
}
public WebResourceSet(String route, List files) throws URISyntaxException, MalformedURLException {
super(null, null, route, null, false);
if (files != null) {
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(StandardCharsets.UTF_8))));
messageDigest = builder.toString();
builder.delete(0, builder.length());
DATE_FORMATTER.formatDate(lastModified, builder);
lastModifiedString = builder.toString();
}
}