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.ArrayList; 022import java.util.Map; 023 024/** 025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 026 */ 027public class AuthenticationService { 028 029 protected Map<String, AuthenticationHandler> handlers; 030 031 public void addHander(AuthenticationHandlerDescriptor desc) throws ReflectiveOperationException { 032 handlers.put(desc.name, desc.newInstance()); 033 } 034 035 public void removeHander(AuthenticationHandlerDescriptor desc) { 036 handlers.remove(desc.name); 037 } 038 039 public void addHandler(String name, AuthenticationHandler handler) { 040 handlers.put(name, handler); 041 } 042 043 public AuthenticationHandler removeHandler(String key) { 044 return handlers.remove(key); 045 } 046 047 public AuthenticationHandler getHandler(String name) { 048 return handlers.get(name); 049 } 050 051 /** 052 * Create a handler instance for the given comma separated list of handler names. 053 * 054 * @param names 055 * @return 056 */ 057 public AuthenticationHandler createHandler(String names) { 058 int i = names.indexOf(','); 059 if (i == -1) { 060 AuthenticationHandler handler = handlers.get(names.trim()); 061 return handler; 062 } 063 int s = 0; 064 ArrayList<AuthenticationHandler> result = new ArrayList<AuthenticationHandler>(); 065 do { 066 String name = names.substring(s, i).trim(); 067 AuthenticationHandler handler = getHandler(name); 068 result.add(handler); 069 s = i + 1; 070 i = names.indexOf(',', s); 071 } while (i > -1); 072 if (s < names.length()) { 073 String name = names.substring(s).trim(); 074 if (name.length() > 0) { 075 AuthenticationHandler handler = getHandler(name); 076 result.add(handler); 077 } 078 } 079 return new CompositeAuthenticationHandler(result.toArray(new AuthenticationHandler[result.size()])); 080 } 081 082}