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 java.util.*; 025 026/** 027 * Internal class, managing a collection of {@link AuthMethod}s. 028 * <p> 029 * This holds a collection of AuthMethod instances, in order of preference, and 030 * allow for simple selection of an appropriate AuthMethod for a particular REST endpoint. 031 */ 032public class AuthCollection { 033 private SortedSet<AuthMethod> authList; 034 035 /** 036 * Create a new AuthCollection with an empty set of AuthMethods. 037 */ 038 public AuthCollection() { 039 this.authList = new TreeSet<>(); 040 } 041 042 public AuthCollection(AuthMethod... authMethods) { 043 this(); 044 for (AuthMethod method : authMethods) { 045 add(method); 046 } 047 } 048 049 public AuthCollection(SortedSet<AuthMethod> authMethods) { 050 this.authList = authMethods; 051 } 052 053 /** 054 * Add a new {@link AuthMethod} to the set managed by this AuthCollection 055 * 056 * @param auth AuthMethod method to be added to this collection 057 */ 058 public void add(AuthMethod auth) { 059 this.authList.add(auth); 060 } 061 062 /** 063 * Obtain an AuthMethod of type T, if one is contained in this collection. 064 * 065 * @param type The type of AuthMethod to be located 066 * @param <T> The type of AuthMethod which will be returned 067 * 068 * @return An AuthMethod subclass matching type 069 * 070 * @throws NexmoUnacceptableAuthException if no matching AuthMethod is found. 071 */ 072 public <T extends AuthMethod> T getAuth(Class<T> type) throws NexmoUnacceptableAuthException { 073 for (AuthMethod availableAuthMethod : this.authList) { 074 if (type.isInstance(availableAuthMethod)) { 075 return (T) availableAuthMethod; 076 } 077 } 078 throw new NexmoUnacceptableAuthException(this.authList, new HashSet<>(Arrays.asList(new Class[]{type}))); 079 } 080 081 /** 082 * Obtain an {@link AuthMethod} instance for a set of acceptable AuthMethod classes. 083 * 084 * @param acceptableAuthMethodClasses A Set of AuthMethod classes which are suitable for the target REST endpoint. 085 * 086 * @return the preferred AuthMethod from the provided set of acceptable AuthMethod classes 087 * 088 * @throws NexmoUnacceptableAuthException if no appropriate AuthMethod is held by this AuthCollection 089 */ 090 public AuthMethod getAcceptableAuthMethod(Set<Class> acceptableAuthMethodClasses) throws NexmoUnacceptableAuthException { 091 for (AuthMethod availableAuthMethod : this.authList) { 092 if (acceptableAuthMethodClasses.contains(availableAuthMethod.getClass())) { 093 return availableAuthMethod; 094 } 095 } 096 throw new NexmoUnacceptableAuthException(this.authList, acceptableAuthMethodClasses); 097 } 098}