1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.exoplatform.calendar.ws;
19
20 import javax.ws.rs.Path;
21 import javax.ws.rs.core.UriInfo;
22
23 import java.lang.reflect.Method;
24 import java.net.URI;
25 import java.util.LinkedList;
26 import java.util.List;
27
28 import org.exoplatform.services.rest.resource.ResourceContainer;
29
30 public class SubResourceHrefBuilder {
31 private List<String> subResources = new LinkedList<String>();
32
33 public SubResourceHrefBuilder(ResourceContainer restService) {
34 subResources = getResourcesInfo(restService);
35 }
36
37 public String[] buildResourceMap(UriInfo uriInfo) {
38 List<String> resources = new LinkedList<String>();
39
40 StringBuilder uriBuilder = new StringBuilder();
41 URI uri = uriInfo.getBaseUri();
42 uriBuilder.append(uri.getScheme()).append("://");
43 uriBuilder.append(uri.getHost());
44 if (uri.getPort() != 80 && uri.getPort() != 443) {
45 uriBuilder.append(":").append(uri.getPort());
46 }
47 uriBuilder.append(uri.getPath());
48 String base = uriBuilder.toString();
49
50 for (String rs : subResources) {
51 resources.add(base + rs);
52 }
53 return resources.toArray(new String[resources.size()]);
54 }
55
56 private List<String> getResourcesInfo(ResourceContainer restService) {
57 List<String> subResources = new LinkedList<String>();
58
59 Path path = restService.getClass().getAnnotation(Path.class);
60 if (path == null) {
61 throw new IllegalStateException("base path for " + restService + " is not found");
62 }
63 String basePath = path.value();
64
65 for (Method method : restService.getClass().getMethods()) {
66 Path mPath = method.getAnnotation(Path.class);
67
68 if (mPath != null) {
69 String methodPath = mPath.value();
70
71 subResources.add(basePath + methodPath);
72 }
73 }
74
75 return subResources;
76 }
77 }