RestClient.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey.client.http;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.maxkey.client.http.AuthorizationHeader;
  20. import org.maxkey.client.http.HttpVerb;
  21. import org.maxkey.client.http.Response;
  22. import org.maxkey.client.utils.Preconditions;
  23. public class RestClient {
  24. private static Log _log = LogFactory.getLog(RestClient. class );
  25. //action method
  26. private HttpVerb method = HttpVerb.GET;
  27. //request Service
  28. private Request request;
  29. public RestClient(String url) {
  30. Preconditions.checkEmptyString(url, "Invalid request url");
  31. request = new Request(method, url);
  32. }
  33. /**
  34. * 鏋勯�犳柟娉�
  35. *
  36. * @param method 璇锋眰鏂规硶
  37. * @param url 璇锋眰url
  38. */
  39. public RestClient(String url,HttpVerb method) {
  40. Preconditions.checkEmptyString(url, "Invalid request url");
  41. if(method != null) {
  42. this.method = method;
  43. }
  44. request = new Request(method, url);
  45. }
  46. /**
  47. * 娣诲姞璇锋眰鍙傛暟
  48. *
  49. * @param name 鍙傛暟鍚�
  50. * @param value 鍙傛暟鍊�
  51. */
  52. public void addParameter(String name,String value) {
  53. Preconditions.checkEmptyString(name, "parameter name is null");
  54. Preconditions.checkNotNull(request, "RestClient is null");
  55. if(HttpVerb.GET == method) {
  56. request.addQuerystringParameter(name, value);
  57. } else if(HttpVerb.POST == method) {
  58. request.addBodyParameter(name, value);
  59. }
  60. }
  61. /**
  62. * 娣诲姞璇锋眰Header
  63. *
  64. * @param name 鍙傛暟鍚�
  65. * @param value 鍙傛暟鍊�
  66. */
  67. public void addHeader(String name,String value) {
  68. Preconditions.checkEmptyString(name, "parameter name is null");
  69. Preconditions.checkNotNull(request, "OAuthRequest is null");
  70. request.addHeader(name, value);
  71. }
  72. /**
  73. * 鍙戦�佽姹傦紝鑾峰彇杩斿洖鐨勬暟鎹�
  74. *
  75. * @return Response 鏈嶅姟绔繑鍥炵殑鏁版嵁
  76. */
  77. public Response execute() {
  78. return request.send();
  79. }
  80. public void addBasicAuthorization(String username,String password) {
  81. Preconditions.checkEmptyString(username, "parameter username is null");
  82. Preconditions.checkEmptyString(password, "parameter password is null");
  83. Preconditions.checkNotNull(request, "RestClient is null");
  84. request.addHeader(AuthorizationHeader.AUTHORIZATION_HEADERNAME, AuthorizationHeader.createBasic(username, password));
  85. }
  86. public void addBearerAuthorization(String bearer) {
  87. Preconditions.checkEmptyString(bearer, "parameter bearer is null");
  88. Preconditions.checkNotNull(request, "RestClient is null");
  89. request.addHeader(AuthorizationHeader.AUTHORIZATION_HEADERNAME, AuthorizationHeader.createBearer(bearer));
  90. }
  91. /**
  92. * set REST Content
  93. *
  94. * @param content
  95. */
  96. public void addRestContent(String content)
  97. {
  98. this.request.addRestContent(content) ;
  99. }
  100. /**
  101. * set REST Content
  102. *
  103. * @param content
  104. */
  105. public void addRestObject(Object content)
  106. {
  107. this.request.addRestObject(content);
  108. }
  109. /**
  110. * set REST Content
  111. *
  112. * @param content
  113. */
  114. public void addRestContent(byte[] content)
  115. {
  116. this.request.addRestContent(content);
  117. }
  118. }