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

io.dropwizard.bundles.assets.AssetsConfiguration Maven / Gradle / Ivy

There is a newer version: 1.3.5
Show newest version
package io.dropwizard.bundles.assets;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.util.Collections;
import java.util.Map;
import javax.validation.constraints.NotNull;

@JsonDeserialize(builder = AssetsConfiguration.Builder.class)
public class AssetsConfiguration {
  public static final String SLASH = "/";

  private Map mappings = Maps.newHashMap();

  /**
   * Initialize cacheSpec to null so that whatever may be specified by code is able to be
   * by configuration. If null the default cache spec of "maximumSize=100" will be used.
   *
   * @see ConfiguredAssetsBundle#DEFAULT_CACHE_SPEC
   */

  private String cacheSpec = null;

  @NotNull
  private Map overrides = Maps.newHashMap();

  @NotNull
  private Map mimeTypes = Maps.newHashMap();

  private String cacheControlHeader = null;

  private Map resourcePathToUriMappings;

  private AssetsConfiguration(
      String cacheControlHeader,
      String cacheSpec,
      Map mappings,
      Map mimeTypes,
      Map overrides) {

    this.cacheControlHeader = cacheControlHeader;
    this.cacheSpec = cacheSpec;
    this.mappings = Collections.unmodifiableMap(mappings);
    this.mimeTypes = Collections.unmodifiableMap(mimeTypes);
    this.overrides = Collections.unmodifiableMap(overrides);
  }

  /**
   * A series of mappings from resource paths (in the classpath)
   * to the uri path that hosts the resource
   * @return The resourcePathToUriMappings.
   */
  public Map getResourcePathToUriMappings() {
    if (resourcePathToUriMappings == null) {
      ImmutableMap.Builder mapBuilder = ImmutableMap.builder();
      // Ensure that resourcePath and uri ends with a '/'
      for (Map.Entry mapping : mappings().entrySet()) {
        mapBuilder
            .put(ensureEndsWithSlash(mapping.getKey()), ensureEndsWithSlash(mapping.getValue()));
      }
      resourcePathToUriMappings = mapBuilder.build();
    }
    return resourcePathToUriMappings;
  }

  private String ensureEndsWithSlash(String value) {
    return value != null ? (value.endsWith(SLASH) ? value : value + SLASH) : SLASH;
  }

  protected Map mappings() {
    return mappings;
  }

  /**
   * The caching specification for how to memoize assets.
   *
   * @return The cacheSpec.
   */
  public String getCacheSpec() {
    return cacheSpec;
  }

  public Map getOverrides() {
    return Collections.unmodifiableMap(overrides);
  }

  public Map getMimeTypes() {
    return Collections.unmodifiableMap(mimeTypes);
  }

  public String getCacheControlHeader() {
    return cacheControlHeader;
  }

  public static Builder builder() {
    return new Builder();
  }

  public static final class Builder {
    @JsonProperty
    private String cacheControlHeader;
    @JsonProperty
    private String cacheSpec;
    @JsonProperty
    private Map mappings = Maps.newHashMap();
    @JsonProperty
    private Map mimeTypes = Maps.newHashMap();
    @JsonProperty
    private Map overrides = Maps.newHashMap();

    private Builder() {}

    public Builder cacheControlHeader(String cacheControlHeader) {
      this.cacheControlHeader = cacheControlHeader;
      return this;
    }

    public Builder cacheSpec(String cacheSpec) {
      this.cacheSpec = cacheSpec;
      return this;
    }

    public Builder mappings(Map mappings) {
      this.mappings = Preconditions.checkNotNull(mappings);
      return this;
    }

    public Builder mimeTypes(Map mimeTypes) {
      this.mimeTypes = Preconditions.checkNotNull(mimeTypes);
      return this;
    }

    public Builder overrides(Map overrides) {
      this.overrides = Preconditions.checkNotNull(overrides);
      return this;
    }

    public AssetsConfiguration build() {
      return new AssetsConfiguration(cacheControlHeader, cacheSpec, mappings, mimeTypes, overrides);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy