Mercurial > hg > AnnotationManagerN4J
annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationResource.java @ 105:7417f5915181 default tip
check admin permission before changing permissions.
Enum for typesafe actions.
author | casties |
---|---|
date | Fri, 10 Feb 2017 15:45:35 +0100 |
parents | 199143c669e4 |
children |
rev | line source |
---|---|
50 | 1 /** |
2 * | |
3 */ | |
4 package de.mpiwg.itgroup.annotations.restlet.annotations_ui; | |
5 | |
70 | 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 | |
65 | 28 import java.io.UnsupportedEncodingException; |
29 import java.net.URLDecoder; | |
75 | 30 import java.util.logging.Logger; |
65 | 31 |
50 | 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.Annotation; | |
45 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | |
46 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; | |
47 | |
48 /** | |
49 * Resource class for a single annotation. | |
50 * | |
51 * @author casties | |
52 * | |
53 */ | |
54 public class AnnotationResource extends ServerResource { | |
55 | |
99 | 56 public static Logger logger = Logger.getLogger(AnnotationResource.class.getCanonicalName()); |
50 | 57 |
58 protected AnnotationStore store; | |
59 | |
60 protected String requestId; | |
61 | |
62 protected Annotation annotation; | |
63 | |
64 @Override | |
65 protected void doInit() throws ResourceException { | |
66 super.doInit(); | |
95
acd44dfec9c8
added last update field for annotations in database.
casties
parents:
78
diff
changeset
|
67 // id from URI /annotations/annotation/{id} |
50 | 68 requestId = (String) getRequest().getAttributes().get("id"); |
75 | 69 logger.fine("annoation-id=" + requestId); |
50 | 70 // get store instance |
71 if (store == null) { | |
72 store = ((BaseRestlet) getApplication()).getAnnotationStore(); | |
73 } | |
74 // get annotation from store | |
75 if (requestId != null) { | |
65 | 76 // URL decode |
77 try { | |
78 requestId = URLDecoder.decode(requestId, "UTF-8"); | |
79 } catch (UnsupportedEncodingException e) { | |
80 // this shouldn't happen | |
81 } | |
50 | 82 // the ID in the path is encoded |
83 String id = Annotation.decodeId(requestId); | |
84 annotation = store.getAnnotationById(id); | |
85 } | |
86 } | |
87 | |
88 /** | |
95
acd44dfec9c8
added last update field for annotations in database.
casties
parents:
78
diff
changeset
|
89 * GET with HTML content type. Shows the annotation. |
50 | 90 * |
91 * @param entity | |
92 * @return | |
93 */ | |
94 @Get("html") | |
95 public Representation doGetHTML(Representation entity) { | |
96 if (annotation == null) { | |
97 // invalid id | |
98 setStatus(Status.CLIENT_ERROR_NOT_FOUND); | |
99 return null; | |
100 } | |
101 String result = null; | |
102 // get form parameter | |
103 Form f = this.getQuery(); | |
104 String form = f.getFirstValue("form"); | |
105 if (form != null && form.equals("edit")) { | |
106 /* | |
107 // output edit form | |
108 result = "<html><body>\n"; | |
109 result += String.format("<h1>Edit person %s</h1>\n", annotation.getId()); | |
110 result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference().getParentRef()); | |
111 // tunnel PUT method through POST | |
112 result += String.format("<form method=\"post\" action=\"%s?method=PUT\">\n", this.getReference().getHierarchicalPart()); | |
113 result += "<table>"; | |
114 result += String.format("<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\" value=\"%s\"/></td></tr>\n", | |
115 annotation.getName()); | |
116 result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n", | |
117 annotation.getUriString()); | |
118 result += "</table>\n"; | |
119 result += "<p><input type=\"submit\"/></p>"; | |
120 result += "</table>\n</form>\n</body>\n</html>"; | |
121 */ | |
122 } else { | |
95
acd44dfec9c8
added last update field for annotations in database.
casties
parents:
78
diff
changeset
|
123 // output content |
50 | 124 result = "<html><body>\n<h1>Annotation</h1>\n"; |
125 result += String.format("<p><a href=\"%s\">All annotations</a></p>", this.getReference().getParentRef()); | |
126 result += "<table>"; | |
127 result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", annotation.getUri()); | |
128 result += String.format("<tr><td><b>text</b></td><td>%s</td></tr>\n", annotation.getBodyText()); | |
99 | 129 result += String.format("<tr><td><b>target</b></td><td><a href=\"%s\" target=\"_blank\">%s</a></td></tr>\n", annotation.getTargetBaseUri(), annotation.getTargetBaseUri()); |
50 | 130 result += String.format("<tr><td><b>fragment</b></td><td>%s</td></tr>\n", annotation.getTargetFragment()); |
77 | 131 result += String.format("<tr><td><b>quote</b></td><td>%s</td></tr>\n", annotation.getQuote()); |
132 result += String.format("<tr><td><b>resource</b></td><td>%s</td></tr>\n", annotation.getResourceUri()); | |
50 | 133 result += String.format("<tr><td><b>creator</b></td><td>%s</td></tr>\n", annotation.getCreatorName()); |
95
acd44dfec9c8
added last update field for annotations in database.
casties
parents:
78
diff
changeset
|
134 result += String.format("<tr><td><b>created</b></td><td>%s</td></tr>\n", annotation.getCreated()); |
acd44dfec9c8
added last update field for annotations in database.
casties
parents:
78
diff
changeset
|
135 result += String.format("<tr><td><b>updated</b></td><td>%s</td></tr>\n", annotation.getUpdated()); |
50 | 136 result += "</table>\n"; |
78
e973cbf5d58f
more annotation attributes displayed in AnnotationResource.
casties
parents:
77
diff
changeset
|
137 result += "<br/>\n"; |
e973cbf5d58f
more annotation attributes displayed in AnnotationResource.
casties
parents:
77
diff
changeset
|
138 result += "<table>"; |
e973cbf5d58f
more annotation attributes displayed in AnnotationResource.
casties
parents:
77
diff
changeset
|
139 result += String.format("<tr><td><b>admin permission</b></td><td>%s</td></tr>\n", annotation.getAdminPermission() != null ? annotation.getAdminPermission().getIdString() : "null"); |
e973cbf5d58f
more annotation attributes displayed in AnnotationResource.
casties
parents:
77
diff
changeset
|
140 result += String.format("<tr><td><b>delete permission</b></td><td>%s</td></tr>\n", annotation.getDeletePermission() != null ? annotation.getDeletePermission().getIdString() : "null"); |
e973cbf5d58f
more annotation attributes displayed in AnnotationResource.
casties
parents:
77
diff
changeset
|
141 result += String.format("<tr><td><b>update permission</b></td><td>%s</td></tr>\n", annotation.getUpdatePermission() != null ? annotation.getUpdatePermission().getIdString() : "null"); |
e973cbf5d58f
more annotation attributes displayed in AnnotationResource.
casties
parents:
77
diff
changeset
|
142 result += String.format("<tr><td><b>read permission</b></td><td>%s</td></tr>\n", annotation.getReadPermission() != null ? annotation.getReadPermission().getIdString() : "null"); |
e973cbf5d58f
more annotation attributes displayed in AnnotationResource.
casties
parents:
77
diff
changeset
|
143 result += "</table>\n"; |
50 | 144 //result += "<p><a href=\"?form=edit\">Edit annotation</a></p>\n"; |
145 // tunnel POST as DELETE | |
146 result += String.format( | |
147 "<form method=\"post\" action=\"%s?method=DELETE\"><input type=\"submit\" value=\"Delete annotation\"/></form>\n", | |
148 this.getReference().getHierarchicalPart()); | |
149 result += "</body>\n</html>"; | |
150 } | |
151 return new StringRepresentation(result, MediaType.TEXT_HTML); | |
152 } | |
153 | |
154 /** | |
155 * PUT updates the annotation. | |
156 * | |
157 * @param entity | |
158 * @return | |
159 */ | |
160 @Put | |
161 public Representation doPut(Representation entity) { | |
75 | 162 logger.fine("AnnotationResource.doPut!"); |
50 | 163 if (annotation == null) { |
164 // invalid id | |
165 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
166 return null; | |
167 } | |
168 // NOT YET | |
169 return null; | |
170 /* | |
171 // TODO: do authentication | |
172 Form form = new Form(entity); | |
173 String name = form.getFirstValue("name"); | |
174 String uri = form.getFirstValue("uri"); | |
175 if (name != null && !name.isEmpty()) { | |
176 annotation.setName(name); | |
177 } | |
178 if (uri != null && !uri.isEmpty()) { | |
179 annotation.setUri(uri); | |
180 } | |
181 store.storeActor(annotation); | |
182 // return 303: see other | |
183 setStatus(Status.REDIRECTION_SEE_OTHER); | |
184 // go GET same URL | |
185 Reference url = this.getReference(); | |
186 this.getResponse().setLocationRef(url); | |
187 return null; | |
188 */ | |
189 } | |
190 | |
191 /** | |
192 * DELETE deletes the annotation. | |
193 * | |
194 * @param entity | |
195 * @return | |
196 */ | |
197 @Delete | |
198 public Representation doDelete(Representation entity) { | |
75 | 199 logger.fine("AnnotationResource.doDelete!"); |
50 | 200 if (annotation == null) { |
201 // invalid id | |
202 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
203 return null; | |
204 } | |
205 // TODO: do authentication | |
206 store.deleteAnnotationById(annotation.getUri()); | |
207 // return 303: see other | |
208 setStatus(Status.REDIRECTION_SEE_OTHER); | |
209 // go GET parent URL | |
210 Reference url = this.getReference().getParentRef(); | |
211 this.getResponse().setLocationRef(url); | |
212 return null; | |
213 } | |
214 } |