diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java deleted file mode 100644 index 4c349a806..000000000 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/exception/ConstraintViolationException.java +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright (c) 2015 Bosch Software Innovations GmbH and others. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package org.eclipse.hawkbit.repository.exception; - -import org.eclipse.hawkbit.exception.AbstractServerRtException; -import org.eclipse.hawkbit.exception.SpServerError; - -/** - * the {@link ConstraintViolationException} is thrown when an entity is tried to - * be saved and violates a constraints, like value > 0 - */ -public class ConstraintViolationException extends AbstractServerRtException { - - private static final long serialVersionUID = 1L; - private static final SpServerError THIS_ERROR = SpServerError.SP_REPO_CONSTRAINT_VIOLATION; - - /** - * Default constructor. - */ - public ConstraintViolationException() { - super(THIS_ERROR); - } - - /** - * Parameterized constructor. - * - * @param cause - * of the exception - */ - public ConstraintViolationException(final Throwable cause) { - super(THIS_ERROR, cause); - } - - /** - * Parameterized constructor. - * - * @param message - * of the exception - * @param cause - * of the exception - */ - public ConstraintViolationException(final String message, final Throwable cause) { - super(message, THIS_ERROR, cause); - } - - /** - * Parameterized constructor. - * - * @param message - * of the exception - */ - public ConstraintViolationException(final String message) { - super(message, THIS_ERROR); - } -} diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSoftwareManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSoftwareManagement.java index c0cad26cc..3f8c1a54b 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSoftwareManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSoftwareManagement.java @@ -31,7 +31,6 @@ import org.eclipse.hawkbit.repository.SoftwareManagement; import org.eclipse.hawkbit.repository.SoftwareModuleFields; import org.eclipse.hawkbit.repository.SoftwareModuleMetadataFields; import org.eclipse.hawkbit.repository.SoftwareModuleTypeFields; -import org.eclipse.hawkbit.repository.exception.ConstraintViolationException; import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException; import org.eclipse.hawkbit.repository.exception.EntityNotFoundException; import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet; @@ -515,10 +514,6 @@ public class JpaSoftwareManagement implements SoftwareManagement { throw new EntityAlreadyExistsException("Given type contains an Id!"); } - if (type.getMaxAssignments() <= 0) { - throw new ConstraintViolationException("The value for max assignments has to be greater than 0!"); - } - return softwareModuleTypeRepository.save((JpaSoftwareModuleType) type); } diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql index 88f12d65d..dfdb6ee1d 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/H2/V1_9_0__software_module_type_constraint__H2.sql @@ -1,2 +1,2 @@ alter table sp_software_module_type - add constraint maxAssignmentCheck check (maxAssignments > 0); \ No newline at end of file + add constraint maxAssignmentCheck check (max_ds_assignments > 0); \ No newline at end of file diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql index 88f12d65d..dfdb6ee1d 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/resources/db/migration/MYSQL/V1_9_0__software_module_type_constraint__MYSQL.sql @@ -1,2 +1,2 @@ alter table sp_software_module_type - add constraint maxAssignmentCheck check (maxAssignments > 0); \ No newline at end of file + add constraint maxAssignmentCheck check (max_ds_assignments > 0); \ No newline at end of file diff --git a/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java b/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java index 744624667..0c9f7cf68 100644 --- a/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java +++ b/hawkbit-rest-core/src/main/java/org/eclipse/hawkbit/rest/exception/ResponseExceptionHandler.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; +import javax.validation.ConstraintViolationException; import org.apache.commons.lang3.exception.ExceptionUtils; import org.eclipse.hawkbit.exception.AbstractServerRtException; @@ -63,7 +64,6 @@ public class ResponseExceptionHandler { ERROR_TO_HTTP_STATUS.put(SpServerError.SP_DS_MODULE_UNSUPPORTED, HttpStatus.BAD_REQUEST); ERROR_TO_HTTP_STATUS.put(SpServerError.SP_DS_TYPE_UNDEFINED, HttpStatus.BAD_REQUEST); ERROR_TO_HTTP_STATUS.put(SpServerError.SP_REPO_TENANT_NOT_EXISTS, HttpStatus.BAD_REQUEST); - ERROR_TO_HTTP_STATUS.put(SpServerError.SP_REPO_CONSTRAINT_VIOLATION, HttpStatus.BAD_REQUEST); ERROR_TO_HTTP_STATUS.put(SpServerError.SP_ENTITY_LOCKED, HttpStatus.LOCKED); ERROR_TO_HTTP_STATUS.put(SpServerError.SP_ROLLOUT_ILLEGAL_STATE, HttpStatus.BAD_REQUEST); ERROR_TO_HTTP_STATUS.put(SpServerError.SP_CONFIGURATION_VALUE_INVALID, HttpStatus.BAD_REQUEST); @@ -121,6 +121,27 @@ public class ResponseExceptionHandler { return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + /** + * Method for handling exception of type ConstraintViolationException which + * is thrown in case the request is rejected due to a constraint violation. + * Called by the Spring-Framework for exception handling. + * + * @param request + * the Http request + * @param ex + * the exception which occurred + * @return the entity to be responded containing the exception information + * as entity. + */ + @ExceptionHandler(ConstraintViolationException.class) + public ResponseEntity handleConstraintViolationException(final HttpServletRequest request, + final Exception ex) { + logRequest(request, ex); + final ExceptionInfo response = createExceptionInfo( + new ConstraintViolationException(((ConstraintViolationException) ex).getConstraintViolations())); + return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); + } + /** * Method for handling exception of type {@link MultipartException} which is * thrown in case the request body is not well formed and cannot be