com.conwet.silbops.connectors.comet.JSONUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.connectors.comet.JSONUtils.java

Source

package com.conwet.silbops.connectors.comet;

/*
 * #%L
 * SilboPS Service
 * %%
 * 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 org.json.simple.JSONObject;

/**
 *
 * @author sergio
 */
public class JSONUtils {

    public static String sendData(String command, String endpoint, String message) {

        String payload = toJSONReply(command, buildResponseMessage(endpoint, message));
        payload = payload.replaceAll("\n", "\ndata:");
        return "\n\ndata:" + payload + "\n";
    }

    @SuppressWarnings("unchecked")
    public static String toJSONReply(String command, JSONObject parameters) {

        JSONObject json = new JSONObject();
        json.put("command", command);
        json.put("parameters", parameters);

        return json.toJSONString();
    }

    @SuppressWarnings("unchecked")
    public static JSONObject buildResponseMessage(String endpoinID, String string) {

        JSONObject json = new JSONObject();
        json.put("endpointID", endpoinID);
        json.put("message", string);

        return json;
    }
}