Some notes about Jax-RS, HTTP statuses and Tomcat

0

A quick note about an issue my team and I struggled with last day. The context is the following: using Jersey client, we had to check a HTTP response status before moving on with our process. The check was done this way:

 


Jersey being a Jax-RS implementation, it is not chocking that the Jersey client also rely on Jax-RS. It is the case in this piece of code where Status is an enum defined by Jax-RS. But we had an issue with this code: on some environment it happened that our condition was false whereas the HTTP response status was actually 200 (meaning OK).
Truth is, the actual return type of getStatusInfo() is not Status but its intertace StatusType which is also defined by Jax-RS. StatusType define methods allowing to access to the content of a HTTP response status line: the status code (200, 400…) and the reason phrase which provide a short textual description of the status code (OK, BAD REQUEST…).


So, why using an interface here and not directly the enum ? First of all, because the Jax-RS Status enum is limited to standard response codes. The second reason is related to the reason phrase: all the classical reason phrases we know of (FORBIDDEN, BAD REQUEST…) are actually NOT mandatory to be used. As the RFC 2616 says:

The reason phrases listed here are only recommendations — they MAY be replaced by local equivalents without affecting the protocol

So, the following response is then perfectly valid (but not advised !):
So, what is the exact behavior of the Jersey client regarding status lines ? The HttpUrlConnector give us the answer:
We can see from this code that Jersey will use a Status enum value as a response to get getStatusInfo() call if and only if the reason phrase is not defined and the status code is standard (i.e. the status code is defined in the enum). Otherwise the response will be an instance of StatusImpl (a Jersey class of StatusType).
So, the truth is we can’t rely on a reference check using Status enum values in our conditions. The only safe solution is to use the status code integer value directly:

Another solution, less verbose but more risky, is to rely on the equals implementation of the status info that is returned:

This solution is not safe because you cannot really guarantee that the reason phrase returned by your server will match the one defined in the enum. Also, the order of the expression matters because calling equals on an enum will actually compare references. This means that the following code will not work even if the reason phrase returned by the server is the same than the one contained in the enum:

There is still a question to answer: why did this code failed on some of our environment ? Well, it happened that our local environment was using tomcat 8.5.x whereas other environments were using tomcat 7.x. And guess what: starting from 8.5, tomcat does not send reason phrases anymore by default.

Partagez cet article.

A propos de l'auteur

Enjoy simplicity in code. Appreciate complexity in life. Not at all megalomaniac.

Ajouter un commentaire