1 | /** |
---|
2 | * |
---|
3 | */ |
---|
4 | package de.mpiwg.itgroup.annotations.restlet.annotations_ui; |
---|
5 | |
---|
6 | /* |
---|
7 | * #%L |
---|
8 | * AnnotationManager |
---|
9 | * %% |
---|
10 | * Copyright (C) 2012 - 2014 MPIWG Berlin |
---|
11 | * %% |
---|
12 | * This program is free software: you can redistribute it and/or modify |
---|
13 | * it under the terms of the GNU Lesser General Public License as |
---|
14 | * published by the Free Software Foundation, either version 3 of the |
---|
15 | * License, or (at your option) any later version. |
---|
16 | * |
---|
17 | * This program is distributed in the hope that it will be useful, |
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
20 | * GNU General Lesser Public License for more details. |
---|
21 | * |
---|
22 | * You should have received a copy of the GNU General Lesser Public |
---|
23 | * License along with this program. If not, see |
---|
24 | * <http://www.gnu.org/licenses/lgpl-3.0.html>. |
---|
25 | * #L% |
---|
26 | */ |
---|
27 | |
---|
28 | import java.io.UnsupportedEncodingException; |
---|
29 | import java.net.URLDecoder; |
---|
30 | |
---|
31 | import org.apache.log4j.Logger; |
---|
32 | import org.restlet.data.Form; |
---|
33 | import org.restlet.data.MediaType; |
---|
34 | import org.restlet.data.Reference; |
---|
35 | import org.restlet.data.Status; |
---|
36 | import org.restlet.representation.Representation; |
---|
37 | import org.restlet.representation.StringRepresentation; |
---|
38 | import org.restlet.resource.Delete; |
---|
39 | import org.restlet.resource.Get; |
---|
40 | import org.restlet.resource.Put; |
---|
41 | import org.restlet.resource.ResourceException; |
---|
42 | import org.restlet.resource.ServerResource; |
---|
43 | |
---|
44 | import de.mpiwg.itgroup.annotations.Group; |
---|
45 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
46 | import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; |
---|
47 | |
---|
48 | /** |
---|
49 | * Resource class for a single group. |
---|
50 | * |
---|
51 | * @author casties |
---|
52 | * |
---|
53 | */ |
---|
54 | public class GroupResource extends ServerResource { |
---|
55 | |
---|
56 | public static Logger logger = Logger.getLogger(GroupResource.class); |
---|
57 | |
---|
58 | protected AnnotationStore store; |
---|
59 | |
---|
60 | protected String requestId; |
---|
61 | |
---|
62 | protected Group group; |
---|
63 | |
---|
64 | @Override |
---|
65 | protected void doInit() throws ResourceException { |
---|
66 | super.doInit(); |
---|
67 | // id from URI /annotations/groups/{id} |
---|
68 | requestId = (String) getRequest().getAttributes().get("id"); |
---|
69 | logger.debug("group-id=" + requestId); |
---|
70 | // get store instance |
---|
71 | if (store == null) { |
---|
72 | store = ((BaseRestlet) getApplication()).getAnnotationStore(); |
---|
73 | } |
---|
74 | // get group from store |
---|
75 | if (requestId != null) { |
---|
76 | // URL decode |
---|
77 | try { |
---|
78 | requestId = URLDecoder.decode(requestId, "UTF-8"); |
---|
79 | } catch (UnsupportedEncodingException e) { |
---|
80 | // this shouldn't happen |
---|
81 | } |
---|
82 | group = (Group) store.getActor(new Group(requestId)); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * GET with HTML content type. Shows the group. |
---|
88 | * |
---|
89 | * @param entity |
---|
90 | * @return |
---|
91 | */ |
---|
92 | @Get("html") |
---|
93 | public Representation doGetHTML(Representation entity) { |
---|
94 | if (group == null) { |
---|
95 | // invalid id |
---|
96 | setStatus(Status.CLIENT_ERROR_NOT_FOUND); |
---|
97 | return null; |
---|
98 | } |
---|
99 | String result = null; |
---|
100 | // get form parameter |
---|
101 | Form f = this.getQuery(); |
---|
102 | String form = f.getFirstValue("form"); |
---|
103 | if (form != null && form.equals("edit")) { |
---|
104 | // output edit form |
---|
105 | result = "<html><body>\n"; |
---|
106 | result += String.format("<h1>Edit group %s</h1>\n", group.getId()); |
---|
107 | result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef()); |
---|
108 | // tunnel PUT method through POST |
---|
109 | result += String.format("<form method=\"post\" action=\"%s?method=PUT\">\n", this.getReference().getHierarchicalPart()); |
---|
110 | result += "<table>"; |
---|
111 | result += String.format("<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\" value=\"%s\"/></td></tr>\n", |
---|
112 | group.getName()); |
---|
113 | result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n", |
---|
114 | group.getUriString()); |
---|
115 | result += "</table>\n"; |
---|
116 | result += "<p><input type=\"submit\"/></p>"; |
---|
117 | result += "</table>\n</form>\n</body>\n</html>"; |
---|
118 | } else { |
---|
119 | // output group content |
---|
120 | result = "<html><body>\n<h1>Group</h1>\n"; |
---|
121 | result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef()); |
---|
122 | result += "<table>"; |
---|
123 | result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId()); |
---|
124 | result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName()); |
---|
125 | result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri()); |
---|
126 | result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this |
---|
127 | .getReference().addSegment("members")); |
---|
128 | result += "</table>\n"; |
---|
129 | result += "<p><a href=\"?form=edit\">Edit group</a></p>\n"; |
---|
130 | // tunnel POST as DELETE |
---|
131 | result += String.format( |
---|
132 | "<form method=\"post\" action=\"%s?method=DELETE\"><input type=\"submit\" value=\"Delete group\"/></form>\n", |
---|
133 | this.getReference().getHierarchicalPart()); |
---|
134 | result += "</body>\n</html>"; |
---|
135 | } |
---|
136 | return new StringRepresentation(result, MediaType.TEXT_HTML); |
---|
137 | } |
---|
138 | |
---|
139 | /** |
---|
140 | * PUT updates the group. |
---|
141 | * |
---|
142 | * @param entity |
---|
143 | * @return |
---|
144 | */ |
---|
145 | @Put |
---|
146 | public Representation doPut(Representation entity) { |
---|
147 | logger.debug("GroupResource.doPut!"); |
---|
148 | if (group == null) { |
---|
149 | // invalid id |
---|
150 | setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
---|
151 | return null; |
---|
152 | } |
---|
153 | // TODO: do authentication |
---|
154 | Form form = new Form(entity); |
---|
155 | String name = form.getFirstValue("name"); |
---|
156 | String uri = form.getFirstValue("uri"); |
---|
157 | if (name != null && !name.isEmpty()) { |
---|
158 | group.setName(name); |
---|
159 | } |
---|
160 | if (uri != null && !uri.isEmpty()) { |
---|
161 | group.setUri(uri); |
---|
162 | } |
---|
163 | store.storeActor(group); |
---|
164 | // return 303: see other |
---|
165 | setStatus(Status.REDIRECTION_SEE_OTHER); |
---|
166 | // go GET same URL |
---|
167 | Reference url = this.getReference(); |
---|
168 | this.getResponse().setLocationRef(url); |
---|
169 | return null; |
---|
170 | } |
---|
171 | |
---|
172 | /** |
---|
173 | * DELETE deletes the group. |
---|
174 | * |
---|
175 | * @param entity |
---|
176 | * @return |
---|
177 | */ |
---|
178 | @Delete |
---|
179 | public Representation doDelete(Representation entity) { |
---|
180 | logger.debug("GroupResource.doDelete!"); |
---|
181 | if (group == null) { |
---|
182 | // invalid id |
---|
183 | setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
---|
184 | return null; |
---|
185 | } |
---|
186 | // TODO: do authentication |
---|
187 | store.deleteActor(group); |
---|
188 | // return 303: see other |
---|
189 | setStatus(Status.REDIRECTION_SEE_OTHER); |
---|
190 | // go GET parent URL |
---|
191 | Reference url = this.getReference().getParentRef(); |
---|
192 | this.getResponse().setLocationRef(url); |
---|
193 | return null; |
---|
194 | } |
---|
195 | } |
---|