source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.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: 4.3 KB
Line 
1/**
2 *
3 */
4package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
5
6import java.util.List;
7
8import org.apache.log4j.Logger;
9import org.restlet.data.Form;
10import org.restlet.data.MediaType;
11import org.restlet.data.Reference;
12import org.restlet.data.Status;
13import org.restlet.representation.Representation;
14import org.restlet.representation.StringRepresentation;
15import org.restlet.resource.Get;
16import org.restlet.resource.Post;
17import org.restlet.resource.ResourceException;
18import org.restlet.resource.ServerResource;
19
20import de.mpiwg.itgroup.annotations.Actor;
21import de.mpiwg.itgroup.annotations.Person;
22import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
23import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
24
25/**
26 * Resource class for the list of annotation users (Person class).
27 *
28 * @author casties
29 *
30 */
31public class PersonsResource extends ServerResource {
32
33    public static Logger logger = Logger.getLogger(PersonsResource.class);
34
35    private AnnotationStore store;
36
37    @Override
38    protected void doInit() throws ResourceException {
39        super.doInit();
40        // get store instance
41        if (store == null) {
42            store = ((BaseRestlet) getApplication()).getAnnotationStore();
43        }
44    }
45
46    /**
47     * GET with HTML content type. Lists all persons.
48     *
49     * @param entity
50     * @return
51     */
52    @Get("html")
53    public Representation doGetHTML(Representation entity) {
54        String result = null;
55        // get form parameter
56        Form f = this.getQuery();
57        String form = f.getFirstValue("form");
58        if (form != null && form.equals("new_person")) {
59            // output new person form
60            result = "<html><body>\n";
61            result += "<h1>New person</h1>\n";
62            result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference());
63            result += String.format("<form method=\"post\" action=\"%s\">\n", this.getReference().getHierarchicalPart());
64            result += "<table>";
65            result += "<tr><td><b>id</b></td><td><input type=\"text\" name=\"id\"/></td></tr>\n";
66            result += "<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\"/></td></tr>\n";
67            result += "</table>\n";
68            result += "<p><input type=\"submit\"/></p>\n";
69            result += "</form>\n</body>\n</html>";
70        } else {
71            // list all persons
72            result = "<html><body>\n<h1>Persons</h1>\n<table>";
73            result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
74            List<Person> persons = store.getPersons("uri", "*");
75            for (Person person : persons) {
76                Reference url = this.getReference().clone();
77                url.addSegment(person.getIdString());
78                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url,
79                        person.getIdString(), person.getName(), person.getUri());
80            }
81            result += "</table>\n";
82            result += "<p><a href=\"?form=new_person\">Add new person</a></p>\n";
83            result += "</body>\n</html>";
84        }
85        return new StringRepresentation(result, MediaType.TEXT_HTML);
86    }
87
88    /**
89     * POST creates a new person.
90     *
91     * @return
92     */
93    @Post
94    public Representation doPost(Representation entity) {
95        logger.debug("PersonsResource doPost!");
96        // TODO: do authentication
97        Form form = new Form(entity);
98        String id = form.getFirstValue("id");
99        if (id == null || id.isEmpty() || id.matches("\\W")) {
100            // invalid id
101            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
102            return null;
103        }
104        String name = form.getFirstValue("name");
105        if (name == null || name.isEmpty()) {
106            name = ((BaseRestlet) getApplication()).getFullNameFromLdap(id);
107        }
108        String uri = form.getFirstValue("uri");
109        if (uri != null && uri.isEmpty()) uri = null;
110        Person newPerson = new Person(id, uri, name);
111        Actor storedPerson = store.storeActor(newPerson);
112        id = storedPerson.getId();
113        // return 303: see other
114        setStatus(Status.REDIRECTION_SEE_OTHER);
115        // go GET URL for this person
116        Reference url = this.getReference().clone();
117        url.addSegment(id);
118        this.getResponse().setLocationRef(url);
119        return null;
120    }
121
122}
Note: See TracBrowser for help on using the repository browser.