Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. //Singleton class: Tenant
  2. public ArrayList<GuestAgent> gAgentList() {
  3. final ArrayList<GuestAgent> guestAgents = new ArrayList<>();
  4. String url = "http://localhost:8080/StackUI/v2.0/";
  5. url = url + this.tenantId;
  6. url = url + "/os-agents";
  7.  
  8. RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
  9. builder.setHeader("X-Auth-Token", this.tokenId);
  10.  
  11. try {
  12. builder.sendRequest(null, new RequestCallback() {
  13. @Override
  14. public void onError(Request request, Throwable exception) {
  15. Window.alert("Attensione si è verificato un errore");
  16. }
  17.  
  18. @Override
  19. public void onResponseReceived(Request request, Response response) {
  20. if (200 == response.getStatusCode()) {
  21. final HTML respBox = new HTML();
  22. respBox.setHTML(response.getText());
  23.  
  24. String risposta = response.getText();
  25.  
  26. JSONValue jsonValue;
  27. JSONArray jsonArray;
  28. JSONObject jsonObject;
  29. JSONString jsonString;
  30. JSONNumber jsonNumber;
  31.  
  32. jsonValue = JSONParser.parseStrict(risposta);
  33.  
  34. if ((jsonObject = jsonValue.isObject()) == null) {
  35. Window.alert("Error parsing the JSON");
  36. }
  37.  
  38. jsonValue = jsonObject.get("agents");
  39. if ((jsonArray = jsonValue.isArray()) == null) {
  40. Window.alert("Error parsing the JSON");
  41. }
  42.  
  43. for (int i = 0; i < jsonArray.size(); i++) {
  44. GuestAgent guestAgent = new GuestAgent();
  45. jsonValue = jsonArray.get(i);
  46.  
  47. if ((jsonObject = jsonValue.isObject()) == null) {
  48. Window.alert("Error parsing the JSON");
  49. }
  50.  
  51. jsonValue = jsonObject.get("agent_id");
  52. if ((jsonNumber = jsonValue.isNumber()) == null) {
  53. Window.alert("Error parsing the JSON");
  54. }
  55. guestAgent.setAgentId(jsonNumber.toString());
  56.  
  57. jsonValue = jsonObject.get("architecture");
  58. if ((jsonString = jsonValue.isString()) == null) {
  59. Window.alert("Error parsing the JSON");
  60. }
  61. guestAgent.setArchitecture(jsonString.stringValue());
  62.  
  63. jsonValue = jsonObject.get("hypervisor");
  64. if ((jsonString = jsonValue.isString()) == null) {
  65. Window.alert("Error parsing the JSON");
  66. }
  67. guestAgent.setHypervisor(jsonString.stringValue());
  68.  
  69. jsonValue = jsonObject.get("md5hash");
  70. if ((jsonString = jsonValue.isString()) == null) {
  71. Window.alert("Error parsing the JSON");
  72. }
  73. guestAgent.setMd5hash(jsonString.stringValue());
  74.  
  75. jsonValue = jsonObject.get("os");
  76. if ((jsonString = jsonValue.isString()) == null) {
  77. Window.alert("Error parsing the JSON");
  78. }
  79. guestAgent.setOs(jsonString.stringValue());
  80.  
  81. jsonValue = jsonObject.get("url");
  82. if ((jsonString = jsonValue.isString()) == null) {
  83. Window.alert("Error parsing the JSON");
  84. }
  85. guestAgent.setUrl(jsonString.stringValue());
  86.  
  87. jsonValue = jsonObject.get("version");
  88. if ((jsonString = jsonValue.isString()) == null) {
  89. Window.alert("Error parsing the JSON");
  90. }
  91. guestAgent.setVersion(jsonString.stringValue());
  92.  
  93. guestAgents.add(guestAgent);
  94. }
  95.  
  96. } else {
  97. // Handle the error. Can get the status text from response.getStatusText()
  98. Window.alert("Errore " + response.getStatusCode() + " " + response.getStatusText());
  99. }
  100. }
  101. });
  102. } catch (RequestException e) {
  103. // Couldn't connect to server
  104. Window.alert("Impossibile connettersi al server");
  105. }
  106.  
  107. return guestAgents;
  108. }
  109.  
  110. //Other class
  111. ArrayList<GuestAgent> agents;
  112. agents = Tenant.getTenantObject().gAgentList();
  113. Window.alert(Integer.toString(agents.size()));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement