Search in sources :

Example 21 with BadRequestException

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

the class AppService method createAppInLocal.

@Transactional
public App createAppInLocal(App app) {
    String appId = app.getAppId();
    App managedApp = appRepository.findByAppId(appId);
    if (managedApp != null) {
        throw new BadRequestException(String.format("App already exists. AppId = %s", appId));
    }
    UserInfo owner = userService.findByUserId(app.getOwnerName());
    if (owner == null) {
        throw new BadRequestException("Application's owner not exist.");
    }
    app.setOwnerEmail(owner.getEmail());
    String operator = userInfoHolder.getUser().getUserId();
    app.setDataChangeCreatedBy(operator);
    app.setDataChangeLastModifiedBy(operator);
    App createdApp = appRepository.save(app);
    appNamespaceService.createDefaultAppNamespace(appId);
    roleInitializationService.initAppRoles(createdApp);
    Tracer.logEvent(TracerEventType.CREATE_APP, appId);
    return createdApp;
}
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 22 with BadRequestException

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

the class FavoriteService method addFavorite.

public Favorite addFavorite(Favorite favorite) {
    UserInfo user = userService.findByUserId(favorite.getUserId());
    if (user == null) {
        throw new BadRequestException("user not exist");
    }
    UserInfo loginUser = userInfoHolder.getUser();
    // user can only add himself favorite app
    if (!loginUser.equals(user)) {
        throw new BadRequestException("add favorite fail. " + "because favorite's user is not current login user.");
    }
    Favorite checkedFavorite = favoriteRepository.findByUserIdAndAppId(loginUser.getUserId(), favorite.getAppId());
    if (checkedFavorite != null) {
        return checkedFavorite;
    }
    favorite.setPosition(POSITION_DEFAULT);
    favorite.setDataChangeCreatedBy(user.getUserId());
    favorite.setDataChangeLastModifiedBy(user.getUserId());
    return favoriteRepository.save(favorite);
}
Also used : Favorite(com.ctrip.framework.apollo.portal.entity.po.Favorite) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo)

Example 23 with BadRequestException

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

the class NamespaceBranchService method calculateBranchChangeSet.

private ItemChangeSets calculateBranchChangeSet(String appId, Env env, String clusterName, String namespaceName, String branchName) {
    NamespaceBO parentNamespace = namespaceService.loadNamespaceBO(appId, env, clusterName, namespaceName);
    if (parentNamespace == null) {
        throw new BadRequestException("base namespace not existed");
    }
    if (parentNamespace.getItemModifiedCnt() > 0) {
        throw new BadRequestException("Merge operation failed. Because master has modified items");
    }
    List<ItemDTO> masterItems = itemService.findItems(appId, env, clusterName, namespaceName);
    List<ItemDTO> branchItems = itemService.findItems(appId, env, branchName, namespaceName);
    ItemChangeSets changeSets = itemsComparator.compareIgnoreBlankAndCommentItem(parentNamespace.getBaseInfo().getId(), masterItems, branchItems);
    changeSets.setDeleteItems(Collections.emptyList());
    changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId());
    return changeSets;
}
Also used : NamespaceBO(com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException)

Example 24 with BadRequestException

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

the class NamespaceService method deleteNamespace.

@Transactional
public void deleteNamespace(String appId, Env env, String clusterName, String namespaceName) {
    // 1. check private namespace
    AppNamespace appNamespace = appNamespaceService.findByAppIdAndName(appId, namespaceName);
    if (appNamespace != null && !appNamespace.isPublic()) {
        throw new BadRequestException("Private namespace can not be deleted");
    }
    // 2. check parent namespace has not instances
    if (namespaceHasInstances(appId, env, clusterName, namespaceName)) {
        throw new BadRequestException("Can not delete namespace because namespace has active instances");
    }
    // 3. check child namespace has not instances
    NamespaceDTO childNamespace = branchService.findBranchBaseInfo(appId, env, clusterName, namespaceName);
    if (childNamespace != null && namespaceHasInstances(appId, env, childNamespace.getClusterName(), namespaceName)) {
        throw new BadRequestException("Can not delete namespace because namespace's branch has active instances");
    }
    // 4. check public namespace has not associated namespace
    if (appNamespace != null && publicAppNamespaceHasAssociatedNamespace(namespaceName, env)) {
        throw new BadRequestException("Can not delete public namespace which has associated namespaces");
    }
    String operator = userInfoHolder.getUser().getUserId();
    namespaceAPI.deleteNamespace(env, appId, clusterName, namespaceName, operator);
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 25 with BadRequestException

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

the class NamespaceAcquireLockAspect method requireLockAdvice.

// delete item
@Before("@annotation(PreAcquireNamespaceLock) && args(itemId, operator, ..)")
public void requireLockAdvice(long itemId, String operator) {
    Item item = itemService.findOne(itemId);
    if (item == null) {
        throw new BadRequestException("item not exist.");
    }
    acquireLock(item.getNamespaceId(), operator);
}
Also used : Item(com.ctrip.framework.apollo.biz.entity.Item) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Before(org.aspectj.lang.annotation.Before)

Aggregations

BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)46 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 Transactional (org.springframework.transaction.annotation.Transactional)12 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)9 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)7 App (com.ctrip.framework.apollo.common.entity.App)6 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)5 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)5 Item (com.ctrip.framework.apollo.biz.entity.Item)4 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)4 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)3 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)3 OpenItemDTO (com.ctrip.framework.apollo.openapi.dto.OpenItemDTO)3 ConfigPublishEvent (com.ctrip.framework.apollo.portal.listener.ConfigPublishEvent)3 PreAcquireNamespaceLock (com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)2 Commit (com.ctrip.framework.apollo.biz.entity.Commit)2 NamespaceLock (com.ctrip.framework.apollo.biz.entity.NamespaceLock)2 Release (com.ctrip.framework.apollo.biz.entity.Release)2