Search in sources :

Example 41 with BadRequestException

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

the class ItemController method createItem.

@PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST)
public OpenItemDTO createItem(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, @RequestBody OpenItemDTO item, HttpServletRequest request) {
    RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(item.getKey(), item.getValue(), item.getDataChangeCreatedBy()), "key,value,dataChangeCreatedBy 字段不能为空");
    if (userService.findByUserId(item.getDataChangeCreatedBy()) == null) {
        throw new BadRequestException("用户不存在.");
    }
    ItemDTO toCreate = OpenApiBeanUtils.transformToItemDTO(item);
    // protect
    toCreate.setLineNum(0);
    toCreate.setId(0);
    toCreate.setDataChangeLastModifiedBy(toCreate.getDataChangeCreatedBy());
    toCreate.setDataChangeLastModifiedTime(null);
    toCreate.setDataChangeCreatedTime(null);
    ItemDTO createdItem = itemService.createItem(appId, Env.fromString(env), clusterName, namespaceName, toCreate);
    return OpenApiBeanUtils.transformFromItemDTO(createdItem);
}
Also used : ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) OpenItemDTO(com.ctrip.framework.apollo.openapi.dto.OpenItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 42 with BadRequestException

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

the class NamespaceController method createNamespace.

@PreAuthorize(value = "@consumerPermissionValidator.hasCreateNamespacePermission(#request, #appId)")
@RequestMapping(value = "/openapi/v1/apps/{appId}/appnamespaces", method = RequestMethod.POST)
public OpenAppNamespaceDTO createNamespace(@PathVariable String appId, @RequestBody OpenAppNamespaceDTO appNamespaceDTO, HttpServletRequest request) {
    if (!Objects.equals(appId, appNamespaceDTO.getAppId())) {
        throw new BadRequestException(String.format("AppId not equal. AppId in path = %s, AppId in payload = %s", appId, appNamespaceDTO.getAppId()));
    }
    RequestPrecondition.checkArgumentsNotEmpty(appNamespaceDTO.getAppId(), appNamespaceDTO.getName(), appNamespaceDTO.getFormat(), appNamespaceDTO.getDataChangeCreatedBy());
    if (!InputValidator.isValidAppNamespace(appNamespaceDTO.getName())) {
        throw new BadRequestException(String.format("Namespace格式错误: %s", InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE + " & " + InputValidator.INVALID_NAMESPACE_NAMESPACE_MESSAGE));
    }
    if (!ConfigFileFormat.isValidFormat(appNamespaceDTO.getFormat())) {
        throw new BadRequestException(String.format("Invalid namespace format. format = %s", appNamespaceDTO.getFormat()));
    }
    String operator = appNamespaceDTO.getDataChangeCreatedBy();
    if (userService.findByUserId(operator) == null) {
        throw new BadRequestException(String.format("Illegal user. user = %s", operator));
    }
    AppNamespace appNamespace = OpenApiBeanUtils.transformToAppNamespace(appNamespaceDTO);
    AppNamespace createdAppNamespace = appNamespaceService.createAppNamespaceInLocal(appNamespace);
    publisher.publishEvent(new AppNamespaceCreationEvent(createdAppNamespace));
    return OpenApiBeanUtils.transformToOpenAppNamespaceDTO(createdAppNamespace);
}
Also used : AppNamespaceCreationEvent(com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 43 with BadRequestException

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

the class NamespaceController method createAppNamespace.

@PreAuthorize(value = "@permissionValidator.hasCreateAppNamespacePermission(#appId, #appNamespace)")
@RequestMapping(value = "/apps/{appId}/appnamespaces", method = RequestMethod.POST)
public AppNamespace createAppNamespace(@PathVariable String appId, @RequestBody AppNamespace appNamespace) {
    RequestPrecondition.checkArgumentsNotEmpty(appNamespace.getAppId(), appNamespace.getName());
    if (!InputValidator.isValidAppNamespace(appNamespace.getName())) {
        throw new BadRequestException(String.format("Namespace格式错误: %s", InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE + " & " + InputValidator.INVALID_NAMESPACE_NAMESPACE_MESSAGE));
    }
    AppNamespace createdAppNamespace = appNamespaceService.createAppNamespaceInLocal(appNamespace);
    if (portalConfig.canAppAdminCreatePrivateNamespace() || createdAppNamespace.isPublic()) {
        assignNamespaceRoleToOperator(appId, appNamespace.getName());
    }
    publisher.publishEvent(new AppNamespaceCreationEvent(createdAppNamespace));
    return createdAppNamespace;
}
Also used : AppNamespaceCreationEvent(com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 44 with BadRequestException

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

the class AppNamespaceService method createAppNamespaceInLocal.

@Transactional
public AppNamespace createAppNamespaceInLocal(AppNamespace appNamespace) {
    String appId = appNamespace.getAppId();
    // add app org id as prefix
    App app = appService.load(appId);
    if (app == null) {
        throw new BadRequestException("App not exist. AppId = " + appId);
    }
    StringBuilder appNamespaceName = new StringBuilder();
    // add prefix postfix
    appNamespaceName.append(appNamespace.isPublic() ? app.getOrgId() + "." : "").append(appNamespace.getName()).append(appNamespace.formatAsEnum() == ConfigFileFormat.Properties ? "" : "." + appNamespace.getFormat());
    appNamespace.setName(appNamespaceName.toString());
    if (appNamespace.getComment() == null) {
        appNamespace.setComment("");
    }
    if (!ConfigFileFormat.isValidFormat(appNamespace.getFormat())) {
        throw new BadRequestException("Invalid namespace format. format must be properties、json、yaml、yml、xml");
    }
    String operator = appNamespace.getDataChangeCreatedBy();
    if (StringUtils.isEmpty(operator)) {
        operator = userInfoHolder.getUser().getUserId();
        appNamespace.setDataChangeCreatedBy(operator);
    }
    appNamespace.setDataChangeLastModifiedBy(operator);
    // unique check
    if (appNamespace.isPublic() && findPublicAppNamespace(appNamespace.getName()) != null) {
        throw new BadRequestException(appNamespace.getName() + "已存在");
    }
    if (!appNamespace.isPublic() && appNamespaceRepository.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName()) != null) {
        throw new BadRequestException(appNamespace.getName() + "已存在");
    }
    AppNamespace createdAppNamespace = appNamespaceRepository.save(appNamespace);
    roleInitializationService.initNamespaceRoles(appNamespace.getAppId(), appNamespace.getName(), operator);
    return createdAppNamespace;
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 45 with BadRequestException

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

the class AppNamespaceService method createDefaultAppNamespace.

@Transactional
public void createDefaultAppNamespace(String appId) {
    if (!isAppNamespaceNameUnique(appId, ConfigConsts.NAMESPACE_APPLICATION)) {
        throw new BadRequestException(String.format("App already has application namespace. AppId = %s", appId));
    }
    AppNamespace appNs = new AppNamespace();
    appNs.setAppId(appId);
    appNs.setName(ConfigConsts.NAMESPACE_APPLICATION);
    appNs.setComment("default app namespace");
    appNs.setFormat(ConfigFileFormat.Properties.getValue());
    String userId = userInfoHolder.getUser().getUserId();
    appNs.setDataChangeCreatedBy(userId);
    appNs.setDataChangeLastModifiedBy(userId);
    appNamespaceRepository.save(appNs);
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

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