I want to be able to return a specific jsp page from restful service given the page name as a PathParam. These pages will be added and removed by a different group of people after the application is deployed.
I believe using the Viewable object is a decent way to go (I’m open to a better/correct way.)
@GET
@Produces(MediaType.TEXT_HTML)
@Path("{page}")
public Response showJSP(@PathParam("page") String page) {
Viewable loadPage = null;
try {
loadPage = new Viewable("/" + page, null);
} catch (Exception e) {
return Response.status(404).build();
}
Response ret = Response.ok(loadPage).build();
// if (ret.getStatus() == 500){
// ret = Response.status(404).build();
// }
return ret;
}
The above is my experimental code and contains my attempts to handle the error.
This works fine as long as the page name is a valid jsp page. If I pass in an invalid page I get
500 Internal Server Error
java.io.IOException: The template name, /someWrongFileName, could not be resolved to a fully qualified template name.....
What I’ve figured out is this error is generated internal to the Viewable object so it’s not throwing an exception and the Response of course is a valid page so checking for the 500 status doesn’t work.
I can hack in several things that I’m pretty sure are wrong such as, I really do not want to toString the generated page and regex for the error text.
I would like to be able to detect that the page is invalid and simply return a 404.
Am I on the right path or is there a better way?
If I’m on the right path how do I detect the bad page name?
I found what I’m looking for in another post and modified my method below.
This gives me my 404 that I’m looking for and all of my services still work.
If this is the ‘wrong’ way to do this I’m still open for ideas but for now it works.