/*** Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.*@return Grizzly HTTP server.*/publicstatic HttpServer startServer(){// create a resource config that scans for JAX-RS resources and providers// in fr.univtln.bruno.demos.jaxrs packagefinal ResourceConfig rc =newResourceConfig().packages("fr.univtln.bruno.demos.jaxrs");// create and start a new instance of grizzly http server// exposing the Jersey application at BASE_URIreturn GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);}
/*** Method handling HTTP GET requests. The returned object will be sent* to the client as "text/plain" media type.**@return String that will be returned as a text/plain response.*/@GET@Produces(MediaType.TEXT_PLAIN)publicStringgetIt(){return"Got it!";}
/*** The simpliest method that just return "hello" in plain text with GET on the default path "biblio".**@return the string*/@SuppressWarnings("SameReturnValue")@GET@Path("hello")@Produces(MediaType.TEXT_PLAIN)publicStringsayHello(){return"hello";}
/*** An init method that add two authors with a PUT on the default path.**@return the number of generated authors.*@throwsIllegalArgumentExceptionthe illegal argument exception*/@PUT@Path("init")publicintinit()throws BusinessException { Library.demoLibrary.removesAuthors(); Library.Author author1 = Library.demoLibrary.addAuthor(Library.Author.builder().firstname("Alfred").name("Martin").build()); Library.Author author2 = Library.demoLibrary.addAuthor(Library.Author.builder().firstname("Marie").name("Durand").build()); Library.demoLibrary.addBook(Library.Book.builder().title("title1").authors(Set.of(author1)).build()); Library.demoLibrary.addBook(Library.Book.builder().title("title2").authors(Set.of(author1, author2)).build()); Library.demoLibrary.addBook(Library.Book.builder().title("title3").authors(Set.of(author2)).build()); Library.demoLibrary.addBook(Library.Book.builder().title("title4").authors(Set.of(author2)).build());return Library.demoLibrary.getAuthorsNumber();}
/*** Find and return an author by id with a GET on the path "biblio/auteurs/{id}" where {id} is the needed id.* The path parameter "id" is injected with @PathParam.**@paramidthe needed author id.*@return the auteur with id.*@throwsNotFoundExceptionis returned if no author has the "id".*/@GET@Path("{id}")public Library.AuthorgetAuthor(@PathParam("id")finallong id)throws BusinessException {return Library.demoLibrary.getAuthor(id);}
/*** Gets a list of "filtered" authors.**@paramnamean optional exact filter on the name.*@paramfirstnamean optional exact filter on the firstname.*@parambiographyan optional contains filter on the biography.*@paramsortKeythe sort key (prenom or nom).*@return the filtered auteurs*/@GET@Path("filter")public Page<Library.Author>getFilteredAuthors(@QueryParam("name")String name,@QueryParam("firstname")String firstname,@QueryParam("biography")String biography,@HeaderParam("sortKey")@DefaultValue("name")String sortKey){ PaginationInfo paginationInfo = PaginationInfo.builder().name(name).firstname(firstname).biography(biography).sortKey(sortKey).build();return Library.demoLibrary.getAuthorsWithFilter(paginationInfo);}
/*** Gets a page of authors after applying a sort.**@parampaginationInfothe pagination info represented as a class injected with @BeanParam.*@return the page of authors.*/@GET@Path("page")public Page<Library.Author>getAuthorsPage(@BeanParam PaginationInfo paginationInfo){return Library.demoLibrary.getAuthorsWithFilter(paginationInfo);}