/** * Obtain an {@link AuthMethod} instance for a set of acceptable AuthMethod classes. * * @param acceptableAuthMethodClasses A Set of AuthMethod classes which are suitable for the target REST endpoint. * * @return the preferred AuthMethod from the provided set of acceptable AuthMethod classes * * @throws NexmoUnacceptableAuthException if no appropriate AuthMethod is held by this AuthCollection */ public AuthMethod getAcceptableAuthMethod(Set<Class> acceptableAuthMethodClasses) throws NexmoUnacceptableAuthException { for (AuthMethod availableAuthMethod : this.authList) { if (acceptableAuthMethodClasses.contains(availableAuthMethod.getClass())) { return availableAuthMethod; } } throw new NexmoUnacceptableAuthException(this.authList, acceptableAuthMethodClasses); } }
public String getMessage() { return generateErrorMessage(); }
@Test public void testNoAcceptableAuthMethod() throws Exception { AuthCollection auths = new AuthCollection(); Set<Class> acceptableAuths = new HashSet<>(); acceptableAuths.add(JWTAuthMethod.class); try { auths.getAcceptableAuthMethod(acceptableAuths); fail("No acceptable auth method should throw a NexmoClientException"); } catch (NexmoUnacceptableAuthException ex) { assertEquals( "No acceptable authentication type could be found. Acceptable types are: JWTAuthMethod. Supplied " + "types were: ", ex.getMessage()); } }
@Test public void testIncompatibleAuths() throws Exception { TokenAuthMethod tAuth = new TokenAuthMethod("key", "secret"); AuthCollection auths = new AuthCollection(); auths.add(tAuth); Set<Class> acceptableAuths = new HashSet<>(); acceptableAuths.add(JWTAuthMethod.class); try { auths.getAcceptableAuthMethod(acceptableAuths); fail("No acceptable auth method should throw a NexmoClientException"); } catch (NexmoUnacceptableAuthException ex) { assertEquals("No acceptable authentication type could be found. Acceptable types are: JWTAuthMethod. Supplied " + "types were: TokenAuthMethod", ex.getMessage()); } }
/** * Obtain an AuthMethod of type T, if one is contained in this collection. * * @param type The type of AuthMethod to be located * @param <T> The type of AuthMethod which will be returned * * @return An AuthMethod subclass matching type * * @throws NexmoUnacceptableAuthException if no matching AuthMethod is found. */ public <T extends AuthMethod> T getAuth(Class<T> type) throws NexmoUnacceptableAuthException { for (AuthMethod availableAuthMethod : this.authList) { if (type.isInstance(availableAuthMethod)) { return (T) availableAuthMethod; } } throw new NexmoUnacceptableAuthException(this.authList, new HashSet<>(Arrays.asList(new Class[]{type}))); }