Search in sources :

Example 16 with LocatorProcessException

use of jetbrains.buildServer.server.rest.errors.LocatorProcessException in project teamcity-rest by JetBrains.

the class BuildTypeFinder method findSingleItem.

@Override
@Nullable
public BuildTypeOrTemplate findSingleItem(@NotNull final Locator locator) {
    if (locator.isSingleValue()) {
        // no dimensions found, assume it's an internal id, external id or name
        final String value = locator.getSingleValue();
        assert value != null;
        BuildTypeOrTemplate buildType = findBuildTypeOrTemplateByInternalId(value, null);
        if (buildType != null) {
            return buildType;
        }
        buildType = findBuildTypeOrTemplateByExternalId(value, null);
        if (buildType != null) {
            return buildType;
        }
        // assume it's a name
        final BuildTypeOrTemplate buildTypeByName = findBuildTypebyName(value, null, null);
        if (buildTypeByName != null) {
            return buildTypeByName;
        }
        throw new NotFoundException("No build type or template is found by id, internal id or name '" + value + "'.");
    }
    String internalId = locator.getSingleDimensionValue(DIMENSION_INTERNAL_ID);
    if (!StringUtil.isEmpty(internalId)) {
        Boolean template = locator.getSingleDimensionValueAsBoolean(TEMPLATE_FLAG_DIMENSION_NAME);
        if (template == null) {
            // legacy support for boolean value
            try {
                template = locator.getSingleDimensionValueAsBoolean(TEMPLATE_DIMENSION_NAME);
            } catch (LocatorProcessException e) {
                // override default message as it might be confusing here due to legacy support
                throw new BadRequestException("Try omitting dimension '" + TEMPLATE_DIMENSION_NAME + "' here");
            }
        }
        BuildTypeOrTemplate buildType = findBuildTypeOrTemplateByInternalId(internalId, template);
        if (buildType != null) {
            return buildType;
        }
        throw new NotFoundException("No " + getName(template) + " is found by internal id '" + internalId + "'.");
    }
    String uuid = locator.getSingleDimensionValue(DIMENSION_UUID);
    if (!StringUtil.isEmpty(uuid)) {
        Boolean template = locator.getSingleDimensionValueAsBoolean(TEMPLATE_FLAG_DIMENSION_NAME);
        BuildTypeOrTemplate buildType = findBuildTypeOrTemplateByUuid(uuid, template);
        if (buildType != null) {
            return buildType;
        }
        // protecting against brute force uuid guessing
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        // ignore
        }
        throw new NotFoundException("No " + getName(template) + " is found by uuid '" + uuid + "'.");
    }
    String id = locator.getSingleDimensionValue(DIMENSION_ID);
    if (!StringUtil.isEmpty(id)) {
        Boolean template = locator.getSingleDimensionValueAsBoolean(TEMPLATE_FLAG_DIMENSION_NAME);
        if (template == null) {
            // legacy support for boolean value
            try {
                template = locator.getSingleDimensionValueAsBoolean(TEMPLATE_DIMENSION_NAME);
            } catch (LocatorProcessException e) {
                // override default message as it might be confusing here due to legacy support
                throw new BadRequestException("Try omitting dimension '" + TEMPLATE_DIMENSION_NAME + "' here");
            }
        }
        BuildTypeOrTemplate buildType = findBuildTypeOrTemplateByExternalId(id, template);
        if (buildType != null) {
            return buildType;
        }
        // support pre-8.0 style of template ids
        final BuildTypeOrTemplate templateByOldIdWithPrefix = findTemplateByOldIdWithPrefix(id);
        if (templateByOldIdWithPrefix != null) {
            return templateByOldIdWithPrefix;
        }
        if (TeamCityProperties.getBoolean(APIController.REST_COMPATIBILITY_ALLOW_EXTERNAL_ID_AS_INTERNAL)) {
            buildType = findBuildTypeOrTemplateByInternalId(id, template);
            if (buildType != null) {
                return buildType;
            }
            throw new NotFoundException("No " + getName(template) + " is found by id '" + id + "' in compatibility mode." + " Cannot be found by external or internal id '" + id + "'.");
        }
        throw new NotFoundException("No " + getName(template) + " is found by id '" + id + "'.");
    }
    return null;
}
Also used : BuildTypeOrTemplate(jetbrains.buildServer.server.rest.util.BuildTypeOrTemplate) NotFoundException(jetbrains.buildServer.server.rest.errors.NotFoundException) BadRequestException(jetbrains.buildServer.server.rest.errors.BadRequestException) LocatorProcessException(jetbrains.buildServer.server.rest.errors.LocatorProcessException) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LocatorProcessException (jetbrains.buildServer.server.rest.errors.LocatorProcessException)16 NotNull (org.jetbrains.annotations.NotNull)13 BadRequestException (jetbrains.buildServer.server.rest.errors.BadRequestException)12 ServiceLocator (jetbrains.buildServer.ServiceLocator)8 NotFoundException (jetbrains.buildServer.server.rest.errors.NotFoundException)7 Nullable (org.jetbrains.annotations.Nullable)7 PagerData (jetbrains.buildServer.server.rest.model.PagerData)4 SUser (jetbrains.buildServer.users.SUser)4 Logger (com.intellij.openapi.diagnostic.Logger)3 java.util (java.util)3 Collectors (java.util.stream.Collectors)3 GenericBuildsFilter (jetbrains.buildServer.server.rest.data.build.GenericBuildsFilter)3 LocatorDimension (jetbrains.buildServer.server.rest.swagger.annotations.LocatorDimension)3 LocatorResource (jetbrains.buildServer.server.rest.swagger.annotations.LocatorResource)3 LocatorDimensionDataType (jetbrains.buildServer.server.rest.swagger.constants.LocatorDimensionDataType)3 LocatorName (jetbrains.buildServer.server.rest.swagger.constants.LocatorName)3 jetbrains.buildServer.serverSide (jetbrains.buildServer.serverSide)3 Permission (jetbrains.buildServer.serverSide.auth.Permission)3 CollectionsUtil (jetbrains.buildServer.util.CollectionsUtil)3 Converter (jetbrains.buildServer.util.Converter)3