updated UserResource
git-svn-id: svn://anubis/gvsu@280 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
1ec2298033
commit
d044e20d13
@ -23,13 +23,72 @@ import org.restlet.resource.Variant;
|
|||||||
|
|
||||||
public class UserResource extends Resource
|
public class UserResource extends Resource
|
||||||
{
|
{
|
||||||
|
private User myUser;
|
||||||
|
|
||||||
public UserResource(Context context, Request request, Response response)
|
public UserResource(Context context, Request request, Response response)
|
||||||
{
|
{
|
||||||
super(context, request, response);
|
super(context, request, response);
|
||||||
String id = (String) request.getAttributes().get("id");
|
String name = (String) request.getAttributes().get("id");
|
||||||
|
myUser = findUser(name);
|
||||||
|
if(myUser != null)
|
||||||
|
{
|
||||||
getVariants().add(new Variant(MediaType.TEXT_XML));
|
getVariants().add(new Variant(MediaType.TEXT_XML));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the parent application.
|
||||||
|
*
|
||||||
|
* @return the parent application.
|
||||||
|
*/
|
||||||
|
public ChatServer getApplication()
|
||||||
|
{
|
||||||
|
return (ChatServer) getContext().getAttributes().get(ChatServer.KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the database container.
|
||||||
|
*
|
||||||
|
* @return the database container.
|
||||||
|
*/
|
||||||
|
public ObjectContainer getContainer()
|
||||||
|
{
|
||||||
|
return getApplication().getContainer();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds the user resource info in the database.
|
||||||
|
* @param name The name of the user to be found.
|
||||||
|
* @return The a reference to a User instance or null if there is none.
|
||||||
|
*/
|
||||||
|
public User findUser(final String name)
|
||||||
|
{
|
||||||
|
User result = null;
|
||||||
|
|
||||||
|
if (name != null)
|
||||||
|
{
|
||||||
|
// Create the query predicate
|
||||||
|
Predicate<User> predicate = new Predicate<User>()
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean match(User candidate)
|
||||||
|
{
|
||||||
|
return name.equals(candidate.getUserName());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Query the database and get the first result
|
||||||
|
List<User> users = getContainer().query(predicate);
|
||||||
|
if ((users != null) && (users.size() > 0))
|
||||||
|
{
|
||||||
|
result = users.get(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* handle HTTP GET requests.
|
* handle HTTP GET requests.
|
||||||
@ -41,9 +100,16 @@ public class UserResource extends Resource
|
|||||||
if (variant.getMediaType().equals(MediaType.TEXT_XML))
|
if (variant.getMediaType().equals(MediaType.TEXT_XML))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
if (myUser != null)
|
||||||
|
{
|
||||||
|
rep = myUser.getDomRepresentation();
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
rep = new DomRepresentation(MediaType.TEXT_XML);
|
rep = new DomRepresentation(MediaType.TEXT_XML);
|
||||||
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
||||||
|
}
|
||||||
rep.getDocument().normalizeDocument();
|
rep.getDocument().normalizeDocument();
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
@ -63,16 +129,16 @@ public class UserResource extends Resource
|
|||||||
@Override
|
@Override
|
||||||
public void delete()
|
public void delete()
|
||||||
{
|
{
|
||||||
/*
|
if (myUser != null)
|
||||||
if (this.book != null)
|
|
||||||
{
|
{
|
||||||
getContainer().delete(this.book);
|
getContainer().delete(myUser);
|
||||||
getContainer().commit();
|
getContainer().commit();
|
||||||
getResponse().setStatus(Status.SUCCESS_OK);
|
getResponse().setStatus(Status.SUCCESS_OK);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -90,5 +156,49 @@ public class UserResource extends Resource
|
|||||||
@Override
|
@Override
|
||||||
public void put(Representation entity)
|
public void put(Representation entity)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (entity.getMediaType().equals(MediaType.APPLICATION_WWW_FORM, true)
|
||||||
|
&& (myUser != null))
|
||||||
|
{
|
||||||
|
// Parse the entity as a web form
|
||||||
|
Form form = new Form(entity);
|
||||||
|
|
||||||
|
// update the loaded Book object
|
||||||
|
String value = form.getFirstValue("user[host]");
|
||||||
|
if (value != null)
|
||||||
|
{
|
||||||
|
myUser.setHost(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
value = form.getFirstValue("user[port]");
|
||||||
|
if (value != null)
|
||||||
|
{
|
||||||
|
myUser.setPort(Integer.parseInt(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
value = form.getFirstValue("user[status]");
|
||||||
|
if (value != null)
|
||||||
|
{
|
||||||
|
myUser.setStatus(value.equals("available"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// commit the changes.
|
||||||
|
getContainer().set(myUser);
|
||||||
|
getContainer().commit();
|
||||||
|
|
||||||
|
getResponse().setStatus(Status.SUCCESS_OK);
|
||||||
|
getResponse().setEntity(myUser.getDomRepresentation());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user