-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElasticClientResource.java
executable file
·59 lines (52 loc) · 2.44 KB
/
ElasticClientResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package dev.dropwizard.bundler.elastic;
import dev.dropwizard.bundler.refmodel.RefModelBundle;
import javax.inject.Inject;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import java.util.Collection;
import java.util.List;
/**
*
*/
@Path("elastic/client")
public class ElasticClientResource {
@Inject MapperHelper mapper;
@Inject ElasticClient elasticClient;
@Inject RefModelBundle refModelBundle;
@GET
@Path("{model}/{id}")
@Produces("application/json")
public Response get(@PathParam("model") String model, @PathParam("id") String id) {
Class<?> modelClass = refModelBundle.getModel(Elastic.class, model);
if (modelClass == null) return Response.status(Response.Status.BAD_REQUEST).build();
Object entity = elasticClient.get(modelClass, id);
return entity != null ? Response.ok(entity).build() : Response.noContent().build();
}
@PUT
@Path("{model}")
@Consumes("application/json")
public <T> Response put(@PathParam("model") String model, Object object) {
Class<?> modelClass = refModelBundle.getModel(Elastic.class, model);
if (modelClass == null) return Response.status(Response.Status.BAD_REQUEST).build();
String value = mapper.write(object); //map to string
T t = (T) mapper.read(modelClass, value); //string to object
boolean added = elasticClient.put(t);
return Response.status(added ? Response.Status.CREATED : Response.Status.NOT_MODIFIED).build();
}
@GET
@Path("{model}/{property}/{value}")
@Produces("application/json")
public Response searchByProperty(@PathParam("model") String model, @PathParam("property") String property,
@PathParam("value") String value) {
Class<?> modelClass = refModelBundle.getModel(Elastic.class, model);
if (modelClass == null) return Response.status(Response.Status.BAD_REQUEST).build();
Collection<String> properties = refModelBundle.getPropertyMap().get(modelClass);
if (!properties.contains(property)) {
return Response.status(Response.Status.BAD_REQUEST).
entity("Property '" + property + "' is invalid. Use one of " + refModelBundle.getRefModelKeys(modelClass)).
build();
}
List<?> result = elasticClient.getByProperty(modelClass, property, value);
return !result.isEmpty() ? Response.ok(result).build() : Response.noContent().build();
}
}