001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.webengine.jaxrs.login;
020
021import java.util.Map;
022
023import javax.security.auth.login.LoginContext;
024import javax.security.auth.login.LoginException;
025import javax.servlet.http.HttpServletRequest;
026import javax.servlet.http.HttpServletResponse;
027
028import org.nuxeo.ecm.webengine.jaxrs.BundleNotFoundException;
029import org.nuxeo.ecm.webengine.jaxrs.Utils;
030
031/**
032 * An authentication handlers that delegate the authentication to the first registered handler that knows how to
033 * authenticate.
034 *
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037public class CompositeAuthenticationHandler implements AuthenticationHandler {
038
039    protected AuthenticationHandler[] handlers;
040
041    public CompositeAuthenticationHandler(String classRefs) throws ReflectiveOperationException,
042            BundleNotFoundException {
043        handlers = Utils.newInstances(AuthenticationHandler.class, classRefs);
044    }
045
046    public CompositeAuthenticationHandler(AuthenticationHandler[] handlers) {
047        this.handlers = handlers;
048    }
049
050    @Override
051    public void init(Map<String, String> properties) {
052        // do nothing
053    }
054
055    @Override
056    public LoginContext handleAuthentication(HttpServletRequest request, HttpServletResponse response)
057            throws LoginException {
058        for (AuthenticationHandler handler : handlers) {
059            LoginContext lc = handler.handleAuthentication(request, response);
060            if (lc != null) {
061                return lc;
062            }
063        }
064        return null;
065    }
066
067}