An application I work on exposes various REST Web Services with the following pattern:
@RequestScoped
@Path("/path")
public class SomeResource {
@GET
public SomeResponse get(
@HeaderParam("authenticationHeader1") String authenticationHeader1,
@HeaderParam("authenticationHeader2") String authenticationHeader2,
@QueryParam("stuff") String stuff,
@QueryParam("moreStuff") String moreStuff)
{
final AuthenticationBean authBean = validateCredentials(authenticationHeader1, authenticationHeader2)
if (!authBean.isValid()) {
return someStronglyWordedResponse(authBean);
}
else {
return someProcessing(authBean, stuff, moreStuff);
}
}
Parsing, validation, and other handling of these authentication headers is done for nearly all resources.
I could make an abstract superclass and simply define validateCredentials()
there, as the AuthenticationBean
extraction is the same everywhere.
But this strikes me as moderately inelegant in this Java EE7 context, and more important, what if Jimmy forgets to add authentication management when coding a new resource?
Is there a recommended way to parse the HTTP headers of all requests no matter the target resource, and do some generic processing with the results?
Edit:
This app is using Resteasy. Sorry for not mentioning it in the first place. I would prefer to avoid implementation-dependent solutions, but Resteasy mechanisms are also an option.