Database Reference
In-Depth Information
Client
The
Client
object manages the underlying connection to the HTTP Server and
any configuration required for that connection. The
Client
also allows you to
construct
WebResource
objects. As it is mostly likely that we will want to
authenticate with eXist when we manipulate resources via the REST Server API,
we will actually make use of the Apache HTTP client integration for Jersey, as
this allows us to provide authentication credentials. See
Example 13-6
.
Example 13-6. Constructing a suitable Client object for communicating with eXist
using Jersey
//set up authentication
final
CredentialsProvider
credentialsProvider
=
new
BasicCredentialsProvider
();
credentialsProvider
.
setCredentials
(
AuthScope
.
ANY
,
new
UsernamePasswordCredentials
(
"admin"
,
"my-admin-password"
));
final
DefaultApacheHttpClient4Config
config
=
new
DefaultApacheHttpClient4Config
();
config
.
getProperties
().
put
(
ApacheHttpClient4Config
.
PROPERTY_CREDENTIALS_PROVIDER
,
credentialsProvider
);
//construct the client
final
Client
client
=
ApacheHttpClient4
.
create
(
config
);
WebResource
A
WebResource
object indicates a resource on the REST Server (although it may
not exist yet) that is addressable by a URI. You may construct as many
WebRe
source
objects as you wish from a single client; you then perform actions on
these resources, such as
PUT
or
GET
. See
Example 13-7
.
Example 13-7. Constructing a Jersey WebResource object for communicating with
eXist
final
String
uri
=
"http://localhost:8080/exist/rest/db/some-document.xml"
;
final
WebResource
resource
=
client
.
resource
(
uri
);
ClientResponse
Once you have a
WebResource
object, you can make a request to the server. Jersey
offers some easy-to-use facilities to allow you to serialize/deserialize Java objects
will just consider the raw response object that Jersey can provide from any
request: the
ClientResponse
. The
ClientResponse
object allows you access to all
of the HTTP responses sent from the REST Server, including all headers and
bodies. See
Example 13-8
.
Search WWH ::
Custom Search