source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonResource.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: 5.9 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.Person;
23import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
24import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
25
26/**
27 * Resource class for a single person.
28 *
29 * @author casties
30 *
31 */
32public class PersonResource extends ServerResource {
33
34    public static Logger logger = Logger.getLogger(PersonResource.class);
35
36    protected AnnotationStore store;
37
38    protected String requestId;
39
40    protected Person person;
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("person-id=" + requestId);
48        // get store instance
49        if (store == null) {
50            store = ((BaseRestlet) getApplication()).getAnnotationStore();
51        }
52        // get person 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            person = (Person) store.getActor(new Person(requestId));
61        }
62    }
63
64    /**
65     * GET with HTML content type. Shows the person.
66     *
67     * @param entity
68     * @return
69     */
70    @Get("html")
71    public Representation doGetHTML(Representation entity) {
72        if (person == 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 person %s</h1>\n", person.getId());
85            result += String.format("<p><a href=\"%s\">All persons</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                    person.getName());
91            result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n",
92                    person.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 person content
98            result = "<html><body>\n<h1>Person</h1>\n";
99            result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference().getParentRef());
100            result += "<table>";
101            result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", person.getId());
102            result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", person.getName());
103            result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", person.getUri());
104            result += "</table>\n";
105            result += "<p><a href=\"?form=edit\">Edit person</a></p>\n";
106            // tunnel POST as DELETE
107            result += String.format(
108                    "<form method=\"post\" action=\"%s?method=DELETE\"><input type=\"submit\" value=\"Delete person\"/></form>\n",
109                    this.getReference().getHierarchicalPart());
110            result += "</body>\n</html>";
111        }
112        return new StringRepresentation(result, MediaType.TEXT_HTML);
113    }
114
115    /**
116     * PUT updates the person.
117     *
118     * @param entity
119     * @return
120     */
121    @Put
122    public Representation doPut(Representation entity) {
123        logger.debug("PersonResource.doPut!");
124        if (person == null) {
125            // invalid id
126            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
127            return null;
128        }
129        // TODO: do authentication
130        Form form = new Form(entity);
131        String name = form.getFirstValue("name");
132        String uri = form.getFirstValue("uri");
133        if (name != null && !name.isEmpty()) {
134            person.setName(name);
135        }
136        if (uri != null && !uri.isEmpty()) {
137            person.setUri(uri);
138        }
139        store.storeActor(person);
140        // return 303: see other
141        setStatus(Status.REDIRECTION_SEE_OTHER);
142        // go GET same URL
143        Reference url = this.getReference();
144        this.getResponse().setLocationRef(url);
145        return null;
146    }
147
148    /**
149     * DELETE deletes the person.
150     *
151     * @param entity
152     * @return
153     */
154    @Delete
155    public Representation doDelete(Representation entity) {
156        logger.debug("PersonResource.doDelete!");
157        if (person == null) {
158            // invalid id
159            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
160            return null;
161        }
162        // TODO: do authentication
163        store.deleteActor(person);
164        // return 303: see other
165        setStatus(Status.REDIRECTION_SEE_OTHER);
166        // go GET parent URL
167        Reference url = this.getReference().getParentRef();
168        this.getResponse().setLocationRef(url);
169        return null;
170    }
171}
Note: See TracBrowser for help on using the repository browser.