From 951927cd3cd0b492b32c7ddcb06fa117910d7a16 Mon Sep 17 00:00:00 2001 From: Alberto Codutti Date: Fri, 7 Feb 2025 16:53:51 +0100 Subject: [PATCH] :sparkles: [Service] Added KapuaFeatureDisabledException Signed-off-by: Alberto Codutti --- .../KapuaFeatureDisabledExceptionMapper.java | 53 +++++++++++++++++++ .../org/eclipse/kapua/KapuaErrorCodes.java | 7 +++ .../KapuaFeatureDisabledException.java | 48 +++++++++++++++++ .../kapua-service-error-messages.properties | 1 + 4 files changed, 109 insertions(+) create mode 100644 commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaFeatureDisabledExceptionMapper.java create mode 100644 service/api/src/main/java/org/eclipse/kapua/exception/KapuaFeatureDisabledException.java diff --git a/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaFeatureDisabledExceptionMapper.java b/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaFeatureDisabledExceptionMapper.java new file mode 100644 index 00000000000..ed33162eb98 --- /dev/null +++ b/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/KapuaFeatureDisabledExceptionMapper.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2025, 2025 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.commons.rest.errors; + +import org.eclipse.kapua.commons.rest.model.errors.ExceptionInfo; +import org.eclipse.kapua.commons.service.internal.KapuaServiceDisabledException; +import org.eclipse.kapua.exception.KapuaFeatureDisabledException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +/** + * {@link KapuaFeatureDisabledException}'s {@link ExceptionMapper} + * + * @since 2.1.0 + */ +@Provider +public class KapuaFeatureDisabledExceptionMapper implements ExceptionMapper { + + private static final Logger LOG = LoggerFactory.getLogger(KapuaServiceDisabledException.class); + + private static final Status STATUS = Status.FORBIDDEN; + + @Inject + public ExceptionConfigurationProvider exceptionConfigurationProvider; + + @Override + public Response toResponse(KapuaFeatureDisabledException kapuaFeatureDisabledException) { + LOG.error(kapuaFeatureDisabledException.getMessage(), kapuaFeatureDisabledException); + + boolean showStackTrace = exceptionConfigurationProvider.showStackTrace(); + return Response + .status(STATUS) + .entity(new ExceptionInfo(STATUS.getStatusCode(), kapuaFeatureDisabledException, showStackTrace)) + .build(); + } + +} diff --git a/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java b/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java index 6b92a09b55f..7b2aa93ba34 100644 --- a/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java +++ b/service/api/src/main/java/org/eclipse/kapua/KapuaErrorCodes.java @@ -135,6 +135,13 @@ public enum KapuaErrorCodes implements KapuaErrorCode { */ SERVICE_DISABLED, + /** + * @see org.eclipse.kapua.exception.KapuaFeatureDisabledException + * + * @since 2.1.0 + */ + FEATURE_DISABLED, + /** * Some parsing failed for some reason * @since 2.0.0 diff --git a/service/api/src/main/java/org/eclipse/kapua/exception/KapuaFeatureDisabledException.java b/service/api/src/main/java/org/eclipse/kapua/exception/KapuaFeatureDisabledException.java new file mode 100644 index 00000000000..23dcd3f6a67 --- /dev/null +++ b/service/api/src/main/java/org/eclipse/kapua/exception/KapuaFeatureDisabledException.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2025, 2025 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.exception; + +import org.eclipse.kapua.KapuaErrorCodes; +import org.eclipse.kapua.KapuaException; +import org.eclipse.kapua.model.id.KapuaId; +import org.eclipse.kapua.service.KapuaService; + +/** + * {@link KapuaException} to throw when a {@link KapuaService} feature is disabled per {@link KapuaService} configuration or by other means/ + * + * @since 2.1.0 + */ +public class KapuaFeatureDisabledException extends KapuaException { + + private final KapuaId scopeId; + + /** + * Constructor. + * + * @since 2.1.0 + */ + public KapuaFeatureDisabledException(KapuaId scopeId) { + super(KapuaErrorCodes.SERVICE_DISABLED, scopeId); + + this.scopeId = scopeId; + } + + /** + * Gets the Account.id for which the feature is disabled + * + * @return The Account.id for which the feature is disabled + */ + public KapuaId getScopeId() { + return scopeId; + } +} diff --git a/service/api/src/main/resources/kapua-service-error-messages.properties b/service/api/src/main/resources/kapua-service-error-messages.properties index 2ebbb483f36..ad31cc54d5d 100644 --- a/service/api/src/main/resources/kapua-service-error-messages.properties +++ b/service/api/src/main/resources/kapua-service-error-messages.properties @@ -35,6 +35,7 @@ PERMISSION_DELETE_NOT_ALLOWED=Operation not allowed on this specific permission. RESOURCE_RESTRICTED_TO_FIRST_LEVEL_ACCOUNTS=The resource {0} can only be created and managed in the first level accounts (direct child account of the Sys Admin Account). RESOURCE_RESTRICTED_TO_SYS_ADMIN_ACCOUNT=The resource {0} can only be created and managed in the system administrator account. SERVICE_DISABLED=The Service is disabled: {0} +FEATURE_DISABLED=This feature is disabled for Account: {0} UNAUTHENTICATED=No authenticated Subject found in context. # Deprecated codes USER_ALREADY_RESERVED_BY_ANOTHER_CONNECTION=This user is already reserved for another connection. Please select different user for this connection.