Java Reference
In-Depth Information
The
testCustomerResource()
method starts off the same way as in
ex06_1
. It creates a
customer and obtains its URI from the response. Creating a customer is not authenticated so
we do not need to worry about setting up authorization here.
System
.
out
.
println
(
"*** GET Created Customer **"
);
Customer customer
=
null
null
;
WebTarget target
=
client
.
target
(
location
);
try
try
{
customer
=
target
.
request
().
get
(
Customer
.
class
);
Assert
.
fail
();
// should have thrown an exception
}
catch
catch
(
NotAuthorizedException e
)
{
}
This particular code shows what happens when an unauthenticated request is made. It makes
a GET request on the new customer's URI that fails with a
NotAuthorizedException
be-
cause we have not set up our OTP filter yet.
target
.
register
(
new
new
OneTimePasswordGenerator
(
"bburke"
,
"geheim"
));
We register an instance of our
OneTimePasswordGenerator
filter initialized with our user-
name and static password. We can now make an authenticated GET request without error.
customer
=
target
.
request
().
get
(
Customer
.
class
);
System
.
out
.
println
(
customer
);
To show our allowed-per-day policy in action, the code executes a customer update twice.
customer
.
setFirstName
(
"William"
);
response
=
target
.
request
().
put
(
Entity
.
xml
(
customer
));
iif
(
response
.
getStatus
() !=
204
)
throw
throw new
new
RuntimeException
(
"Failed to update"
);
++++
<?
hard
-
pagebreak
?>
++++
// Show the update
System
.
out
.
println
(
"**** After Update ***"
);
customer
=
target
.
request
().
get
(
Customer
.
class
);
System
.
out
.
println
(
customer
);
// only allowed to update once per day
customer
.
setFirstName
(
"Bill"
);
response
=
target
.
request
().
put
(
Entity
.
xml
(
customer
));
Assert
.
assertEquals
(
Response
.
Status
.
FORBIDDEN
,
response
.
getStatusInfo
());