001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027package org.apache.http.auth; 028 029import java.util.ArrayList; 030import java.util.List; 031import java.util.Locale; 032import java.util.Map; 033import java.util.concurrent.ConcurrentHashMap; 034 035import org.apache.http.HttpRequest; 036import org.apache.http.annotation.Contract; 037import org.apache.http.annotation.ThreadingBehavior; 038import org.apache.http.config.Lookup; 039import org.apache.http.params.HttpParams; 040import org.apache.http.protocol.ExecutionContext; 041import org.apache.http.protocol.HttpContext; 042import org.apache.http.util.Args; 043 044/** 045 * Authentication scheme registry that can be used to obtain the corresponding 046 * authentication scheme implementation for a given type of authorization challenge. 047 * 048 * @since 4.0 049 * 050 * @deprecated (4.3) use {@link org.apache.http.config.Registry} 051 */ 052@Contract(threading = ThreadingBehavior.SAFE) 053@Deprecated 054public final class AuthSchemeRegistry implements Lookup<AuthSchemeProvider> { 055 056 private final ConcurrentHashMap<String,AuthSchemeFactory> registeredSchemes; 057 058 public AuthSchemeRegistry() { 059 super(); 060 this.registeredSchemes = new ConcurrentHashMap<String,AuthSchemeFactory>(); 061 } 062 063 /** 064 * Registers a {@link AuthSchemeFactory} with the given identifier. If a factory with the 065 * given name already exists it will be overridden. This name is the same one used to 066 * retrieve the {@link AuthScheme authentication scheme} from {@link #getAuthScheme}. 067 * 068 * <p> 069 * Please note that custom authentication preferences, if used, need to be updated accordingly 070 * for the new {@link AuthScheme authentication scheme} to take effect. 071 * </p> 072 * 073 * @param name the identifier for this scheme 074 * @param factory the {@link AuthSchemeFactory} class to register 075 * 076 * @see #getAuthScheme 077 */ 078 public void register( 079 final String name, 080 final AuthSchemeFactory factory) { 081 Args.notNull(name, "Name"); 082 Args.notNull(factory, "Authentication scheme factory"); 083 registeredSchemes.put(name.toLowerCase(Locale.ENGLISH), factory); 084 } 085 086 /** 087 * Unregisters the class implementing an {@link AuthScheme authentication scheme} with 088 * the given name. 089 * 090 * @param name the identifier of the class to unregister 091 */ 092 public void unregister(final String name) { 093 Args.notNull(name, "Name"); 094 registeredSchemes.remove(name.toLowerCase(Locale.ENGLISH)); 095 } 096 097 /** 098 * Gets the {@link AuthScheme authentication scheme} with the given name. 099 * 100 * @param name the {@link AuthScheme authentication scheme} identifier 101 * @param params the {@link HttpParams HTTP parameters} for the authentication 102 * scheme. 103 * 104 * @return {@link AuthScheme authentication scheme} 105 * 106 * @throws IllegalStateException if a scheme with the given name cannot be found 107 */ 108 public AuthScheme getAuthScheme(final String name, final HttpParams params) 109 throws IllegalStateException { 110 111 Args.notNull(name, "Name"); 112 final AuthSchemeFactory factory = registeredSchemes.get(name.toLowerCase(Locale.ENGLISH)); 113 if (factory != null) { 114 return factory.newInstance(params); 115 } else { 116 throw new IllegalStateException("Unsupported authentication scheme: " + name); 117 } 118 } 119 120 /** 121 * Obtains a list containing the names of all registered {@link AuthScheme authentication 122 * schemes} 123 * 124 * @return list of registered scheme names 125 */ 126 public List<String> getSchemeNames() { 127 return new ArrayList<String>(registeredSchemes.keySet()); 128 } 129 130 /** 131 * Populates the internal collection of registered {@link AuthScheme authentication schemes} 132 * with the content of the map passed as a parameter. 133 * 134 * @param map authentication schemes 135 */ 136 public void setItems(final Map<String, AuthSchemeFactory> map) { 137 if (map == null) { 138 return; 139 } 140 registeredSchemes.clear(); 141 registeredSchemes.putAll(map); 142 } 143 144 @Override 145 public AuthSchemeProvider lookup(final String name) { 146 return new AuthSchemeProvider() { 147 148 @Override 149 public AuthScheme create(final HttpContext context) { 150 final HttpRequest request = (HttpRequest) context.getAttribute( 151 ExecutionContext.HTTP_REQUEST); 152 return getAuthScheme(name, request.getParams()); 153 } 154 155 }; 156 } 157 158}