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 12:31:57 +0200

On 09/01/2011 11:09 AM, Sergey Beryozkin wrote:
> Hi Marek
>
> On 31/08/11 18:46, Marek Potociar wrote:
>> As I said, you can iterate over path hierarchies using BFS ( http://en.wikipedia.org/wiki/Breadth-first_search )
>> algorithm. If that's not practical, you can use DFS + stack (
>> http://en.wikipedia.org/wiki/Depth-first_search#Algorithm_using_a_stack ). The stack can be explicit or you can leverage
>> the implicit call stack by implementing recursive iteration.
>>
>> Btw. I would like to see the real life use case.
>
> I hope the following won't appear to be too hypothetic:
>
> 1. We have a base URI. We have a collection of Book IDs. We know the service allocates a unique URI to each Book using a
> baseURI + "/" + BookID, example, http://books/1, etc. I'd like to use the client API to
> do repetitive invocations to retrieve all or some of the Books

int[] bookIndexesToRetrieve = ...;
Target booksResource = client.target("http://books/");
List<Book> books = new ArrayList(bookIndexesToRetrieve.length);
for (int bookIndex : bookIndexesToRetrieve) {
    books.add(booksResource.path(String.valueOf(bookIndex)).request("text/plain").get(Book.class));
}

Or alternatively via path templates:

int[] bookIndexesToRetrieve = ...;
Target bookResource = client.target("http://books/{id}");
List<Book> books = new ArrayList(bookIndexesToRetrieve.length);
for (int bookIndex : bookIndexesToRetrieve) {
    books.add(bookResource.pathParam("id", String.valueOf(bookIndex)).request("text/plain").get(Book.class));
}

I don't see the need for back() here.

>
> 2. Similarly to one, but using a query param.

Very similar code to the above:

int[] bookIndexesToRetrieve = ...;
Target booksResource = client.target("http://books");
List<Book> books = new ArrayList(bookIndexesToRetrieve.length);
for (int bookIndex : bookIndexesToRetrieve) {
    books.add(booksResource.queryParam("id", String.valueOf(bookIndex)).request("text/plain").get(Book.class));
}

Again, no need for back().

>
> I think the fluent API should be flexible enough to accommodate for repetitive invocations against the same targetURI
> without forcing a user to retype things which are shared across requests, ex, an accept type, base URI, Content-type.

It is - via path templates or via query params.

>
> Can you give me a favor and actually type some code showing how say 1 can be done, please don't hesitate to use BFS or
> DFS techniques

I didn't have to use DFS or BFS for such simple case. Give me a more complex problem! :)

Marek