diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/ApplicationAuthenticatorService.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/ApplicationAuthenticatorService.java index 2164d7e51099..cbdda0bb33e2 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/ApplicationAuthenticatorService.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/ApplicationAuthenticatorService.java @@ -20,15 +20,14 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages; import org.wso2.carbon.identity.application.common.dao.impl.AuthenticatorManagementDAOImpl; import org.wso2.carbon.identity.application.common.dao.impl.CacheBackedAuthenticatorMgtDAO; -import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtClientException; import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtException; import org.wso2.carbon.identity.application.common.model.FederatedAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.LocalAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.RequestPathAuthenticatorConfig; import org.wso2.carbon.identity.application.common.model.UserDefinedLocalAuthenticatorConfig; +import org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.AuthenticatorMgtError; import org.wso2.carbon.identity.application.common.util.UserDefinedLocalAuthenticatorValidator; import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; @@ -36,7 +35,7 @@ import java.util.ArrayList; import java.util.List; -import static org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages.ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED; +import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.*; /** * Application authenticator service. @@ -150,8 +149,8 @@ public void addLocalAuthenticator(LocalAuthenticatorConfig authenticator) { if (authenticator != null) { if (authenticator.getDefinedByType() != DefinedByType.SYSTEM) { - throw new AuthenticatorMgtServerRuntimeException( - ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED.getMessage()); + throw buildRuntimeServerException( + AuthenticatorMgtError.ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED, null); } localAuthenticators.add(authenticator); } @@ -201,9 +200,8 @@ public UserDefinedLocalAuthenticatorConfig addUserDefinedLocalAuthenticator( LocalAuthenticatorConfig config = getLocalAuthenticatorByName(authenticatorConfig.getName(), tenantDomain); if (config != null) { - ErrorMessages error = ErrorMessages.ERROR_AUTHENTICATOR_ALREADY_EXIST; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), - String.format(error.getDescription(), authenticatorConfig.getName())); + throw buildClientException(AuthenticatorMgtError.ERROR_AUTHENTICATOR_ALREADY_EXIST, + authenticatorConfig.getName()); } authenticatorValidator.validateAuthenticatorName(authenticatorConfig.getName()); authenticatorValidator.validateForBlank("Display name", authenticatorConfig.getDisplayName()); @@ -281,9 +279,7 @@ private UserDefinedLocalAuthenticatorConfig resolveExistingAuthenticator(String getUserDefinedLocalAuthenticator(authenticatorName, IdentityTenantUtil.getTenantId(tenantDomain)); if (existingAuthenticatorConfig == null) { - ErrorMessages error = ErrorMessages.ERROR_NOT_FOUND_AUTHENTICATOR; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), - String.format(error.getDescription(), authenticatorName)); + throw buildClientException(AuthenticatorMgtError.ERROR_NOT_FOUND_AUTHENTICATOR, authenticatorName); } return existingAuthenticatorConfig; diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/constant/AuthenticatorMgtErrorConstants.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/constant/AuthenticatorMgtErrorConstants.java deleted file mode 100644 index e7053b61d03e..000000000000 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/constant/AuthenticatorMgtErrorConstants.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.identity.application.common.constant; - -/** - * Constants for authenticator configuration management service. - */ -public class AuthenticatorMgtErrorConstants { - - /** - * Error messages. - */ - public enum ErrorMessages { - - // Client errors. - ERROR_NOT_FOUND_AUTHENTICATOR("60001", "No Authenticator found.", - "No Authenticator found by given authenticator name: %s."), - ERROR_OP_ON_SYSTEM_AUTHENTICATOR("60002", "No operations allowed on system authenticators.", - "Do not allow to perform any operation on system defined authenticator: %s."), - ERROR_AUTHENTICATOR_ALREADY_EXIST("60003", "An authenticator already exists.", - "As authenticator already exists for the given name: %s."), - ERROR_INVALID_AUTHENTICATOR_NAME("60004", "Authenticator name is invalid.", - "The provided authenticator name %s is not in the expected format %s."), - ERROR_BLANK_FIELD_VALUE("60004", "Invalid empty or blank value.", - "Value for %s should not be empty or blank."), - - // Server errors. - ERROR_WHILE_ADDING_AUTHENTICATOR("65001", "Error while adding authenticator.", - "Error while persisting authenticator from the system."), - ERROR_WHILE_UPDATING_AUTHENTICATOR("65002", "Error while updating authenticator.", - "Error while updating authenticator from the system."), - ERROR_WHILE_RETRIEVING_AUTHENTICATOR_BY_NAME("65003", "Error while retrieving authenticator.", - "Error while retrieving authenticator from the system."), - ERROR_WHILE_DELETING_AUTHENTICATOR("65004", "Error while deleting authenticator.", - "Error while deleting authenticator from the system."), - ERROR_CODE_ENDPOINT_CONFIG_MGT("65005", "Error while managing endpoint configurations.", - "Error while managing endpoint configurations for the user defined local authenticator %s."), - ERROR_CODE_INVALID_DEFINED_BY_AUTH_PROVIDED("65006", "Error while adding local authenticator.", - "Only system defined authenticators are allowed to add via this method."); - - private final String code; - private final String message; - private final String description; - - ErrorMessages(String code, String message, String description) { - - this.code = code; - this.message = message; - this.description = description; - } - - public String getCode() { - - return code; - } - - public String getMessage() { - - return message; - } - - public String getDescription() { - - return description; - } - } -} diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerRuntimeException.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerRuntimeException.java index 5c11ce9d22ee..2f90d762d0ef 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerRuntimeException.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/exception/AuthenticatorMgtServerRuntimeException.java @@ -26,7 +26,7 @@ public class AuthenticatorMgtServerRuntimeException extends RuntimeException { private final String errorCode; private final String description; - public AuthenticatorMgtServerRuntimeException(String message, String description, String errorCode) { + public AuthenticatorMgtServerRuntimeException(String errorCode, String message, String description) { super(message); this.errorCode = errorCode; diff --git a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedLocalAuthenticatorValidator.java b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedLocalAuthenticatorValidator.java index cc02731ba9d8..a457c1d49f91 100644 --- a/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedLocalAuthenticatorValidator.java +++ b/components/application-mgt/org.wso2.carbon.identity.application.common/src/main/java/org/wso2/carbon/identity/application/common/util/UserDefinedLocalAuthenticatorValidator.java @@ -19,12 +19,14 @@ package org.wso2.carbon.identity.application.common.util; import org.apache.commons.lang.StringUtils; -import org.wso2.carbon.identity.application.common.constant.AuthenticatorMgtErrorConstants.ErrorMessages; import org.wso2.carbon.identity.application.common.exception.AuthenticatorMgtClientException; +import org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.AuthenticatorMgtError; import org.wso2.carbon.identity.base.AuthenticatorPropertyConstants.DefinedByType; import java.util.regex.Pattern; +import static org.wso2.carbon.identity.application.common.util.AuthenticatorMgtExceptionBuilder.buildClientException; + /** * User Defined Local Authenticator Validator class. */ @@ -43,9 +45,7 @@ public class UserDefinedLocalAuthenticatorValidator { public void validateForBlank(String fieldName, String fieldValue) throws AuthenticatorMgtClientException { if (StringUtils.isBlank(fieldValue)) { - ErrorMessages error = ErrorMessages.ERROR_BLANK_FIELD_VALUE; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), - String.format(error.getDescription(), fieldName)); + throw buildClientException(AuthenticatorMgtError.ERROR_BLANK_FIELD_VALUE, fieldName); } } @@ -59,9 +59,8 @@ public void validateAuthenticatorName(String name) throws AuthenticatorMgtClient boolean isValidName = authenticatorNameRegexPattern.matcher(name).matches(); if (!isValidName) { - ErrorMessages error = ErrorMessages.ERROR_INVALID_AUTHENTICATOR_NAME; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), - String.format(error.getDescription(), name, AUTHENTICATOR_NAME_REGEX)); + throw buildClientException(AuthenticatorMgtError.ERROR_INVALID_AUTHENTICATOR_NAME, + name, AUTHENTICATOR_NAME_REGEX); } } @@ -75,8 +74,7 @@ public void validateDefinedByType(DefinedByType definedByType) throws AuthenticatorMgtClientException { if (definedByType != DefinedByType.USER) { - ErrorMessages error = ErrorMessages.ERROR_OP_ON_SYSTEM_AUTHENTICATOR; - throw new AuthenticatorMgtClientException(error.getCode(), error.getMessage(), error.getDescription()); + throw buildClientException(AuthenticatorMgtError.ERROR_OP_ON_SYSTEM_AUTHENTICATOR); } } }