https://github.com/luanpotter/spark-decorator
Simple and pretty decorators for your Spark routes.
https://github.com/luanpotter/spark-decorator
decorators routes sparkjava
Last synced: 10 months ago
JSON representation
Simple and pretty decorators for your Spark routes.
- Host: GitHub
- URL: https://github.com/luanpotter/spark-decorator
- Owner: luanpotter
- License: mit
- Created: 2018-07-27T12:17:21.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-09T11:06:59.000Z (over 7 years ago)
- Last Synced: 2025-03-31T01:49:15.849Z (about 1 year ago)
- Topics: decorators, routes, sparkjava
- Language: Java
- Size: 26.4 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# spark-decorator
[](https://travis-ci.org/luanpotter/spark-decorator) [](https://coveralls.io/github/luanpotter/spark-decorator?branch=master)
Simple and pretty decorators for your routes.
Want some routes to have auth, but some don't? Or base it off a parameter?
Spark's `before` and `after` work great if the distinction your are making is by path. But what if it's not? Maybe some users can GET but cannot POST on the same route.
This libs helps you to decorate your routes in order to add custom behaviour to a selection of them, without duplication and hassles.
You can hook `before` and `after` filters and do anything you want!
## Install
Of course you must have Spark, we include it as provided (version 2.7.2).
Then, add to your `pom.xml`:
```xml
xyz.luan.spark.decorator
spark-decorator
0.2.0
```
Or, if you are using gradle:
```groovy
compile 'xyz.luan.spark.decorator:spark-decorator:0.2.0'
```
## Usage
You can do a plethora of things using this, basically anything you would do in a before or after filter.
For example, let's add a secret header to some of our routes; create a `RouteDecorator` instance with a singleton instance (this is to follow Spark's convention and is optional).
```java
public class AfterDecorator extends RouteDecorator {
public static final AfterDecorator secretPanda = new AfterDecorator();
private AfterDecorator() {
}
@Override
protected Route after() {
return (req, resp) -> {
resp.header("secret", "panda");
return null;
};
}
}
```
Now, in your controller/application, instead of
```java
get("foo", (req, resp) -> "bar");
```
You do:
```java
secretPanda.get("/foo", (req, resp) -> "bar");
```
And your request has been enriched. You'd of course statically import `get` from `Spark`, and now also statically import `secretPanda` from your class.
Pretty neat, hum? Let's see a more complex example now!
## Auth Example
Assume each route might or might not require auth, and the ones that require auth require a specific role to access. You could create a decorator like so:
```java
public class AuthDecorator extends RouteDecorator {
public static AuthDecorator requiresRole(String role) {
return new AuthDecorator(role);
}
private String requiredRole;
private AuthDecorator(String requiredRole) {
this.requiredRole = requiredRole;
}
@Override
protected Route before() {
return (req, resp) -> {
String auth = req.header("Auth");
if (auth == null) {
if (requiredRole == null) {
return null; // ok
}
throw halt(401, "You must be authenticated to access.");
}
String role = getProfile(auth).getRole();
if (!role.equals(requiredRole)) {
throw halt(403, "You must be role " + requiredRole + " to access.");
}
return null;
};
}
}
```
Then, you can use it like so:
```java
get("/foo", (req, resp) -> "bar");
requiresRole("user").post("/foo", (req, resp) -> "ok");
requiresRole("admin").delete("/foo", (req, resp) -> "deleted");
```
## Contributing
Want to contribute? Please do so! Leave a star, open PR and issues! To submit a PR, it's really simple:
* Fork it!
* Create a feature branch.
* Make your modifications
* Open a Pull Request :)