Created
December 18, 2013 22:21
-
-
Save vaskaloidis/8030917 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package edu.hartford.cs375.northpole.web; | |
| import java.util.List; | |
| import javax.ejb.EJB; | |
| import javax.persistence.EntityManager; | |
| import javax.persistence.PersistenceContext; | |
| import javax.persistence.TypedQuery; | |
| import javax.ws.rs.Consumes; | |
| import javax.ws.rs.DELETE; | |
| import javax.ws.rs.FormParam; | |
| import javax.ws.rs.GET; | |
| import javax.ws.rs.POST; | |
| import javax.ws.rs.PUT; | |
| import javax.ws.rs.Path; | |
| import javax.ws.rs.PathParam; | |
| import javax.ws.rs.Produces; | |
| import javax.ws.rs.QueryParam; | |
| import javax.ws.rs.WebApplicationException; | |
| import javax.ws.rs.core.MediaType; | |
| import javax.ws.rs.core.Response; | |
| import javax.ws.rs.core.Response.Status; | |
| import org.dom4j.Document; | |
| import org.dom4j.DocumentHelper; | |
| import org.dom4j.Element; | |
| import edu.hartford.cs375.northpole.ejb.DuplicateReindeerException; | |
| import edu.hartford.cs375.northpole.ejb.Reindeer; | |
| import edu.hartford.cs375.northpole.ejb.ReindeerDataService; | |
| @Path("reindeer") | |
| public class ReindeerRestService { | |
| @EJB | |
| ReindeerDataService ejb; | |
| @GET | |
| @Produces("application/xml") | |
| public List<Reindeer> getAllReindeer(){ | |
| return ejb.getAll(); | |
| } | |
| @GET | |
| @Path("{id}") | |
| @Produces("application/xml") | |
| public Reindeer getReindeer(@PathParam("id") int id){ | |
| return ejb.get(id); | |
| } | |
| @POST | |
| @Consumes("application/xml") | |
| @Produces("application/xml") | |
| public Reindeer createReindeer(Reindeer reindeer){ | |
| return ejb.create(reindeer); | |
| } | |
| @PUT | |
| @Consumes("appliation/xml") | |
| @Produces("application/xml") | |
| public Reindeer updateReindeer(Reindeer reindeer){ | |
| return ejb.update(reindeer); | |
| } | |
| @DELETE | |
| @Path("{id}") | |
| @Produces("text/plain") | |
| public void deleteReindeer(@PathParam("id") int id){ | |
| ejb.delete(id); | |
| } | |
| @GET | |
| @Path("{id}") | |
| @Produces("text/plain") | |
| public Reindeer getAReindeer(@QueryParam("id") int id){ | |
| return ejb.get(id); | |
| } | |
| @POST | |
| @Path("param") | |
| @Consumes("application/x-www-forum-urlencoded") | |
| @Produces("application/xml") | |
| public Reindeer createReindeer( | |
| @FormParam("name") String name){ | |
| try{ | |
| return ejb.create(new Reindeer(name)); | |
| } catch(DuplicateReindeerException e) { | |
| String xml = buildDuplicateReindeerExceptionXml(name); | |
| throw new WebApplicationException(Response | |
| .status(Status.BAD_REQUEST) | |
| .entity(xml) | |
| .build()); | |
| } | |
| } | |
| /** | |
| * Builds the XML portion of the response for a DuplicateReindeerException | |
| * Note: Adapted from code from your classmate, Rob. Thanks, Rob. | |
| * The code uses dom4j, the dependency for which was added to your Maven | |
| * POM file already. | |
| * @param name The name of the reindeer | |
| */ | |
| private String buildDuplicateReindeerExceptionXml(String name){ | |
| Document doc = DocumentHelper.createDocument(); | |
| Element error = doc.addElement("error"); | |
| error.addElement("description") | |
| .addText("A reindeer with this name already exists in Santa's team. Please choose another name."); | |
| error.addElement("reindeerName") | |
| .addText(name); | |
| return doc.asXML(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment