-
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 #4154 from MDeLuise/fix-statusCodeMaxNumOfItem
🐛 [REST API] Respond with `403` when max allowed limit for entities is exceeded
- Loading branch information
Showing
10 changed files
with
148 additions
and
11 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...va/org/eclipse/kapua/commons/rest/errors/KapuaMaxNumberOfItemsReachedExceptionMapper.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) 2024 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 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; | ||
|
||
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException; | ||
import org.eclipse.kapua.commons.rest.model.errors.MaxNumberOfItemsReachedExceptionInfo; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@Provider | ||
public class KapuaMaxNumberOfItemsReachedExceptionMapper implements ExceptionMapper<KapuaMaxNumberOfItemsReachedException> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(KapuaMaxNumberOfItemsReachedExceptionMapper.class); | ||
private final boolean showStackTrace; | ||
|
||
|
||
@Inject | ||
public KapuaMaxNumberOfItemsReachedExceptionMapper(ExceptionConfigurationProvider exceptionConfigurationProvider) { | ||
this.showStackTrace = exceptionConfigurationProvider.showStackTrace(); | ||
} | ||
|
||
|
||
@Override | ||
public Response toResponse(KapuaMaxNumberOfItemsReachedException kapuaMaxNumberOfItemsReachedException) { | ||
LOG.error(kapuaMaxNumberOfItemsReachedException.getMessage(), kapuaMaxNumberOfItemsReachedException); | ||
return Response | ||
.status(Status.FORBIDDEN) | ||
.entity(new MaxNumberOfItemsReachedExceptionInfo(Status.FORBIDDEN.getStatusCode(), kapuaMaxNumberOfItemsReachedException, showStackTrace)) | ||
.build(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...ava/org/eclipse/kapua/commons/rest/model/errors/MaxNumberOfItemsReachedExceptionInfo.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,63 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 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 javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
import org.eclipse.kapua.KapuaMaxNumberOfItemsReachedException; | ||
|
||
@XmlRootElement(name = "maxNumberOfItemsReachedExceptionInfo") | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
public class MaxNumberOfItemsReachedExceptionInfo extends ExceptionInfo { | ||
|
||
@XmlElement(name = "entityType") | ||
private String entityType; | ||
|
||
|
||
/** | ||
* Constructor. | ||
* | ||
* @since 2.0.0 | ||
*/ | ||
protected MaxNumberOfItemsReachedExceptionInfo() { | ||
super(); | ||
} | ||
|
||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param httpStatusCode The http status code of the response containing this info | ||
* @param kapuaMaxNumberOfItemsReachedException The root exception. | ||
* @since 2.0.0 | ||
*/ | ||
public MaxNumberOfItemsReachedExceptionInfo(int httpStatusCode, KapuaMaxNumberOfItemsReachedException kapuaMaxNumberOfItemsReachedException, boolean showStackTrace) { | ||
super(httpStatusCode, kapuaMaxNumberOfItemsReachedException, showStackTrace); | ||
this.entityType = kapuaMaxNumberOfItemsReachedException.getEntityType(); | ||
} | ||
|
||
|
||
/** | ||
* Gets the {@link KapuaMaxNumberOfItemsReachedException#getEntityType()}. | ||
* | ||
* @return The {@link KapuaMaxNumberOfItemsReachedException#getEntityType()}. | ||
* @since 2.0.0 | ||
*/ | ||
public String getEntityType() { | ||
return entityType; | ||
} | ||
|
||
} |
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
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