1 package com.atlassian.refapp.charlie;
2
3 import org.codehaus.jettison.json.JSONArray;
4
5 import javax.ws.rs.Consumes;
6 import javax.ws.rs.GET;
7 import javax.ws.rs.POST;
8 import javax.ws.rs.Path;
9 import javax.ws.rs.PathParam;
10 import javax.ws.rs.Produces;
11 import javax.ws.rs.WebApplicationException;
12 import javax.ws.rs.core.MediaType;
13 import javax.ws.rs.core.Response;
14 import java.util.List;
15
16 import static org.apache.commons.lang.StringUtils.isBlank;
17
18
19
20
21 @Path("admin")
22 @Consumes(MediaType.APPLICATION_JSON)
23 @Produces(MediaType.APPLICATION_JSON)
24 public class CharlieAdminResource {
25 private final CharlieStore store;
26
27 public CharlieAdminResource(CharlieStore store) {
28 this.store = store;
29 }
30
31
32
33
34
35
36 @GET
37 public String listCharlies() {
38 JSONArray result = new JSONArray();
39 for (String charlies : store.getCharlies()) {
40 result.put(charlies);
41 }
42
43 return result.toString();
44 }
45
46
47
48
49
50
51
52 @Path("{key}/{name}")
53 @POST
54 public void createCharlie(@PathParam("key") String key, @PathParam("name") String name) {
55 if (isBlank(key) || isBlank(name)) {
56 throw new WebApplicationException(Response.Status.BAD_REQUEST);
57 }
58
59 List<String> charlies = store.getCharlies();
60 charlies.add(key);
61 store.storeCharlies(charlies);
62 store.setCharlieName(key, name);
63 }
64 }