Search in sources :

Example 66 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class ItemService method getNamespaceId.

private long getNamespaceId(NamespaceIdentifier namespaceIdentifier) {
    String appId = namespaceIdentifier.getAppId();
    String clusterName = namespaceIdentifier.getClusterName();
    String namespaceName = namespaceIdentifier.getNamespaceName();
    Env env = namespaceIdentifier.getEnv();
    NamespaceDTO namespaceDTO;
    try {
        namespaceDTO = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
    } catch (HttpClientErrorException e) {
        if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
            throw new BadRequestException(String.format("namespace not exist. appId:%s, env:%s, clusterName:%s, namespaceName:%s", appId, env, clusterName, namespaceName));
        }
        throw e;
    }
    return namespaceDTO.getId();
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Env(com.ctrip.framework.apollo.portal.environment.Env)

Example 67 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class AppService method updateAppInLocal.

@Transactional
public App updateAppInLocal(App app) {
    String appId = app.getAppId();
    App managedApp = appRepository.findByAppId(appId);
    if (managedApp == null) {
        throw new BadRequestException(String.format("App not exists. AppId = %s", appId));
    }
    managedApp.setName(app.getName());
    managedApp.setOrgId(app.getOrgId());
    managedApp.setOrgName(app.getOrgName());
    String ownerName = app.getOwnerName();
    UserInfo owner = userService.findByUserId(ownerName);
    if (owner == null) {
        throw new BadRequestException(String.format("App's owner not exists. owner = %s", ownerName));
    }
    managedApp.setOwnerName(owner.getUserId());
    managedApp.setOwnerEmail(owner.getEmail());
    String operator = userInfoHolder.getUser().getUserId();
    managedApp.setDataChangeLastModifiedBy(operator);
    return appRepository.save(managedApp);
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) Transactional(org.springframework.transaction.annotation.Transactional)

Example 68 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class AppNamespaceService method importAppNamespaceInLocal.

@Transactional
public AppNamespace importAppNamespaceInLocal(AppNamespace appNamespace) {
    // globally uniqueness check for public app namespace
    if (appNamespace.isPublic()) {
        checkAppNamespaceGlobalUniqueness(appNamespace);
    } else {
        // check private app namespace
        if (appNamespaceRepository.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName()) != null) {
            throw new BadRequestException("Private AppNamespace " + appNamespace.getName() + " already exists!");
        }
        // should not have the same with public app namespace
        checkPublicAppNamespaceGlobalUniqueness(appNamespace);
    }
    AppNamespace createdAppNamespace = appNamespaceRepository.save(appNamespace);
    String operator = appNamespace.getDataChangeCreatedBy();
    roleInitializationService.initNamespaceRoles(appNamespace.getAppId(), appNamespace.getName(), operator);
    roleInitializationService.initNamespaceEnvRoles(appNamespace.getAppId(), appNamespace.getName(), operator);
    return createdAppNamespace;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 69 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class AppNamespaceService method createDefaultAppNamespace.

@Transactional
public void createDefaultAppNamespace(String appId) {
    if (!isAppNamespaceNameUnique(appId, ConfigConsts.NAMESPACE_APPLICATION)) {
        throw new BadRequestException(String.format("App already has application namespace. AppId = %s", appId));
    }
    AppNamespace appNs = new AppNamespace();
    appNs.setAppId(appId);
    appNs.setName(ConfigConsts.NAMESPACE_APPLICATION);
    appNs.setComment("default app namespace");
    appNs.setFormat(ConfigFileFormat.Properties.getValue());
    String userId = userInfoHolder.getUser().getUserId();
    appNs.setDataChangeCreatedBy(userId);
    appNs.setDataChangeLastModifiedBy(userId);
    appNamespaceRepository.save(appNs);
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 70 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class AppNamespaceService method checkAppNamespaceGlobalUniqueness.

private void checkAppNamespaceGlobalUniqueness(AppNamespace appNamespace) {
    checkPublicAppNamespaceGlobalUniqueness(appNamespace);
    List<AppNamespace> privateAppNamespaces = findAllPrivateAppNamespaces(appNamespace.getName());
    if (!CollectionUtils.isEmpty(privateAppNamespaces)) {
        Set<String> appIds = Sets.newHashSet();
        for (AppNamespace ans : privateAppNamespaces) {
            appIds.add(ans.getAppId());
            if (appIds.size() == PRIVATE_APP_NAMESPACE_NOTIFICATION_COUNT) {
                break;
            }
        }
        throw new BadRequestException("Public AppNamespace " + appNamespace.getName() + " already exists as private AppNamespace in appId: " + APP_NAMESPACE_JOINER.join(appIds) + ", etc. Please select another name!");
    }
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace)

Aggregations

BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)74 Transactional (org.springframework.transaction.annotation.Transactional)20 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)16 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)16 PostMapping (org.springframework.web.bind.annotation.PostMapping)14 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)11 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)9 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)8 App (com.ctrip.framework.apollo.common.entity.App)8 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)6 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)6 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)6 Item (com.ctrip.framework.apollo.biz.entity.Item)5 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)4 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)4 AccessKey (com.ctrip.framework.apollo.biz.entity.AccessKey)3 Release (com.ctrip.framework.apollo.biz.entity.Release)3 OpenItemDTO (com.ctrip.framework.apollo.openapi.dto.OpenItemDTO)3