An open API service indexing awesome lists of open source software.

https://github.com/dropwizard-bundles/dropwizard-redirect-bundle

A simple bundle for Dropwizard that allows for HTTP redirects
https://github.com/dropwizard-bundles/dropwizard-redirect-bundle

dropwizard-bundle

Last synced: 5 months ago
JSON representation

A simple bundle for Dropwizard that allows for HTTP redirects

Awesome Lists containing this project

README

          

# dropwizard-redirect-bundle

A [Dropwizard](http://dropwizard.io) bundle that makes redirection a blast.

[![Build Status](https://travis-ci.org/dropwizard-bundles/dropwizard-redirect-bundle.png)](https://travis-ci.org/dropwizard-bundles/dropwizard-redirect-bundle)

## Getting Started

Just add this maven dependency:
```xml

io.dropwizard-bundles
dropwizard-redirect-bundle
1.0.5

```

To redirect one path to another path:
```java
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:
```java
public class MyApplication extends Application<...> {
// ...

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

// ...
}
```

## Non-HTTPS to HTTPS Redirect

To redirect non-HTTPS traffic to the HTTPS port:
```java
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:
```java
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:
```java
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:
```java
public class MyApplication extends Application<...> {
// ...

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

// ...
}
```