1 | /** |
---|
2 | * |
---|
3 | */ |
---|
4 | package de.mpiwg.itgroup.annotations.restlet.annotations_ui; |
---|
5 | |
---|
6 | import java.io.UnsupportedEncodingException; |
---|
7 | import java.net.URLDecoder; |
---|
8 | |
---|
9 | import org.apache.log4j.Logger; |
---|
10 | import org.restlet.data.Form; |
---|
11 | import org.restlet.data.MediaType; |
---|
12 | import org.restlet.data.Reference; |
---|
13 | import org.restlet.data.Status; |
---|
14 | import org.restlet.representation.Representation; |
---|
15 | import org.restlet.representation.StringRepresentation; |
---|
16 | import org.restlet.resource.Delete; |
---|
17 | import org.restlet.resource.Get; |
---|
18 | import org.restlet.resource.Put; |
---|
19 | import org.restlet.resource.ResourceException; |
---|
20 | import org.restlet.resource.ServerResource; |
---|
21 | |
---|
22 | import de.mpiwg.itgroup.annotations.Group; |
---|
23 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
24 | import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; |
---|
25 | |
---|
26 | /** |
---|
27 | * Resource class for a single group. |
---|
28 | * |
---|
29 | * @author casties |
---|
30 | * |
---|
31 | */ |
---|
32 | public class GroupResource extends ServerResource { |
---|
33 | |
---|
34 | public static Logger logger = Logger.getLogger(GroupResource.class); |
---|
35 | |
---|
36 | protected AnnotationStore store; |
---|
37 | |
---|
38 | protected String requestId; |
---|
39 | |
---|
40 | protected Group group; |
---|
41 | |
---|
42 | @Override |
---|
43 | protected void doInit() throws ResourceException { |
---|
44 | super.doInit(); |
---|
45 | // id from URI /annotations/groups/{id} |
---|
46 | requestId = (String) getRequest().getAttributes().get("id"); |
---|
47 | logger.debug("group-id=" + requestId); |
---|
48 | // get store instance |
---|
49 | if (store == null) { |
---|
50 | store = ((BaseRestlet) getApplication()).getAnnotationStore(); |
---|
51 | } |
---|
52 | // get group from store |
---|
53 | if (requestId != null) { |
---|
54 | // URL decode |
---|
55 | try { |
---|
56 | requestId = URLDecoder.decode(requestId, "UTF-8"); |
---|
57 | } catch (UnsupportedEncodingException e) { |
---|
58 | // this shouldn't happen |
---|
59 | } |
---|
60 | group = (Group) store.getActor(new Group(requestId)); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * GET with HTML content type. Shows the group. |
---|
66 | * |
---|
67 | * @param entity |
---|
68 | * @return |
---|
69 | */ |
---|
70 | @Get("html") |
---|
71 | public Representation doGetHTML(Representation entity) { |
---|
72 | if (group == null) { |
---|
73 | // invalid id |
---|
74 | setStatus(Status.CLIENT_ERROR_NOT_FOUND); |
---|
75 | return null; |
---|
76 | } |
---|
77 | String result = null; |
---|
78 | // get form parameter |
---|
79 | Form f = this.getQuery(); |
---|
80 | String form = f.getFirstValue("form"); |
---|
81 | if (form != null && form.equals("edit")) { |
---|
82 | // output edit form |
---|
83 | result = "<html><body>\n"; |
---|
84 | result += String.format("<h1>Edit group %s</h1>\n", group.getId()); |
---|
85 | result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef()); |
---|
86 | // tunnel PUT method through POST |
---|
87 | result += String.format("<form method=\"post\" action=\"%s?method=PUT\">\n", this.getReference().getHierarchicalPart()); |
---|
88 | result += "<table>"; |
---|
89 | result += String.format("<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\" value=\"%s\"/></td></tr>\n", |
---|
90 | group.getName()); |
---|
91 | result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n", |
---|
92 | group.getUriString()); |
---|
93 | result += "</table>\n"; |
---|
94 | result += "<p><input type=\"submit\"/></p>"; |
---|
95 | result += "</table>\n</form>\n</body>\n</html>"; |
---|
96 | } else { |
---|
97 | // output group content |
---|
98 | result = "<html><body>\n<h1>Group</h1>\n"; |
---|
99 | result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef()); |
---|
100 | result += "<table>"; |
---|
101 | result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId()); |
---|
102 | result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName()); |
---|
103 | result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri()); |
---|
104 | result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this |
---|
105 | .getReference().addSegment("members")); |
---|
106 | result += "</table>\n"; |
---|
107 | result += "<p><a href=\"?form=edit\">Edit group</a></p>\n"; |
---|
108 | // tunnel POST as DELETE |
---|
109 | result += String.format( |
---|
110 | "<form method=\"post\" action=\"%s?method=DELETE\"><input type=\"submit\" value=\"Delete group\"/></form>\n", |
---|
111 | this.getReference().getHierarchicalPart()); |
---|
112 | result += "</body>\n</html>"; |
---|
113 | } |
---|
114 | return new StringRepresentation(result, MediaType.TEXT_HTML); |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * PUT updates the group. |
---|
119 | * |
---|
120 | * @param entity |
---|
121 | * @return |
---|
122 | */ |
---|
123 | @Put |
---|
124 | public Representation doPut(Representation entity) { |
---|
125 | logger.debug("GroupResource.doPut!"); |
---|
126 | if (group == null) { |
---|
127 | // invalid id |
---|
128 | setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
---|
129 | return null; |
---|
130 | } |
---|
131 | // TODO: do authentication |
---|
132 | Form form = new Form(entity); |
---|
133 | String name = form.getFirstValue("name"); |
---|
134 | String uri = form.getFirstValue("uri"); |
---|
135 | if (name != null && !name.isEmpty()) { |
---|
136 | group.setName(name); |
---|
137 | } |
---|
138 | if (uri != null && !uri.isEmpty()) { |
---|
139 | group.setUri(uri); |
---|
140 | } |
---|
141 | store.storeActor(group); |
---|
142 | // return 303: see other |
---|
143 | setStatus(Status.REDIRECTION_SEE_OTHER); |
---|
144 | // go GET same URL |
---|
145 | Reference url = this.getReference(); |
---|
146 | this.getResponse().setLocationRef(url); |
---|
147 | return null; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * DELETE deletes the group. |
---|
152 | * |
---|
153 | * @param entity |
---|
154 | * @return |
---|
155 | */ |
---|
156 | @Delete |
---|
157 | public Representation doDelete(Representation entity) { |
---|
158 | logger.debug("GroupResource.doDelete!"); |
---|
159 | if (group == null) { |
---|
160 | // invalid id |
---|
161 | setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
---|
162 | return null; |
---|
163 | } |
---|
164 | // TODO: do authentication |
---|
165 | store.deleteActor(group); |
---|
166 | // return 303: see other |
---|
167 | setStatus(Status.REDIRECTION_SEE_OTHER); |
---|
168 | // go GET parent URL |
---|
169 | Reference url = this.getReference().getParentRef(); |
---|
170 | this.getResponse().setLocationRef(url); |
---|
171 | return null; |
---|
172 | } |
---|
173 | } |
---|