1 package info.ejava.examples.secureping.client;
2
3 import java.net.URI;
4
5 import javax.ws.rs.client.Client;
6 import javax.ws.rs.core.Response;
7 import javax.ws.rs.core.UriBuilder;
8
9 public class SecurePingJaxRsClient {
10 private Client client;
11 private URI baseUrl;
12
13 public SecurePingJaxRsClient(Client client, URI baseUrl) {
14 this.client = client;
15 this.baseUrl = baseUrl;
16 }
17
18 public Response whoAmI() {
19 URI uri = getURI(null, "whoAmI");
20 return client.target(uri).request().get();
21 }
22 public Response isCallerInRole(String role) {
23 URI uri = getURI(null, "roles/" + role);
24 return client.target(uri).request().get();
25 }
26
27 public Response ping(String context, String targetResource) {
28 URI uri = getURI(context, targetResource);
29 return client.target(uri)
30 .request()
31 .buildGet()
32 .invoke();
33 }
34
35 public Response pingAdmin(String context) {
36 return ping(context, "pingAdmin");
37 }
38 public Response pingUser(String context) {
39 return ping(context, "pingUser");
40 }
41 public Response pingAll(String context) {
42 return ping(context, "pingAll");
43 }
44
45 private URI getURI(String context, String targetResource) {
46 UriBuilder b = UriBuilder.fromUri(baseUrl).path("ping");
47 if (context!=null) {
48 b.path(context);
49 }
50 b.path(targetResource);
51 return b.build();
52 }
53
54 }