io.dropwizard.bundles.webjars.WebJarBundle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dropwizard-webjars-bundle Show documentation
Show all versions of dropwizard-webjars-bundle Show documentation
Dropwizard bundle to make working with Webjars (http://webjars.org) easier.
package io.dropwizard.bundles.webjars;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists;
import io.dropwizard.Bundle;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import java.util.Collections;
import java.util.List;
public class WebJarBundle implements Bundle {
private CacheBuilder cacheBuilder = null;
private List packages = Lists.newArrayList(WebJarServlet.DEFAULT_MAVEN_GROUPS);
private String urlPrefix = WebJarServlet.DEFAULT_URL_PREFIX;
public WebJarBundle() {}
public WebJarBundle(CacheBuilder builder) {
cacheBuilder = builder;
}
public WebJarBundle(String... additionalPackages) {
Collections.addAll(packages, additionalPackages);
}
public WebJarBundle(CacheBuilder builder, String... additionalPackages) {
cacheBuilder = builder;
Collections.addAll(packages, additionalPackages);
}
public WebJarBundle withUrlPrefix(String prefix) {
urlPrefix = prefix;
return this;
}
private String normalizedUrlPrefix() {
final StringBuilder pathBuilder = new StringBuilder();
if (!urlPrefix.startsWith("/")) {
pathBuilder.append('/');
}
pathBuilder.append(urlPrefix);
if (!urlPrefix.endsWith("/")) {
pathBuilder.append('/');
}
return pathBuilder.toString();
}
@Override
public void initialize(Bootstrap> bootstrap) {
}
@Override
public void run(Environment environment) {
String prefix = normalizedUrlPrefix();
WebJarServlet servlet = new WebJarServlet(cacheBuilder, packages, prefix);
environment.servlets()
.addServlet("webjars", servlet)
.addMapping(prefix + "*");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy