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.nttdata.emea.nuoffice.CallHandler.java

License:Open Source License

private void register(WebSocketSession session, JsonObject jsonMessage) throws IOException {
    String name = jsonMessage.getAsJsonPrimitive("name").getAsString();
    if (name.equals("Agent")) {
        chat = new StringBuffer("Welcome to NTT DATA Textchat!");
    }//  ww w  .  j  a  v a  2s. co m
    boolean cobrowsing = jsonMessage.getAsJsonPrimitive("cobrowsing").getAsBoolean();
    UserSession caller = new UserSession(session, name, cobrowsing);
    String responseMsg = "accepted";
    if (name.isEmpty()) {
        responseMsg = "rejected: empty user name";
    } else if (registry.exists(name)) {
        responseMsg = "rejected: user '" + name + "' already registered";
    } else {
        registry.register(caller);
    }

    JsonObject response = new JsonObject();
    response.addProperty("id", "registerResponse");
    response.addProperty("response", responseMsg);
    caller.sendMessage(response);

    response = new JsonObject();
    response.addProperty("id", "textChat");
    response.addProperty("response", chat.toString());
    TextMessage tm = new TextMessage(response.toString());
    session.sendMessage(tm);

}

From source file:com.nttdata.emea.nuoffice.CallHandler.java

License:Open Source License

private void call(UserSession caller, JsonObject jsonMessage) throws IOException {
    String to = jsonMessage.get("to").getAsString();
    String from = jsonMessage.get("from").getAsString();
    JsonObject response = new JsonObject();

    if (registry.exists(to)) {
        UserSession callee = registry.getByName(to);
        caller.setSdpOffer(jsonMessage.getAsJsonPrimitive("sdpOffer").getAsString());
        caller.setCallingTo(to);//from  w w  w . j a va  2s.  co  m

        response.addProperty("id", "incomingCall");
        response.addProperty("from", from);

        callee.sendMessage(response);
        callee.setCallingFrom(from);
    } else {
        response.addProperty("id", "callResponse");
        response.addProperty("response", "rejected: user '" + to + "' is not registered");

        caller.sendMessage(response);
    }
}

From source file:com.nttdata.emea.nuoffice.CallHandler.java

License:Open Source License

private void play(WebSocketSession session, JsonObject jsonMessage) throws IOException {
    String id = "LAST_SESSION";
    String recordId = (jsonMessage.get("recordId") != null
            && !jsonMessage.getAsJsonPrimitive("recordId").getAsString().isEmpty())
                    ? jsonMessage.get("recordId").getAsString()
                    : id;//from  w w w.  jav  a2s. c  om
    log.debug("Playing recorded call with id '{}'", recordId);
    PlayMediaPipeline pipeline = new PlayMediaPipeline(kurento, recordId, session);
    String sdpOffer = jsonMessage.get("sdpOffer").getAsString();
    String sdpAnswer = pipeline.generateSdpAnswer(sdpOffer);

    JsonObject response = new JsonObject();
    response.addProperty("id", "playResponse");
    response.addProperty("response", "accepted");
    response.addProperty("sdpAnswer", sdpAnswer);
    session.sendMessage(new TextMessage(response.toString()));

    pipeline.play();

}

From source file:com.nttdata.emea.nuoffice.CallHandler.java

License:Open Source License

private void textChat(WebSocketSession session, JsonObject jsonMessage) throws IOException {
    String user = jsonMessage.getAsJsonPrimitive("user").getAsString();
    JsonObject response = new JsonObject();
    response.addProperty("id", "textChat");
    String text = jsonMessage.getAsJsonPrimitive("input").getAsString();
    response.addProperty("response", addToChat(user, text));
    TextMessage tm = new TextMessage(response.toString());
    for (UserSession s : registry.getSessions()) {
        s.getSession().sendMessage(tm);//from   w w w.  j  a v  a  2s  . c  o m
    }
}

From source file:com.objy.se.IngestMapper.java

private void processRelationships(JsonArray jsonArray) {

    for (JsonElement element : jsonArray) {
        JsonObject obj = (JsonObject) element;
        String relationshipName = obj.get(RelationshipNameJSON).getAsString();
        //String toClass = obj.get(ToClassJSON).getAsString();
        JsonPrimitive prim = obj.getAsJsonPrimitive(ToClassJSON);
        String toClass = prim.getAsString();
        String toClassRelationshipName = null;
        if (obj.has(ToClassRelationshipNameJSON)) {
            toClassRelationshipName = obj.get(ToClassRelationshipNameJSON).getAsString();
        }//from  www.  ja v  a  2 s  .  com

        // configure relationship.
        Relationship rel = new Relationship(toClass);

        ClassAccessor toClassAccessor = SchemaManager.getInstance().getClassProxy(toClass);

        JsonArray keyArray = obj.get(KeyJSON).getAsJsonArray();
        ArrayList<JsonObject> keys = new ArrayList<>();

        for (JsonElement keyElement : keyArray) {
            JsonObject keyObj = (JsonObject) keyElement;
            keys.add(keyObj);
        }

        if (keys.size() > 1) // composite key
        {
            ArrayList<SingleKey> singleKeys = new ArrayList<>();
            for (JsonObject keyObj : keys) {
                String keySchemaName = keyObj.get(SchemaNameJSON).getAsString();
                String keyRawName = keyObj.get(RawNameJSON).getAsString();
                // get the type of the keySchemaName 
                ClassAccessor.AttributeInfo attrInfo = toClassAccessor.getAttribute(keySchemaName);
                SingleKey key = new SingleKey(keySchemaName, keyRawName, attrInfo.logicalType());
                singleKeys.add(key);
            }
            CompositeKey compositeKey = new CompositeKey(singleKeys.toArray(new SingleKey[0]));
            rel.add(compositeKey, relationshipName, toClassRelationshipName);
        } else {
            JsonObject keyObj = keys.get(0);
            String keySchemaName = keyObj.get(SchemaNameJSON).getAsString();
            String keyRawName = keyObj.get(RawNameJSON).getAsString();
            // get the type of the keySchemaName 
            ClassAccessor.AttributeInfo attrInfo = toClassAccessor.getAttribute(keySchemaName);
            SingleKey key = new SingleKey(keySchemaName, keyRawName, attrInfo.logicalType());
            rel.add(key, relationshipName, toClassRelationshipName);
        }
        relationshipList.add(rel);
    }

}

From source file:com.ontotext.s4.SBTDemo.parse.JsonToRDF.java

License:Apache License

/**
 * This method should create//from  ww w . j  a  v a  2 s .  c o  m
 * 
 * @param jsonLine
 * @param fileSubject
 * @return
 */
public List<String[]> parse(String jsonLine, String fileSubject) throws Exception {
    List<String[]> hashList = new LinkedList<String[]>();

    JsonElement jelement = new JsonParser().parse(jsonLine);
    JsonObject jobject = jelement.getAsJsonObject();
    String text = null;
    if (jobject.has(textJson)) {
        text = jobject.getAsJsonPrimitive(textJson).getAsString();
    }
    if (jobject.has(entity)) {

        JsonObject entities = jobject.getAsJsonObject(entity);
        for (Iterator<Entry<String, JsonElement>> iterator = entities.entrySet().iterator(); iterator
                .hasNext();) {
            Entry<String, JsonElement> element = iterator.next();
            JsonArray jsonArrayOfEntities = element.getValue().getAsJsonArray();
            for (JsonElement jsonElement : jsonArrayOfEntities) {
                JsonObject annotation = jsonElement.getAsJsonObject();
                try {
                    String[] fileMentionsArray = new String[] { fileSubject, MENTIONS_URI,
                            annotation.getAsJsonPrimitive(inst).getAsString() };
                    String[] instanceTypeArray = new String[] {
                            annotation.getAsJsonPrimitive(inst).getAsString(), RDF_TYPE_URI,
                            annotation.getAsJsonPrimitive(classProperty).getAsString() };
                    String[] instanceLabelArray = new String[] {
                            annotation.getAsJsonPrimitive(inst).getAsString(), LABEL_URI,
                            annotation.getAsJsonPrimitive(stringProperty).getAsString() };
                    String[] classLableArray = new String[] {
                            annotation.getAsJsonPrimitive(classProperty).getAsString(), LABEL_URI,
                            annotation.getAsJsonPrimitive(type).getAsString().replaceAll("_", " ") };
                    String[] stringAbstractText = new String[] { fileSubject, ABSTRACT_TEXT_URI, text };

                    hashList.add(fileMentionsArray);
                    hashList.add(instanceTypeArray);
                    hashList.add(classLableArray);
                    hashList.add(instanceLabelArray);
                    hashList.add(stringAbstractText);

                } catch (Exception e) {
                    logger.error(e);
                }

            }

        }

    }
    return hashList;
}

From source file:com.openyelp.server.JsonCacheRpcExecutor.java

License:Apache License

public void execute(JsonRpcServerTransport transport) {
    if (!locked) {
        synchronized (handlers) {
            locked = true;/*from  w  w  w. ja  va  2 s  .c  om*/
        }
    }
    String methodName = null;
    JsonArray params = null;
    JsonObject resp = new JsonObject();
    resp.addProperty("jsonrpc", "2.0");
    String errorMessage = null;
    Integer errorCode = null;
    String errorData = null;
    JsonObject req = null;
    String requestData;
    String keys = null;
    try {
        requestData = transport.readRequest();
        if (requestData != null) {
            keys = Utils.getMD5Str(requestData);
        }
        JsonParser parser = new JsonParser();
        req = (JsonObject) parser.parse(new StringReader(requestData));
    } catch (Throwable t) {
        errorCode = JsonRpcErrorCodes.PARSE_ERROR_CODE;
        errorMessage = "unable to parse json-rpc request";
        errorData = getStackTrace(t);

        sendError(transport, resp, errorCode, errorMessage, errorData);
        return;
    }
    try {
        assert req != null;
        resp.add("id", req.get("id"));
        methodName = req.getAsJsonPrimitive("method").getAsString();
        params = (JsonArray) req.get("params");
        if (params == null) {
            params = new JsonArray();
        }
    } catch (Throwable t) {
        errorCode = JsonRpcErrorCodes.INVALID_REQUEST_ERROR_CODE;
        errorMessage = "unable to read request";
        errorData = getStackTrace(t);

        sendError(transport, resp, errorCode, errorMessage, errorData);
        return;
    }

    try {
        if (keys != null && keys.length() > 30) {
            if (rpcCache != null) {
                JsonElement result = rpcCache.get(keys);
                if (result == null) {
                    result = executeMethod(methodName, params);
                    rpcCache.put(keys, result);
                } else {
                    if (showlog) {
                        System.out.println("?,key:" + keys);
                    }
                    cachsize++;
                }
                resp.add("result", result);
            } else {
                JsonElement result = executeMethod(methodName, params);
                resp.add("result", result);
            }
        } else {
            JsonElement result = executeMethod(methodName, params);
            resp.add("result", result);
        }
    } catch (Throwable t) {
        if (t instanceof JsonRpcRemoteException) {
            sendError(transport, resp, (JsonRpcRemoteException) t);
            return;
        }
        errorCode = JsonRpcErrorCodes.getServerError(1);
        errorMessage = t.getMessage();
        errorData = getStackTrace(t);
        sendError(transport, resp, errorCode, errorMessage, errorData);
        return;
    }
    try {
        String responseData = resp.toString();
        transport.writeResponse(responseData);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.openyelp.server.JsonRpcExecutor.java

License:Apache License

public void execute(JsonRpcServerTransport transport) {
    if (!locked) {
        synchronized (handlers) {
            locked = true;/* w  ww  .j  a  va 2s . c o  m*/
        }
    }
    String methodName = null;
    String key = null;
    JsonArray params = null;
    JsonObject resp = new JsonObject();
    resp.addProperty("jsonrpc", "2.0");
    String errorMessage = null;
    Integer errorCode = null;
    String errorData = null;
    JsonObject req = null;
    try {
        String requestData = transport.readRequest();
        JsonParser parser = new JsonParser();
        req = (JsonObject) parser.parse(new StringReader(requestData));
    } catch (Throwable t) {
        errorCode = JsonRpcErrorCodes.PARSE_ERROR_CODE;
        errorMessage = "unable to parse json-rpc request";
        errorData = getStackTrace(t);

        sendError(transport, resp, errorCode, errorMessage, errorData);
        return;
    }
    try {
        assert req != null;
        resp.add("id", req.get("id"));
        try {
            key = req.get("key").getAsString();
        } catch (Exception e) {
        }
        methodName = req.getAsJsonPrimitive("method").getAsString();
        params = (JsonArray) req.get("params");
        if (params == null) {
            params = new JsonArray();
        }
    } catch (Throwable t) {
        errorCode = JsonRpcErrorCodes.INVALID_REQUEST_ERROR_CODE;
        errorMessage = "unable to read request";
        errorData = getStackTrace(t);

        sendError(transport, resp, errorCode, errorMessage, errorData);
        return;
    }

    try {
        if (key != null && key.length() > 30) {
            if (rpcCache != null) {
                JsonElement result = rpcCache.get(key);
                if (result == null) {
                    result = executeMethod(methodName, params);
                    rpcCache.put(key, result);
                } else {
                    if (showlog) {
                        System.out.println("???,key:" + key);
                    }
                    cachsize++;
                }
                resp.add("result", result);
            } else {
                JsonElement result = executeMethod(methodName, params);
                resp.add("result", result);
            }
        } else {
            JsonElement result = executeMethod(methodName, params);
            resp.add("result", result);
        }

    } catch (Throwable t) {
        if (t instanceof JsonRpcRemoteException) {
            sendError(transport, resp, (JsonRpcRemoteException) t);
            return;
        }
        errorCode = JsonRpcErrorCodes.getServerError(1);
        errorMessage = t.getMessage();
        errorData = getStackTrace(t);
        sendError(transport, resp, errorCode, errorMessage, errorData);
        return;
    }

    try {
        String responseData = resp.toString();
        transport.writeResponse(responseData);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.perl5.lang.perl.idea.run.debugger.PerlDebugThread.java

License:Apache License

@Nullable
public VirtualFile loadRemoteSource(String filePath) {
    if (DEV_MODE) {
        System.err.println("Loading file " + filePath);
    }//from  w  w  w. jav  a  2  s  . co m
    final Semaphore responseSemaphore = new Semaphore();
    responseSemaphore.down();

    final String[] response = new String[] { "# Source could not be loaded..." };

    PerlDebuggingTransactionHandler perlDebuggingTransactionHandler = new PerlDebuggingTransactionHandler() {

        @Override
        public void run(JsonObject eventObject, JsonDeserializationContext jsonDeserializationContext) {
            response[0] = eventObject.getAsJsonPrimitive("data").getAsString();
            responseSemaphore.up();

        }
    };

    if (mySocket != null) {
        sendCommandAndGetResponse("get_source", new PerlSourceRequestDescriptor(filePath),
                perlDebuggingTransactionHandler);
        responseSemaphore.waitFor(2000);
    }

    return myPerlRemoteFileSystem.registerRemoteFile(filePath, response[0]);
}

From source file:com.pinterest.arcee.lease.QuboleLeaseManager.java

License:Apache License

public QuboleClusterBean fromJson(String clusterId, String configuration, String clusterState) {
    if (StringUtils.isEmpty(configuration) || StringUtils.isEmpty(clusterState)) {
        return null;
    }/* w  w  w . j  a va  2 s  .  c  om*/

    QuboleClusterBean quboleClusterBean = new QuboleClusterBean();
    quboleClusterBean.setClusterId(clusterId);
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = (JsonObject) parser.parse(configuration);
    // http://docs.qubole.com/en/latest/rest-api/cluster_api/get-cluster-information.html
    JsonObject nodeConfiguration = jsonObj.getAsJsonObject("node_configuration");
    quboleClusterBean.setMinSize(nodeConfiguration.getAsJsonPrimitive(MIN_SIZE_TAG).getAsInt());
    quboleClusterBean.setMaxSize(nodeConfiguration.getAsJsonPrimitive(MAX_SIZE_TAG).getAsInt());

    JsonObject stateObj = (JsonObject) parser.parse(clusterState);
    JsonArray array = stateObj.getAsJsonArray("nodes");
    int reservedInsanceCount = 0;
    int spotInstanceCount = 0;
    for (int i = 0; i < array.size(); ++i) {
        JsonObject nodeObject = array.get(i).getAsJsonObject();
        Boolean isSpotInstance = nodeObject.getAsJsonPrimitive("is_spot_instance").getAsBoolean();
        if (isSpotInstance) {
            spotInstanceCount++;
        } else {
            reservedInsanceCount++;
        }
    }
    quboleClusterBean.setRunningReservedInstanceCount(reservedInsanceCount);
    quboleClusterBean.setRunningSpotInstanceCount(spotInstanceCount);
    return quboleClusterBean;
}