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
- Host: GitHub
- URL: https://github.com/dropwizard-bundles/dropwizard-redirect-bundle
- Owner: dropwizard-bundles
- License: apache-2.0
- Created: 2015-03-09T22:54:06.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2018-07-13T20:00:25.000Z (almost 8 years ago)
- Last Synced: 2025-05-15T08:26:00.969Z (about 1 year ago)
- Topics: dropwizard-bundle
- Language: Java
- Homepage:
- Size: 70.3 KB
- Stars: 9
- Watchers: 4
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# dropwizard-redirect-bundle
A [Dropwizard](http://dropwizard.io) bundle that makes redirection a blast.
[](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")
));
}
// ...
}
```