inti.ws.spring.resource.CommonMinifyableConfig 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.
/**
* 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.config.ConfigParser;
import inti.ws.spring.resource.config.ConfigParserSettings;
import inti.ws.spring.resource.minify.Minifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.servlet.ServletContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Component
@Scope("prototype")
public class CommonMinifyableConfig implements ConfigParser {
@Inject
protected ApplicationContext context;
@Inject
protected ServletContext ctx;
protected ConfigParserSettings settings;
protected Minifier minifier;
@SuppressWarnings("unchecked")
@Override
public Map instanceWebResources(ObjectMapper mapper, JsonNode node) throws Exception {
return parseJs(mapper.convertValue(node, Map.class));
}
@Override
public void configureWebResources(ObjectMapper mapper, JsonNode node, Map resources,
Map> resourceStack) throws Exception {
}
protected Map parseJs(Map> node) throws Exception {
Map resources;
List files;
resources = new HashMap();
for (Map.Entry> entry : node.entrySet()) {
files = new ArrayList();
for (Map.Entry file : entry.getValue().entrySet()) {
files.add(new WebResource(ctx, file.getKey(), minifier, file.getValue()));
}
resources.put(entry.getKey(), new WebResourceSet(files));
}
return resources;
}
@Override
public ConfigParserSettings getConfigParserSettings() {
return settings;
}
@Override
public void setConfigParserSettings(ConfigParserSettings settings) {
this.settings = settings;
minifier = context.getBean(settings.getMinifier());
}
}