Home

Awesome

NOTE: THIS PROJECT IS DEPRECATED. This projects is no longer maintained. It is deprecated for dropwizard-bundles maintained fork. Users of this project should update their project dependencies appropriately.

Getting Started

Just add this maven dependency:

<dependency>
  <groupId>com.bazaarvoice.dropwizard</groupId>
  <artifactId>dropwizard-redirect-bundle</artifactId>
  <version>0.4.0</version>
</dependency>

To redirect one path to another path:

public class MyApplication extends Application<...> {
  // ...

  @Override
  public void initialize(Bootstrap<?> bootstrap) {
    bootstrap.addBundle(new RedirectBundle(
      new PathRedirect("/old", "/new")
    ));
  }

  // ...
}

To redirect many paths at once:

public class MyApplication extends Application<...> {
  // ...

  @Override
  public void initialize(Bootstrap<?> bootstrap) {
    bootstrap.addBundle(new RedirectBundle(
      new PathRedirect(ImmutableMap.<String, String>builder()
        .put("/old1", "/new1")
        .put("/old2", "/new2")
        .build())
    ));
  }

  // ...
}

To redirect non-HTTPS traffic to the HTTPS port:

public class MyApplication extends Application<...> {
  // ...

  @Override
  public void initialize(Bootstrap<?> bootstrap) {
    bootstrap.addBundle(new RedirectBundle(
      new HttpsRedirect()
    ));
  }

  // ...
}

For more advanced users, there is also a regular expression based redirector that has access to the full URI. This operates in a similar fashion to the mod-rewrite module for Apache:

public class MyApplication extends Application<...> {
  // ...

  @Override
  public void initialize(Bootstrap<?> bootstrap) {
    bootstrap.addBundle(new RedirectBundle(
      new UriRedirect("(.*)/welcome.html$", "$1/index.html")
    ));
  }

  // ...
}

If you have to combine http to https redirect and path redirect at the same time, then you want to do the following:

public class MyApplication extends Application<...> {
  // ...

  @Override
  public void initialize(Bootstrap<?> bootstrap) {
    bootstrap.addBundle(new RedirectBundle(
        bootstrap.addBundle(new RedirectBundle(new PathRedirect("/", "docs"), new HttpsRedirect()));
    ));
  }

  // ...
}