Skip to content

Latest commit

 

History

History
121 lines (97 loc) · 2.62 KB

readme.md

File metadata and controls

121 lines (97 loc) · 2.62 KB

dropwizard-redirect-bundle

A Dropwizard bundle that makes redirection a blast.

Build Status

Getting Started

Just add this maven dependency:

<dependency>
  <groupId>io.dropwizard-bundles</groupId>
  <artifactId>dropwizard-redirect-bundle</artifactId>
  <version>1.0.5</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())
    ));
  }

  // ...
}

Non-HTTPS to HTTPS Redirect

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()
    ));
  }

  // ...
}

To redirect non-HTTPS traffic to HTTPS and redirect a path to another path:

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

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

  // ...
}

Regular Expression Redirect

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")
    ));
  }

  // ...
}

To redirect non-HTTPS traffic to HTTPS in Dropwizard v0.9.0, use a Regular Expression Redirect:

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

  @Override
  public void initialize(Bootstrap<?> bootstrap) {
    bootstrap.addBundle(new RedirectBundle(
      new UriRedirect("http://localhost:8080(.*)$", "https://localhost:8443$1")
    ));
  }

  // ...
}