Search in sources :

Example 1 with BadRequestException

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

the class ItemController method create.

@PreAcquireNamespaceLock
@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
public ItemDTO create(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO dto) {
    Item entity = BeanUtils.transfrom(Item.class, dto);
    ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
    Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
    if (managedEntity != null) {
        throw new BadRequestException("item already exist");
    } else {
        entity = itemService.save(entity);
        builder.createItem(entity);
    }
    dto = BeanUtils.transfrom(ItemDTO.class, entity);
    Commit commit = new Commit();
    commit.setAppId(appId);
    commit.setClusterName(clusterName);
    commit.setNamespaceName(namespaceName);
    commit.setChangeSets(builder.build());
    commit.setDataChangeCreatedBy(dto.getDataChangeLastModifiedBy());
    commit.setDataChangeLastModifiedBy(dto.getDataChangeLastModifiedBy());
    commitService.save(commit);
    return dto;
}
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) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with BadRequestException

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

the class NamespaceController method create.

@RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.POST)
public NamespaceDTO create(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @RequestBody NamespaceDTO dto) {
    if (!InputValidator.isValidClusterNamespace(dto.getNamespaceName())) {
        throw new BadRequestException(String.format("Namespace格式错误: %s", InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
    }
    Namespace entity = BeanUtils.transfrom(Namespace.class, dto);
    Namespace managedEntity = namespaceService.findOne(appId, clusterName, entity.getNamespaceName());
    if (managedEntity != null) {
        throw new BadRequestException("namespace already exist.");
    }
    entity = namespaceService.save(entity);
    dto = BeanUtils.transfrom(NamespaceDTO.class, entity);
    return dto;
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with BadRequestException

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

the class NamespaceLockController method getNamespaceLockOwner.

@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/lock", method = RequestMethod.GET)
public NamespaceLockDTO getNamespaceLockOwner(@PathVariable String appId, @PathVariable String clusterName, @PathVariable String namespaceName) {
    Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
    if (namespace == null) {
        throw new BadRequestException("namespace not exist.");
    }
    if (bizConfig.isNamespaceLockSwitchOff()) {
        return null;
    }
    NamespaceLock lock = namespaceLockService.findLock(namespace.getId());
    if (lock == null) {
        return null;
    }
    return BeanUtils.transfrom(NamespaceLockDTO.class, lock);
}
Also used : NamespaceLock(com.ctrip.framework.apollo.biz.entity.NamespaceLock) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with BadRequestException

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

the class NamespaceAcquireLockAspect method acquireLock.

private void acquireLock(Namespace namespace, String currentUser) {
    if (namespace == null) {
        throw new BadRequestException("namespace not exist.");
    }
    long namespaceId = namespace.getId();
    NamespaceLock namespaceLock = namespaceLockService.findLock(namespaceId);
    if (namespaceLock == null) {
        try {
            tryLock(namespaceId, currentUser);
        // lock success
        } catch (DataIntegrityViolationException e) {
            // lock fail
            namespaceLock = namespaceLockService.findLock(namespaceId);
            checkLock(namespace, namespaceLock, currentUser);
        } catch (Exception e) {
            logger.error("try lock error", e);
            throw e;
        }
    } else {
        // check lock owner is current user
        checkLock(namespace, namespaceLock, currentUser);
    }
}
Also used : NamespaceLock(com.ctrip.framework.apollo.biz.entity.NamespaceLock) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 5 with BadRequestException

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

the class AppNamespaceController method create.

@RequestMapping(value = "/apps/{appId}/appnamespaces", method = RequestMethod.POST)
public AppNamespaceDTO create(@RequestBody AppNamespaceDTO appNamespace) {
    AppNamespace entity = BeanUtils.transfrom(AppNamespace.class, appNamespace);
    AppNamespace managedEntity = appNamespaceService.findOne(entity.getAppId(), entity.getName());
    if (managedEntity != null) {
        throw new BadRequestException("app namespaces already exist.");
    }
    if (StringUtils.isEmpty(entity.getFormat())) {
        entity.setFormat(ConfigFileFormat.Properties.getValue());
    }
    entity = appNamespaceService.createAppNamespace(entity);
    return BeanUtils.transfrom(AppNamespaceDTO.class, entity);
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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