Java Reference
In-Depth Information
UriBuilder builder
=
uriInfo
.
getAbsolutePathBuilder
();
builder
.
queryParam
(
"start"
,
"{start}"
);
builder
.
queryParam
(
"size"
,
"{size}"
);
Here, the code defines a URI template by using the
UriBuilder
passed back from
UriInfo.getAbsolutePathBuilder()
. The
start
and
size
query parameters are added to
the template. Their values are populated using template parameters later on when the actual
links are built.
ArrayList
<
Customer
>
list
=
new
new
ArrayList
<
Customer
>();
ArrayList
<
Link
>
links
=
new
new
ArrayList
<
Link
>();
synchronized
synchronized
(
customerDB
)
{
int
int
i
=
0
;
for
for
(
Customer customer
:
customerDB
.
values
())
{
iif
(
i
>=
start
&&
i
<
start
+
size
)
list
.
add
(
customer
);
i
++;
}
The code then gathers up the
Customer
instances that will be returned to the client based on
the
start
and
size
parameters. All this code is done within a
synchronized
block to pro-
tect against concurrent access on the
customerDB
map.
// next link
iif
(
start
+
size
<
customerDB
.
size
())
{
int
int
next
=
start
+
size
;
URI nextUri
=
builder
.
clone
().
build
(
next
,
size
);
Link nextLink
=
Link
.
fromUri
(
nextUri
)
.
rel
(
"next"
).
type
(
"application/xml"
).
build
();
links
.
add
(
nextLink
);
}
// previous link
iif
(
start
>
0
)
{
int
int
previous
=
start
-
size
;
iif
(
previous
<
0
)
previous
=
0
;
URI previousUri
=
builder
.
clone
().
build
(
previous
,
size
);
Link previousLink
=
Link
.
fromUri
(
previousUri
)
.
rel
(
"previous"
)
.
type
(
"application/xml"
).
build
();
links
.
add
(
previousLink
);
}