package services; import beans.Dictionary; import beans.Word; import javax.ws.rs.*; import javax.ws.rs.core.Response; @Path("dictionary") public class DictionaryRestService { @Path("add") @POST @Consumes({"application/json", "application/xml"}) public Response addWord(Word w){ Dictionary dict = Dictionary.getInstance(); int ret = dict.addWord(w.getWord().toLowerCase(), w.getDefinition()); if(ret == -1){ return Response.status(Response.Status.NOT_ACCEPTABLE).build(); }else{ return Response.ok().build(); } } @Path("modify") @PUT @Consumes({"application/xml", "application/json"}) public Response changeDefinition(Word w){ Dictionary dict = Dictionary.getInstance(); int ret = dict.changeWordDefinition(w.getWord().toLowerCase(), w.getDefinition()); if(ret == -1){ return Response.status(Response.Status.NOT_FOUND).build(); }else{ return Response.ok().build(); } } @Path("get/{word}") @GET @Produces({"text/plain"}) public String getDefinition(@PathParam("word") String word){ Dictionary dict = Dictionary.getInstance(); String ret = dict.viewDefinition(word.toLowerCase()); if(ret == null){ return "Word not in dictionary"; } return "Definition of " + word + ": " + ret; } @Path("delete/{word}") @DELETE public Response deleteWord(@PathParam("word") String word){ Dictionary dict = Dictionary.getInstance(); dict.deleteWord(word.toLowerCase()); return Response.ok().build(); } }