Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. ClientAuthentication.
  2. ---------------------------
  3.  
  4. package client;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import javax.ws.rs.client.ClientRequestContext;
  8. import javax.ws.rs.client.ClientRequestFilter;
  9. import javax.ws.rs.core.MultivaluedMap;
  10. import javax.xml.bind.DatatypeConverter;
  11.  
  12. public class ClientAuthentication implements ClientRequestFilter {
  13.  
  14. private final String user;
  15. private final String password;
  16.  
  17. public ClientAuthentication(String user, String password) {
  18. this.user = user;
  19. this.password = password;
  20. }
  21.  
  22. public void filter(ClientRequestContext requestContext) throws IOException {
  23. MultivaluedMap<String, Object> headers = requestContext.getHeaders();
  24. final String basicAuthentication = getBasicAuthentication();
  25. headers.add("Authorization", basicAuthentication);
  26.  
  27. }
  28.  
  29. private String getBasicAuthentication() {
  30. String token = this.user + ":" + this.password;
  31. try {
  32. return "BASIC " + DatatypeConverter.printBase64Binary(token.getBytes("UTF-8"));
  33. } catch (UnsupportedEncodingException ex) {
  34. throw new IllegalStateException("Cannot encode with UTF-8", ex);
  35. }
  36. }
  37. }
  38.  
  39.  
  40. *******************************************************
  41. *******************************************************
  42. Client.
  43. --------
  44. package client;
  45. import javax.json.Json;
  46. import javax.ws.rs.client.ClientBuilder;
  47. import javax.ws.rs.client.Invocation;
  48. import javax.ws.rs.client.WebTarget;
  49. import javax.ws.rs.core.Response;
  50.  
  51. import com.google.gson.Gson;
  52. import com.google.gson.JsonElement;
  53. import com.google.gson.JsonObject;
  54.  
  55. public class Client {
  56.  
  57. private String uri;
  58. private String user;
  59. private String password;
  60.  
  61. public Client(String uri, String user, String password) {
  62. this.uri = uri;
  63. this.user = user;
  64. this.password = password;
  65. }
  66.  
  67. public String getUri() {
  68. return uri;
  69. }
  70.  
  71. public void setUri(String uri) {
  72. this.uri = uri;
  73. }
  74.  
  75. public String getUser() {
  76. return user;
  77. }
  78.  
  79. public void setUser(String user) {
  80. this.user = user;
  81. }
  82.  
  83. public String getPassword() {
  84. return password;
  85. }
  86.  
  87. public void setPassword(String password) {
  88. this.password = password;
  89. }
  90.  
  91. public double add(double one, double two) {
  92. try {
  93. javax.ws.rs.client.Client client = ClientBuilder.newClient()
  94. .register(new ClientAuthentication(user, password));
  95. WebTarget target = client.target(uri);
  96. target = target.path("add").queryParam("num1", one).queryParam("num2", two);
  97. Invocation.Builder builder = target.request();
  98. Response jsonString = builder.get();
  99. String res = jsonString.readEntity(String.class);
  100. Gson g = new Gson();
  101. JsonObject resJson = g.fromJson(res, JsonObject.class);
  102. double value = resJson.get("result").getAsDouble();
  103. return value;
  104. } catch (Exception e) {
  105. return 0.0;
  106. }
  107. }
  108.  
  109. public double multiply(double one, double two) {
  110. try {
  111. javax.ws.rs.client.Client client = ClientBuilder.newClient()
  112. .register(new ClientAuthentication(user, password));
  113. WebTarget target = client.target(uri);
  114. target = target.path("multiply").queryParam("num1", one).queryParam("num2", two);
  115. Invocation.Builder builder = target.request();
  116. Response jsonString = builder.get();
  117. String res = jsonString.readEntity(String.class);
  118. Gson g = new Gson();
  119. JsonObject resJson = g.fromJson(res, JsonObject.class);
  120. double value = resJson.get("result").getAsDouble();
  121. return value;
  122. } catch (Exception e) {
  123. return 0.0;
  124. }
  125. }
  126.  
  127. public double sqrt(double squaret) {
  128. try {
  129. javax.ws.rs.client.Client client = ClientBuilder.newClient()
  130. .register(new ClientAuthentication(user, password));
  131. WebTarget target = client.target(uri);
  132. target = target.path("square").queryParam("sqrt", squaret);
  133. Invocation.Builder builder = target.request();
  134. Response jsonString = builder.get();
  135. String res = jsonString.readEntity(String.class);
  136. Gson g = new Gson();
  137. JsonObject resJson = g.fromJson(res, JsonObject.class);
  138. double value = resJson.get("result").getAsDouble();
  139. return value;
  140. } catch (Exception e) {
  141. return 0.0;
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement