Package com.cloud.bridge.util

Examples of com.cloud.bridge.util.JsonAccessor$ArrayPropertyResolver


    protected final static Logger logger = Logger.getLogger(UtilTestCase.class);
 
    public void testJsonAccessor() {
      JsonParser parser = new JsonParser();
      JsonElement json = parser.parse("{firstName: 'Kelven', lastName: 'Yang', arrayObj: [{name: 'elem1'}, {name: 'elem2'}]}");
      JsonAccessor jsonAccessor = new JsonAccessor(json);
     
      Assert.assertTrue("Kelven".equals(jsonAccessor.getAsString("firstName")));
      Assert.assertTrue("Kelven".equals(jsonAccessor.getAsString("this.firstName")));
      Assert.assertTrue("Yang".equals(jsonAccessor.getAsString("lastName")));
      Assert.assertTrue("Yang".equals(jsonAccessor.getAsString("this.lastName")));
     
      Assert.assertTrue("elem1".equals(jsonAccessor.getAsString("arrayObj[0].name")));
      Assert.assertTrue("elem2".equals(jsonAccessor.getAsString("arrayObj[1].name")));
     
      Assert.assertTrue("elem1".equals(jsonAccessor.getAsString("this.arrayObj.this[0].name")));
      Assert.assertTrue("elem2".equals(jsonAccessor.getAsString("this.arrayObj.this[1].name")));
     
      Assert.assertTrue(jsonAccessor.getMatchCount("firstName") == 1);
      Assert.assertTrue(jsonAccessor.getMatchCount("middleName") == -1);
      Assert.assertTrue(jsonAccessor.getMatchCount("arrayObj") == 2);
      Assert.assertTrue(jsonAccessor.getMatchCount("arrayObj[0]") == 1);
    }
View Full Code Here


    public void testGson() {
      String response = "{ \"queryasyncjobresultresponse\" : {\"jobid\":5868,\"jobstatus\":1,\"jobprocstatus\":0,\"jobresultcode\":0,\"jobresulttype\":\"object\",\"jobresult\":{\"snapshot\":{\"id\":3161,\"account\":\"admin\",\"domainid\":1,\"domain\":\"ROOT\",\"snapshottype\":\"MANUAL\",\"volumeid\":186928,\"volumename\":\"KY-DATA-VOL\",\"volumetype\":\"DATADISK\",\"created\":\"2011-06-02T05:05:41-0700\",\"name\":\"i-2-246446-VM_KY-DATA-VOL_20110602120541\",\"intervaltype\":\"MANUAL\",\"state\":\"BackedUp\"}}}}";
     
      JsonParser parser = new JsonParser();
      JsonElement json = parser.parse(response);
      JsonAccessor jsonAccessor = new JsonAccessor(json);
     
      Gson gson = new Gson();
      CloudStackSnapshot snapshot = gson.fromJson(jsonAccessor.eval("queryasyncjobresultresponse.jobresult.snapshot"), CloudStackSnapshot.class);
     
      Assert.assertTrue("BackedUp".equals(snapshot.getState()));
    }
View Full Code Here

  public <T> T call(CloudStackCommand cmd, String apiKey, String secretKey, boolean followToAsyncResult,
    String responseName, String responseObjName, Class<T> responseClzthrows Exception {
   
    assert(responseName != null);
   
    JsonAccessor json = execute(cmd, apiKey, secretKey);
    if(followToAsyncResult && json.tryEval(responseName + ".jobid") != null) {
      long startMs = System.currentTimeMillis();
          while(System.currentTimeMillis() -  startMs < _pollTimeoutMs) {
        CloudStackCommand queryJobCmd = new CloudStackCommand("queryAsyncJobResult");
            queryJobCmd.setParam("jobId", json.getAsString(responseName + ".jobid"));
           
            JsonAccessor queryAsyncJobResponse = execute(queryJobCmd, apiKey, secretKey);

          if(queryAsyncJobResponse.tryEval("queryasyncjobresultresponse") != null) {
            int jobStatus = queryAsyncJobResponse.getAsInt("queryasyncjobresultresponse.jobstatus");
            switch(jobStatus) {
            case 2:
                throw new Exception(queryAsyncJobResponse.getAsString("queryasyncjobresultresponse.jobresult.errorcode") + " " +
                  queryAsyncJobResponse.getAsString("queryasyncjobresultresponse.jobresult.errortext"));
               
            case 0 :
                    try {
                      Thread.sleep( _pollIntervalMs );
                    } catch( Exception e ) {}
                    break;
                   
            case 1 :
              if(responseObjName != null)
                return (T)(new Gson()).fromJson(queryAsyncJobResponse.eval("queryasyncjobresultresponse.jobresult." + responseObjName), responseClz);
              else
                return (T)(new Gson()).fromJson(queryAsyncJobResponse.eval("queryasyncjobresultresponse.jobresult"), responseClz);
             
            default :
              assert(false);
                      throw new Exception("Operation failed - invalid job status response");
            }
View Full Code Here

  public <T> List<T> listCall(CloudStackCommand cmd, String apiKey, String secretKey,
    String responseName, String responseObjName, Type collectionType) throws Exception {
   
    assert(responseName != null);
   
    JsonAccessor json = execute(cmd, apiKey, secretKey);
   
   

    if(responseObjName != null)
      try {
        return (new Gson()).fromJson(json.eval(responseName + "." + responseObjName), collectionType);
      } catch(Exception e) {
        // this happens because responseObjName won't exist if there are no objects in the list.
        logger.debug("Unable to find responseObjName:[" + responseObjName + "].  Returning null! Exception: " + e.getMessage());
        return null;
      }
    return (new Gson()).fromJson(json.eval(responseName), collectionType);
  }
View Full Code Here

          throw new IOException("CloudStack API call error : invalid JSON response");
    }
   
    if(logger.isDebugEnabled())
      logger.debug("Cloud API call + [" + url.toString() + "] returned: " + jsonElement.toString());
    return new JsonAccessor(jsonElement);
  }
View Full Code Here

  public <T> T call(CloudStackCommand cmd, String apiKey, String secretKey, boolean followToAsyncResult,
    String responseName, String responseObjName, Class<T> responseClzthrows Exception {
   
    assert(responseName != null);
   
    JsonAccessor json = execute(cmd, apiKey, secretKey);
    if(followToAsyncResult && json.tryEval(responseName + ".jobid") != null) {
      long startMs = System.currentTimeMillis();
          while(System.currentTimeMillis() -  startMs < _pollTimeoutMs) {
        CloudStackCommand queryJobCmd = new CloudStackCommand("queryAsyncJobResult");
            queryJobCmd.setParam("jobId", json.getAsString(responseName + ".jobid"));
           
            JsonAccessor queryAsyncJobResponse = execute(queryJobCmd, apiKey, secretKey);

          if(queryAsyncJobResponse.tryEval("queryasyncjobresultresponse") != null) {
            int jobStatus = queryAsyncJobResponse.getAsInt("queryasyncjobresultresponse.jobstatus");
            switch(jobStatus) {
            case 2:
                throw new Exception(queryAsyncJobResponse.getAsString("queryasyncjobresultresponse.jobresult.errorcode") + " " +
                  queryAsyncJobResponse.getAsString("queryasyncjobresultresponse.jobresult.errortext"));
               
            case 0 :
                    try {
                      Thread.sleep( _pollIntervalMs );
                    } catch( Exception e ) {}
                    break;
                   
            case 1 :
              if(responseObjName != null)
                return (T)(new Gson()).fromJson(queryAsyncJobResponse.eval("queryasyncjobresultresponse.jobresult." + responseObjName), responseClz);
              else
                return (T)(new Gson()).fromJson(queryAsyncJobResponse.eval("queryasyncjobresultresponse.jobresult"), responseClz);
             
            default :
              assert(false);
                      throw new Exception("Operation failed - invalid job status response");
            }
View Full Code Here

  public <T> List<T> listCall(CloudStackCommand cmd, String apiKey, String secretKey,
    String responseName, String responseObjName, Type collectionType) throws Exception {
   
    assert(responseName != null);
   
    JsonAccessor json = execute(cmd, apiKey, secretKey);
   
   

    if(responseObjName != null)
      try {
        return (new Gson()).fromJson(json.eval(responseName + "." + responseObjName), collectionType);
      } catch(Exception e) {
        // this happens because responseObjName won't exist if there are no objects in the list.
        logger.debug("Unable to find responseObjName:[" + responseObjName + "].  Returning null! Exception: " + e.getMessage());
        return null;
      }
    return (new Gson()).fromJson(json.eval(responseName), collectionType);
  }
View Full Code Here

          throw new IOException("CloudStack API call error : invalid JSON response");
    }
   
    if(logger.isDebugEnabled())
      logger.debug("Cloud API call + [" + url.toString() + "] returned: " + jsonElement.toString());
    return new JsonAccessor(jsonElement);
  }
View Full Code Here

  public <T> T call(CloudStackCommand cmd, String apiKey, String secretKey, boolean followToAsyncResult,
    String responseName, String responseObjName, Class<T> responseClzthrows Exception {
   
    assert(responseName != null);
   
    JsonAccessor json = execute(cmd, apiKey, secretKey);
    if(followToAsyncResult && json.tryEval(responseName + ".jobid") != null) {
      long startMs = System.currentTimeMillis();
          while(System.currentTimeMillis() -  startMs < _pollTimeoutMs) {
        CloudStackCommand queryJobCmd = new CloudStackCommand("queryAsyncJobResult");
            queryJobCmd.setParam("jobId", json.getAsString(responseName + ".jobid"));
           
            JsonAccessor queryAsyncJobResponse = execute(queryJobCmd, apiKey, secretKey);

          if(queryAsyncJobResponse.tryEval("queryasyncjobresultresponse") != null) {
            int jobStatus = queryAsyncJobResponse.getAsInt("queryasyncjobresultresponse.jobstatus");
            switch(jobStatus) {
            case 2:
                throw new Exception(queryAsyncJobResponse.getAsString("queryasyncjobresultresponse.jobresult.errorcode") + " " +
                  queryAsyncJobResponse.getAsString("queryasyncjobresultresponse.jobresult.errortext"));
               
            case 0 :
                    try {
                      Thread.sleep( _pollIntervalMs );
                    } catch( Exception e ) {}
                    break;
                   
            case 1 :
              if(responseObjName != null)
                return (T)(new Gson()).fromJson(queryAsyncJobResponse.eval("queryasyncjobresultresponse.jobresult." + responseObjName), responseClz);
              else
                return (T)(new Gson()).fromJson(queryAsyncJobResponse.eval("queryasyncjobresultresponse.jobresult"), responseClz);
             
            default :
              assert(false);
                      throw new Exception("Operation failed - invalid job status response");
            }
View Full Code Here

  public <T> List<T> listCall(CloudStackCommand cmd, String apiKey, String secretKey,
    String responseName, String responseObjName, Type collectionType) throws Exception {
   
    assert(responseName != null);
   
    JsonAccessor json = execute(cmd, apiKey, secretKey);
   
   

    if(responseObjName != null)
      try {
        return (new Gson()).fromJson(json.eval(responseName + "." + responseObjName), collectionType);
      } catch(Exception e) {
        // this happens because responseObjName won't exist if there are no objects in the list.
                                logger.debug("CloudSatck API response doesn't contain responseObjName:" + responseObjName +
                                        " because response is empty");
        return null;
      }
    return (new Gson()).fromJson(json.eval(responseName), collectionType);
  }
View Full Code Here

TOP

Related Classes of com.cloud.bridge.util.JsonAccessor$ArrayPropertyResolver

Copyright © 2018 www.massapicom. 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.