I am using websockets to pass information about a resource from the server to the client. The websocket Endpoint uses a pathparam to identify the specific resource. For example:
@ServerEndpoint("/resources/{resource-id}/updates")
The Endpoint class has the following logic in its onOpen
method:
@OnOpen
public void onOpen(@PathParam("resource-id")int resourceId, Session session) throws IOException {
boolean resourceExists = checkIfResourceExists(resourceId);
if (!resourceExists) {
// what should I do here?
}
}
When using AJAX long-polling, I'd send a 404 response if the resource doesn't exist. The client would then know not to try again. Is there an equivalent technique to use for websockets? My two thoughts are:
Use the following code in the
if (!resourceExists)
block, above:session.close(new CloseReason(CUSTOM_NOT_FOUND_CODE, "Resource ID not found")); return;
Where
CUSTOM_NOT_FOUND_CODE
is a CloseCode in the 4000-4999 range.Perform this logic somewhere in the
ServerEndpointConfig.Configurator
implementation for this Endpoint and return a 404 there before the connection upgrade occurs. This sounds like the best approach to me, but there doesn't appear to be any way to send a standard HTTP error response from themodifyHandshake
method.Perform this logic in a Filter. The problem here, though, is if I load the resource in the filter, how do I pass it to the Endpoint? With an HTTP request, I can add a request attribute in a filter which is then available in the servlet. But there doesn't seem to be an easy way to achieve the same thing with websockets. I could, of course, load the resource again in the Endpoint class, but that's duplication of work.
I'm sure this must be a common scenario, so is there an accepted best-practice approach here?