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

io.paradoxical.dropwizard.swagger.bundles.AdminAssetsBundle Maven / Gradle / Ivy

There is a newer version: 2.4
Show newest version
package io.paradoxical.dropwizard.swagger.bundles;

import com.google.common.base.Charsets;
import io.dropwizard.Bundle;
import io.dropwizard.servlets.assets.AssetServlet;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.paradoxical.dropwizard.swagger.AssetsDefinition;
import lombok.Getter;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.checkArgument;

/**
 * A bundle for serving static asset files from the classpath.
 */
@Getter
public class AdminAssetsBundle implements Bundle {
    private static final Logger LOGGER = LoggerFactory.getLogger(AdminAssetsBundle.class);

    private static final String DEFAULT_ASSETS_NAME = "assets";
    private static final String DEFAULT_INDEX_FILE = "index.htm";
    private static final String DEFAULT_PATH = "/assets";

    public static final AssetsDefinition Defaults =
            AssetsDefinition.builder()
                            .resourcePath(DEFAULT_PATH)
                            .uriPath(DEFAULT_PATH)
                            .assetsName(DEFAULT_ASSETS_NAME)
                            .indexFile(DEFAULT_INDEX_FILE)
                            .build();

    private final String resourcePath;
    private final String uriPath;
    private final String indexFile;
    private final String assetsName;

    public AdminAssetsBundle(@NonNull @Nonnull final AssetsDefinition assetsDefinition) {
        this(assetsDefinition.resourcePath(),
             assetsDefinition.uriPath(),
             assetsDefinition.indexFile(),
             assetsDefinition.assetsName());
    }

    /**
     * Creates a new AdminAssetsBundle which serves up static assets from
     * {@code src/main/resources/assets/*} as {@code /assets/*}.
     *
     * @see AdminAssetsBundle#AdminAssetsBundle(String, String, String)
     */
    public AdminAssetsBundle() {
        this(Defaults);
    }

    /**
     * Creates a new AdminAssetsBundle which will configure the application to serve the static files
     * located in {@code src/main/resources/${path}} as {@code /${path}}. For example, given a
     * {@code path} of {@code "/assets"}, {@code src/main/resources/assets/example.js} would be
     * served up from {@code /assets/example.js}.
     *
     * @param path the classpath and URI root of the static asset files
     * @see AdminAssetsBundle#AdminAssetsBundle(String, String, String)
     */
    public AdminAssetsBundle(String path) {
        this(Defaults.toBuilder()
                     .resourcePath(path)
                     .uriPath(path)
                     .build());
    }

    /**
     * Creates a new AdminAssetsBundle which will configure the application to serve the static files
     * located in {@code src/main/resources/${resourcePath}} as {@code /${uriPath}}. For example, given a
     * {@code resourcePath} of {@code "/assets"} and a uriPath of {@code "/js"},
     * {@code src/main/resources/assets/example.js} would be served up from {@code /js/example.js}.
     *
     * @param resourcePath the resource path (in the classpath) of the static asset files
     * @param uriPath      the uri path for the static asset files
     * @see AdminAssetsBundle#AdminAssetsBundle(String, String, String)
     */
    public AdminAssetsBundle(String resourcePath, String uriPath) {
        this(Defaults.toBuilder()
                     .resourcePath(resourcePath)
                     .uriPath(uriPath)
                     .build());
    }

    /**
     * Creates a new AdminAssetsBundle which will configure the application to serve the static files
     * located in {@code src/main/resources/${resourcePath}} as {@code /${uriPath}}. If no file name is
     * in ${uriPath}, ${indexFile} is appended before serving. For example, given a
     * {@code resourcePath} of {@code "/assets"} and a uriPath of {@code "/js"},
     * {@code src/main/resources/assets/example.js} would be served up from {@code /js/example.js}.
     *
     * @param resourcePath the resource path (in the classpath) of the static asset files
     * @param uriPath      the uri path for the static asset files
     * @param indexFile    the name of the index file to use
     */
    public AdminAssetsBundle(String resourcePath, String uriPath, String indexFile) {
        this(Defaults.toBuilder()
                     .resourcePath(resourcePath)
                     .uriPath(uriPath)
                     .indexFile(indexFile)
                     .build());
    }

    /**
     * Creates a new AdminAssetsBundle which will configure the application to serve the static files
     * located in {@code src/main/resources/${resourcePath}} as {@code /${uriPath}}. If no file name is
     * in ${uriPath}, ${indexFile} is appended before serving. For example, given a
     * {@code resourcePath} of {@code "/assets"} and a uriPath of {@code "/js"},
     * {@code src/main/resources/assets/example.js} would be served up from {@code /js/example.js}.
     *
     * @param resourcePath the resource path (in the classpath) of the static asset files
     * @param uriPath      the uri path for the static asset files
     * @param indexFile    the name of the index file to use
     * @param assetsName   the name of servlet mapping used for this assets bundle
     */
    public AdminAssetsBundle(String resourcePath, String uriPath, String indexFile, String assetsName) {
        checkArgument(resourcePath.startsWith("/"), "%s is not an absolute path", resourcePath);
        checkArgument(!"/".equals(resourcePath), "%s is the classpath root", resourcePath);
        this.resourcePath = resourcePath.endsWith("/") ? resourcePath : (resourcePath + '/');
        this.uriPath = uriPath.endsWith("/") ? uriPath : (uriPath + '/');
        this.indexFile = indexFile;
        this.assetsName = assetsName;
    }

    @Override
    public void initialize(Bootstrap bootstrap) {
        // nothing doing
    }

    @Override
    public void run(Environment environment) {
        LOGGER.info("Registering AdminAssetBundle with name: {} for path {}", assetsName, uriPath + '*');

        environment.admin()
                   .addServlet(assetsName, createServlet())
                   .addMapping(uriPath + '*');
    }

    protected AssetServlet createServlet() {
        return new AssetServlet(resourcePath, uriPath, indexFile, Charsets.UTF_8);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy