1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.properties.admin.resources.representation.share;
17
18 import java.net.URI;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.ws.rs.core.UriBuilder;
23 import javax.ws.rs.core.UriInfo;
24
25 import de.smartics.properties.admin.domain.model.ApiServices;
26 import de.smartics.properties.admin.domain.model.AppRelations;
27 import de.smartics.properties.admin.resources.controller.ApiResource;
28 import de.smartics.properties.admin.resources.controller.PropertyDescriptorResource;
29 import de.smartics.properties.admin.resources.controller.PropertyKeysResource;
30 import de.smartics.properties.admin.resources.controller.PropertyResource;
31 import de.smartics.resteasy.hypermedia.relations.Relations;
32 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor;
33
34
35
36
37 public final class BreadcrumbHelper
38 {
39
40
41
42
43
44
45
46
47
48 private final LinkDescriptorFactory linkDescriptorFactory;
49
50
51
52
53 private final String method;
54
55
56
57
58
59
60
61
62
63
64
65 public BreadcrumbHelper(final LinkDescriptorFactory linkDescriptorFactory,
66 final String method)
67 {
68 this.linkDescriptorFactory = linkDescriptorFactory;
69 this.method = method;
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 public LinkDescriptor service(final ApiServices.ServiceDescriptor descriptor)
90 {
91 final LinkDescriptor desc =
92 linkDescriptorFactory.createDescriptor(descriptor, method);
93 return desc;
94 }
95
96
97
98
99
100
101 public List<LinkDescriptor> home()
102 {
103 final UriInfo uriInfo = linkDescriptorFactory.getUriInfo();
104 final String full = uriInfo.getBaseUri().toString();
105 final int index = full.lastIndexOf("/api");
106 final UriBuilder builder = UriBuilder.fromPath(full.substring(0, index));
107 final URI uri = builder.build();
108 final LinkDescriptor desc =
109 linkDescriptorFactory.createDescriptor("home", uri);
110 desc.addRels(Relations.HOME);
111
112 final List<LinkDescriptor> links = new ArrayList<LinkDescriptor>();
113 links.add(desc);
114 return links;
115 }
116
117
118
119
120
121
122
123 public List<LinkDescriptor> api()
124 {
125 final UriInfo uriInfo = linkDescriptorFactory.getUriInfo();
126 final UriBuilder builder = uriInfo.getBaseUriBuilder();
127 final URI uri = builder.build();
128 final LinkDescriptor desc =
129 linkDescriptorFactory.createDescriptor("api", uri);
130 desc.addRels(AppRelations.API_HOME);
131
132 final List<LinkDescriptor> links = home();
133 links.add(desc);
134 return links;
135 }
136
137
138
139
140
141
142
143 public List<LinkDescriptor> descriptors()
144 {
145 final LinkDescriptor desc =
146 linkDescriptorFactory.createDescriptor(ApiResource.DESCRIPTORS, method);
147 desc.addRels(AppRelations.DESCRIPTORS);
148
149 final List<LinkDescriptor> links = api();
150 adjust(links);
151 links.add(desc);
152
153 return links;
154 }
155
156
157
158
159
160
161
162
163 public List<LinkDescriptor> descriptor(final String propertyKey)
164 {
165 final LinkDescriptor desc =
166 linkDescriptorFactory.createDescriptor("descriptor",
167 PropertyDescriptorResource.class, method, propertyKey);
168 desc.setShortLabel(propertyKey);
169 desc.addRels(AppRelations.DESCRIPTOR);
170
171 final List<LinkDescriptor> links = descriptors();
172 adjust(links);
173 links.add(desc);
174
175 return links;
176 }
177
178 private static void adjust(final List<LinkDescriptor> links)
179 {
180 for (final LinkDescriptor link : links)
181 {
182 link.addRels(Relations.UP);
183 }
184 }
185
186
187
188
189
190
191
192 public static List<LinkDescriptor> adjustSelf(final List<LinkDescriptor> links)
193 {
194 final int lastIndex = links.size() - 1;
195 if (lastIndex >= 0)
196 {
197 final LinkDescriptor link = links.get(lastIndex);
198 link.addRels(Relations.SELF);
199 }
200
201 return links;
202 }
203
204
205
206
207
208
209
210 public List<LinkDescriptor> configurations()
211 {
212 final LinkDescriptor desc =
213 linkDescriptorFactory.createDescriptor(ApiResource.CONFIGURATIONS,
214 method);
215 desc.addRels(AppRelations.CONFIGURATIONS);
216
217 final List<LinkDescriptor> links = api();
218 adjust(links);
219 links.add(desc);
220
221 return links;
222 }
223
224
225
226
227
228
229
230
231 public List<LinkDescriptor> configuration(final String configurationKey)
232 {
233 final LinkDescriptor desc =
234 linkDescriptorFactory.createDescriptor("configuration",
235 PropertyKeysResource.class, method, configurationKey);
236 desc.addRels(AppRelations.CONFIGURATION);
237 desc.setShortLabel(configurationKey);
238
239 final List<LinkDescriptor> links = configurations();
240 adjust(links);
241 links.add(desc);
242
243 return links;
244 }
245
246
247
248
249
250
251
252
253 public List<LinkDescriptor> property(final String configurationKey,
254 final String propertyKey)
255 {
256 final LinkDescriptor desc =
257 linkDescriptorFactory.createDescriptor("property",
258 PropertyResource.class, method, configurationKey, propertyKey);
259 desc.addRels(AppRelations.PROPERTY);
260 desc.setShortLabel(propertyKey);
261
262 final List<LinkDescriptor> links = configuration(configurationKey);
263 adjust(links);
264 links.add(desc);
265
266 return links;
267 }
268
269
270
271
272
273
274
275 public List<LinkDescriptor> security()
276 {
277 final LinkDescriptor desc =
278 linkDescriptorFactory.createDescriptor(ApiResource.SECURITY, method);
279 desc.addRels(AppRelations.SECURITY);
280
281 final List<LinkDescriptor> links = api();
282 adjust(links);
283 links.add(desc);
284
285 return links;
286 }
287
288
289
290
291
292
293
294 public List<LinkDescriptor> jndi()
295 {
296 final LinkDescriptor desc =
297 linkDescriptorFactory.createDescriptor(ApiResource.JNDI, method);
298 desc.addRels(AppRelations.JNDI);
299
300 final List<LinkDescriptor> links = api();
301 adjust(links);
302 links.add(desc);
303
304 return links;
305 }
306
307
308
309 }