I would start by including the rewrite-integration-cdi
module:
https://github.com/ocpsoft/rewrite/blob/master/documentation/src/main/asciidoc/integration/cdi.asciidoc
Then create a CDI bean/Java class that can access your database.
Inject an instance of that class into your Rewrite ConfigurationProvider
, then use it to build / create your Rewrite rules.
Here is an example of both @Inject
ing beans into your ConfigurationProvider, and also defining custom HTTP operations:
https://github.com/ocpsoft/rewrite/blob/master/showcase/rest-ws/src/main/java/org/ocpsoft/rewrite/showcase/rest/RestRewriteConfiguration.java
Depending on how dynamic you want your database lookups to be, you could either preload them at startup time (when the Config is built), or load & cache them inside the Request/Response lifecycle itself using a custom HttpCondition
and `HttpOperation:
https://github.com/ocpsoft/rewrite/blob/master/api-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/HttpCondition.java
https://github.com/ocpsoft/rewrite/blob/master/api-servlet/src/main/java/org/ocpsoft/rewrite/servlet/config/HttpOperation.java
Then use those operations in your ConfigurationProvider
to perform the database operations. Essentially you will do something like this, but check the database to see if the requested URL is a ‘known/stored’ redirect:
https://github.com/ocpsoft/rewrite/blob/851ccbabb8b6248c66589076fa67cb0ec07b3132/impl-servlet-tests/src/test/java/org/ocpsoft/rewrite/servlet/config/HttpRequestParameterTestProvider.java#L45
And then use an custom HttpOperation
to perform the actions you want to take.
https://github.com/ocpsoft/rewrite/blob/851ccbabb8b6248c66589076fa67cb0ec07b3132/config-servlet/src/test/java/org/ocpsoft/rewrite/servlet/config/JoinBindingConfigurationProvider.java#L48
You could also technically just use a Condition
that matches all requests, and an HttpOperation
That only takes action if there is a database entry.
All that said, this sounds more complicated than it is, but there’s no good example I can find to link to that does exactly what you want, so I’m trying to piece it together.