users@jax-rs-spec.java.net

[jax-rs-spec users] [jsr339-experts] Re: is get(Class<T>) really a convenience?

From: Santiago Pericas-Geertsen <Santiago.PericasGeertsen_at_oracle.com>
Date: Tue, 12 Feb 2013 09:21:56 -0500

On Feb 11, 2013, at 9:05 AM, Bill Burke <bburke_at_redhat.com> wrote:

> I've come across this while explaining the Client API in my book. Just something to think about...
>
> Is SyncInvoker.get(Class<T>) style of invocations really that convenient? In order to make sure the response is closed, there's a bit of extra code you have to write. All you're saving from this style is a call to readEntity(). Other than that, its the same amount of code.
>
>
> Response response = target.request().get();
> try {
> if (response.getStatus() == 200) {
> String str = response.readEntity(String.class);
> }
> } finally {
> response.close();
> }
>
> vs.
>
> try {
> String str = target.request().get(String.class);
> } catch (WebApplicationException wae) {
> wae.getResponse().close();
> } catch (ResponseProcessingException rpe) {
> rpe.getResponse().close();
> }
 
 What about Java 7 multi-catch feature?

 } catch (WebApplicationException | ResponseProcessingException e) {
     e.getResponse().close();
 }

>
> With the async API its even worse:
>
> Future<Response> future = target.request().async().get();
> Response response = future.get();
> try {
> if (response.getStatus() == 200) {
> String str = response.readEntity(String.class);
> }
> } finally {
> response.close();
> }

 Shouldn't the future.get() be in the try block above?

>
> vs.
>
> Future<String> future = target.request().async().get(String.class);
> try {
> String str = future.get();
> } catch (ExecutionException ex) {
> if (ex.getCause() instanceof WebApplicationException) {
> WebApplicationExceptoin wae = (WebApplicationException)ex.getCause();
> wae.getResponse().close();
> } else if (ex.getCause() instanceof ResponseProcessingException {
> ResponseProcessingException rpe = (ResponseProcessingException)ex.getCause();
> rpe.getResponse().close();
> }
> }

 Yes, this isn't very nice. Any suggestions?

-- Santiago