当前位置: 首页>>代码示例>>Java>>正文


Java UserPrincipal.getName方法代码示例

本文整理汇总了Java中com.sun.security.auth.UserPrincipal.getName方法的典型用法代码示例。如果您正苦于以下问题:Java UserPrincipal.getName方法的具体用法?Java UserPrincipal.getName怎么用?Java UserPrincipal.getName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.sun.security.auth.UserPrincipal的用法示例。


在下文中一共展示了UserPrincipal.getName方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: subscribe

import com.sun.security.auth.UserPrincipal; //导入方法依赖的package包/类
@PermitAll
@POST
/**
 * Handle MQTT Subscribe Request in RESTful style
 * Granted QoS Levels will send back to client.
 * Retain Messages matched the subscriptions will NOT send back to client.
 */
public ResultEntity<List<MqttGrantedQoS>> subscribe(@PathParam("clientId") String clientId, @Auth UserPrincipal user, @QueryParam("protocol") @DefaultValue("4") byte protocol,
                                                    @QueryParam("packetId") @DefaultValue("0") int packetId,
                                                    List<Subscription> subscriptions) {
    String userName = user.getName();
    MqttVersion version = MqttVersion.fromProtocolLevel(protocol);
    List<MqttTopicSubscription> requestSubscriptions = new ArrayList<>();
    List<TopicSubscription> grantedSubscriptions = new ArrayList<>();

    // HTTP interface require valid Client Id
    if (!this.validator.isClientIdValid(clientId)) {
        logger.debug("Protocol violation: Client id {} not valid based on configuration", clientId);
        throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
    }

    // Validate Topic Filter based on configuration
    for (Subscription subscription : subscriptions) {
        if (!this.validator.isTopicFilterValid(subscription.getTopic())) {
            logger.debug("Protocol violation: Client {} subscription {} is not valid based on configuration", clientId, subscription.getTopic());
            throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
        }
        MqttQoS requestQos;
        try {
            requestQos = MqttQoS.valueOf(subscription.getQos());
        } catch (IllegalArgumentException e) {
            logger.debug("Protocol violation: Client {} subscription qos {} is not valid", clientId, subscription.getQos());
            throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
        }
        requestSubscriptions.add(new MqttTopicSubscription(subscription.getTopic(), requestQos));
    }

    logger.debug("Message received: Received SUBSCRIBE message from client {} user {}", clientId, userName);

    // Authorize client subscribe using provided Authenticator
    List<MqttGrantedQoS> grantedQosLevels = this.authenticator.authSubscribe(clientId, userName, requestSubscriptions);
    logger.trace("Authorization granted: Subscribe to topic {} granted as {} for client {}", ArrayUtils.toString(requestSubscriptions), ArrayUtils.toString(grantedQosLevels), clientId);

    for (int i = 0; i < requestSubscriptions.size(); i++) {

        MqttGrantedQoS grantedQoS = grantedQosLevels.get(i);
        String topic = requestSubscriptions.get(i).topic();
        List<String> topicLevels = Topics.sanitize(topic);
        grantedSubscriptions.add(new TopicSubscription(topic, grantedQoS));

        // Granted only
        if (grantedQoS != MqttGrantedQoS.FAILURE) {

            // If a Server receives a SUBSCRIBE Packet containing a Topic Filter that is identical to an existing
            // Subscription’s Topic Filter then it MUST completely replace that existing Subscription with a new
            // Subscription. The Topic Filter in the new Subscription will be identical to that in the previous Subscription,
            // although its maximum QoS value could be different.
            logger.trace("Update subscription: Update client {} subscription with topic {} QoS {}", clientId, topic, grantedQoS);
            this.redis.updateSubscription(clientId, topicLevels, MqttQoS.valueOf(grantedQoS.value()));
        }
    }

    // Pass message to 3rd party application
    Subscribe s = new Subscribe(packetId, grantedSubscriptions);
    InternalMessage<Subscribe> m = new InternalMessage<>(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE, false, version, clientId, null, null, s);
    this.communicator.sendToApplication(m);

    return new ResultEntity<>(grantedQosLevels);
}
 
开发者ID:12315jack,项目名称:j1st-mqtt,代码行数:70,代码来源:MqttSubscribeResource.java

示例2: unsubscribe

import com.sun.security.auth.UserPrincipal; //导入方法依赖的package包/类
@PermitAll
@POST
/**
 * Handle MQTT Un-Subscribe Request in RESTful style
 */
public ResultEntity<Boolean> unsubscribe(@PathParam("clientId") String clientId, @Auth UserPrincipal user, @QueryParam("protocol") @DefaultValue("4") byte protocol,
                                         @QueryParam("packetId") @DefaultValue("0") int packetId,
                                         List<String> topics) {
    String userName = user.getName();
    MqttVersion version = MqttVersion.fromProtocolLevel(protocol);

    // HTTP interface require valid Client Id
    if (!this.validator.isClientIdValid(clientId)) {
        logger.debug("Protocol violation: Client id {} not valid based on configuration", clientId);
        throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
    }

    // Validate Topic Filter based on configuration
    for (String topic : topics) {
        if (!this.validator.isTopicFilterValid(topic)) {
            logger.debug("Protocol violation: Client {} un-subscription {} is not valid based on configuration", clientId, topic);
            throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
        }
    }

    logger.debug("Message received: Received UNSUBSCRIBE message from client {} user {} topics {}", clientId, userName, ArrayUtils.toString(topics));

    // The Topic Filters (whether they contain wildcards or not) supplied in an UNSUBSCRIBE packet MUST be
    // compared character-by-character with the current set of Topic Filters held by the Server for the Client. If
    // any filter matches exactly then its owning Subscription is deleted, otherwise no additional processing
    // occurs
    // If a Server deletes a Subscription:
    // It MUST stop adding any new messages for delivery to the Client.
    //1 It MUST complete the delivery of any QoS 1 or QoS 2 messages which it has started to send to
    // the Client.
    // It MAY continue to deliver any existing messages buffered for delivery to the Client.
    topics.forEach(topic -> {
        logger.trace("Remove subscription: Remove client {} subscription with topic {}", clientId, topic);
        this.redis.removeSubscription(clientId, Topics.sanitize(topic));
    });

    // Pass message to 3rd party application
    Unsubscribe us = new Unsubscribe(packetId, topics);
    InternalMessage<Unsubscribe> m = new InternalMessage<>(MqttMessageType.UNSUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE, false, version, clientId, null, null, us);
    this.communicator.sendToApplication(m);

    return new ResultEntity<>(true);
}
 
开发者ID:12315jack,项目名称:j1st-mqtt,代码行数:49,代码来源:MqttUnsubscribeResource.java

示例3: subscribe

import com.sun.security.auth.UserPrincipal; //导入方法依赖的package包/类
/**
 * Handle MQTT Subscribe Request in RESTful style
 * Granted QoS Levels will send back to client.
 * Retain Messages matched the subscriptions will NOT send back to client.
 */
@PermitAll
@POST
public ResultEntity<List<MqttGrantedQoS>> subscribe(@PathParam("clientId") String clientId, @Auth UserPrincipal user, @QueryParam("protocol") @DefaultValue("4") byte protocol,
                                                    @QueryParam("packetId") @DefaultValue("0") int packetId,
                                                    List<Subscription> subscriptions) {
    String userName = user.getName();
    MqttVersion version = MqttVersion.fromProtocolLevel(protocol);
    List<MqttTopicSubscription> requestSubscriptions = new ArrayList<>();
    List<MqttTopicSubscriptionGranted> grantedSubscriptions = new ArrayList<>();

    // HTTP interface require valid Client Id
    if (!this.validator.isClientIdValid(clientId)) {
        logger.debug("Protocol violation: Client id {} not valid based on configuration", clientId);
        throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
    }

    // Validate Topic Filter based on configuration
    for (Subscription subscription : subscriptions) {
        if (!this.validator.isTopicFilterValid(subscription.getTopic())) {
            logger.debug("Protocol violation: Client {} subscription {} is not valid based on configuration", clientId, subscription.getTopic());
            throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
        }
        MqttQoS requestQos;
        try {
            requestQos = MqttQoS.valueOf(subscription.getQos());
        } catch (IllegalArgumentException e) {
            logger.debug("Protocol violation: Client {} subscription qos {} is not valid", clientId, subscription.getQos());
            throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
        }
        requestSubscriptions.add(new MqttTopicSubscription(subscription.getTopic(), requestQos));
    }

    logger.debug("Message received: Received SUBSCRIBE message from client {} user {}", clientId, userName);

    // Authorize client subscribe using provided Authenticator
    List<MqttGrantedQoS> grantedQosLevels = this.authenticator.authSubscribe(clientId, userName, requestSubscriptions);
    if (subscriptions.size() != grantedQosLevels.size()) {
        logger.warn("Authorization error: SUBSCRIBE message's subscriptions count not equal to granted QoS count");
        throw new AuthorizeException(new ErrorEntity(ErrorCode.UNAUTHORIZED));
    }
    logger.trace("Authorization granted on topic {} as {} for client {}", ArrayUtils.toString(requestSubscriptions), ArrayUtils.toString(grantedQosLevels), clientId);

    for (int i = 0; i < requestSubscriptions.size(); i++) {

        MqttGrantedQoS grantedQoS = grantedQosLevels.get(i);
        String topic = requestSubscriptions.get(i).topic();
        List<String> topicLevels = Topics.sanitize(topic);
        grantedSubscriptions.add(new MqttTopicSubscriptionGranted(topic, grantedQoS));

        // Granted only
        if (grantedQoS != MqttGrantedQoS.NOT_GRANTED) {

            // If a Server receives a SUBSCRIBE Packet containing a Topic Filter that is identical to an existing
            // Subscription’s Topic Filter then it MUST completely replace that existing Subscription with a new
            // Subscription. The Topic Filter in the new Subscription will be identical to that in the previous Subscription,
            // although its maximum QoS value could be different.
            logger.trace("Update subscription: Update client {} subscription with topic {} QoS {}", clientId, topic, grantedQoS);
            this.storage.updateSubscription(clientId, topicLevels, MqttQoS.valueOf(grantedQoS.value()));
        }
    }

    // Pass message to 3rd party application
    Message<MqttPacketIdVariableHeader, MqttSubscribePayloadGranted> msg = new Message<>(
            new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE, false, 0),
            new MqttAdditionalHeader(version, clientId, userName, null),
            MqttPacketIdVariableHeader.from(packetId),
            new MqttSubscribePayloadGranted(grantedSubscriptions));
    this.cluster.sendToApplication(msg);

    return new ResultEntity<>(grantedQosLevels);
}
 
开发者ID:longkerdandy,项目名称:mithqtt,代码行数:77,代码来源:MqttSubscribeResource.java

示例4: unsubscribe

import com.sun.security.auth.UserPrincipal; //导入方法依赖的package包/类
/**
 * Handle MQTT Un-Subscribe Request in RESTful style
 */
@PermitAll
@POST
public ResultEntity<Boolean> unsubscribe(@PathParam("clientId") String clientId, @Auth UserPrincipal user, @QueryParam("protocol") @DefaultValue("4") byte protocol,
                                         @QueryParam("packetId") @DefaultValue("0") int packetId,
                                         List<String> topics) {
    String userName = user.getName();
    MqttVersion version = MqttVersion.fromProtocolLevel(protocol);

    // HTTP interface require valid Client Id
    if (!this.validator.isClientIdValid(clientId)) {
        logger.debug("Protocol violation: Client id {} not valid based on configuration", clientId);
        throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
    }

    // Validate Topic Filter based on configuration
    for (String topic : topics) {
        if (!this.validator.isTopicFilterValid(topic)) {
            logger.debug("Protocol violation: Client {} un-subscription {} is not valid based on configuration", clientId, topic);
            throw new ValidateException(new ErrorEntity(ErrorCode.INVALID));
        }
    }

    logger.debug("Message received: Received UNSUBSCRIBE message from client {} user {} topics {}", clientId, userName, ArrayUtils.toString(topics));

    // The Topic Filters (whether they contain wildcards or not) supplied in an UNSUBSCRIBE packet MUST be
    // compared character-by-character with the current set of Topic Filters held by the Server for the Client. If
    // any filter matches exactly then its owning Subscription is deleted, otherwise no additional processing
    // occurs
    // If a Server deletes a Subscription:
    // It MUST stop adding any new messages for delivery to the Client.
    //1 It MUST complete the delivery of any QoS 1 or QoS 2 messages which it has started to send to
    // the Client.
    // It MAY continue to deliver any existing messages buffered for delivery to the Client.
    topics.forEach(topic -> {
        logger.trace("Remove subscription: Remove client {} subscription with topic {}", clientId, topic);
        this.storage.removeSubscription(clientId, Topics.sanitize(topic));
    });

    // Pass message to 3rd party application
    Message<MqttPacketIdVariableHeader, MqttUnsubscribePayload> msg = new Message<>(
            new MqttFixedHeader(MqttMessageType.UNSUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE, false, 0),
            new MqttAdditionalHeader(version, clientId, userName, null),
            MqttPacketIdVariableHeader.from(packetId),
            new MqttUnsubscribePayload(topics));
    this.cluster.sendToApplication(msg);

    return new ResultEntity<>(true);
}
 
开发者ID:longkerdandy,项目名称:mithqtt,代码行数:52,代码来源:MqttUnsubscribeResource.java


注:本文中的com.sun.security.auth.UserPrincipal.getName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。