Search in sources :

Example 46 with BadRequestException

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

the class PermissionController method getNamespaceEnvRoles.

@GetMapping("/apps/{appId}/envs/{env}/namespaces/{namespaceName}/role_users")
public NamespaceEnvRolesAssignedUsers getNamespaceEnvRoles(@PathVariable String appId, @PathVariable String env, @PathVariable String namespaceName) {
    // validate env parameter
    if (Env.UNKNOWN == Env.transformEnv(env)) {
        throw new BadRequestException("env is illegal");
    }
    NamespaceEnvRolesAssignedUsers assignedUsers = new NamespaceEnvRolesAssignedUsers();
    assignedUsers.setNamespaceName(namespaceName);
    assignedUsers.setAppId(appId);
    assignedUsers.setEnv(Env.valueOf(env));
    Set<UserInfo> releaseNamespaceUsers = rolePermissionService.queryUsersWithRole(RoleUtils.buildReleaseNamespaceRoleName(appId, namespaceName, env));
    assignedUsers.setReleaseRoleUsers(releaseNamespaceUsers);
    Set<UserInfo> modifyNamespaceUsers = rolePermissionService.queryUsersWithRole(RoleUtils.buildModifyNamespaceRoleName(appId, namespaceName, env));
    assignedUsers.setModifyRoleUsers(modifyNamespaceUsers);
    return assignedUsers;
}
Also used : NamespaceEnvRolesAssignedUsers(com.ctrip.framework.apollo.portal.entity.vo.NamespaceEnvRolesAssignedUsers) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo)

Example 47 with BadRequestException

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

the class ReleaseController method createGrayRelease.

@PreAuthorize(value = "@permissionValidator.hasReleaseNamespacePermission(#appId, #namespaceName, #env)")
@PostMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/branches/{branchName}/releases")
public ReleaseDTO createGrayRelease(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, @PathVariable String branchName, @RequestBody NamespaceReleaseModel model) {
    model.setAppId(appId);
    model.setEnv(env);
    model.setClusterName(branchName);
    model.setNamespaceName(namespaceName);
    if (model.isEmergencyPublish() && !portalConfig.isEmergencyPublishAllowed(Env.valueOf(env))) {
        throw new BadRequestException(String.format("Env: %s is not supported emergency publish now", env));
    }
    ReleaseDTO createdRelease = releaseService.publish(model);
    ConfigPublishEvent event = ConfigPublishEvent.instance();
    event.withAppId(appId).withCluster(clusterName).withNamespace(namespaceName).withReleaseId(createdRelease.getId()).setGrayPublishEvent(true).setEnv(Env.valueOf(env));
    publisher.publishEvent(event);
    return createdRelease;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) ConfigPublishEvent(com.ctrip.framework.apollo.portal.listener.ConfigPublishEvent) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 48 with BadRequestException

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

the class NamespaceService method namespacePublishInfo.

public Map<String, Boolean> namespacePublishInfo(String appId) {
    List<Cluster> clusters = clusterService.findParentClusters(appId);
    if (CollectionUtils.isEmpty(clusters)) {
        throw new BadRequestException("app not exist");
    }
    Map<String, Boolean> clusterHasNotPublishedItems = Maps.newHashMap();
    for (Cluster cluster : clusters) {
        String clusterName = cluster.getName();
        List<Namespace> namespaces = findNamespaces(appId, clusterName);
        for (Namespace namespace : namespaces) {
            boolean isNamespaceNotPublished = isNamespaceNotPublished(namespace);
            if (isNamespaceNotPublished) {
                clusterHasNotPublishedItems.put(clusterName, true);
                break;
            }
        }
        clusterHasNotPublishedItems.putIfAbsent(clusterName, false);
    }
    return clusterHasNotPublishedItems;
}
Also used : Cluster(com.ctrip.framework.apollo.biz.entity.Cluster) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace)

Example 49 with BadRequestException

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

the class NotificationControllerV2 method pollNotification.

@GetMapping
public DeferredResult<ResponseEntity<List<ApolloConfigNotification>>> pollNotification(@RequestParam(value = "appId") String appId, @RequestParam(value = "cluster") String cluster, @RequestParam(value = "notifications") String notificationsAsString, @RequestParam(value = "dataCenter", required = false) String dataCenter, @RequestParam(value = "ip", required = false) String clientIp) {
    List<ApolloConfigNotification> notifications = null;
    try {
        notifications = gson.fromJson(notificationsAsString, notificationsTypeReference);
    } catch (Throwable ex) {
        Tracer.logError(ex);
    }
    if (CollectionUtils.isEmpty(notifications)) {
        throw new BadRequestException("Invalid format of notifications: " + notificationsAsString);
    }
    Map<String, ApolloConfigNotification> filteredNotifications = filterNotifications(appId, notifications);
    if (CollectionUtils.isEmpty(filteredNotifications)) {
        throw new BadRequestException("Invalid format of notifications: " + notificationsAsString);
    }
    DeferredResultWrapper deferredResultWrapper = new DeferredResultWrapper(bizConfig.longPollingTimeoutInMilli());
    Set<String> namespaces = Sets.newHashSetWithExpectedSize(filteredNotifications.size());
    Map<String, Long> clientSideNotifications = Maps.newHashMapWithExpectedSize(filteredNotifications.size());
    for (Map.Entry<String, ApolloConfigNotification> notificationEntry : filteredNotifications.entrySet()) {
        String normalizedNamespace = notificationEntry.getKey();
        ApolloConfigNotification notification = notificationEntry.getValue();
        namespaces.add(normalizedNamespace);
        clientSideNotifications.put(normalizedNamespace, notification.getNotificationId());
        if (!Objects.equals(notification.getNamespaceName(), normalizedNamespace)) {
            deferredResultWrapper.recordNamespaceNameNormalizedResult(notification.getNamespaceName(), normalizedNamespace);
        }
    }
    Multimap<String, String> watchedKeysMap = watchKeysUtil.assembleAllWatchKeys(appId, cluster, namespaces, dataCenter);
    Set<String> watchedKeys = Sets.newHashSet(watchedKeysMap.values());
    /**
     * 1、set deferredResult before the check, for avoid more waiting
     * If the check before setting deferredResult,it may receive a notification the next time
     * when method handleMessage is executed between check and set deferredResult.
     */
    deferredResultWrapper.onTimeout(() -> logWatchedKeys(watchedKeys, "Apollo.LongPoll.TimeOutKeys"));
    deferredResultWrapper.onCompletion(() -> {
        // unregister all keys
        for (String key : watchedKeys) {
            deferredResults.remove(key, deferredResultWrapper);
        }
        logWatchedKeys(watchedKeys, "Apollo.LongPoll.CompletedKeys");
    });
    // register all keys
    for (String key : watchedKeys) {
        this.deferredResults.put(key, deferredResultWrapper);
    }
    logWatchedKeys(watchedKeys, "Apollo.LongPoll.RegisteredKeys");
    logger.debug("Listening {} from appId: {}, cluster: {}, namespace: {}, datacenter: {}", watchedKeys, appId, cluster, namespaces, dataCenter);
    /**
     * 2、check new release
     */
    List<ReleaseMessage> latestReleaseMessages = releaseMessageService.findLatestReleaseMessagesGroupByMessages(watchedKeys);
    /**
     * Manually close the entity manager.
     * Since for async request, Spring won't do so until the request is finished,
     * which is unacceptable since we are doing long polling - means the db connection would be hold
     * for a very long time
     */
    entityManagerUtil.closeEntityManager();
    List<ApolloConfigNotification> newNotifications = getApolloConfigNotifications(namespaces, clientSideNotifications, watchedKeysMap, latestReleaseMessages);
    if (!CollectionUtils.isEmpty(newNotifications)) {
        deferredResultWrapper.setResult(newNotifications);
    }
    return deferredResultWrapper.getResult();
}
Also used : ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) DeferredResultWrapper(com.ctrip.framework.apollo.configservice.wrapper.DeferredResultWrapper) Map(java.util.Map) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 50 with BadRequestException

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

the class AppController method create.

@PostMapping("/apps")
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.");
    }
    entity = adminService.createNewApp(entity);
    return BeanUtils.transform(AppDTO.class, entity);
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PostMapping(org.springframework.web.bind.annotation.PostMapping)

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