com.conwet.silbops.msg.Message.java Source code

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.msg.Message.java

Source

package com.conwet.silbops.msg;

/*
 * #%L
 * SilboPS API Broker
 * %%
 * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import com.conwet.silbops.model.MessageType;
import com.conwet.silbops.util.JSONizable;
import java.lang.reflect.Constructor;
import java.util.EnumMap;
import java.util.Map;
import org.json.simple.JSONObject;

/**
 * 
 * @author sortega
 */
public abstract class Message implements JSONizable {

    private static final Map<MessageType, Class<? extends Message>> typeMap;

    static {
        typeMap = new EnumMap<>(MessageType.class);
        typeMap.put(MessageType.ADVERTISE, AdvertiseMsg.class);
        typeMap.put(MessageType.UNADVERTISE, UnadvertiseMsg.class);
        typeMap.put(MessageType.PUBLISH, PublishMsg.class);
        typeMap.put(MessageType.SUBSCRIBE, SubscribeMsg.class);
        typeMap.put(MessageType.UNSUBSCRIBE, UnsubscribeMsg.class);
        typeMap.put(MessageType.NOTIFY, NotifyMsg.class);
        typeMap.put(MessageType.CONTEXT, ContextMsg.class);
    }

    private MessageType type;

    public Message() {
        this.type = MessageType.NONE;
    }

    public Message(MessageType type) {
        this.type = type;
    }

    public MessageType getType() {
        return type;
    }

    @Override
    @SuppressWarnings("unchecked")
    public final JSONObject toJSON() {

        JSONObject json = new JSONObject();
        json.put("type", type.toJSON());
        json.put("payload", getPayloadAsJSON());

        return json;
    }

    @Override
    public final String toJSONString() {

        return toJSON().toJSONString();
    }

    @Override
    public final String toString() {

        return "Message<" + toJSONString() + ">";
    }

    /**
     * Serialize to JSON the message payload.
     * 
     * @return the JSONObject representing the message.
     */
    public abstract JSONObject getPayloadAsJSON();

    public static Message fromJSON(JSONObject json) {

        try {
            MessageType type = MessageType.fromJSON((String) json.get("type"));
            Constructor<? extends Message> constructor = typeMap.get(type).getConstructor(JSONObject.class);
            return constructor.newInstance(json.get("payload"));
        } catch (Exception ex) {
            throw new IllegalArgumentException("Malformed object[" + json.toJSONString() + "]", ex);
        }
    }
}