public class CXFAuthenticationHandler implements RequestHandler {
public Response handleRequest(Message m, ClassResourceInfo resourceClass) {
if (Contexts.isApplicationContextActive()) {
//If the request is from same session, the user should be logged already.
Identity ids = Identity.instance();
if (ids.isLoggedIn()) {
return null;
}
AuthorizationPolicy policy = (AuthorizationPolicy) m
.get(AuthorizationPolicy.class);
// The policy can be null when the user did not specify credentials
if (policy != null) {
String username = policy.getUserName();
String password = policy.getPassword();
ids.getCredentials().setUsername(username);
ids.getCredentials().setPassword(password);
}
try {
ids.authenticate();
return null;
} catch (LoginException e) {
e.printStackTrace();
throw new WebApplicationException(getErrorResponse());
}