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;
}
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;
}
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);
}
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);
}
}
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);
}
Aggregations