Search in sources :

Example 31 with BadRequestException

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

the class ItemService method getNamespaceId.

private long getNamespaceId(NamespaceIdentifier namespaceIdentifier) {
    String appId = namespaceIdentifier.getAppId();
    String clusterName = namespaceIdentifier.getClusterName();
    String namespaceName = namespaceIdentifier.getNamespaceName();
    Env env = namespaceIdentifier.getEnv();
    NamespaceDTO namespaceDTO = null;
    try {
        namespaceDTO = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
    } catch (HttpClientErrorException e) {
        if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
            throw new BadRequestException(String.format("namespace not exist. appId:%s, env:%s, clusterName:%s, namespaceName:%s", appId, env, clusterName, namespaceName));
        }
    }
    return namespaceDTO.getId();
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Env(com.ctrip.framework.apollo.core.enums.Env)

Example 32 with BadRequestException

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

the class ItemService method compare.

public List<ItemDiffs> compare(List<NamespaceIdentifier> comparedNamespaces, List<ItemDTO> sourceItems) {
    List<ItemDiffs> result = new LinkedList<>();
    for (NamespaceIdentifier namespace : comparedNamespaces) {
        ItemDiffs itemDiffs = new ItemDiffs(namespace);
        try {
            itemDiffs.setDiffs(parseChangeSets(namespace, sourceItems));
        } catch (BadRequestException e) {
            itemDiffs.setDiffs(new ItemChangeSets());
            itemDiffs.setExtInfo("该集群下没有名为 " + namespace.getNamespaceName() + " 的namespace");
        }
        result.add(itemDiffs);
    }
    return result;
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) ItemDiffs(com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs) LinkedList(java.util.LinkedList)

Example 33 with BadRequestException

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

the class NamespaceService method findNamespaceBOs.

/**
 * load cluster all namespace info with items
 */
public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName) {
    List<NamespaceDTO> namespaces = namespaceAPI.findNamespaceByCluster(appId, env, clusterName);
    if (namespaces == null || namespaces.size() == 0) {
        throw new BadRequestException("namespaces not exist");
    }
    List<NamespaceBO> namespaceBOs = new LinkedList<>();
    for (NamespaceDTO namespace : namespaces) {
        NamespaceBO namespaceBO;
        try {
            namespaceBO = transformNamespace2BO(env, namespace);
            namespaceBOs.add(namespaceBO);
        } catch (Exception e) {
            logger.error("parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}", appId, env, clusterName, namespace.getNamespaceName(), e);
            throw e;
        }
    }
    return namespaceBOs;
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) NamespaceBO(com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException)

Example 34 with BadRequestException

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

the class NotificationControllerV2 method pollNotification.

@RequestMapping(method = RequestMethod.GET)
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);
    }
    DeferredResultWrapper deferredResultWrapper = new DeferredResultWrapper();
    Set<String> namespaces = Sets.newHashSet();
    Map<String, Long> clientSideNotifications = Maps.newHashMap();
    Map<String, ApolloConfigNotification> filteredNotifications = filterNotifications(appId, notifications);
    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);
        }
    }
    if (CollectionUtils.isEmpty(namespaces)) {
        throw new BadRequestException("Invalid format of notifications: " + notificationsAsString);
    }
    Multimap<String, String> watchedKeysMap = watchKeysUtil.assembleAllWatchKeys(appId, cluster, namespaces, dataCenter);
    Set<String> watchedKeys = Sets.newHashSet(watchedKeysMap.values());
    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);
    } else {
        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);
    }
    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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 35 with BadRequestException

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

the class ClusterService method delete.

@Transactional
public void delete(long id, String operator) {
    Cluster cluster = clusterRepository.findOne(id);
    if (cluster == null) {
        throw new BadRequestException("cluster not exist");
    }
    // delete linked namespaces
    namespaceService.deleteByAppIdAndClusterName(cluster.getAppId(), cluster.getName(), operator);
    cluster.setDeleted(true);
    cluster.setDataChangeLastModifiedBy(operator);
    clusterRepository.save(cluster);
    auditService.audit(Cluster.class.getSimpleName(), id, Audit.OP.DELETE, operator);
}
Also used : Cluster(com.ctrip.framework.apollo.biz.entity.Cluster) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) 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