-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4199 from Coduz/fix-serviceConfigHttpValueForForb…
…iddenChange 🐛 [Service Config] Fixed returned exception when forbidden change is detected
- Loading branch information
Showing
10 changed files
with
320 additions
and
3 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...eclipse/kapua/commons/rest/errors/ServiceConfigurationUpdateForbiddenExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/******************************************************************************* | ||
* 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.configuration.exception.ServiceConfigurationForbiddenException; | ||
import org.eclipse.kapua.commons.rest.model.errors.ServiceConfigurationUpdateForbiddenExceptionInfo; | ||
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; | ||
|
||
@Provider | ||
public class ServiceConfigurationUpdateForbiddenExceptionMapper implements ExceptionMapper<ServiceConfigurationForbiddenException> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ServiceConfigurationUpdateForbiddenExceptionMapper.class); | ||
|
||
private final boolean showStackTrace; | ||
|
||
@Inject | ||
public ServiceConfigurationUpdateForbiddenExceptionMapper(ExceptionConfigurationProvider exceptionConfigurationProvider) { | ||
this.showStackTrace = exceptionConfigurationProvider.showStackTrace(); | ||
} | ||
|
||
@Override | ||
public Response toResponse(ServiceConfigurationForbiddenException serviceConfigurationForbiddenException) { | ||
LOG.error(serviceConfigurationForbiddenException.getMessage(), serviceConfigurationForbiddenException); | ||
|
||
return Response | ||
.status(Status.FORBIDDEN) | ||
.entity(new ServiceConfigurationUpdateForbiddenExceptionInfo(serviceConfigurationForbiddenException, showStackTrace)) | ||
.build(); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
...pse/kapua/commons/rest/model/errors/ServiceConfigurationUpdateForbiddenExceptionInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/******************************************************************************* | ||
* 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.model.errors; | ||
|
||
import org.eclipse.kapua.commons.configuration.exception.ServiceConfigurationForbiddenException; | ||
import org.eclipse.kapua.model.config.metatype.KapuaTad; | ||
import org.eclipse.kapua.model.id.KapuaId; | ||
import org.eclipse.kapua.model.id.KapuaIdAdapter; | ||
import org.eclipse.kapua.service.config.KapuaConfigurableService; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; | ||
|
||
@XmlRootElement(name = "serviceConfigurationUpdateForbiddenExceptionInfo") | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
public class ServiceConfigurationUpdateForbiddenExceptionInfo extends ExceptionInfo { | ||
|
||
@XmlElement(name = "scopeId") | ||
@XmlJavaTypeAdapter(KapuaIdAdapter.class) | ||
private KapuaId scopeId; | ||
|
||
@XmlElement(name = "servicePid") | ||
private String servicePid; | ||
|
||
@XmlElement(name = "propertyId") | ||
private String propertyId; | ||
|
||
@XmlElement(name = "propertyValue") | ||
private String propertyValue; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since 2.1.0 | ||
*/ | ||
protected ServiceConfigurationUpdateForbiddenExceptionInfo() { | ||
super(); | ||
} | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param serviceConfigurationForbiddenException The original exception. | ||
* @since 2.1.0 | ||
*/ | ||
public ServiceConfigurationUpdateForbiddenExceptionInfo(ServiceConfigurationForbiddenException serviceConfigurationForbiddenException, boolean showStackTrace) { | ||
super(403/*Response.Status.FORBIDDEN*/, serviceConfigurationForbiddenException, showStackTrace); | ||
|
||
this.servicePid = serviceConfigurationForbiddenException.getServicePid(); | ||
this.scopeId = serviceConfigurationForbiddenException.getScopeId(); | ||
this.propertyId = serviceConfigurationForbiddenException.getPropertyId(); | ||
this.propertyValue = serviceConfigurationForbiddenException.getPropertyValue(); | ||
} | ||
|
||
/** | ||
* Gets the scope {@link KapuaId} for which the update is forbidden. | ||
* | ||
* @return The scope {@link KapuaId} for which the update is forbidden. | ||
* @since 2.1.0 | ||
*/ | ||
public KapuaId getScopeId() { | ||
return scopeId; | ||
} | ||
|
||
/** | ||
* Gets the {@link KapuaConfigurableService} pid. | ||
* | ||
* @return he {@link KapuaConfigurableService} pid. | ||
* @since 2.1.0 | ||
*/ | ||
public String getServicePid() { | ||
return servicePid; | ||
} | ||
|
||
/** | ||
* Gets the {@link KapuaTad#getId()} for which the update is forbidden. | ||
* | ||
* @return The {@link KapuaTad#getId()} for which the update is forbidden. | ||
* @since 2.1.0 | ||
*/ | ||
public String getPropertyId() { | ||
return propertyId; | ||
} | ||
|
||
/** | ||
* Gets the {@link KapuaTad} value for which the update is forbidden. | ||
* | ||
* @return The {@link KapuaTad} value for which the update is forbidden. | ||
* @since 2.1.0 | ||
*/ | ||
public String getPropertyValue() { | ||
return propertyValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...eclipse/kapua/commons/configuration/exception/ServiceConfigurationForbiddenException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/******************************************************************************* | ||
* 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.configuration.exception; | ||
|
||
import org.eclipse.kapua.model.config.metatype.KapuaTad; | ||
import org.eclipse.kapua.model.id.KapuaId; | ||
import org.eclipse.kapua.service.account.Account; | ||
import org.eclipse.kapua.service.config.KapuaConfigurableService; | ||
import org.eclipse.kapua.service.config.ServiceComponentConfiguration; | ||
import org.eclipse.kapua.service.user.User; | ||
|
||
/** | ||
* {@link KapuaConfigurationException} to {@code throw} when an update of a {@link ServiceComponentConfiguration#getProperties()} contains updates to values that cannot be updated by the curren {@link User}. | ||
* <p> | ||
* {@link ServiceComponentConfiguration#getProperties()} can be updated under the following conditions: | ||
* <ul> | ||
* <li>If the current {@link User} is root. Root {@link User} can do whatever he wants</li> | ||
* <li>If the {@link KapuaTad} is marked to allow self-edit. For those properties a {@link User} can update the values for its {@link Account}</li> | ||
* <li>If the {@link ServiceComponentConfiguration} belongs to a child {@link Account} of the current {@link User}</li> | ||
* </ul> | ||
* | ||
* @since 2.1.0 | ||
*/ | ||
public class ServiceConfigurationForbiddenException extends KapuaConfigurationException { | ||
|
||
private final KapuaId scopeId; | ||
private final String servicePid; | ||
|
||
private final String propertyId; | ||
|
||
private final String propertyValue; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param servicePid The {@link KapuaConfigurableService} pid. | ||
* @param scopeId The scope {@link KapuaId} for which limit has been exceeded. | ||
* @param propertyId The {@link KapuaTad#getId()} for which the update is forbidden. | ||
* @param propertyValue The {@link KapuaTad} value for which the update is forbidden. | ||
* @since 2.0.0 | ||
*/ | ||
public ServiceConfigurationForbiddenException(KapuaId scopeId, String servicePid, String propertyId, String propertyValue) { | ||
super(KapuaConfigurationErrorCodes.UPDATE_PROPERTY_FORBIDDEN, scopeId, servicePid, propertyId, propertyValue); | ||
|
||
this.scopeId = scopeId; | ||
this.servicePid = servicePid; | ||
this.propertyId = propertyId; | ||
this.propertyValue = propertyValue; | ||
} | ||
|
||
/** | ||
* Gets the scope {@link KapuaId} for which the update is forbidden. | ||
* | ||
* @return The scope {@link KapuaId} for which the update is forbidden. | ||
* @since 2.1.0 | ||
*/ | ||
public KapuaId getScopeId() { | ||
return scopeId; | ||
} | ||
|
||
/** | ||
* Gets the {@link KapuaConfigurableService} pid for which the update is forbidden. | ||
* | ||
* @return he {@link KapuaConfigurableService} pid for which the update is forbidden. | ||
* @since 2.1.0 | ||
*/ | ||
public String getServicePid() { | ||
return servicePid; | ||
} | ||
|
||
/** | ||
* Gets the {@link KapuaTad#getId()} for which the update is forbidden. | ||
* | ||
* @return The {@link KapuaTad#getId()} for which the update is forbidden. | ||
* @since 2.1.0 | ||
*/ | ||
public String getPropertyId() { | ||
return propertyId; | ||
} | ||
|
||
/** | ||
* Gets the {@link KapuaTad} value for which the update is forbidden. | ||
* | ||
* @return The {@link KapuaTad} value for which the update is forbidden. | ||
* @since 2.1.0 | ||
*/ | ||
public String getPropertyValue() { | ||
return propertyValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters