1 /* 2 * ==================================================================== 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * ==================================================================== 20 * 21 * This software consists of voluntary contributions made by many 22 * individuals on behalf of the Apache Software Foundation. For more 23 * information on the Apache Software Foundation, please see 24 * <http://www.apache.org/>. 25 * 26 */ 27 package org.apache.http.auth; 28 29 import java.util.ArrayList; 30 import java.util.List; 31 import java.util.Locale; 32 import java.util.Map; 33 import java.util.concurrent.ConcurrentHashMap; 34 35 import org.apache.http.HttpRequest; 36 import org.apache.http.annotation.Contract; 37 import org.apache.http.annotation.ThreadingBehavior; 38 import org.apache.http.config.Lookup; 39 import org.apache.http.params.HttpParams; 40 import org.apache.http.protocol.ExecutionContext; 41 import org.apache.http.protocol.HttpContext; 42 import org.apache.http.util.Args; 43 44 /** 45 * Authentication scheme registry that can be used to obtain the corresponding 46 * authentication scheme implementation for a given type of authorization challenge. 47 * 48 * @since 4.0 49 * 50 * @deprecated (4.3) use {@link org.apache.http.config.Registry} 51 */ 52 @Contract(threading = ThreadingBehavior.SAFE) 53 @Deprecated 54 public final class AuthSchemeRegistry implements Lookup<AuthSchemeProvider> { 55 56 private final ConcurrentHashMap<String,AuthSchemeFactory> registeredSchemes; 57 58 public AuthSchemeRegistry() { 59 super(); 60 this.registeredSchemes = new ConcurrentHashMap<String,AuthSchemeFactory>(); 61 } 62 63 /** 64 * Registers a {@link AuthSchemeFactory} with the given identifier. If a factory with the 65 * given name already exists it will be overridden. This name is the same one used to 66 * retrieve the {@link AuthScheme authentication scheme} from {@link #getAuthScheme}. 67 * 68 * <p> 69 * Please note that custom authentication preferences, if used, need to be updated accordingly 70 * for the new {@link AuthScheme authentication scheme} to take effect. 71 * </p> 72 * 73 * @param name the identifier for this scheme 74 * @param factory the {@link AuthSchemeFactory} class to register 75 * 76 * @see #getAuthScheme 77 */ 78 public void register( 79 final String name, 80 final AuthSchemeFactory factory) { 81 Args.notNull(name, "Name"); 82 Args.notNull(factory, "Authentication scheme factory"); 83 registeredSchemes.put(name.toLowerCase(Locale.ENGLISH), factory); 84 } 85 86 /** 87 * Unregisters the class implementing an {@link AuthScheme authentication scheme} with 88 * the given name. 89 * 90 * @param name the identifier of the class to unregister 91 */ 92 public void unregister(final String name) { 93 Args.notNull(name, "Name"); 94 registeredSchemes.remove(name.toLowerCase(Locale.ENGLISH)); 95 } 96 97 /** 98 * Gets the {@link AuthScheme authentication scheme} with the given name. 99 * 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 } 116 throw new IllegalStateException("Unsupported authentication scheme: " + name); 117 } 118 119 /** 120 * Obtains a list containing the names of all registered {@link AuthScheme authentication 121 * schemes} 122 * 123 * @return list of registered scheme names 124 */ 125 public List<String> getSchemeNames() { 126 return new ArrayList<String>(registeredSchemes.keySet()); 127 } 128 129 /** 130 * Populates the internal collection of registered {@link AuthScheme authentication schemes} 131 * with the content of the map passed as a parameter. 132 * 133 * @param map authentication schemes 134 */ 135 public void setItems(final Map<String, AuthSchemeFactory> map) { 136 if (map == null) { 137 return; 138 } 139 registeredSchemes.clear(); 140 registeredSchemes.putAll(map); 141 } 142 143 @Override 144 public AuthSchemeProvider lookup(final String name) { 145 return new AuthSchemeProvider() { 146 147 @Override 148 public AuthScheme create(final HttpContext context) { 149 final HttpRequest request = (HttpRequest) context.getAttribute( 150 ExecutionContext.HTTP_REQUEST); 151 return getAuthScheme(name, request.getParams()); 152 } 153 154 }; 155 } 156 157 }