jsr339-experts@jax-rs-spec.java.net

[jsr339-experts] Re: Some comments about Target and Invocation

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Thu, 01 Sep 2011 14:49:36 +0200

Just to clarify, target is immutable with respect to it's URI - you can update it's configuration.

One reason was certainly to allow for easier transitions (e.g. iterations) in a URI hierarchy. Another reason was to
make sure that you can do easy configuration scoping based on the nested URIs:

Target library = client.target("http://library/")

// add common filter for adding a header with my personal library API key to all requests sent to the library service
library.register(new LibraryApiKeyFilter(myKey));
Target books = library.path("books"); // inherits API key filter

Target myAccount = library.path("users/marek"); // inherits API key filter
// add the authentication filter for accessing my account resource
myAccount.register(new LibraryAuthenticationFilter(myCredentials));
Target myBooks = myAccount.path("books"); // inherits API key and authentication filters

// everybody can access the list of available books
List<Book> listOfAvailableBooks = books.request("text/plain").get(new TypeLiteral<List<Book>>(){});

// only I can access the list of my books with proper authentication
List<Book> listOfMyBooks = myBooks.request("text/plain").get(new TypeLiteral<List<Book>>(){});

// this would fail with 401 or more safely with 404
List<Book> listOfMareksBooks = library.path("users/marek/books").request("text/plain").get(new
TypeLiteral<List<Book>>(){});


Marek


On 09/01/2011 02:01 PM, Sergey Beryozkin wrote:
> Well, that was definitely a new thing to learn,
> that is why you could obviously see why I typed
> target.accept(...)
>
> What was the rationale behind making it immutable ?
>
> Cheers, Sergey
>
> On 01/09/11 12:55, Marek Potociar wrote:
>> No, Target is *NOT* mutable (unlike Invocation.Builder or HttpRequest). IOW:
>>
>> Target t1 = client.target("http://books");
>> Target t2 = t1.path("1");
>> t1 != t2; // !!!