{"id":15602655,"url":"https://github.com/luanpotter/spark-decorator","last_synced_at":"2025-08-12T16:40:57.179Z","repository":{"id":145921448,"uuid":"142573351","full_name":"luanpotter/spark-decorator","owner":"luanpotter","description":"Simple and pretty decorators for your Spark routes.","archived":false,"fork":false,"pushed_at":"2018-09-09T11:06:59.000Z","size":27,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T01:49:15.849Z","etag":null,"topics":["decorators","routes","sparkjava"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luanpotter.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-27T12:17:21.000Z","updated_at":"2023-09-24T01:23:48.000Z","dependencies_parsed_at":"2023-04-09T19:02:18.483Z","dependency_job_id":null,"html_url":"https://github.com/luanpotter/spark-decorator","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/luanpotter/spark-decorator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luanpotter%2Fspark-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luanpotter%2Fspark-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luanpotter%2Fspark-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luanpotter%2Fspark-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luanpotter","download_url":"https://codeload.github.com/luanpotter/spark-decorator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luanpotter%2Fspark-decorator/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270099386,"owners_count":24527028,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-12T02:00:09.011Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decorators","routes","sparkjava"],"created_at":"2024-10-03T02:49:40.397Z","updated_at":"2025-08-12T16:40:57.135Z","avatar_url":"https://github.com/luanpotter.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# spark-decorator\n\n[![Build Status](https://travis-ci.org/luanpotter/spark-decorator.svg?branch=master)](https://travis-ci.org/luanpotter/spark-decorator) [![Coverage Status](https://coveralls.io/repos/github/luanpotter/spark-decorator/badge.svg?branch=master)](https://coveralls.io/github/luanpotter/spark-decorator?branch=master)\n\nSimple and pretty decorators for your routes.\n\nWant some routes to have auth, but some don't? Or base it off a parameter?\n\nSpark'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.\n\nThis libs helps you to decorate your routes in order to add custom behaviour to a selection of them, without duplication and hassles.\n\nYou can hook `before` and `after` filters and do anything you want!\n\n## Install\n\nOf course you must have Spark, we include it as provided (version 2.7.2).\n\nThen, add to your `pom.xml`:\n\n```xml\n    \u003cdependency\u003e\n        \u003cgroupId\u003exyz.luan.spark.decorator\u003c/groupId\u003e\n        \u003cartifactId\u003espark-decorator\u003c/artifactId\u003e\n        \u003cversion\u003e0.2.0\u003c/version\u003e\n    \u003c/dependency\u003e\n```\n\nOr, if you are using gradle:\n\n```groovy\n    compile 'xyz.luan.spark.decorator:spark-decorator:0.2.0'\n```\n\n## Usage\n\nYou can do a plethora of things using this, basically anything you would do in a before or after filter.\n\nFor 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).\n\n```java\n    public class AfterDecorator extends RouteDecorator {\n        public static final AfterDecorator secretPanda = new AfterDecorator();\n\n        private AfterDecorator() {\n        }\n\n        @Override\n        protected Route after() {\n            return (req, resp) -\u003e {\n                resp.header(\"secret\", \"panda\");\n                return null;\n            };\n        }\n    }\n```\n\nNow, in your controller/application, instead of\n```java\n    get(\"foo\", (req, resp) -\u003e \"bar\");\n```\n\nYou do:\n\n```java\n    secretPanda.get(\"/foo\", (req, resp) -\u003e \"bar\");\n```\n\nAnd your request has been enriched. You'd of course statically import `get` from `Spark`, and now also statically import `secretPanda` from your class.\n\nPretty neat, hum? Let's see a more complex example now!\n\n## Auth Example\n\nAssume 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:\n\n```java\npublic class AuthDecorator extends RouteDecorator {\n\n    public static AuthDecorator requiresRole(String role) {\n        return new AuthDecorator(role);\n    }\n\n    private String requiredRole;\n\n    private AuthDecorator(String requiredRole) {\n        this.requiredRole = requiredRole;\n    }\n\n    @Override\n    protected Route before() {\n        return (req, resp) -\u003e {\n            String auth = req.header(\"Auth\");\n            if (auth == null) {\n                if (requiredRole == null) {\n                    return null; // ok\n                }\n                throw halt(401, \"You must be authenticated to access.\");\n            }\n            String role = getProfile(auth).getRole();\n            if (!role.equals(requiredRole)) {\n                throw halt(403, \"You must be role \" + requiredRole + \" to access.\");\n            }\n            return null;\n        };\n    }\n}\n```\n\nThen, you can use it like so:\n\n```java\n    get(\"/foo\", (req, resp) -\u003e \"bar\");\n    requiresRole(\"user\").post(\"/foo\", (req, resp) -\u003e \"ok\");\n    requiresRole(\"admin\").delete(\"/foo\", (req, resp) -\u003e \"deleted\");\n```\n\n## Contributing\n\nWant to contribute? Please do so! Leave a star, open PR and issues! To submit a PR, it's really simple:\n\n* Fork it!\n* Create a feature branch.\n* Make your modifications\n* Open a Pull Request :)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluanpotter%2Fspark-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluanpotter%2Fspark-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluanpotter%2Fspark-decorator/lists"}