001 package org.hackystat.sensorbase.resource.users; 002 003 import org.hackystat.sensorbase.resource.sensorbase.SensorBaseResource; 004 import org.hackystat.sensorbase.resource.users.jaxb.Properties; 005 import org.hackystat.sensorbase.resource.users.jaxb.User; 006 import org.restlet.Context; 007 import org.restlet.data.MediaType; 008 import org.restlet.data.Request; 009 import org.restlet.data.Response; 010 import org.restlet.data.Status; 011 import org.restlet.resource.Representation; 012 import org.restlet.resource.Variant; 013 014 /** 015 * Implements a Restlet Resource for manipulating individual User resources. 016 * @author Philip Johnson 017 */ 018 public class UserResource extends SensorBaseResource { 019 020 /** 021 * Provides the following representational variants: TEXT_XML. 022 * @param context The context. 023 * @param request The request object. 024 * @param response The response object. 025 */ 026 public UserResource(Context context, Request request, Response response) { 027 super(context, request, response); 028 } 029 030 /** 031 * Returns the representation of the User resource when requested via GET. 032 * Only the authenticated user (or the admin) can request their User resource. 033 * @param variant The representational variant requested. 034 * @return The representation. 035 */ 036 @Override 037 public Representation represent(Variant variant) { 038 if (!validateUriUserIsUser() || 039 !validateAuthUserIsAdminOrUriUser()) { 040 return null; 041 } 042 043 try { 044 if (variant.getMediaType().equals(MediaType.TEXT_XML)) { 045 String xmlData = super.userManager.getUserString(this.uriUser); 046 return super.getStringRepresentation(xmlData); 047 } 048 } 049 catch (RuntimeException e) { 050 setStatusInternalError(e); 051 } 052 return null; 053 } 054 055 056 /** 057 * Indicate the DELETE method is supported. 058 * @return True. 059 */ 060 @Override 061 public boolean allowDelete() { 062 return true; 063 } 064 065 /** 066 * Implement the DELETE method that deletes an existing User given their email. 067 * Only the authenticated user (or the admin) can delete their User resource. 068 */ 069 @Override 070 public void removeRepresentations() { 071 if (!validateAuthUserIsAdminOrUriUser()) { 072 return; 073 } 074 075 try { 076 super.userManager.deleteUser(uriUser); 077 getResponse().setStatus(Status.SUCCESS_OK); 078 } 079 catch (RuntimeException e) { 080 setStatusInternalError(e); 081 } 082 } 083 084 /** 085 * Indicate the POST method is supported. 086 * @return True. 087 */ 088 @Override 089 public boolean allowPost() { 090 return true; 091 } 092 093 /** 094 * Implement the POST method that updates the properties associated with a user. 095 * <ul> 096 * <li> The User must be currently defined in this UserManager. 097 * <li> Only the authenticated User or the Admin can update their user's properties. 098 * <li> The payload must be an XML representation of a Properties instance. 099 * </ul> 100 * @param entity The entity to be posted. 101 */ 102 @Override 103 public void acceptRepresentation(Representation entity) { 104 if (!validateUriUserIsUser() || 105 !validateAuthUserIsAdminOrUriUser()) { 106 return; 107 } 108 109 // Attempt to construct a Properties object. 110 String entityString = null; 111 Properties newProperties; 112 // Try to make the XML payload into a Properties instance, return failure if this fails. 113 try { 114 entityString = entity.getText(); 115 newProperties = super.userManager.makeProperties(entityString); 116 } 117 catch (Exception e) { 118 setStatusMiscError("Bad properties representation: " + entityString); 119 return; 120 } 121 122 try { 123 User user = super.userManager.getUser(this.uriUser); 124 super.userManager.updateProperties(user, newProperties); 125 getResponse().setStatus(Status.SUCCESS_OK); 126 } 127 catch (RuntimeException e) { 128 setStatusInternalError(e); 129 } 130 } 131 }