Skip to content

Commit

Permalink
refactor(apollo-common): Refactor BadRequestException
Browse files Browse the repository at this point in the history
  • Loading branch information
klboke committed Mar 22, 2023
1 parent 80c87e6 commit ba758f4
Show file tree
Hide file tree
Showing 40 changed files with 378 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void requireLockAdvice(String appId, String clusterName, String namespace
public void requireLockAdvice(long itemId, String operator) {
Item item = itemService.findOne(itemId);
if (item == null){
throw new BadRequestException("item not exist.");
throw BadRequestException.itemNotExists(itemId);
}
acquireLock(item.getNamespaceId(), operator);
}
Expand Down Expand Up @@ -117,7 +117,7 @@ void acquireLock(long namespaceId, String currentUser) {

private void acquireLock(Namespace namespace, String currentUser) {
if (namespace == null) {
throw new BadRequestException("namespace not exist.");
throw BadRequestException.namespaceNotExists();
}

long namespaceId = namespace.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void requireLockAdvice(String appId, String clusterName, String namespace
public void requireLockAdvice(long itemId, String operator) {
Item item = itemService.findOne(itemId);
if (item == null) {
throw new BadRequestException("item not exist.");
throw BadRequestException.itemNotExists(itemId);
}
tryUnlock(namespaceService.findOne(item.getNamespaceId()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public AppDTO create(@Valid @RequestBody AppDTO dto) {
App entity = BeanUtils.transform(App.class, dto);
App managedEntity = appService.findOne(entity.getAppId());
if (managedEntity != null) {
throw new BadRequestException("app already exist.");
throw BadRequestException.appAlreadyExists(entity.getAppId());
}

entity = adminService.createNewApp(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public AppNamespaceDTO create(@RequestBody AppNamespaceDTO appNamespace,

entity = managedEntity;
} else {
throw new BadRequestException("app namespaces already exist.");
throw BadRequestException.appNamespaceAlreadyExists(entity.getAppId(), entity.getName());
}

return BeanUtils.transform(AppNamespaceDTO.class, entity);
Expand All @@ -80,7 +80,7 @@ public void delete(@PathVariable("appId") String appId, @PathVariable("namespace
@RequestParam String operator) {
AppNamespace entity = appNamespaceService.findOne(appId, namespaceName);
if (entity == null) {
throw new BadRequestException("app namespace not found for appId: " + appId + " namespace: " + namespaceName);
throw BadRequestException.appNamespaceNotExists(appId, namespaceName);
}
appNamespaceService.deleteAppNamespace(entity, operator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ClusterDTO create(@PathVariable("appId") String appId,
Cluster entity = BeanUtils.transform(Cluster.class, dto);
Cluster managedEntity = clusterService.findOne(appId, entity.getName());
if (managedEntity != null) {
throw new BadRequestException("cluster already exist.");
throw BadRequestException.clusterAlreadyExists(entity.getName());
}

if (autoCreatePrivateNamespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.dto.PageDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.ItemNotFoundException;
import com.ctrip.framework.apollo.common.exception.NotFoundException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.utils.StringUtils;
Expand Down Expand Up @@ -75,7 +76,7 @@ public ItemDTO create(@PathVariable("appId") String appId,

Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
if (managedEntity != null) {
throw new BadRequestException("item already exists");
throw BadRequestException.itemAlreadyExists(entity.getKey());
}
entity = itemService.save(entity);
dto = BeanUtils.transform(ItemDTO.class, entity);
Expand Down Expand Up @@ -128,13 +129,13 @@ public ItemDTO update(@PathVariable("appId") String appId,
@RequestBody ItemDTO itemDTO) {
Item managedEntity = itemService.findOne(itemId);
if (managedEntity == null) {
throw new NotFoundException("item not found for itemId " + itemId);
throw new ItemNotFoundException(appId, clusterName, namespaceName, 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!");
throw BadRequestException.namespaceNotMatch();
}

Item entity = BeanUtils.transform(Item.class, itemDTO);
Expand Down Expand Up @@ -172,7 +173,7 @@ public ItemDTO update(@PathVariable("appId") String appId,
public void delete(@PathVariable("itemId") long itemId, @RequestParam String operator) {
Item entity = itemService.findOne(itemId);
if (entity == null) {
throw new NotFoundException("item not found for itemId " + itemId);
throw new ItemNotFoundException(itemId);
}
itemService.delete(entity.getId(), operator);

Expand Down Expand Up @@ -222,7 +223,7 @@ public List<ItemDTO> findDeletedItems(@PathVariable("appId") String appId,
public ItemDTO get(@PathVariable("itemId") long itemId) {
Item item = itemService.findOne(itemId);
if (item == null) {
throw new NotFoundException("item not found for itemId " + itemId);
throw new ItemNotFoundException(itemId);
}
return BeanUtils.transform(ItemDTO.class, item);
}
Expand All @@ -233,8 +234,7 @@ public ItemDTO get(@PathVariable("appId") String appId,
@PathVariable("namespaceName") String namespaceName, @PathVariable("key") String key) {
Item item = itemService.findOne(appId, clusterName, namespaceName, key);
if (item == null) {
throw new NotFoundException("item not found for %s %s %s %s", appId, clusterName,
namespaceName, key);
throw new ItemNotFoundException(appId, clusterName, namespaceName, key);
}
return BeanUtils.transform(ItemDTO.class, item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,7 @@ private void checkBranch(String appId, String clusterName, String namespaceName,
private void checkNamespace(String appId, String clusterName, String namespaceName) {
Namespace parentNamespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (parentNamespace == null) {
throw new BadRequestException(
"Namespace not exist. AppId = %s, ClusterName = %s, NamespaceName = %s", appId,
clusterName, namespaceName);
throw BadRequestException.namespaceNotExists(appId, clusterName, namespaceName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public NamespaceDTO create(@PathVariable("appId") String appId,
Namespace entity = BeanUtils.transform(Namespace.class, dto);
Namespace managedEntity = namespaceService.findOne(appId, clusterName, entity.getNamespaceName());
if (managedEntity != null) {
throw new BadRequestException("namespace already exist.");
throw BadRequestException.namespaceAlreadyExists(entity.getNamespaceName());
}

entity = namespaceService.save(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public NamespaceLockDTO getNamespaceLockOwner(@PathVariable String appId, @PathV
@PathVariable String namespaceName) {
Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
if (namespace == null) {
throw new BadRequestException("namespace not exist.");
throw BadRequestException.namespaceNotExists(appId, clusterName, namespaceName);
}

if (bizConfig.isNamespaceLockSwitchOff()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public AccessKey update(String appId, AccessKey entity) {

AccessKey accessKey = accessKeyRepository.findOneByAppIdAndId(appId, id);
if (accessKey == null) {
throw new BadRequestException("AccessKey not exist");
throw BadRequestException.accessKeyNotExists();
}

accessKey.setEnabled(entity.isEnabled());
Expand All @@ -86,7 +86,7 @@ public AccessKey update(String appId, AccessKey entity) {
public void delete(String appId, long id, String operator) {
AccessKey accessKey = accessKeyRepository.findOneByAppIdAndId(appId, id);
if (accessKey == null) {
throw new BadRequestException("AccessKey not exist");
throw BadRequestException.accessKeyNotExists();
}

if (accessKey.isEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void update(App app) {

App managedApp = appRepository.findByAppId(appId);
if (managedApp == null) {
throw new BadRequestException("App not exists. AppId = %s", appId);
throw BadRequestException.appNotExists(appId);
}

managedApp.setName(app.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public Cluster saveWithoutInstanceOfAppNamespaces(Cluster entity) {
public void delete(long id, String operator) {
Cluster cluster = clusterRepository.findById(id).orElse(null);
if (cluster == null) {
throw new BadRequestException("cluster not exist");
throw BadRequestException.clusterNotExists("");
}

//delete linked namespaces
Expand Down Expand Up @@ -151,7 +151,7 @@ public void createDefaultCluster(String appId, String createBy) {
public List<Cluster> findChildClusters(String appId, String parentClusterName) {
Cluster parentCluster = findOne(appId, parentClusterName);
if (parentCluster == null) {
throw new BadRequestException("parent cluster not exist");
throw BadRequestException.clusterNotExists(parentClusterName);
}

return clusterRepository.findByParentClusterId(parentCluster.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.ctrip.framework.apollo.common.dto.ItemChangeSets;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.ItemNotFoundException;
import com.ctrip.framework.apollo.common.exception.NotFoundException;
import com.ctrip.framework.apollo.common.exception.NamespaceNotFoundException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
Expand Down Expand Up @@ -98,7 +99,7 @@ private void doDeleteItems(List<ItemDTO> toDeleteItems, Namespace namespace, Str
for (ItemDTO item : toDeleteItems) {
Item deletedItem = itemService.delete(item.getId(), operator);
if (deletedItem.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
throw BadRequestException.namespaceNotMatch();
}

configChangeContentBuilder.deleteItem(deletedItem);
Expand All @@ -113,10 +114,10 @@ private void doUpdateItems(List<ItemDTO> toUpdateItems, Namespace namespace, Str

Item managedItem = itemService.findOne(entity.getId());
if (managedItem == null) {
throw new NotFoundException("item not found.(key=%s)", entity.getKey());
throw new ItemNotFoundException(entity.getKey());
}
if (managedItem.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
throw BadRequestException.namespaceNotMatch();
}
Item beforeUpdateItem = BeanUtils.transform(Item.class, managedItem);

Expand All @@ -137,7 +138,7 @@ private void doCreateItems(List<ItemDTO> toCreateItems, Namespace namespace, Str

for (ItemDTO item : toCreateItems) {
if (item.getNamespaceId() != namespace.getId()) {
throw new BadRequestException("Invalid request, item and namespace do not match!");
throw BadRequestException.namespaceNotMatch();
}

Item entity = BeanUtils.transform(Item.class, item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public NamespaceBranchService(
public Namespace createBranch(String appId, String parentClusterName, String namespaceName, String operator){
Namespace childNamespace = findBranch(appId, parentClusterName, namespaceName);
if (childNamespace != null){
throw new BadRequestException("namespace already has branch");
throw BadRequestException.namespaceNotExists(appId, parentClusterName, namespaceName);
}

Cluster parentCluster = clusterService.findOne(appId, parentClusterName);
if (parentCluster == null || parentCluster.getParentClusterId() != 0) {
throw new BadRequestException("cluster not exist or illegal cluster");
throw BadRequestException.clusterNotExists(parentClusterName);
}

//create child cluster
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public Page<Namespace> findByItem(String itemKey, Pageable pageable) {
public Namespace findPublicNamespaceForAssociatedNamespace(String clusterName, String namespaceName) {
AppNamespace appNamespace = appNamespaceService.findPublicNamespaceByName(namespaceName);
if (appNamespace == null) {
throw new BadRequestException("namespace not exist");
throw BadRequestException.namespaceNotExists("", clusterName, namespaceName);
}

String appId = appNamespace.getAppId();
Expand Down Expand Up @@ -392,7 +392,7 @@ public void instanceOfAppNamespaces(String appId, String clusterName, String cre
public Map<String, Boolean> namespacePublishInfo(String appId) {
List<Cluster> clusters = clusterService.findParentClusters(appId);
if (CollectionUtils.isEmpty(clusters)) {
throw new BadRequestException("app not exist");
throw BadRequestException.appNotExists(appId);
}

Map<String, Boolean> clusterHasNotPublishedItems = Maps.newHashMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,97 @@
public class BadRequestException extends AbstractApolloHttpException {

/**
* @see AbstractApolloHttpException#AbstractApolloHttpException(String, Object...)
* @see AbstractApolloHttpException#AbstractApolloHttpException(String, Object...)
*/
public BadRequestException(String msgtpl, Object... args) {
super(msgtpl, args);
setHttpStatus(HttpStatus.BAD_REQUEST);
}

public static BadRequestException itemAlreadyExists(String itemKey) {
return new BadRequestException("item already exists for itemKey:%s", itemKey);
}

public static BadRequestException itemNotExists(long itemId) {
return new BadRequestException("item not exists for itemId:%s", itemId);
}

public static BadRequestException namespaceNotExists() {
return new BadRequestException("namespace not exist.");
}

public static BadRequestException namespaceNotExists(String appId, String clusterName,
String namespaceName) {
return new BadRequestException(
"namespace not exist for appId:%s clusterName:%s namespaceName:%s", appId, clusterName,
namespaceName);
}

public static BadRequestException namespaceAlreadyExists(String namespaceName) {
return new BadRequestException("namespace already exists for namespaceName:%s", namespaceName);
}

public static BadRequestException appNamespaceNotExists(String appId, String namespaceName) {
return new BadRequestException("appNamespace not exist for appId:%s namespaceName:%s", appId, namespaceName);
}

public static BadRequestException appNamespaceAlreadyExists(String appId, String namespaceName) {
return new BadRequestException("appNamespace already exists for appId:%s namespaceName:%s", appId, namespaceName);
}

public static BadRequestException invalidNamespaceFormat(String format) {
return new BadRequestException("invalid namespace format:%s", format);
}

public static BadRequestException invalidNotificationsFormat(String format) {
return new BadRequestException("invalid notifications format:%s", format);
}

public static BadRequestException invalidClusterNameFormat(String format) {
return new BadRequestException("invalid clusterName format:%s", format);
}

public static BadRequestException invalidRoleTypeFormat(String format) {
return new BadRequestException("invalid roleType format:%s", format);
}

public static BadRequestException invalidEnvFormat(String format) {
return new BadRequestException("invalid env format:%s", format);
}

public static BadRequestException namespaceNotMatch() {
return new BadRequestException("invalid request, item and namespace do not match!");
}

public static BadRequestException appNotExists(String appId) {
return new BadRequestException("app not exists for appId:%s", appId);
}

public static BadRequestException appAlreadyExists(String appId) {
return new BadRequestException("app already exists for appId:%s", appId);
}

public static BadRequestException clusterNotExists(String clusterName) {
return new BadRequestException("cluster not exists for clusterName:%s", clusterName);
}

public static BadRequestException clusterAlreadyExists(String clusterName) {
return new BadRequestException("cluster already exists for clusterName:%s", clusterName);
}

public static BadRequestException userNotExists(String userName) {
return new BadRequestException("user not exists for userName:%s", userName);
}

public static BadRequestException userAlreadyExists(String userName) {
return new BadRequestException("user already exists for userName:%s", userName);
}

public static BadRequestException userAlreadyAuthorized(String userName) {
return new BadRequestException("%s already authorized", userName);
}

public static BadRequestException accessKeyNotExists() {
return new BadRequestException("accessKey not exist.");
}
}
Loading

0 comments on commit ba758f4

Please sign in to comment.