users@jersey.java.net

[Jersey] Re: Reponse.Status != response.getStatusInfo()

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Wed, 18 Dec 2013 19:17:50 +0100

On 14 Dec 2013, at 01:54, Robert DiFalco <robert.difalco_at_gmail.com> wrote:

> Wow, this migration to 2.4.1 from 1.9 has not been EASY!! Lots of needless changes, horrible documentation on migration, lack of old jersey path proxies that easy broken api fixes, etc.

Here are the Jersey 2.x client proxies: https://github.com/jersey/jersey/tree/master/ext/proxy-client
The unit test shows how it can be used: https://github.com/jersey/jersey/tree/master/ext/proxy-client/src/test/java/org/glassfish/jersey/client/proxy

>
> Anyway,
>
> If I have a line like this:
>
> rsp = target.request().post( bean );
>
> It seems like I Response.Status.CREATED will not compare to rsp.getStatusInfo(). Does anyone else find this to be crazy in the API design department?
>
> Okay, so I can fix it with this huge ass line:
>
> Repsonse.Status.CREATED.getStatusCode() == response.getStatus();
>
> But come on guys, really?
>
> assertEquals( Respone.Status.CREATED, response.getStatusInfo() );
>
> Won't work? This is an example of making it easier for the API implementor, not for the API user. There are about 100 ways to have done this better. Which one am I missing?

First, we are talking about JAX-RS, not Jersey API here. It's ok - I'm the one of those to get beaten :) Still there is a separate forum you may want to use in the future - users_at_jax-rs-spec.java.net or file any bugs or improvements suggestion here.

Now to your issue. To explain:

Response.Status.CREATED is enum value, that implements a StatusType interface. It's been there since JAX-RS 1.0.
Response.getStatusInfo() returns the StatusType interface. All this is necessary to ensure that someone doesn't come to us and beats us for not being able to supporting their fancy new custom HTTP status code, or - surprisingly frequently - for not supporting their own, fancy reason phrase for an existing status code. (Yes, this happens. Quite a lot.)
Unfortunately, Response.getStatus() has already been taken in JAX-RS 1.0 and returns int. We could not change the return type; as you may know, changing standard JAX-RS API in this way would not be possible.

And as for assertEquals, you may be aware of the fact that Java enums only equal to the same enums, no exceptions. Since we need to expect a custom reason phrase, comparing a Response.Status instance to an instance returned from response.getStatusInfo() is not a good idea anyway.
So this leaves us with only a few options, among those your choice is the one I would personally prefer too:

Response.Status.CREATED.getStatusCode() == response.getStatus();

You are right that this line is rather long and we should do something about it in the next JAX-RS API revision. I have already filed a new issue to track this:
https://java.net/jira/browse/JAX_RS_SPEC-442
You say there are 100 ways to do it better. Please feel free to comment on the issue and add your suggestions (let's limit it to top 3) how would you fix this problem in JAX-RS API (without breaking backward compatibility). If nothing else, we would use the suggestions as a few more option to consider.

I know I have not solved your problem now, but at least I hope I was able to explain the background and give you some hope in the future :)

Cheers,
Marek