1
18
19 package io.undertow.security.handlers;
20
21 import io.undertow.Handlers;
22 import io.undertow.security.api.AuthenticationMechanism;
23 import io.undertow.security.api.AuthenticationMechanismContext;
24 import io.undertow.security.api.SecurityContext;
25 import io.undertow.server.HttpHandler;
26 import io.undertow.server.HttpServerExchange;
27 import io.undertow.server.handlers.ResponseCodeHandler;
28
29 import java.util.List;
30
31
37 public class AuthenticationMechanismsHandler implements HttpHandler {
38
39 private volatile HttpHandler next = ResponseCodeHandler.HANDLE_404;
40 private final AuthenticationMechanism[] authenticationMechanisms;
41
42 public AuthenticationMechanismsHandler(final HttpHandler next, final List<AuthenticationMechanism> authenticationMechanisms) {
43 this.next = next;
44 this.authenticationMechanisms = authenticationMechanisms.toArray(new AuthenticationMechanism[authenticationMechanisms.size()]);
45 }
46
47 public AuthenticationMechanismsHandler(final List<AuthenticationMechanism> authenticationHandlers) {
48 this.authenticationMechanisms = authenticationHandlers.toArray(new AuthenticationMechanism[authenticationHandlers.size()]);
49 }
50
51 @Override
52 public void handleRequest(final HttpServerExchange exchange) throws Exception {
53 final SecurityContext sc = exchange.getSecurityContext();
54 if(sc != null && sc instanceof AuthenticationMechanismContext) {
55 AuthenticationMechanismContext amc = (AuthenticationMechanismContext) sc;
56 for(int i = 0; i < authenticationMechanisms.length; ++i) {
57 amc.addAuthenticationMechanism(authenticationMechanisms[i]);
58 }
59 }
60 next.handleRequest(exchange);
61 }
62
63 public HttpHandler getNext() {
64 return next;
65 }
66
67 public AuthenticationMechanismsHandler setNext(final HttpHandler next) {
68 Handlers.handlerNotNull(next);
69 this.next = next;
70 return this;
71 }
72
73 }
74