source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationResource.java @ 65:c0dd5314bada

Last change on this file since 65:c0dd5314bada was 65:c0dd5314bada, checked in by casties, 11 years ago

deal with special characters in urls.

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