com.conwet.silbops.api.impl.XJSPPubEndpoint.java Source code

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.api.impl.XJSPPubEndpoint.java

Source

package com.conwet.silbops.api.impl;

/*
 * #%L
 * SilboPS API - XJSP binding
 * %%
 * 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 java.io.IOException;
import java.util.Objects;

import org.json.simple.JSONObject;

import com.conwet.silbops.api.EndPointException;
import com.conwet.silbops.api.PubEndpoint;
import com.conwet.silbops.model.Advertise;
import com.conwet.silbops.model.Context;
import com.conwet.silbops.model.MessageType;
import com.conwet.silbops.model.Notification;
import com.conwet.xjsp.features.Message;
import com.conwet.xjsp.features.Session;

/**
 *
 * @author sortega
 */
class XJSPPubEndpoint extends XJSPEndpoint implements PubEndpoint {

    private static String FEATURE_NAME = "silbops-publisher";

    private Context context;

    XJSPPubEndpoint() {

        context = new Context();
    }

    @Override
    public String getFeatureName() {

        return FEATURE_NAME;
    }

    @Override
    public void handleMessage(Message message, Session session) throws Exception {

        throw new UnsupportedOperationException("Publisher can't receive messages.");
    }

    @Override
    public void advertise(Advertise subscription) throws EndPointException {

        sendAdvertise(subscription, MessageType.ADVERTISE);
    }

    @Override
    public void unadvertise(Advertise subscription) throws EndPointException {

        sendAdvertise(subscription, MessageType.UNADVERTISE);
    }

    @SuppressWarnings("unchecked")
    private void sendAdvertise(Advertise advertise, MessageType type) throws EndPointException {

        JSONObject payload = new JSONObject();
        payload.put("advertise", advertise.toJSON());
        payload.put("context", Advertise.asAdvertise(context).toJSON());

        writeJSON(type, payload);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void publish(Notification not) throws EndPointException {

        JSONObject payload = new JSONObject();
        payload.put("notification", not.toJSON());
        payload.put("context", context.toJSON());

        writeJSON(MessageType.PUBLISH, payload);
    }

    private void writeJSON(MessageType type, JSONObject payload) throws EndPointException {

        try {
            session.sendMessage(getFeatureName(), type.name(), payload);
        } catch (IOException ex) {
            throw new EndPointException(type + " failed", ex);
        }
    }

    @Override
    public void setContext(Context context) {

        this.context = Objects.requireNonNull(context, "Context is null");
    }
}