Database Reference
In-Depth Information
else
if
(
request:get-method
()
eq
"GET"
)
then
if
(
matches
(
request:get-uri
(),
concat
(
".*/thumbnail/"
,
$
local:uuidv4-pattern
,
"\.jpg$"
)
))
then
let
$
image-name
:=
tokenize
(
request:get-uri
(),
"/"
)[
last
()],
$
image
:=
local:get-or-create-thumbnail
(
$
image-name
)
return
if
(
not
(
empty
(
$
image
)))
then
response:stream-binary
(
$
image
,
"image/jpeg"
,
concat
(
"thumbnail-"
,
$
image-name
)
)
else
(
response:set-status-code
(
$
response:NOT-FOUND
),
<image-not-found>
{
$
image-name
}
</image-not-found>
)
This is similar to retrieving an image, except as well getting as the identifier of
the image we also check the request URI for the prefix
thumbnail/
.
This is similar to retrieving an image, except we now call the function
local:get-or-create-thumbnail
instead of the function
local:get-image
.
While the
rest-stored-query/image-api.xq
example shows how you can simply con‐
struct your own APIs atop the REST Server API, there is a great deal more that you
can achieve, such as URL rewriting and directly producing web pages in HTML. For
further details, see
Chapter 9
.
Using the REST Server API from Java
There are several good HTTP client libraries available for Java, including
java.net.URLConnection
in the standard Java library, but unfortunately for us, most
of them take a somewhat low-level approach to HTTP, which means that you often
need to build abstractions on top of them when using REST over HTTP. So instead,
we will look at the
Jersey
client library, which is specifically designed for talking to
REST Servers over HTTP—where the central abstraction is a resource. Jersey is an
implementation of JAX-RS that enables you to easily construct REST services using
Java annotations
. However, it also has a client library that is very simple and elegant,
and is well suited for communicating with the eXist REST Server API. At the time of
writing, the latest Jersey version was 1.17.1.
There are just a few concepts that you need to understand in the Jersey client library
beyond existing REST principles (such as
GET
,
PUT
,
POST
, and
DELETE
). In Jersey, we
work with three kinds of objects:
Search WWH ::
Custom Search