Java Reference
In-Depth Information
this
this
.
secret
=
secret
;
}
@Override
public
public
void
void
filter
(
ClientRequestContext requestContext
)
throws
throws
IOException
{
String otp
=
OTP
.
generateToken
(
secret
);
requestContext
.
getHeaders
().
putSingle
(
HttpHeaders
.
AUTHORIZATION
,
user
+
" "
+
otp
);
}
}
This filter is very simple. It is constructed with the username and password we will use to
generate the one-time password. The
filter()
method generates the one-time password by
calling the
OTP.generateToken()
method we described earlier in this chapter. The
fil-
ter()
method then generates and sets the
Authorization
header for the HTTP request.
The client test code is the same as
ex06_1
except that we set it up to use OTP authentication.
Let's take a look:
src/test/java/com/restfully/shop/test/CustomerResourceTest.java
@Test
public
public
void
void
testCustomerResource
()
throws
throws
Exception
{
System
.
out
.
println
(
"*** Create a new Customer ***"
);
Customer newCustomer
=
new
new
Customer
();
newCustomer
.
setFirstName
(
"Bill"
);
newCustomer
.
setLastName
(
"Burke"
);
newCustomer
.
setStreet
(
"256 Clarendon Street"
);
newCustomer
.
setCity
(
"Boston"
);
newCustomer
.
setState
(
"MA"
);
newCustomer
.
setZip
(
"02115"
);
newCustomer
.
setCountry
(
"USA"
);
Response response
=
client
.
target
(
"http://localhost:8080/services/customers"
)
.
request
().
post
(
Entity
.
xml
(
newCustomer
));
iif
(
response
.
getStatus
() !=
201
)
throw
throw new
new
RuntimeException
(
"Failed to create"
);
String location
=
response
.
getLocation
().
toString
();
System
.
out
.
println
(
"Location: "
+
location
);
response
.
close
();