Search in sources :

Example 51 with BadRequestException

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

the class ClusterController method delete.

@DeleteMapping("/apps/{appId}/clusters/{clusterName:.+}")
public void delete(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @RequestParam String operator) {
    Cluster entity = clusterService.findOne(appId, clusterName);
    if (entity == null) {
        throw new NotFoundException("cluster not found for clusterName " + clusterName);
    }
    if (ConfigConsts.CLUSTER_NAME_DEFAULT.equals(entity.getName())) {
        throw new BadRequestException("can not delete default cluster!");
    }
    clusterService.delete(entity.getId(), operator);
}
Also used : Cluster(com.ctrip.framework.apollo.biz.entity.Cluster) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping)

Example 52 with BadRequestException

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

the class ItemController method update.

@PreAcquireNamespaceLock
@PutMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}")
public ItemDTO update(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @PathVariable("itemId") long itemId, @RequestBody ItemDTO itemDTO) {
    Item managedEntity = itemService.findOne(itemId);
    if (managedEntity == null) {
        throw new NotFoundException("item not found for itemId " + itemId);
    }
    Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
    // In case someone constructs an attack scenario
    if (namespace == null || namespace.getId() != managedEntity.getNamespaceId()) {
        throw new BadRequestException("Invalid request, item and namespace do not match!");
    }
    Item entity = BeanUtils.transform(Item.class, itemDTO);
    ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
    Item beforeUpdateItem = BeanUtils.transform(Item.class, managedEntity);
    // protect. only value,comment,lastModifiedBy can be modified
    managedEntity.setValue(entity.getValue());
    managedEntity.setComment(entity.getComment());
    managedEntity.setDataChangeLastModifiedBy(entity.getDataChangeLastModifiedBy());
    entity = itemService.update(managedEntity);
    builder.updateItem(beforeUpdateItem, entity);
    itemDTO = BeanUtils.transform(ItemDTO.class, entity);
    if (builder.hasContent()) {
        Commit commit = new Commit();
        commit.setAppId(appId);
        commit.setClusterName(clusterName);
        commit.setNamespaceName(namespaceName);
        commit.setChangeSets(builder.build());
        commit.setDataChangeCreatedBy(itemDTO.getDataChangeLastModifiedBy());
        commit.setDataChangeLastModifiedBy(itemDTO.getDataChangeLastModifiedBy());
        commitService.save(commit);
    }
    return itemDTO;
}
Also used : ConfigChangeContentBuilder(com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder) Item(com.ctrip.framework.apollo.biz.entity.Item) Commit(com.ctrip.framework.apollo.biz.entity.Commit) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)

Example 53 with BadRequestException

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

the class NamespaceBranchController method checkBranch.

private void checkBranch(String appId, String clusterName, String namespaceName, String branchName) {
    // 1. check parent namespace
    checkNamespace(appId, clusterName, namespaceName);
    // 2. check child namespace
    Namespace childNamespace = namespaceService.findOne(appId, branchName, namespaceName);
    if (childNamespace == null) {
        throw new BadRequestException("Namespace's branch not exist. AppId = %s, ClusterName = %s, NamespaceName = %s, BranchName = %s", appId, clusterName, namespaceName, branchName);
    }
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace)

Example 54 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)

Example 55 with BadRequestException

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

the class NamespaceUnlockAspect method requireLockAdvice.

// delete item
@After("@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.");
    }
    tryUnlock(namespaceService.findOne(item.getNamespaceId()));
}
Also used : Item(com.ctrip.framework.apollo.biz.entity.Item) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) After(org.aspectj.lang.annotation.After)

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