001/* 002 * Copyright 2020 Vonage 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package com.vonage.client.auth; 017 018import org.apache.commons.lang3.StringUtils; 019 020import java.util.*; 021 022public class VonageUnacceptableAuthException extends VonageAuthException { 023 private static final Map<Class, String> AUTH_DESCRIPTION_MAP = new HashMap<>(); 024 025 private final Iterable<AuthMethod> availableAuths; 026 private final Iterable<Class> acceptableAuthClasses; 027 028 static { 029 AUTH_DESCRIPTION_MAP.put(TokenAuthMethod.class, "API Key and Secret"); 030 AUTH_DESCRIPTION_MAP.put(SignatureAuthMethod.class, "API Key and Signature Secret"); 031 AUTH_DESCRIPTION_MAP.put(JWTAuthMethod.class, "Application ID and Private Key"); 032 } 033 034 public VonageUnacceptableAuthException(Collection<AuthMethod> availableAuths, Collection<Class> 035 acceptableAuthClasses) { 036 super(); 037 this.availableAuths = new ArrayList<>(availableAuths); 038 this.acceptableAuthClasses = new ArrayList<>(acceptableAuthClasses); 039 } 040 041 public String getMessage() { 042 return generateErrorMessage(); 043 } 044 045 private String generateErrorMessage() { 046 SortedSet<String> availableTypes = new TreeSet<>(); 047 for (AuthMethod auth : this.availableAuths) { 048 availableTypes.add(AUTH_DESCRIPTION_MAP.getOrDefault(auth.getClass(), auth.getClass().getSimpleName())); 049 } 050 051 SortedSet<String> acceptableTypes = new TreeSet<>(); 052 for (Class klass : this.acceptableAuthClasses) { 053 acceptableTypes.add(AUTH_DESCRIPTION_MAP.getOrDefault(klass, klass.getSimpleName())); 054 } 055 056 return String.format("No acceptable authentication type could be found. Acceptable types are: %s. Supplied " + 057 "types " + 058 "were: %s", StringUtils.join(acceptableTypes, ", "), StringUtils.join(availableTypes, ", ")); 059 } 060}