All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.crossbar.autobahn.wamp.messages.Hello Maven / Gradle / Ivy

There is a newer version: 21.7.1
Show newest version
///////////////////////////////////////////////////////////////////////////////
//
//   AutobahnJava - http://crossbar.io/autobahn
//
//   Copyright (c) Crossbar.io Technologies GmbH and contributors
//
//   Licensed under the MIT License.
//   http://www.opensource.org/licenses/mit-license.php
//
///////////////////////////////////////////////////////////////////////////////

package io.crossbar.autobahn.wamp.messages;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.crossbar.autobahn.wamp.interfaces.IMessage;
import io.crossbar.autobahn.wamp.utils.MessageUtil;

import static io.crossbar.autobahn.wamp.utils.Shortcuts.getOrDefault;

public class Hello implements IMessage {

    public static final int MESSAGE_TYPE = 1;

    public final String realm;
    public final Map roles;
    public final List authMethods;
    public final String authID;
    public final Map authextra;

    @Override
    public List marshal() {
        List marshaled = new ArrayList<>();
        marshaled.add(MESSAGE_TYPE);
        marshaled.add(realm);
        Map details = new HashMap<>();
        details.put("roles", roles);
        if (authMethods != null) {
            details.put("authmethods", authMethods);
        }
        if (authID != null) {
            details.put("authid", authID);
        }
        if (authextra != null) {
            details.put("authextra", authextra);
        }
        marshaled.add(details);
        return marshaled;
    }

    public static Hello parse(List wmsg) {
        MessageUtil.validateMessage(wmsg, MESSAGE_TYPE, "HELLO", 3);

        String realm = (String) wmsg.get(1);

        Map details = (Map) wmsg.get(2);
        Map roles = (Map) details.get("roles");
        List authMethods = getOrDefault(details, "authmethods", null);
        String authID = getOrDefault(details, "authid", null);
        Map authextra = getOrDefault(details, "authextra", null);

        return new Hello(realm, roles, authMethods, authID, authextra);
    }

    public Hello(String realm, Map roles) {
        this(realm, roles, null, null, null);
    }

    public Hello(String realm, Map roles, List authMethods, String authID,
                 Map authextra) {
        this.realm = realm;
        this.roles = roles;
        this.authMethods = authMethods;
        this.authID = authID;
        this.authextra = authextra;
    }
}