restored UserResource.java
git-svn-id: svn://anubis/gvsu@277 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
fe87ea7e8f
commit
3a2b1ab9b9
@ -1,12 +1,11 @@
|
|||||||
//----------------------------------------------------------------------
|
//----------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// Filename: BookResource.java
|
// Filename: UserResource.java
|
||||||
// Description:
|
// Description:
|
||||||
//
|
//
|
||||||
// $Id:$
|
// $Id:$
|
||||||
//
|
//
|
||||||
//----------------------------------------------------------------------
|
//----------------------------------------------------------------------
|
||||||
package edu.gvsu.cis.rest.example;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@ -15,97 +14,73 @@ import org.restlet.data.Form;
|
|||||||
import org.restlet.data.MediaType;
|
import org.restlet.data.MediaType;
|
||||||
import org.restlet.data.Request;
|
import org.restlet.data.Request;
|
||||||
import org.restlet.data.Response;
|
import org.restlet.data.Response;
|
||||||
|
import org.restlet.resource.Resource;
|
||||||
import org.restlet.data.Status;
|
import org.restlet.data.Status;
|
||||||
import org.restlet.resource.DomRepresentation;
|
import org.restlet.resource.DomRepresentation;
|
||||||
import org.restlet.resource.Representation;
|
import org.restlet.resource.Representation;
|
||||||
import org.restlet.resource.Variant;
|
import org.restlet.resource.Variant;
|
||||||
|
|
||||||
|
|
||||||
|
public class UserResource extends Resource
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
public UserResource(Context context, Request request, Response response)
|
||||||
*
|
|
||||||
* Book Resource. This handles HTTP requests against a specific Book resource.
|
|
||||||
* e.g. URIs of the form /v1/books/{id}
|
|
||||||
*
|
|
||||||
* @author Jonathan Engelsma
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class BookResource extends BaseResource {
|
|
||||||
|
|
||||||
private Book book;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load up the book resource data that is being called on.
|
|
||||||
* @param context Context of the HTTP request.
|
|
||||||
* @param request The HTTP request.
|
|
||||||
* @param response The HTTP response that will be returned to the caller.
|
|
||||||
*/
|
|
||||||
public BookResource(Context context, Request request, Response response)
|
|
||||||
{
|
{
|
||||||
super(context, request, response);
|
super(context, request, response);
|
||||||
System.out.println("In BookResource");
|
|
||||||
String id = (String) request.getAttributes().get("id");
|
String id = (String) request.getAttributes().get("id");
|
||||||
this.book = findBook(id);
|
|
||||||
if(this.book != null) {
|
|
||||||
getVariants().add(new Variant(MediaType.TEXT_XML));
|
getVariants().add(new Variant(MediaType.TEXT_XML));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* handle HTTP GET requests. Return an XML representation of the book resource.
|
* handle HTTP GET requests.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Representation getRepresentation(Variant variant)
|
public Representation getRepresentation(Variant variant)
|
||||||
{
|
{
|
||||||
DomRepresentation rep=null;
|
DomRepresentation rep = null;
|
||||||
if (variant.getMediaType().equals(MediaType.TEXT_XML)){
|
if (variant.getMediaType().equals(MediaType.TEXT_XML))
|
||||||
try {
|
{
|
||||||
if (book!=null){
|
try
|
||||||
rep = book.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);
|
||||||
//TODO: we need to return a 404 here.
|
|
||||||
//Error.embedErrorInResponse(rep.getDocument(),MediaType.TEXT_XML,Error.ErrorCode.RESOURCE_UNDEFINED);
|
|
||||||
}
|
|
||||||
rep.getDocument().normalizeDocument();
|
rep.getDocument().normalizeDocument();
|
||||||
|
}
|
||||||
} catch (IOException e){
|
catch (IOException e)
|
||||||
|
{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rep;
|
return rep;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean allowDelete() {
|
public boolean allowDelete()
|
||||||
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle an HTTP DELETE request. Delete the book resource.
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void delete()
|
public void delete()
|
||||||
{
|
{
|
||||||
if (this.book != null) {
|
/*
|
||||||
|
if (this.book != null)
|
||||||
|
{
|
||||||
getContainer().delete(this.book);
|
getContainer().delete(this.book);
|
||||||
// TODO: need to delete the book reviews here as well.
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PUT is implemented.
|
* PUT is implemented.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean allowPut() {
|
public boolean allowPut()
|
||||||
// TODO Auto-generated method stub
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,63 +90,5 @@ public class BookResource extends BaseResource {
|
|||||||
@Override
|
@Override
|
||||||
public void put(Representation entity)
|
public void put(Representation entity)
|
||||||
{
|
{
|
||||||
try {
|
|
||||||
if (entity.getMediaType().equals(MediaType.APPLICATION_WWW_FORM, true) && (this.book != null)) {
|
|
||||||
|
|
||||||
// Parse the entity as a web form
|
|
||||||
Form form = new Form(entity);
|
|
||||||
|
|
||||||
// update the loaded Book object
|
|
||||||
String value = form.getFirstValue("book[authors]");
|
|
||||||
if (value != null) {
|
|
||||||
book.setAuthors(value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
value = form.getFirstValue("book[isbn]");
|
|
||||||
if(value != null) {
|
|
||||||
book.setIsbn(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
value = form.getFirstValue("book[title]");
|
|
||||||
if(value != null) {
|
|
||||||
book.setTitle(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
value = form.getFirstValue("book[publisher]");
|
|
||||||
if(value != null) {
|
|
||||||
book.setPublisher(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
value = form.getFirstValue("book[year]");
|
|
||||||
if (value != null ) {
|
|
||||||
book.setYear(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// commit the changes.
|
|
||||||
getContainer().set(book);
|
|
||||||
getContainer().commit();
|
|
||||||
|
|
||||||
getResponse().setStatus(Status.SUCCESS_OK);
|
|
||||||
getResponse().setEntity(this.book.getDomRepresentation());
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
|
||||||
// Intentionnally hide the bookmark existence
|
|
||||||
getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the book instance.
|
|
||||||
* @return a reference to a Book instance.
|
|
||||||
*/
|
|
||||||
public Book getBook() {
|
|
||||||
return this.book;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user