1 package eu.scape_project.watch.rest;
2
3 import com.sun.jersey.api.client.ClientResponse;
4 import com.sun.jersey.api.client.GenericType;
5 import com.sun.jersey.api.client.UniformInterfaceException;
6 import com.sun.jersey.api.client.WebResource;
7 import com.sun.jersey.api.client.WebResource.Builder;
8 import eu.scape_project.watch.domain.AsyncRequest;
9 import eu.scape_project.watch.domain.Entity;
10 import eu.scape_project.watch.domain.EntityType;
11 import eu.scape_project.watch.domain.Property;
12 import eu.scape_project.watch.domain.PropertyValue;
13 import eu.scape_project.watch.domain.RequestTarget;
14 import eu.scape_project.watch.utils.KBUtils;
15 import eu.scape_project.watch.utils.exception.NotFoundException;
16
17 import java.util.List;
18
19 import javax.ws.rs.core.MediaType;
20
21 import thewebsemantic.binding.RdfBean;
22
23
24
25
26
27
28
29 public class WatchClient {
30
31
32
33
34
35 private static final String FS = ".";
36
37
38
39
40 private static final String AS = "/";
41
42
43
44
45 private static final String LIST = "list";
46
47
48
49
50 private static final String START = "start";
51
52
53
54
55 private static final String MAX = "max";
56
57
58
59
60 private static final String TYPE = "type";
61
62
63
64
65 private static final String QUERY = "query";
66
67
68
69
70 private final WebResource resource;
71
72
73
74
75 private final Format format;
76
77
78
79
80
81
82
83 public static enum Format {
84
85
86
87 JSON,
88
89
90
91 XML;
92
93 @Override
94 public String toString() {
95 return super.toString().toLowerCase();
96 }
97
98
99
100
101
102
103 public String getMediaType() {
104 if (this.equals(JSON)) {
105 return MediaType.APPLICATION_JSON;
106 } else if (this.equals(XML)) {
107 return MediaType.APPLICATION_XML;
108 } else {
109 return null;
110 }
111 }
112 }
113
114
115
116
117
118
119
120
121
122 public WatchClient(final WebResource resource, final Format format) {
123 super();
124 this.resource = resource;
125 this.format = format;
126 }
127
128
129
130
131
132
133
134
135
136
137 public Entity createEntity(final String name, final String type) {
138 return this.resource.path(KBUtils.ENTITY + FS + this.format + AS + name).accept(this.format.getMediaType())
139 .post(Entity.class, type);
140 }
141
142
143
144
145
146
147
148
149 public Entity getEntity(final String name) {
150 try {
151 return this.resource.path(KBUtils.ENTITY + FS + this.format + AS + name).accept(this.format.getMediaType())
152 .get(Entity.class);
153 } catch (final UniformInterfaceException e) {
154 final ClientResponse resp = e.getResponse();
155 if (resp.getStatus() == NotFoundException.CODE) {
156 return null;
157 } else {
158 throw e;
159 }
160 }
161 }
162
163
164
165
166
167
168
169
170
171
172 public Entity updateEntity(final String name, final Entity entity) {
173 return this.resource.path(KBUtils.ENTITY + FS + this.format + AS + name).accept(this.format.getMediaType())
174 .put(Entity.class, entity);
175 }
176
177
178
179
180
181
182 public List<Entity> listEntity(int start, int max) {
183 return listEntity(null, start, max);
184 }
185
186 public List<Entity> listEntity(String type, int start, int max) {
187 return (List<Entity>) this.resource.path(KBUtils.ENTITY + FS + this.format + AS + LIST)
188 .queryParam(TYPE, type != null ? type : "").queryParam(START, Integer.toString(start))
189 .queryParam(MAX, Integer.toString(max)).accept(this.format.getMediaType()).get(new GenericType<List<Entity>>() {
190 });
191 }
192
193
194
195
196
197
198
199
200 public Entity deleteEntity(final String name) {
201 return this.resource.path(KBUtils.ENTITY + FS + this.format + AS + name).accept(this.format.getMediaType())
202 .delete(Entity.class);
203 }
204
205
206
207
208
209
210
211
212
213
214 public EntityType createEntityType(final String name, final String description) {
215 return this.resource.path(KBUtils.ENTITY_TYPE + FS + this.format + AS + name).accept(this.format.getMediaType())
216 .post(EntityType.class, description);
217 }
218
219
220
221
222
223
224
225
226 public EntityType getEntityType(final String name) {
227 try {
228 return this.resource.path(KBUtils.ENTITY_TYPE + FS + this.format + AS + name).accept(this.format.getMediaType())
229 .get(EntityType.class);
230 } catch (final UniformInterfaceException e) {
231 final ClientResponse resp = e.getResponse();
232 if (resp.getStatus() == NotFoundException.CODE) {
233 return null;
234 } else {
235 throw e;
236 }
237 }
238 }
239
240
241
242
243
244
245
246
247
248
249 public EntityType updateEntityType(final String name, final EntityType entity) {
250 return this.resource.path(KBUtils.ENTITY_TYPE + FS + this.format + AS + name).accept(this.format.getMediaType())
251 .put(EntityType.class, entity);
252 }
253
254
255
256
257
258
259 public List<EntityType> listEntityType() {
260 return (List<EntityType>) this.resource.path(KBUtils.ENTITY_TYPE + FS + this.format + AS + LIST)
261 .accept(this.format.getMediaType()).get(new GenericType<List<EntityType>>() {
262 });
263 }
264
265
266
267
268
269
270
271
272 public EntityType deleteEntityType(final String name) {
273 return this.resource.path(KBUtils.ENTITY_TYPE + FS + this.format + AS + name).accept(this.format.getMediaType())
274 .delete(EntityType.class);
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288 public Property createProperty(final String type, final String name, final String description) {
289 return this.resource.path(KBUtils.PROPERTY + FS + this.format + AS + type + AS + name)
290 .accept(this.format.getMediaType()).post(Property.class, description);
291 }
292
293
294
295
296
297
298
299
300
301
302 public Property getProperty(final String type, final String name) {
303 try {
304 return this.resource.path(KBUtils.PROPERTY + FS + this.format + AS + type + AS + name)
305 .accept(this.format.getMediaType()).get(Property.class);
306 } catch (final UniformInterfaceException e) {
307 final ClientResponse resp = e.getResponse();
308 if (resp.getStatus() == NotFoundException.CODE) {
309 return null;
310 } else {
311 throw e;
312 }
313 }
314 }
315
316
317
318
319
320
321
322
323
324
325
326
327 public Property updateProperty(final String type, final String name, final Property property) {
328 return this.resource.path(KBUtils.PROPERTY + FS + this.format + AS + type + AS + name)
329 .accept(this.format.getMediaType()).put(Property.class, property);
330 }
331
332
333
334
335
336
337 public List<Property> listProperty() {
338 return (List<Property>) this.resource.path(KBUtils.PROPERTY + FS + this.format + AS + LIST)
339 .accept(this.format.getMediaType()).get(new GenericType<List<Property>>() {
340 });
341 }
342
343
344
345
346
347
348
349
350
351
352 public Property deleteProperty(final String type, final String name) {
353 return this.resource.path(KBUtils.PROPERTY + FS + this.format + AS + type + AS + name)
354 .accept(this.format.getMediaType()).delete(Property.class);
355
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369 public PropertyValue createPropertyValue(final String entity, final String property, final String value) {
370 return this.resource.path(KBUtils.PROPERTY_VALUE + FS + this.format + AS + entity + AS + property)
371 .accept(this.format.getMediaType()).post(PropertyValue.class, value);
372 }
373
374
375
376
377
378
379
380
381
382
383 public PropertyValue getPropertyValue(final String entity, final String property) {
384 try {
385 return this.resource.path(KBUtils.PROPERTY_VALUE + FS + this.format + AS + entity + AS + property)
386 .accept(this.format.getMediaType()).get(PropertyValue.class);
387 } catch (final UniformInterfaceException e) {
388 final ClientResponse resp = e.getResponse();
389 if (resp.getStatus() == NotFoundException.CODE) {
390 return null;
391 } else {
392 throw e;
393 }
394 }
395 }
396
397
398
399
400
401
402
403
404
405
406
407
408
409 public PropertyValue updatePropertyValue(final String entity, final String property, final String value) {
410 return this.resource.path(KBUtils.PROPERTY_VALUE + FS + this.format + AS + entity + AS + property)
411 .accept(this.format.getMediaType()).put(PropertyValue.class, value);
412 }
413
414
415
416
417
418
419 public List<PropertyValue> listPropertyValue() {
420 return (List<PropertyValue>) this.resource.path(KBUtils.PROPERTY_VALUE + FS + this.format + AS + LIST)
421 .accept(this.format.getMediaType()).get(new GenericType<List<PropertyValue>>() {
422 });
423 }
424
425
426
427
428
429
430
431
432
433
434 public PropertyValue deletePropertyValue(final String entity, final String property) {
435 return this.resource.path(KBUtils.PROPERTY_VALUE + FS + this.format + AS + entity + AS + property)
436 .accept(this.format.getMediaType()).delete(PropertyValue.class);
437
438 }
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457 @SuppressWarnings("unchecked")
458 public <T extends RdfBean<T>> List<T> getRequest(final Class<T> targetClass, final String query, final int start,
459 final int max) {
460
461 final RequestTarget target = RequestTarget.getTargetByClass(targetClass);
462
463 final Builder builder = this.resource.path(KBUtils.SYNC_REQUEST + FS + this.format + AS + target)
464 .queryParam(QUERY, query).queryParam(START, Integer.toString(start)).queryParam(MAX, Integer.toString(max))
465 .accept(this.format.getMediaType());
466
467 List<? extends RdfBean<?>> ret;
468 switch (target) {
469 case ENTITY_TYPE:
470 ret = builder.get(new GenericType<List<EntityType>>() {
471 });
472 break;
473 case PROPERTY:
474 ret = builder.get(new GenericType<List<Property>>() {
475 });
476 break;
477 case ENTITY:
478 ret = builder.get(new GenericType<List<Entity>>() {
479 });
480 break;
481 case PROPERTY_VALUE:
482 ret = builder.get(new GenericType<List<PropertyValue>>() {
483 });
484 break;
485 default:
486 ret = null;
487 break;
488 }
489
490
491
492
493
494
495
496 return (List<T>) ret;
497 }
498
499
500
501
502
503
504
505
506 public AsyncRequest createAsyncRequest(final AsyncRequest request) {
507 return this.resource.path(KBUtils.ASYNC_REQUEST + FS + this.format).accept(this.format.getMediaType())
508 .post(AsyncRequest.class, request);
509 }
510 }