Example usage for com.google.gson JsonObject addProperty

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

Introduction

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

Prototype

public void addProperty(String property, Character value) 

Source Link

Document

Convenience method to add a char member.

Usage

From source file:club.jmint.crossing.specs.CrossingService.java

License:Apache License

/**
 * //  ww w .  java  2  s.  c o m
 * @param obj
 * @param encrypt
 * @return
 */
protected String buildOutputParams(JsonObject obj, boolean encrypt) throws CrossException {
    if (obj == null || obj.toString().isEmpty()) {
        //we do resume that it's successful without any response parameters.
        JsonObject jo = new JsonObject();
        jo.addProperty("errorCode", ErrorCode.SUCCESS.getCode());
        jo.addProperty("errorInfo", ErrorCode.SUCCESS.getInfo());
        return jo.toString();
    }

    //create signature
    String op = obj.toString();
    String signValue = buildSign(op, signKey);

    JsonObject jo = new JsonObject();
    jo.addProperty("sign", signValue);
    jo.add("params", obj);

    String encrypted;
    if (encrypt) {
        //encrypt output
        encrypted = buildEncrypt(jo.toString());
        JsonObject joen = new JsonObject();
        joen.addProperty("encrypted", encrypted);
        joen.addProperty("errorCode", ErrorCode.SUCCESS.getCode());
        joen.addProperty("errorInfo", ErrorCode.SUCCESS.getInfo());
        return joen.toString();
    } else {
        //non-encrypt output
        jo.addProperty("errorCode", ErrorCode.SUCCESS.getCode());
        jo.addProperty("errorInfo", ErrorCode.SUCCESS.getInfo());
        return jo.toString();
    }

}

From source file:club.jmint.crossing.specs.CrossingService.java

License:Apache License

/**
 * There exist these kinds of circumstances that we need return by given Errorcode with some output parameters
 * @param obj// ww w. ja v a2 s.  c  o  m
 * @param encrypt
 * @param errorCode
 * @param errorInfo
 * @return
 */
protected String buildOutputParamsWithGivenErrorCode(JsonObject obj, boolean encrypt, int errorCode,
        String errorInfo) throws CrossException {
    if (obj == null) {
        return buildOutputError(errorCode, errorInfo);
    }

    //create signature
    String op = obj.toString();
    String signValue = buildSign(op, signKey);

    JsonObject jo = new JsonObject();
    jo.addProperty("sign", signValue);
    jo.addProperty("params", op);

    String encrypted;
    if (encrypt) {
        //encrypt output
        encrypted = buildEncrypt(jo.toString());
        JsonObject joen = new JsonObject();
        joen.addProperty("encrypted", encrypted);
        joen.addProperty("errorCode", ErrorCode.SUCCESS.getCode());
        joen.addProperty("errorInfo", ErrorCode.SUCCESS.getInfo());
        return joen.toString();
    } else {
        //non-encrypt output
        jo.addProperty("errorCode", ErrorCode.SUCCESS.getCode());
        jo.addProperty("errorInfo", ErrorCode.SUCCESS.getInfo());
        return jo.toString();
    }
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String createErrorParams(int errorcode, String errorinfo) {
    JsonObject jo = new JsonObject();
    jo.addProperty("errorCode", errorcode);
    jo.addProperty("errorInfo", errorinfo);
    String p = jo.toString();/*  ww w  . ja  v a 2  s  . co m*/
    return p;
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String buildSignedParams(String p, String signKey) throws CrossException {
    String md5Value = Security.crossingSign(p, signKey, "");
    JsonObject jo = new JsonObject();
    jo.addProperty("sign", md5Value);
    JsonParser jp = new JsonParser();
    JsonElement jop = null;//from  w  w  w  . ja va2s  .  c  o  m
    try {
        jop = jp.parse(p);
        if (!jop.isJsonObject()) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
    } catch (JsonSyntaxException jse) {
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    jo.add("params", jop);
    return jo.toString();
}

From source file:club.jmint.crossing.specs.ParamBuilder.java

License:Apache License

public static String buildDecryptedParams(String encryptParams, String decryptKey) throws CrossException {
    JsonParser jp = new JsonParser();
    JsonElement jop = null;/*w  w  w  . j  av a2  s.  c om*/
    try {
        jop = jp.parse(encryptParams);
        if (!jop.isJsonObject()) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
    } catch (JsonSyntaxException jse) {
        //jse.printStackTrace();
        throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
    }

    JsonObject jo = (JsonObject) jop;
    String np = null;
    if (jo.has("encrypted")) {
        String encrypted = jo.getAsJsonPrimitive("encrypted").getAsString();
        String paramsValue = Security.crossingDecrypt(encrypted, decryptKey, "DES");
        int ec = jo.getAsJsonPrimitive("errorCode").getAsInt();
        String ed = jo.getAsJsonPrimitive("errorInfo").getAsString();
        JsonParser jpr = new JsonParser();
        JsonObject jor = null;
        try {
            jor = (JsonObject) jpr.parse(paramsValue);
        } catch (JsonSyntaxException je) {
            throw new CrossException(ErrorCode.COMMON_ERR_PARAM_MALFORMED.getCode(),
                    ErrorCode.COMMON_ERR_PARAM_MALFORMED.getInfo());
        }
        jor.addProperty("errorCode", ec);
        jor.addProperty("errorInfo", ed);
        np = jor.toString();
        return np;
    }

    return encryptParams;
}

From source file:club.jmint.crossing.specs.ServiceSpecsExmpleCallImpl.java

License:Apache License

/**
 * Demo non-encrypted interface with simple input parameters and output response.
 * @param params wrappered with JSON format: <br/>
 *       "{"sign":"XXXXYYYYZZZZAAAABBBBCCCCCFFFFJJJJ","params":{"name":"Shao Huicheng","say":"Hello, World","job":"Software Engineer","skills":"Programming,People Management etc."}}"
 * @param encrypt params is encrypted or not
 * @return wrappered with JSON format: <br/>
 *       "{"sign":"XXXXCCCCCFFFFJJJJYYYYZZZZAAAABBBB","errorCode":"0","errorInfo":"success",
 * "params":{"sentence":"Hi, Huicheng. Glad to hear you having finished the Crossing Project. I am deeply impressed with this project."}"
 *//*  ww  w  .ja va 2 s  . c  o m*/
public String getSimpleReply(String params, boolean encrypt) {
    //parse parameters and verify signature
    JsonObject ip;
    try {
        ip = parseInputParams(params, encrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }

    //validate parameters on business side
    String name, say, job, skills;
    try {
        name = getParamAsString(ip, "name");
        say = getParamAsString(ip, "say");
        job = getParamAsString(ip, "job");
        skills = getParamAsString(ip, "skills");
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }
    System.out.println("name: " + name);
    System.out.println("say: " + say);
    System.out.println("job: " + job);
    System.out.println("skills: " + skills);
    //do more checks here

    //do your business logics
    String sentence = "Hi, " + name
            + "Glad to hear you having finished the Crossing Project. I am deeply impressed with this project.";
    //do more things here.

    //build response parameters
    JsonObject op = new JsonObject();
    op.addProperty("sentence", sentence);
    String output = null;
    try {
        output = buildOutputParams(op, encrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }
    return output;
}

From source file:club.jmint.crossing.specs.ServiceSpecsExmpleCallImpl.java

License:Apache License

/**
 * Demo encrypted interface with simple parameters
 * @param params <br/>//  w w  w.jav  a  2  s  .  co m
 *       "{"encrypted":"19adkfljj;adfmzfjk;[afakfjkl;adjfqrdkjk;akdfjkjkdajfjkjkadfkjdajf"}" <br/>
 *       corresponding plain text:   "{"sign":"DDDDTTTTZZZZAAAABBBBCCCCCFFFFJJJJ","params":{"s1":"Are","s2":"you","s3":"ready?"}}"
 * @param encrypt
 * @return       <br/>
 *       {"encrypted":"134780akfd;jadhadleiuqptihjadghlnzvadfjkj/dfa","errorCode":"0","erroDesc":"success"} <br/>
 *       corresponding plain text: "{"sign":"EEEESSSSFFFFJJJJYYYYZZZZAAAABBBB","errorCode":"0","errorInfo":"success","params":{"reply":"Yes, I am ready."}}"
 */
public String doCopyMe(String params, boolean encrypt) {
    //parse parameters and verify signature
    JsonObject ip;
    try {
        ip = parseInputParams(params, encrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }

    //validate parameters on business side
    String s1, s2, s3;
    try {
        s1 = getParamAsString(ip, "s1");
        s2 = getParamAsString(ip, "s2");
        s3 = getParamAsString(ip, "s3");

    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }
    System.out.println("s1: " + s1);
    System.out.println("s2: " + s2);
    System.out.println("s3: " + s3);

    //do more checks here

    //do your business logics
    String copyme = s1 + " " + s2 + " " + s3;
    //do more things here.

    //build response parameters
    JsonObject op = new JsonObject();
    op.addProperty("sentence", copyme);
    String output = null;
    try {
        output = buildOutputParams(op, encrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }
    return output;
}

From source file:club.jmint.mifty.example.ExaClient.java

License:Apache License

public static void main(String[] args) {

    try {//from w ww.  j a  v a 2s  . co  m
        TTransport transport;
        //      if (true) {
        transport = new TSocket("localhost", 9090);
        //transport.open();
        //      }
        //      else {
        //        /*
        //         * Similar to the server, you can use the parameters to setup client parameters or
        //         * use the default settings. On the client side, you will need a TrustStore which
        //         * contains the trusted certificate along with the public key. 
        //         * For this example it's a self-signed cert. 
        //         */
        //        TSSLTransportParameters params = new TSSLTransportParameters();
        //        params.setTrustStore("../../lib/java/test/.truststore", "thrift", "SunX509", "JKS");
        //        /*
        //         * Get a client transport instead of a server transport. The connection is opened on
        //         * invocation of the factory method, no need to specifically call open()
        //         */
        //        transport = TSSLTransportFactory.getClientSocket("localhost", 9091, 0, params);
        //      }

        TProtocol protocol = new TBinaryProtocol(transport);
        TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol, "ExaService");
        ExaService.Client client = new ExaService.Client(mp1);

        TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "DemoService");
        DemoService.Client client2 = new DemoService.Client(mp2);

        transport.open();

        //first call
        JsonObject pp1 = new JsonObject();
        pp1.addProperty("echo", "How are you!");
        pp1.addProperty("name", "Darren");
        String signP1 = null;
        try {
            signP1 = ParamBuilder.buildSignedParams(pp1.toString(), "miftyExampleKey");
        } catch (CrossException e) {
            e.printStackTrace();
        }
        String ret = client.echo(signP1, false);
        System.out.println(ParamBuilder.checkSignAndRemove(ret, "miftyExampleKey"));

        //second call
        JsonObject pp2 = new JsonObject();
        pp2.addProperty("hello", "Good morning!");
        pp2.addProperty("name", "Darren");
        String signP2 = null;
        try {
            signP2 = ParamBuilder.buildEncryptedParams(
                    ParamBuilder.buildSignedParams(pp2.toString(), "miftyExampleKey"), "miftyExampleKey");
        } catch (CrossException e) {
            e.printStackTrace();
        }
        ret = client.sayHello(signP2, true);
        System.out.println(ParamBuilder.checkSignAndRemove(
                ParamBuilder.buildDecryptedParams(ret, "miftyExampleKey"), "miftyExampleKey"));

        //third call  {"echo":"I am a DEMO","name":"DemoService"}
        JsonObject pp3 = new JsonObject();
        pp3.addProperty("echo", "I am a DEMO");
        pp3.addProperty("name", "DemoService");
        String signP3 = null;
        try {
            signP3 = ParamBuilder.buildEncryptedParams(
                    ParamBuilder.buildSignedParams(pp3.toString(), "miftyExampleKey"), "miftyExampleKey");
        } catch (CrossException e) {
            e.printStackTrace();
        }
        ret = client2.demoSay(signP3, true);
        System.out.println(ParamBuilder.checkSignAndRemove(
                ParamBuilder.buildDecryptedParams(ret, "miftyExampleKey"), "miftyExampleKey"));

        transport.close();
    } catch (TException x) {
        x.printStackTrace();
    } catch (CrossException e1) {
        e1.printStackTrace();
    }

}

From source file:club.jmint.mifty.example.ExaServiceImpl.java

License:Apache License

/**
 * @param {"hello":"Good morning!","name":"Darren"}
 *//*from  w ww.  ja v a  2 s.c o  m*/
public String sayHello(String params, boolean isEncrypt) throws TException {
    //parse parameters and verify signature
    CrossLog.logger.debug("sayHello: " + params);
    JsonObject ip;
    try {
        ip = parseInputParams(params, isEncrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }

    //validate parameters on business side
    String name, hello;
    try {
        name = getParamAsString(ip, "name");
        hello = getParamAsString(ip, "hello");
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }

    //do more checks here if you need.
    Session session = Dao.getSessionFactory().openSession();
    session.beginTransaction();
    session.createSQLQuery("insert into test.user (uid,name) values (3,'zhouguoxing'); ").executeUpdate();
    SQLQuery sq = session.createSQLQuery("select * from test.user;");
    System.out.println(sq.getFirstResult());
    session.createSQLQuery("delete from test.user where uid=1; ").executeUpdate();
    session.getTransaction().commit();
    session.close();

    //do your business logics
    System.out.println("name: " + name);
    System.out.println("hello: " + hello);
    CrossLog.logger.info("\nname: " + name + "\nsay: " + hello);

    String sentence = "Hi, " + name + " ," + hello;
    //do more things here.

    //build response parameters
    JsonObject op = new JsonObject();
    op.addProperty("sentence", sentence);
    String output = null;
    try {
        output = buildOutputParams(op, isEncrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }
    CrossLog.logger.debug("sayHello: " + output);
    return output;
}

From source file:club.jmint.mifty.example.ExaServiceImpl.java

License:Apache License

/**
 * @param {"echo":"I am back","name":"Darren"}
 *///  www.  j av  a  2 s  .co  m
public String echo(String params, boolean isEncrypt) throws TException {
    //parse parameters and verify signature
    CrossLog.logger.debug("echo: " + params);
    JsonObject ip;
    try {
        ip = parseInputParams(params, isEncrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }

    //validate parameters on business side
    String name, echo;
    try {
        name = getParamAsString(ip, "name");
        echo = getParamAsString(ip, "echo");
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }

    //do more checks here if you need.

    //do your business logics
    System.out.println("name: " + name);
    System.out.println("echo: " + echo);
    CrossLog.logger.info("\nname: " + name + "\nsay: " + echo);

    String sentence = "Hi, " + name + " ," + echo;
    //do more things here.

    //build response parameters
    JsonObject op = new JsonObject();
    op.addProperty("sentence", sentence);
    String output = null;
    try {
        output = buildOutputParams(op, isEncrypt);
    } catch (CrossException ce) {
        return buildOutputByCrossException(ce);
    }
    CrossLog.logger.debug("sayHello: " + output);
    return output;
}