Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ClientAuthentication.
- ---------------------------
- package client;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import javax.ws.rs.client.ClientRequestContext;
- import javax.ws.rs.client.ClientRequestFilter;
- import javax.ws.rs.core.MultivaluedMap;
- import javax.xml.bind.DatatypeConverter;
- public class ClientAuthentication implements ClientRequestFilter {
- private final String user;
- private final String password;
- public ClientAuthentication(String user, String password) {
- this.user = user;
- this.password = password;
- }
- public void filter(ClientRequestContext requestContext) throws IOException {
- MultivaluedMap<String, Object> headers = requestContext.getHeaders();
- final String basicAuthentication = getBasicAuthentication();
- headers.add("Authorization", basicAuthentication);
- }
- private String getBasicAuthentication() {
- String token = this.user + ":" + this.password;
- try {
- return "BASIC " + DatatypeConverter.printBase64Binary(token.getBytes("UTF-8"));
- } catch (UnsupportedEncodingException ex) {
- throw new IllegalStateException("Cannot encode with UTF-8", ex);
- }
- }
- }
- *******************************************************
- *******************************************************
- Client.
- --------
- package client;
- import javax.json.Json;
- import javax.ws.rs.client.ClientBuilder;
- import javax.ws.rs.client.Invocation;
- import javax.ws.rs.client.WebTarget;
- import javax.ws.rs.core.Response;
- import com.google.gson.Gson;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- public class Client {
- private String uri;
- private String user;
- private String password;
- public Client(String uri, String user, String password) {
- this.uri = uri;
- this.user = user;
- this.password = password;
- }
- public String getUri() {
- return uri;
- }
- public void setUri(String uri) {
- this.uri = uri;
- }
- public String getUser() {
- return user;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public double add(double one, double two) {
- try {
- javax.ws.rs.client.Client client = ClientBuilder.newClient()
- .register(new ClientAuthentication(user, password));
- WebTarget target = client.target(uri);
- target = target.path("add").queryParam("num1", one).queryParam("num2", two);
- Invocation.Builder builder = target.request();
- Response jsonString = builder.get();
- String res = jsonString.readEntity(String.class);
- Gson g = new Gson();
- JsonObject resJson = g.fromJson(res, JsonObject.class);
- double value = resJson.get("result").getAsDouble();
- return value;
- } catch (Exception e) {
- return 0.0;
- }
- }
- public double multiply(double one, double two) {
- try {
- javax.ws.rs.client.Client client = ClientBuilder.newClient()
- .register(new ClientAuthentication(user, password));
- WebTarget target = client.target(uri);
- target = target.path("multiply").queryParam("num1", one).queryParam("num2", two);
- Invocation.Builder builder = target.request();
- Response jsonString = builder.get();
- String res = jsonString.readEntity(String.class);
- Gson g = new Gson();
- JsonObject resJson = g.fromJson(res, JsonObject.class);
- double value = resJson.get("result").getAsDouble();
- return value;
- } catch (Exception e) {
- return 0.0;
- }
- }
- public double sqrt(double squaret) {
- try {
- javax.ws.rs.client.Client client = ClientBuilder.newClient()
- .register(new ClientAuthentication(user, password));
- WebTarget target = client.target(uri);
- target = target.path("square").queryParam("sqrt", squaret);
- Invocation.Builder builder = target.request();
- Response jsonString = builder.get();
- String res = jsonString.readEntity(String.class);
- Gson g = new Gson();
- JsonObject resJson = g.fromJson(res, JsonObject.class);
- double value = resJson.get("result").getAsDouble();
- return value;
- } catch (Exception e) {
- return 0.0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement