//---------------------------------------------------------------------- // // Filename: BookResource.java // Description: // // $Id:$ // //---------------------------------------------------------------------- package edu.gvsu.cis.rest.example; import java.io.IOException; import org.restlet.Context; import org.restlet.data.Form; import org.restlet.data.MediaType; import org.restlet.data.Request; import org.restlet.data.Response; import org.restlet.data.Status; import org.restlet.resource.DomRepresentation; import org.restlet.resource.Representation; import org.restlet.resource.Variant; /** * * 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); System.out.println("In BookResource"); String id = (String) request.getAttributes().get("id"); this.book = findBook(id); if(this.book != null) { getVariants().add(new Variant(MediaType.TEXT_XML)); } } /** * handle HTTP GET requests. Return an XML representation of the book resource. */ @Override public Representation getRepresentation(Variant variant) { DomRepresentation rep=null; if (variant.getMediaType().equals(MediaType.TEXT_XML)){ try { if (book!=null){ rep = book.getDomRepresentation(); } else{ rep = new DomRepresentation(MediaType.TEXT_XML); 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(); } catch (IOException e){ e.printStackTrace(); } } return rep; } @Override public boolean allowDelete() { return true; } /** * Handle an HTTP DELETE request. Delete the book resource. */ @Override public void delete() { if (this.book != null) { getContainer().delete(this.book); // TODO: need to delete the book reviews here as well. getContainer().commit(); getResponse().setStatus(Status.SUCCESS_OK); } else { getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND); } } /** * PUT is implemented. */ @Override public boolean allowPut() { // TODO Auto-generated method stub return true; } /** * Update a resource in response to an HTTP PUT call. */ @Override 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; } }