Advertisement
Guest User

Untitled

a guest
Apr 16th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. public String query(String path, HashMap<String, String> args) {
  2. String answer = "";
  3. boolean httpError = false;
  4. HttpsURLConnection connection=null;
  5. String nonce = String.valueOf(System.currentTimeMillis());
  6. try {
  7. // add nonce and build arg list
  8. args.put("nonce", nonce);
  9. String hash_data = path + "\0" + this.buildQueryString(args); //Should be correct
  10.  
  11. System.out.println("post-data-mac: "+ hash_data);
  12.  
  13. // args signature with apache cryptografic tools
  14. String signature = signRequest(this.keys.getPrivateKey(), hash_data);
  15.  
  16. // build URL
  17. URL queryUrl = new URL(API_BASE_URL + path);
  18.  
  19. // create and setup a HTTP connection
  20. connection = (HttpsURLConnection)queryUrl.openConnection();
  21.  
  22. //Check if the call should be done over POST or GET, depends on the function
  23. if(path.equals(API_TICKER))
  24. connection.setRequestMethod("GET");
  25. else
  26. connection.setRequestMethod("POST");
  27.  
  28. connection.setRequestProperty("User-Agent", settings.APP_TITLE);
  29. connection.setRequestProperty("Rest-Key", this.keys.getApiKey());
  30. connection.setRequestProperty("Rest-Sign", signature.replaceAll("\n", ""));
  31.  
  32. connection.setReadTimeout(10000);
  33. connection.setDoOutput(true);
  34. connection.setDoInput(true);
  35.  
  36.  
  37. //Read the response
  38.  
  39. BufferedReader br = null;
  40. if (connection.getResponseCode() >= 400) {
  41. httpError = true;//TODO , if HTTP error, do something else with output!
  42. br = new BufferedReader(new InputStreamReader((connection.getErrorStream())));
  43. }
  44. else
  45. br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
  46.  
  47. String output;
  48. System.out.println("HTTP response : \n"); //TODO Remove
  49. while ((output = br.readLine()) != null) {
  50. System.out.println(output);
  51. answer+=output;
  52. }
  53. }
  54.  
  55. //Capture Exceptions
  56.  
  57. catch (IllegalStateException ex) {
  58. Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, ex);
  59. }
  60. catch (IOException ex) {
  61. Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, ex);
  62.  
  63. }
  64. finally
  65. {
  66. //close the connection, set all objects to null
  67. connection.disconnect();
  68. connection = null;
  69. }
  70. return answer;
  71. }
  72.  
  73. //Build the query string given a set of query parameters
  74. protected String buildQueryString(HashMap<String, String> args) {
  75. String result = new String();
  76. for (String hashkey : args.keySet()) {
  77. if (result.length() > 0) result += '&';
  78. try {
  79. result += URLEncoder.encode(hashkey, ENCODING) + "="
  80. + URLEncoder.encode(args.get(hashkey), ENCODING);
  81. } catch (Exception ex) {
  82. Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, ex);
  83. }
  84. }
  85. return result;
  86. }
  87.  
  88. private String signRequest(String secret, String hash_data) {
  89. String signature = "";
  90. try{
  91. Mac mac = Mac.getInstance(SIGN_HASH_FUNCTION);
  92. SecretKeySpec secret_spec = new SecretKeySpec(Base64.decodeBase64(secret), SIGN_HASH_FUNCTION);
  93. mac.init(secret_spec);
  94. signature = Base64.encodeBase64String(mac.doFinal(hash_data.getBytes()));
  95. }
  96. catch (NoSuchAlgorithmException | InvalidKeyException e){
  97. Logger.getLogger(MtGox.class.getName()).log(Level.SEVERE, null, e);
  98. }
  99. return signature;
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement