diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-07-22 13:46:06 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-07-22 13:46:06 +0200 |
commit | 95439601477588ba93f5772e16c6fe63b593ccc3 (patch) | |
tree | d6466b40b4457cece7c84b8e7d8739841d4e17ad | |
parent | 7c321815ed5ae9f8748ad8cbf91f79090e695b16 (diff) | |
download | sonarqube-95439601477588ba93f5772e16c6fe63b593ccc3.tar.gz sonarqube-95439601477588ba93f5772e16c6fe63b593ccc3.zip |
Use BadRequestException instead of ServerException with HttpURLConnection.HTTP_CONFLICT
4 files changed, 15 insertions, 19 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java index 34cc26e4df6..8d88085c834 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/CreateAction.java @@ -20,7 +20,6 @@ package org.sonar.server.measure.custom.ws; -import java.net.HttpURLConnection; import org.sonar.api.resources.Qualifiers; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -34,7 +33,7 @@ import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.db.metric.MetricDto; import org.sonar.server.component.ComponentFinder; import org.sonar.server.db.DbClient; -import org.sonar.server.exceptions.ServerException; +import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.user.UserSession; import org.sonar.server.user.index.UserDoc; import org.sonar.server.user.index.UserIndex; @@ -142,14 +141,14 @@ public class CreateAction implements CustomMeasuresWsAction { private static void checkIsProjectOrModule(ComponentDto component) { if (!Qualifiers.PROJECT.equals(component.qualifier()) && !Qualifiers.MODULE.equals(component.qualifier())) { - throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("Component '%s' (id: %s) must be a project or a module.", component.key(), component.uuid())); + throw new BadRequestException(String.format("Component '%s' (id: %s) must be a project or a module.", component.key(), component.uuid())); } } private void checkMeasureDoesNotExistAlready(DbSession dbSession, ComponentDto component, MetricDto metric) { int nbMeasuresOnSameMetricAndMeasure = dbClient.customMeasureDao().countByComponentIdAndMetricId(dbSession, component.uuid(), metric.getId()); if (nbMeasuresOnSameMetricAndMeasure > 0) { - throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("A measure already exists for project '%s' (id: %s) and metric '%s' (id: '%d')", + throw new BadRequestException(String.format("A measure already exists for project '%s' (id: %s) and metric '%s' (id: '%d')", component.key(), component.uuid(), metric.getKey(), metric.getId())); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/CreateAction.java index 9c161eedcc7..2f65f8ebeb0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/CreateAction.java @@ -20,7 +20,6 @@ package org.sonar.server.metric.ws; -import java.net.HttpURLConnection; import java.util.List; import javax.annotation.Nullable; import org.sonar.api.measures.Metric; @@ -34,7 +33,7 @@ import org.sonar.db.MyBatis; import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.db.metric.MetricDto; import org.sonar.server.db.DbClient; -import org.sonar.server.exceptions.ServerException; +import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.ruby.RubyBridge; import org.sonar.server.user.UserSession; @@ -183,15 +182,15 @@ public class CreateAction implements MetricsWsAction { return; } if (isMetricEnabled(metricInDb)) { - throw new ServerException(HttpURLConnection.HTTP_CONFLICT, "An active metric already exist with key: " + metricInDb.getKey()); + throw new BadRequestException("An active metric already exist with key: " + metricInDb.getKey()); } if (isMetricNonCustom(metricInDb)) { - throw new ServerException(HttpURLConnection.HTTP_CONFLICT, "An non custom metric already exist with key: " + metricInDb.getKey()); + throw new BadRequestException("An non custom metric already exist with key: " + metricInDb.getKey()); } if (hasMetricTypeChanged(metricInDb, template)) { List<CustomMeasureDto> customMeasures = dbClient.customMeasureDao().selectByMetricId(dbSession, metricInDb.getId()); if (hasAssociatedCustomMeasures(customMeasures)) { - throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("You're trying to change the type '%s' while there are associated measures.", + throw new BadRequestException(String.format("You're trying to change the type '%s' while there are associated measures.", metricInDb.getValueType())); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/UpdateAction.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/UpdateAction.java index 9272ce1bf83..1e1f0cf01d8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/UpdateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/UpdateAction.java @@ -20,7 +20,6 @@ package org.sonar.server.metric.ws; -import java.net.HttpURLConnection; import java.util.List; import javax.annotation.Nullable; import org.sonar.api.measures.Metric; @@ -28,13 +27,13 @@ import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.utils.text.JsonWriter; -import org.sonar.db.measure.custom.CustomMeasureDto; -import org.sonar.db.metric.MetricDto; import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; +import org.sonar.db.measure.custom.CustomMeasureDto; +import org.sonar.db.metric.MetricDto; import org.sonar.server.db.DbClient; -import org.sonar.server.exceptions.ServerException; +import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.ruby.RubyBridge; import org.sonar.server.user.UserSession; import org.sonar.server.util.MetricKeyValidator; @@ -178,13 +177,13 @@ public class UpdateAction implements MetricsWsAction { private void checkMetricInDbAndTemplate(DbSession dbSession, @Nullable MetricDto metricInDb, MetricDto template) { if (!isMetricFoundInDb(metricInDb) || isMetricDisabled(metricInDb) || !isMetricCustom(metricInDb)) { - throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("No active custom metric has been found for id '%d'.", template.getId())); + throw new BadRequestException(String.format("No active custom metric has been found for id '%d'.", template.getId())); } checkNoOtherMetricWithTargetKey(dbSession, metricInDb, template); if (haveMetricTypeChanged(metricInDb, template)) { List<CustomMeasureDto> customMeasures = dbClient.customMeasureDao().selectByMetricId(dbSession, metricInDb.getId()); if (haveAssociatedCustomMeasures(customMeasures)) { - throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("You're trying to change the type '%s' while there are associated custom measures.", + throw new BadRequestException(String.format("You're trying to change the type '%s' while there are associated custom measures.", metricInDb.getValueType())); } } @@ -194,7 +193,7 @@ public class UpdateAction implements MetricsWsAction { String targetKey = template.getKey(); MetricDto metricWithTargetKey = dbClient.metricDao().selectNullableByKey(dbSession, targetKey); if (isMetricFoundInDb(metricWithTargetKey) && !metricInDb.getId().equals(metricWithTargetKey.getId())) { - throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("The key '%s' is already used by an existing metric.", targetKey)); + throw new BadRequestException(String.format("The key '%s' is already used by an existing metric.", targetKey)); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/GroupUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/GroupUpdater.java index 56c0a0b1b77..8bc42ffc176 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/GroupUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/usergroups/ws/GroupUpdater.java @@ -20,14 +20,13 @@ package org.sonar.server.usergroups.ws; import com.google.common.base.Preconditions; -import java.net.HttpURLConnection; import org.sonar.api.security.DefaultGroups; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.text.JsonWriter; import org.sonar.db.DbSession; import org.sonar.db.user.GroupDto; import org.sonar.server.db.DbClient; -import org.sonar.server.exceptions.ServerException; +import org.sonar.server.exceptions.BadRequestException; @ServerSide public class GroupUpdater { @@ -67,7 +66,7 @@ public class GroupUpdater { // because MySQL cannot create a unique index // on a UTF-8 VARCHAR larger than 255 characters on InnoDB if (dbClient.groupDao().selectNullableByKey(session, name) != null) { - throw new ServerException(HttpURLConnection.HTTP_CONFLICT, String.format("Name '%s' is already taken", name)); + throw new BadRequestException(String.format("Name '%s' is already taken", name)); } } |