updated UserResource
git-svn-id: svn://anubis/gvsu@280 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
1ec2298033
commit
d044e20d13
@ -23,12 +23,71 @@ import org.restlet.resource.Variant;
|
||||
|
||||
public class UserResource extends Resource
|
||||
{
|
||||
private User myUser;
|
||||
|
||||
public UserResource(Context context, Request request, Response 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));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -42,8 +101,15 @@ public class UserResource extends Resource
|
||||
{
|
||||
try
|
||||
{
|
||||
rep = new DomRepresentation(MediaType.TEXT_XML);
|
||||
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
||||
if (myUser != null)
|
||||
{
|
||||
rep = myUser.getDomRepresentation();
|
||||
}
|
||||
else
|
||||
{
|
||||
rep = new DomRepresentation(MediaType.TEXT_XML);
|
||||
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
||||
}
|
||||
rep.getDocument().normalizeDocument();
|
||||
}
|
||||
catch (IOException e)
|
||||
@ -63,16 +129,16 @@ public class UserResource extends Resource
|
||||
@Override
|
||||
public void delete()
|
||||
{
|
||||
/*
|
||||
if (this.book != null)
|
||||
if (myUser != null)
|
||||
{
|
||||
getContainer().delete(this.book);
|
||||
getContainer().delete(myUser);
|
||||
getContainer().commit();
|
||||
getResponse().setStatus(Status.SUCCESS_OK);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
@ -90,5 +156,49 @@ public class UserResource extends Resource
|
||||
@Override
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,48 +29,48 @@ public class UsersResource extends Resource
|
||||
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 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();
|
||||
}
|
||||
/**
|
||||
* Returns the database container.
|
||||
*
|
||||
* @return the database container.
|
||||
*/
|
||||
public ObjectContainer getContainer()
|
||||
{
|
||||
return getApplication().getContainer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the database for all user resources.
|
||||
* @return a List of User references.
|
||||
*/
|
||||
public List<User> getUsers()
|
||||
{
|
||||
// Create the query predicate
|
||||
Predicate<User> predicate = new Predicate<User>()
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean match(User candidate)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// Query the database and get the first result
|
||||
List<User> users = getContainer().query(predicate);
|
||||
return users;
|
||||
}
|
||||
/**
|
||||
* Search the database for all user resources.
|
||||
* @return a List of User references.
|
||||
*/
|
||||
public List<User> getUsers()
|
||||
{
|
||||
// Create the query predicate
|
||||
Predicate<User> predicate = new Predicate<User>()
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public boolean match(User candidate)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// Query the database and get the first result
|
||||
List<User> users = getContainer().query(predicate);
|
||||
return users;
|
||||
}
|
||||
|
||||
/**
|
||||
* handle HTTP GET requests.
|
||||
@ -86,12 +86,12 @@ public class UsersResource extends Resource
|
||||
rep = new DomRepresentation(MediaType.TEXT_XML);
|
||||
Document d = rep.getDocument();
|
||||
|
||||
Element eltRoot = d.createElement("users");
|
||||
d.appendChild(eltRoot);
|
||||
for (User user : getUsers())
|
||||
{
|
||||
user.fillNodes(d, eltRoot);
|
||||
}
|
||||
Element eltRoot = d.createElement("users");
|
||||
d.appendChild(eltRoot);
|
||||
for (User user : getUsers())
|
||||
{
|
||||
user.fillNodes(d, eltRoot);
|
||||
}
|
||||
|
||||
d.normalizeDocument();
|
||||
}
|
||||
@ -115,18 +115,18 @@ public class UsersResource extends Resource
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowPost()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean allowPost()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle HTTP POST. Create a new book resource.
|
||||
*/
|
||||
@Override
|
||||
public void post(Representation entity)
|
||||
{
|
||||
/**
|
||||
* Handle HTTP POST. Create a new book resource.
|
||||
*/
|
||||
@Override
|
||||
public void post(Representation entity)
|
||||
{
|
||||
if (entity.getMediaType().equals(MediaType.APPLICATION_WWW_FORM, true))
|
||||
{
|
||||
// Parse the entity as a web form
|
||||
@ -148,5 +148,5 @@ public class UsersResource extends Resource
|
||||
{
|
||||
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user