Package

Source Code of RestletSignal

import java.util.Map;

import org.restlet.data.*;
import org.restlet.Client;
import org.restlet.resource.Representation;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.util.*;
import org.json.*;

/* An example ReST client using Restlet 1.1
* http://www.restlet.org/
*
*/

public class RestletSignal {
    // need to supply all of these
    static String server = "http://yourappliance.socialtext.net";
    static String username = "devnull1@socialtext.com";
    static String password = "d3vnu11l";
    // target for examplePostSignal()
    static int group_id = 0;
    static int account_id = 4;

    static Client client;

    public static void main(String[] args) throws Throwable {
        switch(args.length) {
            case 3: password = args[2]; // fallthrough
            case 2: username = args[1]; // fallthrough
            case 1: server   = args[0]; // fallthrough
            default: break;
        }

        client = new Client(server.startsWith("https") ? Protocol.HTTPS : Protocol.HTTP);
        exampleGetConfig();
        examplePostSignal();
    }

    /* Does a GET of /data/config, decoding it as JSON */
    public static void exampleGetConfig() throws Throwable {
        Request r = newJsonRequest(Method.GET, "/data/config");
        Response resp = client.handle(r);
        if (resp.getStatus().isError()) {
            throw resp.getStatus().getThrowable();
        }

        try {
            JsonRepresentation repr = new JsonRepresentation(resp.getEntity());
            JSONObject o = repr.toJsonObject();
            System.out.println("Server version:   " + o.optString("server_version"));
            System.out.println("ReST API version: " + o.optInt("api_version"));
        }
        catch (JSONException err) {
            throw new Exception ("Cannot Decode JSON", err);
        }
    }

    /* Generates a JSON signal and attempts to post it.
     *
     * The signal JSON looks like this:
     *
     * {
     *  "signal":"a signal from restlet-signal!",
     *  "group_ids":[...],
     *  "account_ids":[...],
     *  "annotations":[
     *      {"ua":{"name":"ExampleRestlet","java_version":"..."}}
     *  ]
     * }
     *
     * The group and account ids are controled by the statics at the top of
     * this class.
     */
    public static void examplePostSignal () throws Throwable {
        try {
            JSONArray groups = new JSONArray();
            if (group_id > 0) groups.put(group_id);
            JSONArray accounts = new JSONArray();
            if (account_id > 0) accounts.put(account_id);

            JSONObject ua_anno_props = new JSONObject();
            ua_anno_props
                .put("name","ExampleRestlet")
                .put("java_version",System.getProperty("java.version"))
            ;

            JSONObject ua_anno = new JSONObject();
            ua_anno.put("ua",ua_anno_props);
            JSONArray annos = new JSONArray();
            annos.put(ua_anno);

            JSONObject signal = new JSONObject();
            signal
                .put("signal", "a signal from restlet-signal!")
                .put("group_ids", groups)
                .put("account_ids", accounts)
                .put("annotations", annos)
            ;
               
            JsonRepresentation signal_repr = new JsonRepresentation(signal);
            Request r = newJsonRequest(Method.POST, "/data/signals", signal_repr);
            Response resp = client.handle(r);

            if (resp.getStatus().isError()) {
                throw resp.getStatus().getThrowable();
            }
            else {
                Map<String,Object> attrs = resp.getAttributes();
                Form headers = (Form) attrs.get("org.restlet.http.headers");
                System.out.println("Posted signal ID   " + headers.getFirstValue("X-Signal-ID",true,"0"));
                System.out.println("Posted signal Hash " + headers.getFirstValue("X-Signal-Hash",true,"0"));
            }
        }
        catch (Throwable err) {
            throw new Exception("Couln't post signal",err);
        }
    }

    public static Request newJsonRequest (Method method, String uri) {
        return newJsonRequest(method,uri,null);
    }

    public static Request newJsonRequest (Method method, String uri, Representation entity) {
        ChallengeResponse creds = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, username, password);
        String resource = server + uri;

        Request r;
        if (entity != null)
            r = new Request(method,resource,entity);
        else
            r = new Request(method,resource);

        r.setChallengeResponse(creds);
        r.getClientInfo().getAcceptedMediaTypes().add(new Preference<MediaType>(MediaType.APPLICATION_JSON));
        return r;
    }
}
TOP

Related Classes of RestletSignal

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.