001/* 002 * Copyright (c) 2011-2017 Nexmo Inc 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining a copy 005 * of this software and associated documentation files (the "Software"), to deal 006 * in the Software without restriction, including without limitation the rights 007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 008 * copies of the Software, and to permit persons to whom the Software is 009 * furnished to do so, subject to the following conditions: 010 * 011 * The above copyright notice and this permission notice shall be included in 012 * all copies or substantial portions of the Software. 013 * 014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 020 * THE SOFTWARE. 021 */ 022package com.nexmo.client.auth; 023 024import com.nexmo.client.NexmoClientException; 025 026import java.util.*; 027 028/** 029 * Internal class, managing a collection of {@link AuthMethod}s. 030 * <p> 031 * This holds a collection of AuthMethod instances, in order of preference, and 032 * allow for simple selection of an appropriate AuthMethod for a particular REST endpoint. 033 */ 034public class AuthCollection { 035 private SortedSet<AuthMethod> authList; 036 037 /** 038 * Create a new AuthCollection with an empty set of AuthMethods. 039 */ 040 public AuthCollection() { 041 this.authList = new TreeSet<>(); 042 } 043 044 public AuthCollection(AuthMethod... authMethods) { 045 this(); 046 for (AuthMethod method : authMethods) { 047 add(method); 048 } 049 } 050 051 /** 052 * Add a new {@link AuthMethod} to the set managed by this AuthCollection 053 * 054 * @param auth AuthMethod method to be added to this collection 055 */ 056 public void add(AuthMethod auth) { 057 this.authList.add(auth); 058 } 059 060 /** 061 * Obtain an AuthMethod of type T, if one is contained in this collection. 062 * 063 * @param type The type of AuthMethod to be located 064 * @param <T> The type of AuthMethod which will be returned 065 * @return An AuthMethod subclass matching type 066 * @throws NexmoUnacceptableAuthException if no matching AuthMethod is found. 067 */ 068 public <T extends AuthMethod> T getAuth(Class<T> type) throws NexmoUnacceptableAuthException { 069 for (AuthMethod availableAuthMethod : this.authList) { 070 if (type.isInstance(availableAuthMethod)) { 071 return (T) availableAuthMethod; 072 } 073 } 074 throw new NexmoUnacceptableAuthException(this.authList, new HashSet<Class>(Arrays.asList(new Class[]{type}))); 075 } 076 077 /** 078 * Obtain an {@link AuthMethod} instance for a set of acceptable AuthMethod classes. 079 * 080 * @param acceptableAuthMethodClasses A Set of AuthMethod classes which are suitable for the target REST endpoint. 081 * @return the preferred AuthMethod from the provided set of acceptable AuthMethod classes 082 * @throws NexmoUnacceptableAuthException if no appropriate AuthMethod is held by this AuthCollection 083 */ 084 public AuthMethod getAcceptableAuthMethod(Set<Class> acceptableAuthMethodClasses) throws NexmoUnacceptableAuthException { 085 for (AuthMethod availableAuthMethod : this.authList) { 086 if (acceptableAuthMethodClasses.contains(availableAuthMethod.getClass())) { 087 return availableAuthMethod; 088 } 089 } 090 throw new NexmoUnacceptableAuthException(this.authList, acceptableAuthMethodClasses); 091 } 092}