Example usage for com.google.gson JsonObject getAsJsonPrimitive

List of usage examples for com.google.gson JsonObject getAsJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonObject getAsJsonPrimitive.

Prototype

public JsonPrimitive getAsJsonPrimitive(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonPrimitive element.

Usage

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private void handleResponse(JsonObject object) {
    JsonPrimitive token = object.getAsJsonPrimitive("id");
    int id = token.getAsInt();
    synchronized (openRequests) {
        JetMethod method = openRequests.get(id);
        if (method == null) {
            return;
        }/*from  w w w . ja v  a2s.c  o m*/
        openRequests.remove(id);
        method.getFuture().cancel(true);
        method.callResponseCallback(true, object);
    }
}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private JsonPrimitive getFetchId(JsonObject object) {
    JsonPrimitive method = object.getAsJsonPrimitive("method");
    if ((method != null) && (method.isNumber())) {
        return method;
    }//from   ww  w .j av a2  s. c o  m

    return null;
}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private boolean isResponse(JsonObject object) {
    JsonPrimitive id = object.getAsJsonPrimitive("id");
    return (id != null) && (id.isNumber());
}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private void handleStateOrMethodCallbacks(JsonObject object) {
    try {/*from   w  ww .j av  a  2s.c  o m*/
        JsonPrimitive method = object.getAsJsonPrimitive("method");
        if (method == null) {
            throw new JsonRpcException(JsonRpcException.METHOD_NOT_FOUND, "no method given");
        }

        String path = method.getAsString();
        if ((path == null) || (path.length() == 0)) {
            throw new JsonRpcException(JsonRpcException.METHOD_NOT_FOUND, "method is not a string or integer");
        }

        boolean stateHandled = handleStateCallback(object, path);
        if (!stateHandled) {
            handleMethod(object, path);
        }
    } catch (JsonRpcException e) {
        sendResponse(object, e.getJson());
    }
}

From source file:com.hbm.devices.jet.JetPeer.java

License:Open Source License

private void sendResponse(JsonObject request, JsonObject responseObject) {
    JsonPrimitive id = request.getAsJsonPrimitive("id");
    if ((id != null) && ((id.isString()) || (id.isNumber()))) {
        responseObject.add("id", id);
        this.connection.sendMessage(gson.toJson(responseObject));
    }/*  w  ww.  java  2  s .  com*/
}

From source file:com.health.smart.ejb.GCMBean.java

private int sendGCM(String gcmData) throws Exception {
    byte[] postData = gcmData.getBytes(Charset.forName("UTF-8"));
    int postDataLength = postData.length;
    URL url = new URL("http://android.googleapis.com/gcm/send");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setInstanceFollowRedirects(false);
    conn.setDoInput(true);//  ww w .  ja  v a 2s. c o  m
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "key=AIzaSyDWPrVhJqbSf9tkkYw2scP7-qjQcuQxIk8");
    conn.setRequestProperty("content-Type", "application/json; charset=UTF-8");
    conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
    conn.setUseCaches(false);
    try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
        wr.write(postData);
    }
    int responseCode = conn.getResponseCode();
    if (responseCode == 200) {
        String result = "";
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String output;
        while ((output = br.readLine()) != null) {
            result += output;
        }
        System.out.println("Response message: " + result);
        JsonObject obj = GsonHelper.fromString(result);
        return obj.getAsJsonPrimitive("success").getAsInt();
    } else {
        throw new Exception("HTTP Request Failure : " + responseCode);
    }
}

From source file:com.health.smart.ws.MobileAPI.java

@POST
@Path("/getToken")
public Response getToken(String payload) {
    try {//from ww w.  ja  va2s.  c  om
        JsonObject obj = GsonHelper.fromString(payload);
        String userId = obj.getAsJsonPrimitive("userId").getAsString();
        String password = obj.getAsJsonPrimitive("password").getAsString();
        String token = sysEjb.getToken(userId, password);
        JsonObject jObject = new JsonObject();
        jObject.addProperty("token", token);
        return Response.status(200).entity(jObject.toString()).build();
    } catch (Exception e) {
        return Response.status(500).entity(e.getMessage()).build();
    }
}

From source file:com.health.smart.ws.MobileAPI.java

@POST
@Path("/savemeasurement")
public Response saveMeasurement(String payload) {
    try {/*from  w w w  . j a va  2  s  . c o m*/
        JsonObject request = GsonHelper.fromString(payload);
        String token = request.getAsJsonPrimitive("token").getAsString();
        JsonArray mess = request.getAsJsonArray("measurements");
        int pid = sysEjb.validateToken(token);
        List<DayMeasurement> measurements = new ArrayList<>();
        for (int i = 0; i < mess.size(); i++) {
            DayMeasurement m = GsonHelper.fromJsonElement(mess.get(i), DayMeasurement.class);
            m.setPatientId(pid);
            measurements.add(m);
        }
        mongoEjb.saveDayMeasurement(measurements);
        JsonObject jObject = new JsonObject();
        jObject.addProperty("result", "success");
        return Response.status(200).entity(jObject.toString()).build();
    } catch (Exception e) {
        e.printStackTrace();
        return Response.status(500).entity(e.getMessage()).build();
    }
}

From source file:com.health.smart.ws.MobileAPI.java

@POST
@Path("/getmeasurement")
public Response getMeasurement(String payload) {
    try {/*from ww  w . ja  v a 2  s .  com*/
        JsonObject request = GsonHelper.fromString(payload);
        String token = request.getAsJsonPrimitive("token").getAsString();
        int pid = sysEjb.validateToken(token);

        String from = request.getAsJsonPrimitive("from").getAsString();
        String to = request.getAsJsonPrimitive("to").getAsString();

        Date queryFrom = df.parse(from);
        Date queryTo = df.parse(to);

        System.out.println(pid);
        System.out.println(queryFrom);
        System.out.println(queryTo);

        String result = mongoEjb.getDayMeasurement(pid, queryFrom, queryTo);

        return Response.status(200).entity(result).build();
    } catch (Exception e) {
        e.printStackTrace();
        return Response.status(500).entity(e.getMessage()).build();
    }
}

From source file:com.health.smart.ws.MobileAPI.java

@POST
@Path("/registerGCM")
public Response registerGCMDeviceId(String payload) {
    try {//from ww w .  j  av a 2s  .c o m
        JsonObject request = GsonHelper.fromString(payload);
        String token = request.getAsJsonPrimitive("token").getAsString();
        int pid = sysEjb.validateToken(token);

        String deviceId = GsonHelper.fromString(payload).getAsJsonPrimitive("deviceId").getAsString();
        sysEjb.registerDeviceId(pid, deviceId);
        JsonObject jObject = new JsonObject();
        jObject.addProperty("result", "success");

        return Response.status(200).entity(jObject.toString()).build();

    } catch (Exception e) {
        e.printStackTrace();
        return Response.status(500).entity(e.getMessage()).build();
    }
}