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

com.hn.im.easemob.comm.body.MessageBody Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
package com.hn.im.easemob.comm.body;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.hn.im.easemob.comm.wrapper.BodyWrapper;
import cn.hutool.core.util.StrUtil;
import java.util.Iterator;
import java.util.Map;

public abstract class MessageBody implements BodyWrapper{
	private ObjectNode msgBody;

    private String targetType;

    private String[] targets;

    private String from;

    private Map ext;

    private boolean init = false;

    public MessageBody(String targetType, String[] targets, String from, Map ext) {
        this.targetType = targetType;
        this.targets = targets;
        this.from = from;
        this.ext = ext;
    }

    public String getTargetType() {
        return targetType;
    }


    public String[] getTargets() {
        return targets;
    }

    public String getFrom() {
        return from;
    }

    public Map getExt() {
        return ext;
    }

    public boolean isInit() {
        return init;
    }

    public void setInit(boolean init) {
        this.init = init;
    }

    protected ObjectNode getMsgBody() {
        if(null == this.msgBody) {
            this.msgBody = JsonNodeFactory.instance.objectNode();
            msgBody.put("target_type", targetType);
            ArrayNode targetsNode = msgBody.putArray("target");
            for (String target: targets ) {
                targetsNode.add(target);
            }
            msgBody.put("from", from);

            if(null != ext) {
                ObjectNode extNode = msgBody.putObject("ext");
                Iterator iter = ext.keySet().iterator();
                while(iter.hasNext()){
                    String key = iter.next();
                    extNode.put(key, ext.get(key));
                }
            }
        }
		return msgBody;
	}

    public Boolean validate() {
        return StrUtil.isNotBlank(targetType) && isValidTargetType() && (targets!=null && targets.length>0) && StrUtil.isNotBlank(from);
    }

    private boolean isValidTargetType() {
        return "users".equals(targetType) || "chatgroups".equals(targetType) || "chatrooms".equals(targetType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy