use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.
the class Build method getLocatorFromPosted.
@NotNull
public String getLocatorFromPosted(@NotNull final Map<Long, Long> buildPromotionIdQueuedBuildsReplacements) {
String locatorText;
if (submittedLocator != null) {
if (submittedPromotionId != null) {
throw new BadRequestException("Both 'locator' and '" + BuildPromotionFinder.PROMOTION_ID + "' attributes are specified. Only one should be present.");
}
if (submittedId != null) {
throw new BadRequestException("Both 'locator' and '" + BuildPromotionFinder.DIMENSION_ID + "' attributes are specified. Only one should be present.");
}
locatorText = submittedLocator;
} else {
final Locator locator = Locator.createEmptyLocator();
if (submittedPromotionId != null) {
final Long replacementPromotionId = buildPromotionIdQueuedBuildsReplacements.get(submittedPromotionId);
if (replacementPromotionId != null) {
locator.setDimension(BuildPromotionFinder.PROMOTION_ID, String.valueOf(replacementPromotionId));
} else {
locator.setDimension(BuildPromotionFinder.PROMOTION_ID, String.valueOf(submittedPromotionId));
}
}
if (submittedId != null) {
// assuming https://youtrack.jetbrains.com/issue/TW-38777 never takes place
final Long replacementPromotionId = buildPromotionIdQueuedBuildsReplacements.get(submittedId);
if (replacementPromotionId != null) {
locator.setDimension(BuildPromotionFinder.PROMOTION_ID, String.valueOf(replacementPromotionId));
} else {
locator.setDimension(BuildPromotionFinder.DIMENSION_ID, String.valueOf(submittedId));
}
}
if (locator.isEmpty()) {
throw new BadRequestException("No build specified. Either '" + BuildPromotionFinder.DIMENSION_ID + "' or 'locator' attributes should be present.");
}
locatorText = locator.getStringRepresentation();
}
return locatorText;
}
use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.
the class AgentPool method setFieldValue.
public static void setFieldValue(@NotNull final jetbrains.buildServer.serverSide.agentPools.AgentPool agentPool, @NotNull final String fieldName, @Nullable final String newValue, @NotNull final BeanContext beanContext) {
String newName = agentPool.getName();
int minAgents = agentPool.getMinAgents();
int maxAgents = agentPool.getMaxAgents();
boolean modificationsFound = false;
if ("name".equals(fieldName)) {
if (StringUtil.isEmpty(newValue)) {
throw new BadRequestException("Agent pool name cannot be empty");
} else {
newName = newValue;
}
modificationsFound = true;
} else if ("maxAgents".equals(fieldName)) {
if (StringUtil.isEmpty(newValue)) {
maxAgents = AgentPoolLimits.DEFAULT.getMaxAgents();
} else {
maxAgents = Integer.valueOf(newValue);
}
modificationsFound = true;
}
if (!modificationsFound) {
throw new BadRequestException("Setting field '" + fieldName + "' is not supported. Supported are: name, maxAgents.");
}
try {
beanContext.getSingletonService(AgentPoolManager.class).updateAgentPool(agentPool.getAgentPoolId(), newName, new AgentPoolLimitsImpl(minAgents, maxAgents));
} catch (NoSuchAgentPoolException e) {
throw new BadRequestException(e.getMessage());
} catch (AgentPoolCannotBeRenamedException e) {
throw new BadRequestException(e.getMessage());
}
}
use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.
the class Property method getFromPosted.
@NotNull
public Parameter getFromPosted(@NotNull final ServiceLocator serviceLocator) {
isValid();
final ParameterFactory parameterFactory = serviceLocator.getSingletonService(ParameterFactory.class);
if (type == null || type.rawValue == null) {
return parameterFactory.createSimpleParameter(name, value);
}
// workaround for https://youtrack.jetbrains.com/issue/TW-41041
final ParameterDescriptionFactory parameterDescriptionFactory = serviceLocator.getSingletonService(jetbrains.buildServer.serverSide.parameters.ParameterDescriptionFactory.class);
try {
parameterDescriptionFactory.parseDescription(type.rawValue);
} catch (ParseException e) {
throw new BadRequestException("Wrong parameter type \"" + type.rawValue + "\" provided: " + e.getMessage());
}
return parameterFactory.createTypedParameter(name, value, type.rawValue);
}
use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.
the class Property method addTo.
@NotNull
public Parameter addTo(@NotNull final EntityWithModifiableParameters entity, @NotNull final ServiceLocator serviceLocator) {
if (StringUtil.isEmpty(name)) {
throw new BadRequestException("Parameter name cannot be empty.");
}
Parameter originalParameter = entity.getParameter(name);
Parameter originalOwnParameter = entity.getOwnParameter(name);
if (inherited != null && inherited) {
if (originalParameter != null && isSimilar(originalParameter, serviceLocator))
return originalParameter;
}
Parameter fromPosted = getFromPosted(serviceLocator);
try {
entity.addParameter(fromPosted);
return fromPosted;
} catch (Exception e) {
// restore
if (originalOwnParameter != null) {
entity.addParameter(originalOwnParameter);
} else if (entity.getParameter(name) != null) {
entity.removeParameter(name);
}
throw new BadRequestException("Cannot set parameter '" + name + "' to value '" + fromPosted.getValue() + "': " + e.toString(), e);
}
}
use of jetbrains.buildServer.server.rest.errors.BadRequestException in project teamcity-rest by JetBrains.
the class TestFinder method findSingleItem.
@Override
@Nullable
public STest findSingleItem(@NotNull final Locator locator) {
if (locator.isSingleValue()) {
// no dimensions found, assume it's id
final Long parsedId = locator.getSingleValueAsLong();
if (parsedId == null) {
throw new BadRequestException("Expecting id, found empty value.");
}
STest item = findTest(parsedId);
if (item == null) {
throw new NotFoundException("No test can be found by id '" + parsedId + "' on the entire server.");
}
return item;
}
// dimension-specific item search
Long id = locator.getSingleDimensionValueAsLong(DIMENSION_ID);
if (id != null) {
STest item = findTest(id);
if (item == null) {
throw new NotFoundException("No test" + " can be found by " + DIMENSION_ID + " '" + id + "' on the entire server.");
}
return item;
}
String nameDimension = locator.getSingleDimensionValue(NAME);
if (nameDimension != null) {
ValueCondition nameCondition = ParameterCondition.createValueConditionFromPlainValueOrCondition(nameDimension);
String constantName = nameCondition.getConstantValueIfSimpleEqualsCondition();
if (constantName == null) {
return null;
}
final Long testNameId = myTestName2Index.findTestNameId(new TestName(constantName));
if (testNameId == null) {
throw new NotFoundException("No test can be found by " + NAME + " '" + constantName + "' on the entire server.");
}
STest test = findTest(testNameId);
if (test == null) {
throw new NotFoundException("No test can be found by id corresponding to " + NAME + " '" + constantName + "' on the entire server.");
}
return test;
}
return null;
}
Aggregations