import org.sonar.db.property.PropertyDto;
import org.sonar.scanner.protocol.input.FileData;
import org.sonar.scanner.protocol.input.ProjectRepositories;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.user.UserSession;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;
-import static java.lang.String.format;
import static org.sonar.api.web.UserRole.USER;
import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
+import static org.sonar.server.ws.WsUtils.checkRequest;
@ServerSide
public class ProjectDataLoader {
ProjectRepositories data = new ProjectRepositories();
ComponentDto module = checkFoundWithOptional(dbClient.componentDao().selectByKey(session, query.getModuleKey()),
"Project or module with key '%s' is not found", query.getModuleKey());
- if (!isProjectOrModule(module)) {
- throw new BadRequestException(format("Key '%s' belongs to a component which is not a Project", query.getModuleKey()));
- }
+ checkRequest(isProjectOrModule(module), "Key '%s' belongs to a component which is not a Project", query.getModuleKey());
boolean hasScanPerm = userSession.hasComponentPermission(SCAN_EXECUTION, module) ||
userSession.hasOrganizationPermission(module.getOrganizationUuid(), SCAN_EXECUTION);
import org.sonar.db.component.ComponentDto;
import org.sonar.server.es.ProjectIndexer;
import org.sonar.server.es.ProjectIndexer.Cause;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.favorite.FavoriteUpdater;
import org.sonar.server.permission.PermissionTemplateService;
private ComponentDto createRootComponent(DbSession session, NewComponent newComponent) {
checkBranchFormat(newComponent.qualifier(), newComponent.branch());
String keyWithBranch = ComponentKeys.createKey(newComponent.key(), newComponent.branch());
- if (dbClient.componentDao().selectByKey(session, keyWithBranch).isPresent()) {
- throw new BadRequestException(formatMessage("Could not create %s, key already exists: %s", newComponent.qualifier(), keyWithBranch));
- }
+ checkRequest(!dbClient.componentDao().selectByKey(session, keyWithBranch).isPresent(),
+ formatMessage("Could not create %s, key already exists: %s", newComponent.qualifier(), keyWithBranch));
String uuid = Uuids.create();
ComponentDto component = new ComponentDto()
private void handlePermissionTemplate(DbSession dbSession, ComponentDto componentDto, String organizationUuid, @Nullable Long userId) {
permissionTemplateService.applyDefault(dbSession, organizationUuid, componentDto, userId);
if (componentDto.qualifier().equals(PROJECT)
- && permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, componentDto)) {
+ && permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, organizationUuid, componentDto)) {
favoriteUpdater.add(dbSession, componentDto, userId);
}
}
}
private void checkBranchFormat(String qualifier, @Nullable String branch) {
- if (branch != null && !ComponentKeys.isValidBranch(branch)) {
- throw new BadRequestException(formatMessage("Malformed branch for %s: %s. Allowed characters are alphanumeric, '-', '_', '.' and '/', with at least one non-digit.",
- qualifier, branch));
- }
+ checkRequest(branch == null || ComponentKeys.isValidBranch(branch),
+ formatMessage("Malformed branch for %s: %s. Allowed characters are alphanumeric, '-', '_', '.' and '/', with at least one non-digit.", qualifier, branch));
}
private String formatMessage(String message, String qualifier, String key) {
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
+import java.util.List;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
-import org.sonar.server.exceptions.BadRequestException;
-import java.util.List;
+import static org.sonar.server.ws.WsUtils.checkRequest;
/**
* Construct sorting criteria of ES requests. Sortable fields must be previously
public void fill(SearchRequestBuilder request, String name, boolean asc) {
List<Field> list = fields.get(name);
- if (list.isEmpty()) {
- throw new BadRequestException("Bad sort field: " + name);
- }
+ checkRequest(!list.isEmpty(), "Bad sort field: %s", name);
doFill(request, list, asc);
}
import static com.google.common.base.Preconditions.checkArgument;
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
+import static java.util.Arrays.asList;
/**
* Request is not valid and can not be processed.
private final transient Errors errors;
- public BadRequestException(String message) {
- super(HTTP_BAD_REQUEST, message);
- this.errors = new Errors().add(Message.of(message));
- }
-
private BadRequestException(Errors e) {
super(HTTP_BAD_REQUEST, e.messages().get(0).getMessage());
this.errors = e;
return create(new Errors().add(errorMessages.stream().map(Message::of).collect(Collectors.toList())));
}
+ public static BadRequestException create(String... errorMessages) {
+ return create(asList(errorMessages));
+ }
+
public static BadRequestException create(Errors e) {
checkArgument(!e.messages().isEmpty(), "At least one error message is required");
return new BadRequestException(e);
import org.sonar.core.issue.IssueChangeContext;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.issue.index.IssueIndex;
import org.sonar.server.user.UserSession;
+import static org.sonar.server.ws.WsUtils.checkRequest;
+
@ServerSide
@ComputeEngineSide
public class IssueService {
User user = null;
if (!Strings.isNullOrEmpty(assignee)) {
user = userFinder.findByLogin(assignee);
- if (user == null) {
- throw new BadRequestException("Unknown user: " + assignee);
- }
+ checkRequest(user != null, "Unknown user: %s", assignee);
}
IssueChangeContext context = IssueChangeContext.createUser(new Date(), userSession.getLogin());
if (issueFieldsSetter.assign(issue, user, context)) {
import org.sonar.db.metric.MetricDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.component.ComponentFinder;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.user.UserSession;
import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.server.measure.custom.ws.CustomMeasureValidator.checkPermissions;
import static org.sonar.server.measure.custom.ws.CustomMeasureValueDescription.measureValueDescription;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
+import static org.sonar.server.ws.WsUtils.checkRequest;
public class CreateAction implements CustomMeasuresWsAction {
public static final String ACTION = "create";
}
private static void checkIsProjectOrModule(ComponentDto component) {
- if (!Scopes.PROJECT.equals(component.scope())) {
- throw new BadRequestException(String.format("Component '%s' (id: %s) must be a project or a module.", component.key(), component.uuid()));
- }
+ checkRequest(Scopes.PROJECT.equals(component.scope()), "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 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()));
- }
+ checkRequest(nbMeasuresOnSameMetricAndMeasure == 0,
+ "A measure already exists for project '%s' (id: %s) and metric '%s' (id: '%d')",
+ component.key(), component.uuid(), metric.getKey(), metric.getId());
}
private MetricDto searchMetric(DbSession dbSession, Request request) {
return numericalMetricPeriodOrdering(wsRequest, metric, measuresByComponentUuidAndMetric);
}
- throw new BadRequestException(format("Impossible to sort metric '%s' by measure period.", metric.getKey()));
+ throw BadRequestException.create(format("Impossible to sort metric '%s' by measure period.", metric.getKey()));
}
private static Ordering<ComponentDto> numericalMetricOrdering(boolean isAscending, @Nullable MetricDto metric,
import org.sonar.db.DbSession;
import org.sonar.db.measure.custom.CustomMeasureDto;
import org.sonar.db.metric.MetricDto;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.user.UserSession;
+import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.server.util.MetricKeyValidator.checkMetricKeyFormat;
+import static org.sonar.server.ws.WsUtils.checkRequest;
public class CreateAction implements MetricsWsAction {
private static final String ACTION = "create";
}
private void checkMetricInDbAndTemplate(DbSession dbSession, @Nullable MetricDto metricInDb, MetricDto template) {
- if (areOneOfTheMandatoryArgumentsEmpty(template)) {
- throw new IllegalArgumentException(String.format("The mandatory arguments '%s','%s' and '%s' must not be empty", PARAM_KEY, PARAM_NAME, PARAM_TYPE));
- }
+ checkArgument(!areOneOfTheMandatoryArgumentsEmpty(template), "The mandatory arguments '%s','%s' and '%s' must not be empty", PARAM_KEY, PARAM_NAME, PARAM_TYPE);
if (metricIsNotInDb(metricInDb)) {
return;
}
- if (isMetricEnabled(metricInDb)) {
- throw new BadRequestException("An active metric already exist with key: " + metricInDb.getKey());
- }
- if (isMetricNonCustom(metricInDb)) {
- throw new BadRequestException("An non custom metric already exist with key: " + metricInDb.getKey());
- }
+ checkRequest(!isMetricEnabled(metricInDb), "An active metric already exist with key: " + metricInDb.getKey());
+ checkRequest(!isMetricNonCustom(metricInDb), "An non custom metric already exist with key: %s", metricInDb.getKey());
if (hasMetricTypeChanged(metricInDb, template)) {
List<CustomMeasureDto> customMeasures = dbClient.customMeasureDao().selectByMetricId(dbSession, metricInDb.getId());
- if (hasAssociatedCustomMeasures(customMeasures)) {
- throw new BadRequestException(String.format("You're trying to change the type '%s' while there are associated measures.",
- metricInDb.getValueType()));
- }
+ checkRequest(!hasAssociatedCustomMeasures(customMeasures), "You're trying to change the type '%s' while there are associated measures.", metricInDb.getValueType());
}
}
import org.sonar.db.DbSession;
import org.sonar.db.measure.custom.CustomMeasureDto;
import org.sonar.db.metric.MetricDto;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.user.UserSession;
import org.sonar.server.util.MetricKeyValidator;
+import static org.sonar.server.ws.WsUtils.checkRequest;
+
public class UpdateAction implements MetricsWsAction {
private static final String ACTION = "update";
}
private void checkMetricInDbAndTemplate(DbSession dbSession, @Nullable MetricDto metricInDb, MetricDto template) {
- if (!isMetricFoundInDb(metricInDb) || isMetricDisabled(metricInDb) || !isMetricCustom(metricInDb)) {
- throw new BadRequestException(String.format("No active custom metric has been found for id '%d'.", template.getId()));
- }
+ checkRequest(isMetricFoundInDb(metricInDb) && !isMetricDisabled(metricInDb) && isMetricCustom(metricInDb),
+ "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 BadRequestException(String.format("You're trying to change the type '%s' while there are associated custom measures.",
- metricInDb.getValueType()));
- }
+ checkRequest(!haveAssociatedCustomMeasures(customMeasures), "You're trying to change the type '%s' while there are associated custom measures.", metricInDb.getValueType());
}
}
private void checkNoOtherMetricWithTargetKey(DbSession dbSession, MetricDto metricInDb, MetricDto template) {
String targetKey = template.getKey();
MetricDto metricWithTargetKey = dbClient.metricDao().selectByKey(dbSession, targetKey);
- if (isMetricFoundInDb(metricWithTargetKey) && !metricInDb.getId().equals(metricWithTargetKey.getId())) {
- throw new BadRequestException(String.format("The key '%s' is already used by an existing metric.", targetKey));
- }
+ checkRequest(!isMetricFoundInDb(metricWithTargetKey) || metricInDb.getId().equals(metricWithTargetKey.getId()),
+ "The key '%s' is already used by an existing metric.", targetKey);
}
private static void writeMetric(JsonWriter json, MetricDto metric) {
package org.sonar.server.permission;
import java.util.List;
-import org.sonar.server.exceptions.BadRequestException;
-import static com.google.common.base.CharMatcher.WHITESPACE;
+import static org.apache.commons.lang.StringUtils.isNotBlank;
+import static org.sonar.server.ws.WsUtils.checkRequest;
public class ApplyPermissionTemplateQuery {
}
private void validate() {
- if (templateUuid == null || WHITESPACE.trimFrom(templateUuid).isEmpty()) {
- throw new BadRequestException("Permission template is mandatory");
- }
- if (componentKeys == null || componentKeys.isEmpty()) {
- throw new BadRequestException("No project provided. Please provide at least one project.");
- }
+ checkRequest(isNotBlank(templateUuid), "Permission template is mandatory");
+ checkRequest(componentKeys != null && !componentKeys.isEmpty(), "No project provided. Please provide at least one project.");
}
}
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.permission.GroupPermissionDto;
-import org.sonar.server.exceptions.BadRequestException;
-import static java.lang.String.format;
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
import static org.sonar.server.permission.ws.PermissionRequestValidator.validateNotAnyoneAndAdminPermission;
+import static org.sonar.server.ws.WsUtils.checkRequest;
public class GroupPermissionChanger {
// removing global admin permission from group
int remaining = dbClient.authorizationDao().countUsersWithGlobalPermissionExcludingGroup(dbSession,
change.getOrganizationUuid(), SYSTEM_ADMIN, change.getGroupIdOrAnyone().getId());
-
- if (remaining == 0) {
- throw new BadRequestException(format("Last group with permission '%s'. Permission cannot be removed.", SYSTEM_ADMIN));
- }
+ checkRequest(remaining > 0, "Last group with permission '%s'. Permission cannot be removed.", SYSTEM_ADMIN);
}
}
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.permission.UserPermissionDto;
-import org.sonar.server.exceptions.BadRequestException;
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
+import static org.sonar.server.ws.WsUtils.checkRequest;
/**
* Adds and removes user permissions. Both global and project scopes are supported.
if (SYSTEM_ADMIN.equals(change.getPermission()) && !change.getProjectId().isPresent()) {
int remaining = dbClient.authorizationDao().countUsersWithGlobalPermissionExcludingUserPermission(dbSession,
change.getOrganizationUuid(), change.getPermission(), change.getUserId().getId());
- if (remaining == 0) {
- throw new BadRequestException(String.format("Last user with permission '%s'. Permission cannot be removed.", SYSTEM_ADMIN));
- }
+ checkRequest(remaining > 0, "Last user with permission '%s'. Permission cannot be removed.", SYSTEM_ADMIN);
}
}
}
try {
Pattern.compile(projectPattern);
} catch (PatternSyntaxException e) {
- throw new BadRequestException(format("The '%s' parameter must be a valid Java regular expression. '%s' was passed", PARAM_PROJECT_KEY_PATTERN, projectPattern));
+ throw BadRequestException.create(format("The '%s' parameter must be a valid Java regular expression. '%s' was passed", PARAM_PROJECT_KEY_PATTERN, projectPattern));
}
}
}
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.core.platform.PluginInfo;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.platform.ServerFileSystem;
import org.sonar.updatecenter.common.Release;
import org.sonar.updatecenter.common.UpdateCenter;
import static org.apache.commons.lang.StringUtils.substringAfterLast;
import static org.sonar.core.platform.PluginInfo.jarToPluginInfo;
import static org.sonar.core.util.FileUtils.deleteQuietly;
+import static org.sonar.server.ws.WsUtils.checkRequest;
/**
* Downloads plugins from update center. Files are copied in the directory extensions/downloads and then
Optional<UpdateCenter> updateCenter = updateCenterMatrixFactory.getUpdateCenter(true);
if (updateCenter.isPresent()) {
List<Release> installablePlugins = updateCenter.get().findInstallablePlugins(pluginKey, version);
- if (installablePlugins.isEmpty()) {
- throw new BadRequestException(String.format("Error while downloading plugin '%s' with version '%s'. No compatible plugin found.", pluginKey,
- version.getName()));
- }
+ checkRequest(!installablePlugins.isEmpty(), "Error while downloading plugin '%s' with version '%s'. No compatible plugin found.", pluginKey, version.getName());
for (Release release : installablePlugins) {
try {
downloadRelease(release);
import static org.sonar.db.qualitygate.QualityGateConditionDto.isOperatorAllowed;
import static org.sonar.server.computation.task.projectanalysis.qualitymodel.RatingGrid.Rating.E;
import static org.sonar.server.qualitygate.ValidRatingMetrics.isCoreRatingMetric;
+import static org.sonar.server.ws.WsUtils.checkRequest;
public class QualityGateConditionsUpdater {
}
boolean conditionExists = conditions.stream().anyMatch(c -> c.getMetricId() == metric.getId() && ObjectUtils.equals(c.getPeriod(), period));
- if (conditionExists) {
- String errorMessage = period == null
- ? format("Condition on metric '%s' already exists.", metric.getShortName())
- : format("Condition on metric '%s' over leak period already exists.", metric.getShortName());
- throw new BadRequestException(errorMessage);
- }
+ checkRequest(!conditionExists, period == null
+ ? format("Condition on metric '%s' already exists.", metric.getShortName())
+ : format("Condition on metric '%s' over leak period already exists.", metric.getShortName()));
}
private static void checkRatingMetric(MetricDto metric, @Nullable String warningThreshold, @Nullable String errorThreshold, @Nullable Integer period, Errors errors) {
return getProjectThenSnapshot(dbSession, request);
}
- throw new BadRequestException(MSG_ONE_PARAMETER_ONLY);
+ throw BadRequestException.create(MSG_ONE_PARAMETER_ONLY);
}
private ProjectAndSnapshot getProjectThenSnapshot(DbSession dbSession, ProjectStatusWsRequest request) {
try {
return Long.valueOf(request.mandatoryParam(paramName));
} catch (NumberFormatException badFormat) {
- throw new BadRequestException(paramName + " must be a valid long value");
+ throw BadRequestException.create(paramName + " must be a valid long value");
}
}
private static void checkOneOfIdOrNamePresent(@Nullable Long qGateId, @Nullable String qGateName) {
if (qGateId == null && qGateName == null) {
- throw new BadRequestException("Either one of 'id' or 'name' is required.");
+ throw BadRequestException.create("Either one of 'id' or 'name' is required.");
} else if (qGateId != null && qGateName != null) {
- throw new BadRequestException("Only one of 'id' or 'name' must be provided.");
+ throw BadRequestException.create("Only one of 'id' or 'name' must be provided.");
}
}
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
+import static org.sonar.server.ws.WsUtils.checkRequest;
+
@ServerSide
public class QProfileExporters {
return importer;
}
}
- throw new BadRequestException("No such importer : " + importerKey);
+ throw BadRequestException.create("No such importer : " + importerKey);
}
private static void processValidationMessages(ValidationMessages messages, QProfileResult result) {
- if (!messages.getErrors().isEmpty()) {
- throw BadRequestException.create(messages.getErrors());
- }
+ checkRequest(messages.getErrors().isEmpty(), messages.getErrors());
result.addWarnings(messages.getWarnings());
result.addInfos(messages.getInfos());
}
public QualityProfileDto create(DbSession dbSession, QProfileName name) {
QualityProfileDto dto = db.qualityProfileDao().selectByNameAndLanguage(name.getName(), name.getLanguage(), dbSession);
- if (dto != null) {
- throw new BadRequestException("Quality profile already exists: " + name);
- }
+ checkRequest(dto == null, "Quality profile already exists: %s", name);
return doCreate(dbSession, name);
}
private QualityProfileDto doCreate(DbSession dbSession, QProfileName name) {
if (StringUtils.isEmpty(name.getName())) {
- throw new BadRequestException("quality_profiles.profile_name_cant_be_blank");
+ throw BadRequestException.create("quality_profiles.profile_name_cant_be_blank");
}
Date now = new Date();
for (int i = 0; i < 20; i++) {
private static void checkNotDefault(QualityProfileDto p) {
if (p.isDefault()) {
- throw new BadRequestException("The profile marked as default can not be deleted: " + p.getKey());
+ throw BadRequestException.create("The profile marked as default can not be deleted: " + p.getKey());
}
}
throw new NotFoundException("Quality profile not found: " + key);
}
if (!StringUtils.equals(newName, profile.getName())) {
- if (db.qualityProfileDao().selectByNameAndLanguage(newName, profile.getLanguage(), dbSession) != null) {
- throw new BadRequestException("Quality profile already exists: " + newName);
- }
+ checkRequest(db.qualityProfileDao().selectByNameAndLanguage(newName, profile.getLanguage(), dbSession) == null, "Quality profile already exists: %s", newName);
profile.setName(newName);
db.qualityProfileDao().update(dbSession, profile);
dbSession.commit();
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
+import static org.sonar.server.ws.WsUtils.checkRequest;
+
@ServerSide
public class QProfileReset {
}
private void processValidationMessages(ValidationMessages messages) {
- if (!messages.getErrors().isEmpty()) {
- throw BadRequestException.create(messages.getErrors());
- }
+ checkRequest(messages.getErrors().isEmpty(), messages.getErrors());
}
}
import org.sonar.server.util.TypeValidations;
import static com.google.common.collect.Lists.newArrayList;
+import static org.sonar.server.ws.WsUtils.checkRequest;
/**
* Activation and deactivation of rules in Quality profiles
if (activeRuleDto == null) {
return changes;
}
- if (!force && !isCascade && activeRuleDto.getInheritance() != null) {
- throw new BadRequestException("Cannot deactivate inherited rule '" + key.ruleKey() + "'");
- }
+ checkRequest(force || isCascade || activeRuleDto.getInheritance() == null, "Cannot deactivate inherited rule '%s'", key.ruleKey());
change = ActiveRuleChange.createFor(ActiveRuleChange.Type.DEACTIVATED, key);
changes.add(change);
persist(change, context, dbSession);
} else if (profile.getParentKee() == null || !parentKey.equals(profile.getParentKee())) {
QualityProfileDto parentProfile = db.qualityProfileDao().selectOrFailByKey(dbSession, parentKey);
- if (isDescendant(dbSession, profile, parentProfile)) {
- throw new BadRequestException(String.format("Descendant profile '%s' can not be selected as parent of '%s'", parentKey, profileKey));
- }
+ checkRequest(!isDescendant(dbSession, profile, parentProfile), "Descendant profile '%s' can not be selected as parent of '%s'", parentKey, profileKey);
changes.addAll(removeParent(dbSession, profile));
// set new parent
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
-import org.sonar.server.exceptions.BadRequestException;
+
+import static org.sonar.server.ws.WsUtils.checkRequest;
class RuleActivatorContext {
}
for (Map.Entry<String, String> changeParam : change.getParameters().entrySet()) {
ActiveRuleParamDto param = activeRuleParams.get(changeParam.getKey());
- if (changeParam.getValue()==null && param != null && param.getValue()!=null) {
+ if (changeParam.getValue() == null && param != null && param.getValue() != null) {
return false;
}
- if (changeParam.getValue()!=null && (param == null || !StringUtils.equals(changeParam.getValue(), param.getValue()))) {
+ if (changeParam.getValue() != null && (param == null || !StringUtils.equals(changeParam.getValue(), param.getValue()))) {
return false;
}
}
}
void verifyForActivation() {
- if (RuleStatus.REMOVED == rule.getStatus()) {
- throw new BadRequestException("Rule was removed: " + rule.getKey());
- }
- if (rule.isTemplate()) {
- throw new BadRequestException("Rule template can't be activated on a Quality profile: " + rule.getKey());
- }
- if (!profile.getLanguage().equals(rule.getLanguage())) {
- throw new BadRequestException(String.format("Rule %s and profile %s have different languages", rule.getKey(), profile.getKey()));
- }
-
+ checkRequest(RuleStatus.REMOVED != rule.getStatus(), "Rule was removed: %s", rule.getKey());
+ checkRequest(!rule.isTemplate(), "Rule template can't be activated on a Quality profile: %s", rule.getKey());
+ checkRequest(profile.getLanguage().equals(rule.getLanguage()), "Rule %s and profile %s have different languages", rule.getKey(), profile.getKey());
}
}
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
-import org.sonar.server.exceptions.BadRequestException;
+
+import static org.sonar.server.ws.WsUtils.checkRequest;
@ServerSide
public class RuleActivatorContextFactory {
RuleActivatorContext create(String profileKey, RuleKey ruleKey, DbSession session) {
RuleActivatorContext context = new RuleActivatorContext();
QualityProfileDto profile = db.qualityProfileDao().selectByKey(session, profileKey);
- if (profile == null) {
- throw new BadRequestException("Quality profile not found: " + profileKey);
- }
+ checkRequest(profile != null, "Quality profile not found: %s", profileKey);
context.setProfile(profile);
return create(ruleKey, session, context);
}
RuleActivatorContext create(QProfileName profileName, RuleKey ruleKey, DbSession session) {
RuleActivatorContext context = new RuleActivatorContext();
QualityProfileDto profile = db.qualityProfileDao().selectByNameAndLanguage(profileName.getName(), profileName.getLanguage(), session);
- if (profile == null) {
- throw new BadRequestException("Quality profile not found: " + profileName);
- }
+ checkRequest(profile != null, "Quality profile not found: %s", profileName);
context.setProfile(profile);
return create(ruleKey, session, context);
}
private RuleDto initRule(RuleKey ruleKey, RuleActivatorContext context, DbSession dbSession) {
Optional<RuleDto> rule = db.ruleDao().selectByKey(dbSession, ruleKey);
- if (!rule.isPresent()) {
- throw new BadRequestException("Rule not found: " + ruleKey);
- }
+ checkRequest(rule.isPresent(), "Rule not found: %s", ruleKey);
context.setRule(rule.get());
context.setRuleParams(db.ruleDao().selectRuleParamsByRuleKey(dbSession, rule.get().getKey()));
return rule.get();
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.user.UserDto;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.user.UserSession;
import static java.lang.String.format;
import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
+import static org.sonar.server.ws.WsUtils.checkRequest;
public class UnsetRootAction implements RootsWsAction {
private static final String PARAM_LOGIN = "login";
if (userDto == null || !userDto.isActive()) {
throw new NotFoundException(format("User with login '%s' not found", login));
}
-
- if (dbClient.userDao().countRootUsersButLogin(dbSession, login) == 0) {
- throw new BadRequestException("Last root can't be unset");
- }
+ checkRequest(dbClient.userDao().countRootUsersButLogin(dbSession, login) > 0, "Last root can't be unset");
if (userDto.isRoot()) {
dbClient.userDao().setRoot(dbSession, login, false);
dbSession.commit();
}.getType();
Gson gson = GsonHelper.create();
try {
- return (Map<String, String>) gson.fromJson(json, type);
+ return gson.fromJson(json, type);
} catch (JsonSyntaxException e) {
- throw new BadRequestException(String.format("JSON '%s' does not respect expected format for setting '%s'. Ex: {\"field1\":\"value1\", \"field2\":\"value2\"}", json, key));
+ throw BadRequestException.create(String.format("JSON '%s' does not respect expected format for setting '%s'. Ex: {\"field1\":\"value1\", \"field2\":\"value2\"}", json, key));
}
}
.filter(result -> !result.isValid())
.findAny()
.ifPresent(result -> {
- throw new BadRequestException(i18n.message(Locale.ENGLISH, "property.error." + result.getErrorKey(),
+ throw BadRequestException.create(i18n.message(Locale.ENGLISH, "property.error." + result.getErrorKey(),
format("Error when validating setting with key '%s' and value [%s]", data.key, data.values.stream().collect(Collectors.joining(", ")))));
});
}
import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.db.user.UserGroupDto;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ServerException;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.organization.OrganizationCreation;
import static java.lang.String.format;
import static org.sonar.db.user.UserDto.encryptPassword;
import static org.sonar.server.ws.WsUtils.checkFound;
+import static org.sonar.server.ws.WsUtils.checkRequest;
@ServerSide
public class UserUpdater {
setExternalIdentity(userDto, newUser.externalIdentity());
- if (!messages.isEmpty()) {
- throw BadRequestException.create(messages);
- }
+ checkRequest(messages.isEmpty(), messages);
return userDto;
}
changed |= updateExternalIdentity(updateUser, userDto);
changed |= updatePassword(updateUser, userDto, messages);
changed |= updateScmAccounts(dbSession, updateUser, userDto, messages);
- if (!messages.isEmpty()) {
- throw BadRequestException.create(messages);
- }
+ checkRequest(messages.isEmpty(), messages);
return changed;
}
private void ensureNotLastAdministrator(DbSession dbSession, UserDto user) {
List<String> problematicOrgs = selectOrganizationsWithNoMoreAdministrators(dbSession, user);
- if (!problematicOrgs.isEmpty()) {
- if (problematicOrgs.size() == 1 && defaultOrganizationProvider.get().getUuid().equals(problematicOrgs.get(0))) {
- throw new BadRequestException("User is last administrator, and cannot be deactivated");
- }
- String keys = problematicOrgs
- .stream()
- .map(orgUuid -> selectOrganizationByUuid(dbSession, orgUuid, user))
- .map(OrganizationDto::getKey)
- .sorted()
- .collect(Collectors.joining(", "));
- throw new BadRequestException(format("User is last administrator of organizations [%s], and cannot be deactivated", keys));
-
+ if (problematicOrgs.isEmpty()) {
+ return;
}
+ checkRequest(problematicOrgs.size() != 1 || !defaultOrganizationProvider.get().getUuid().equals(problematicOrgs.get(0)),
+ "User is last administrator, and cannot be deactivated");
+ String keys = problematicOrgs
+ .stream()
+ .map(orgUuid -> selectOrganizationByUuid(dbSession, orgUuid, user))
+ .map(OrganizationDto::getKey)
+ .sorted()
+ .collect(Collectors.joining(", "));
+ throw BadRequestException.create(format("User is last administrator of organizations [%s], and cannot be deactivated", keys));
}
private List<String> selectOrganizationsWithNoMoreAdministrators(DbSession dbSession, UserDto user) {
import org.sonar.db.DbSession;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.user.GroupDto;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonarqube.ws.WsUserGroups;
import static com.google.common.base.Preconditions.checkArgument;
-import static java.lang.String.format;
import static org.sonar.server.ws.WsUtils.checkFound;
import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
+import static org.sonar.server.ws.WsUtils.checkRequest;
/**
* Factorizes code about user groups between web services
// There is no database constraint on column groups.name
// because MySQL cannot create a unique index
// on a UTF-8 VARCHAR larger than 255 characters on InnoDB
- if (dbClient.groupDao().selectByName(dbSession, organizationUuid, name).isPresent()) {
- throw new BadRequestException(format("Group '%s' already exists", name));
- }
+ checkRequest(!dbClient.groupDao().selectByName(dbSession, organizationUuid, name).isPresent(), "Group '%s' already exists", name);
}
static WsUserGroups.Group.Builder toProtobuf(OrganizationDto organization, GroupDto group, int membersCount) {
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.user.UserDto;
-import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.user.UserSession;
import static java.lang.String.format;
import static org.sonar.server.usergroups.ws.GroupWsSupport.defineGroupWsParameters;
import static org.sonar.server.usergroups.ws.GroupWsSupport.defineLoginWsParameter;
import static org.sonar.server.ws.WsUtils.checkFound;
+import static org.sonar.server.ws.WsUtils.checkRequest;
public class RemoveUserAction implements UserGroupsWsAction {
private void ensureLastAdminIsNotRemoved(DbSession dbSession, GroupId group, UserDto user) {
int remainingAdmins = dbClient.authorizationDao().countUsersWithGlobalPermissionExcludingGroupMember(dbSession,
group.getOrganizationUuid(), SYSTEM_ADMIN, group.getId(), user.getId());
- if (remainingAdmins == 0) {
- throw new BadRequestException("The last administrator user cannot be removed");
- }
+ checkRequest(remainingAdmins > 0, "The last administrator user cannot be removed");
}
private UserDto getUser(DbSession dbSession, String userLogin) {
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.PropertyType;
-import org.sonar.server.exceptions.BadRequestException;
-import static java.lang.String.format;
+import static org.sonar.server.ws.WsUtils.checkRequest;
public class BooleanTypeValidation implements TypeValidation {
@Override
public void validate(String value, @Nullable List<String> options) {
- if (!StringUtils.equalsIgnoreCase(value, "true") && !StringUtils.equalsIgnoreCase(value, "false")) {
- throw new BadRequestException(format("Value '%s' must be one of \"true\" or \"false\".", value));
- }
+ checkRequest(StringUtils.equalsIgnoreCase(value, "true") || StringUtils.equalsIgnoreCase(value, "false"),
+ "Value '%s' must be one of \"true\" or \"false\".", value);
}
}
try {
Double.parseDouble(value);
} catch (NumberFormatException e) {
- throw new BadRequestException(format("Value '%s' must be an floating point number.", value));
+ throw BadRequestException.create(format("Value '%s' must be an floating point number.", value));
}
}
try {
Integer.parseInt(value);
} catch (NumberFormatException e) {
- throw new BadRequestException(format("Value '%s' must be an integer.", value));
+ throw BadRequestException.create(format("Value '%s' must be an integer.", value));
}
}
try {
Long.parseLong(value);
} catch (NumberFormatException e) {
- throw new BadRequestException(format("Value '%s' must be a long.", value));
+ throw BadRequestException.create(format("Value '%s' must be a long.", value));
}
}
}
try {
Metric.Level.valueOf(value);
} catch (IllegalArgumentException e) {
- throw new BadRequestException(format("Value '%s' must be one of \"OK\", \"WARN\", \"ERROR\".", value));
+ throw BadRequestException.create(format("Value '%s' must be one of \"OK\", \"WARN\", \"ERROR\".", value));
}
}
}
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.PropertyType;
-import org.sonar.server.exceptions.BadRequestException;
-import static java.lang.String.format;
+import static org.sonar.server.ws.WsUtils.checkRequest;
public class StringListTypeValidation implements TypeValidation {
@Override
public void validate(String value, @Nullable List<String> options) {
- if (options != null && !options.contains(value)) {
- String optionsAsString = StringUtils.join(options, ", ");
- throw new BadRequestException(format("Value '%s' must be one of : %s.", value, optionsAsString));
- }
+ checkRequest(options == null || options.contains(value), "Value '%s' must be one of : %s.", value, StringUtils.join(options, ", "));
}
}
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.sonar.api.server.ServerSide;
-import org.sonar.server.exceptions.BadRequestException;
+
+import static org.sonar.server.ws.WsUtils.checkRequest;
@ServerSide
public class TypeValidations {
typeValidation.validate(value, options);
}
- private TypeValidation findByKey(final String key) {
+ private TypeValidation findByKey(String key) {
TypeValidation typeValidation = Iterables.find(typeValidationList, new TypeValidationMatchKey(key), null);
- if (typeValidation == null) {
- throw new BadRequestException(String.format("Type '%s' is not valid.", key));
- }
+ checkRequest(typeValidation != null, "Type '%s' is not valid.", key);
return typeValidation;
}
*/
package org.sonar.server.util;
-import com.google.common.base.Strings;
-import org.sonar.server.exceptions.BadRequestException;
-
-import static java.lang.String.format;
-
public class Validation {
public static final String CANT_BE_EMPTY_MESSAGE = "%s can't be empty";
// only static methods
}
- public static void checkMandatoryParameter(String value, String paramName) {
- if (Strings.isNullOrEmpty(value)) {
- throw new BadRequestException(format(Validation.CANT_BE_EMPTY_MESSAGE, paramName));
- }
- }
-
- public static void checkMandatorySizeParameter(String value, String paramName, Integer size) {
- checkMandatoryParameter(value, paramName);
- if (!Strings.isNullOrEmpty(value) && value.length() > size) {
- throw new BadRequestException(format(Validation.IS_TOO_LONG_MESSAGE, paramName, size));
- }
- }
-
}
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
+import java.util.List;
import javax.annotation.Nullable;
import org.apache.commons.io.IOUtils;
import org.sonar.api.server.ws.Request;
*/
public static void checkRequest(boolean expression, String message, Object... messageArguments) {
if (!expression) {
- throw new BadRequestException(format(message, messageArguments));
+ throw BadRequestException.create(format(message, messageArguments));
+ }
+ }
+
+ public static void checkRequest(boolean expression, List<String> messages) {
+ if (!expression) {
+ throw BadRequestException.create(messages);
}
}
@Test
public void text_error() {
- BadRequestException exception = new BadRequestException("error");
+ BadRequestException exception = BadRequestException.create("error");
assertThat(exception.getMessage()).isEqualTo("error");
}
assertThat(underTest.errors().messages().stream().map(Message::getMessage)).containsOnly("error1", "error2");
}
+ @Test
+ public void create_exception_from_var_args() throws Exception {
+ BadRequestException underTest = BadRequestException.create("error1", "error2");
+
+ assertThat(underTest.errors().messages().stream().map(Message::getMessage)).containsOnly("error1", "error2");
+ }
+
@Test
public void create_exception_from_errors() throws Exception {
Errors errors = new Errors().add(Message.of("error1"), Message.of("error2"));
@Test
public void fail_when_project_already_exists() throws Exception {
OrganizationDto organization = db.organizations().insert();
- when(componentUpdater.create(any(DbSession.class), any(NewComponent.class), anyLong())).thenThrow(new BadRequestException("already exists"));
+ when(componentUpdater.create(any(DbSession.class), any(NewComponent.class), anyLong())).thenThrow(BadRequestException.create("already exists"));
userSession.addOrganizationPermission(organization, PROVISIONING);
expectedException.expect(BadRequestException.class);
});
createNewDefaultAction(newController, "fail_bad_request")
.setHandler((request, response) -> {
- throw new BadRequestException("Bad request !");
+ throw BadRequestException.create("Bad request !");
});
createNewDefaultAction(newController, "fail_with_multiple_messages")
.createParam("count", "Number of error messages to generate")