/** * */ package de.mpiwg.itgroup.annotations.restlet.annotations_ui; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import org.apache.log4j.Logger; import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Reference; import org.restlet.data.Status; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.resource.Delete; import org.restlet.resource.Get; import org.restlet.resource.Put; import org.restlet.resource.ResourceException; import org.restlet.resource.ServerResource; import de.mpiwg.itgroup.annotations.Group; import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; /** * Resource class for a single group. * * @author casties * */ public class GroupResource extends ServerResource { public static Logger logger = Logger.getLogger(GroupResource.class); protected AnnotationStore store; protected String requestId; protected Group group; @Override protected void doInit() throws ResourceException { super.doInit(); // id from URI /annotations/groups/{id} requestId = (String) getRequest().getAttributes().get("id"); logger.debug("group-id=" + requestId); // get store instance if (store == null) { store = ((BaseRestlet) getApplication()).getAnnotationStore(); } // get group from store if (requestId != null) { // URL decode try { requestId = URLDecoder.decode(requestId, "UTF-8"); } catch (UnsupportedEncodingException e) { // this shouldn't happen } group = (Group) store.getActor(new Group(requestId)); } } /** * GET with HTML content type. Shows the group. * * @param entity * @return */ @Get("html") public Representation doGetHTML(Representation entity) { if (group == null) { // invalid id setStatus(Status.CLIENT_ERROR_NOT_FOUND); return null; } String result = null; // get form parameter Form f = this.getQuery(); String form = f.getFirstValue("form"); if (form != null && form.equals("edit")) { // output edit form result = "\n"; result += String.format("

Edit group %s

\n", group.getId()); result += String.format("

All groups

", this.getReference().getParentRef()); // tunnel PUT method through POST result += String.format("
\n", this.getReference().getHierarchicalPart()); result += ""; result += String.format("\n", group.getName()); result += String.format("\n", group.getUriString()); result += "
name
uri
\n"; result += "

"; result += "\n
\n\n"; } else { // output group content result = "\n

Group

\n"; result += String.format("

All groups

", this.getReference().getParentRef()); result += ""; result += String.format("\n", group.getId()); result += String.format("\n", group.getName()); result += String.format("\n", group.getUri()); result += String.format("\n", this .getReference().addSegment("members")); result += "
id%s
name%s
uri%s
membersview members
\n"; result += "

Edit group

\n"; // tunnel POST as DELETE result += String.format( "
\n", this.getReference().getHierarchicalPart()); result += "\n"; } return new StringRepresentation(result, MediaType.TEXT_HTML); } /** * PUT updates the group. * * @param entity * @return */ @Put public Representation doPut(Representation entity) { logger.debug("GroupResource.doPut!"); if (group == null) { // invalid id setStatus(Status.CLIENT_ERROR_BAD_REQUEST); return null; } // TODO: do authentication Form form = new Form(entity); String name = form.getFirstValue("name"); String uri = form.getFirstValue("uri"); if (name != null && !name.isEmpty()) { group.setName(name); } if (uri != null && !uri.isEmpty()) { group.setUri(uri); } store.storeActor(group); // return 303: see other setStatus(Status.REDIRECTION_SEE_OTHER); // go GET same URL Reference url = this.getReference(); this.getResponse().setLocationRef(url); return null; } /** * DELETE deletes the group. * * @param entity * @return */ @Delete public Representation doDelete(Representation entity) { logger.debug("GroupResource.doDelete!"); if (group == null) { // invalid id setStatus(Status.CLIENT_ERROR_BAD_REQUEST); return null; } // TODO: do authentication store.deleteActor(group); // return 303: see other setStatus(Status.REDIRECTION_SEE_OTHER); // go GET parent URL Reference url = this.getReference().getParentRef(); this.getResponse().setLocationRef(url); return null; } }