├── .gitignore ├── README.md ├── java-se ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── readlearncode │ │ ├── enums │ │ ├── listing1 │ │ │ └── Animal.java │ │ └── listing2 │ │ │ └── Animal.java │ │ └── string │ │ └── FormatString.java │ └── test │ └── java │ └── com │ └── readlearncode │ └── enums │ ├── listing1 │ └── AnimalTest.java │ └── listing2 │ └── AnimalTest.java ├── jax-rs-context ├── README.md ├── jax-rs-context.iml ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── readlearncode │ │ ├── RESTConfig.java │ │ └── restserver │ │ ├── Book.java │ │ ├── BookRepository.java │ │ ├── BookResource.java │ │ ├── CalculatorResource.java │ │ ├── HttpHeaderResource.java │ │ ├── HttpServletRequestResource.java │ │ ├── HttpServletResponseResource.java │ │ ├── RequestResource.java │ │ ├── ResourceContextResource.java │ │ ├── SecurityContextResource.java │ │ ├── ServletConfigResource.java │ │ ├── ServletContextResource.java │ │ ├── UriInfoResource.java │ │ └── genericlist │ │ └── BookResource.java │ └── webapp │ └── WEB-INF │ └── beans.xml ├── json-processing ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── readlearncode │ │ ├── Book.java │ │ ├── SimpleObjectModelExample.java │ │ └── SimpleStreamingModelExample.java │ └── test │ └── java │ └── com │ └── readlearncode │ ├── SimpleObjectModelExampleTest.java │ └── SimpleStreamingModelExampleTest.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### Java template 3 | # Compiled class file 4 | *.class 5 | 6 | # Log file 7 | *.log 8 | 9 | # BlueJ files 10 | *.ctxt 11 | 12 | # Mobile Tools for Java (J2ME) 13 | .mtj.tmp/ 14 | 15 | # Package Files # 16 | *.jar 17 | *.war 18 | *.ear 19 | *.zip 20 | *.tar.gz 21 | *.rar 22 | 23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 24 | hs_err_pid* 25 | 26 | .gitignore 27 | .idea/workspace.xml 28 | java-se/src/main/resources/ 29 | */target/ 30 | *.iml 31 | 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Code Repository for readlearncode.com articles 2 | 3 | ## jax-rs-context 4 | This module contains the code that acompanies the series of articles about what the javax.ws.rs.core.context class is and the many different ways the @Context annotation os used to injects instances of the following objects: 5 | 6 | * HttpHeaders -> HTTP header parameters and values 7 | * UriInfo -> Captures path variables and query parameters 8 | * SecurityContext -> Provides access to security related information for a request 9 | * ResourceContext -> Provides access to instances of resource classes 10 | * Request -> Precondition request processing 11 | * Application, Configuration, and Providers -> Provide information about the JAX-RS application environment 12 | * HttpServletRequest -> Provides access to the HttpServletRequest instance 13 | * HttpServletResponse -> Provides access to the HttpServletResponse instance 14 | * ServletConfig -> Provides access to the ServletConfig 15 | * ServletContext -> Provides access to the ServletContext 16 | -------------------------------------------------------------------------------- /java-se/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | readlearncode-articles 8 | com.readlearncode 9 | 1.0 10 | 11 | 12 | 4.0.0 13 | 14 | java-se 15 | 16 | 17 | 18 | junit 19 | junit 20 | 4.12 21 | test 22 | 23 | 24 | 25 | org.assertj 26 | assertj-core 27 | 3.8.0 28 | test 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.apache.maven.plugins 37 | maven-compiler-plugin 38 | 3.6.1 39 | 40 | 1.8 41 | 1.8 42 | 43 | 44 | 45 | 46 | org.apache.maven.plugins 47 | maven-war-plugin 48 | 3.1.0 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /java-se/src/main/java/com/readlearncode/enums/listing1/Animal.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.enums.listing1; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public enum Animal { 10 | DOG 11 | } -------------------------------------------------------------------------------- /java-se/src/main/java/com/readlearncode/enums/listing2/Animal.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.enums.listing2; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public enum Animal { 10 | DOG { 11 | public String toString() { 12 | return "Dog"; 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /java-se/src/main/java/com/readlearncode/string/FormatString.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.string; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class FormatString { 10 | 11 | public static void main(String... args) { 12 | 13 | String output; 14 | 15 | // replace one placeholder 16 | output = String.format("Hello %s", "Alex"); 17 | System.out.println(output); 18 | 19 | // replace multiple placeholders of different types 20 | output = String.format("The %s costs $%f", "Bag", 12.99f); 21 | System.out.println(output); 22 | 23 | // format number to two-decimal places 24 | output = String.format("The %s costs $%.2f", "Bag", 12.99f); 25 | System.out.println(output); 26 | 27 | // add comma number separator 28 | output = String.format("The %s costs $%,.2f", "Car", 54999.99f); 29 | System.out.println(output); 30 | 31 | // Enclose a negative numbers with parenthesis 32 | output = String.format("Absolute zero is %(.2f degrees Celsius", -273.15f); 33 | System.out.println(output); 34 | 35 | // Positive/negative numbers 36 | output = String.format("Temperature of the Sun %,+d K", 5778); 37 | System.out.println(output); 38 | output = String.format("Temperature of Jupiter %,+d K", -145); 39 | System.out.println(output); 40 | 41 | // A padded number 42 | output = String.format("A padded number %010d", 42); 43 | System.out.println(output); 44 | 45 | // A left-justified number 46 | output = String.format("A left-justified number <%-10d>", 42); 47 | System.out.println(output); 48 | 49 | // Octal numbers 50 | output = String.format("An octal number %o", 100); 51 | System.out.println(output); 52 | output = String.format("An octal number %#o", 100); 53 | System.out.println(output); 54 | 55 | // Hexadecimal numbers 56 | output = String.format("An hex number %x", 100); 57 | System.out.println(output); 58 | output = String.format("An hex number %#x", 100); 59 | System.out.println(output); 60 | 61 | // Multiple String arguments of multiple types 62 | output = String.format("The %1s has %2d moons", "Saturn", 53); 63 | System.out.println(output); 64 | 65 | // Specify a width 66 | output = String.format("Fun with <%10s>", "Java"); 67 | System.out.println(output); 68 | 69 | // Specify a left justification with width 70 | output = String.format("Fun with <%-10s>", "Java"); 71 | System.out.println(output); 72 | 73 | // Truncate the maximum number of characters 74 | output = String.format("Fun with <%.1s>", "Java"); 75 | System.out.println(output); 76 | 77 | 78 | 79 | } 80 | 81 | } -------------------------------------------------------------------------------- /java-se/src/test/java/com/readlearncode/enums/listing1/AnimalTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.enums.listing1; 2 | 3 | 4 | import org.junit.Test; 5 | 6 | import static com.readlearncode.enums.listing1.Animal.DOG; 7 | import static org.assertj.core.api.Java6Assertions.assertThat; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | public class AnimalTest { 16 | 17 | @Test 18 | public void givenEnumConstant_whenCallNameAndToString_shouldReturnSameValue() { 19 | assertThat(DOG.toString()).isEqualTo(DOG.name()); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /java-se/src/test/java/com/readlearncode/enums/listing2/AnimalTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.enums.listing2; 2 | 3 | import org.junit.Test; 4 | 5 | import static com.readlearncode.enums.listing2.Animal.DOG; 6 | import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; 7 | import static org.assertj.core.api.Java6Assertions.assertThat; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | public class AnimalTest { 16 | 17 | @Test 18 | public void givenEnumConstantToStringOverwritten_whenCallNameAndToString_shouldReturnDifferentValue() { 19 | assertThat(DOG.toString()).isNotEqualTo(DOG.name()); 20 | } 21 | 22 | @Test 23 | public void givenEnumConstantAndString_whenPassedToValueOf_shouldReturnEnumConstant() { 24 | assertThat(DOG).isEqualTo(Animal.valueOf("DOG")); 25 | } 26 | 27 | @Test 28 | public void givenEnumConstantAndString_whenPassedToValueOf_shouldReturnThrownException() { 29 | assertThatExceptionOfType(IllegalArgumentException.class) 30 | .isThrownBy(() -> Animal.valueOf("Dog")); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /jax-rs-context/README.md: -------------------------------------------------------------------------------- 1 | ## jax-rs-context 2 | This module contains the code that acompanies the series of articles about what the javax.ws.rs.core.context class is and the many different ways the @Context annotation os used to injects instances of the following objects: 3 | 4 | * HttpHeaders -> HTTP header parameters and values 5 | * UriInfo -> Captures path variables and query parameters 6 | * SecurityContext -> Provides access to security related information for a request 7 | * ResourceContext -> Provides access to instances of resource classes 8 | * Request -> Precondition request processing 9 | * Application, Configuration, and Providers -> Provide information about the JAX-RS application environment 10 | * HttpServletRequest -> Provides access to the HttpServletRequest instance 11 | * HttpServletResponse -> Provides access to the HttpServletResponse instance 12 | * ServletConfig -> Provides access to the ServletConfig 13 | * ServletContext -> Provides access to the ServletContext 14 | -------------------------------------------------------------------------------- /jax-rs-context/jax-rs-context.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /jax-rs-context/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | jax-rs-context 5 | war 6 | 7 | 8 | com.readlearncode 9 | readlearncode-articles 10 | 1.0 11 | 12 | 13 | rest-server 14 | 15 | 16 | 3.2.1 17 | 18 | 19 | 20 | 1.8 21 | 7.0 22 | UTF-8 23 | 24 | 25 | 26 | 27 | javax 28 | javaee-api 29 | ${javaee-api.version} 30 | provided 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.apache.maven.plugins 39 | maven-compiler-plugin 40 | 3.6.1 41 | 42 | 1.8 43 | 1.8 44 | 45 | 46 | 47 | 48 | org.apache.maven.plugins 49 | maven-war-plugin 50 | 3.1.0 51 | 52 | 53 | 54 | org.codehaus.cargo 55 | cargo-maven2-plugin 56 | 1.6.4 57 | 58 | 59 | 60 | package 61 | 62 | run 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | liberty 71 | 72 | 73 | https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/wasdev/downloads/wlp/17.0.0.2/wlp-webProfile7-17.0.0.2.zip 74 | 75 | c:/tmp/download 76 | c:/tmp/servers 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | ${project.groupId} 88 | ${project.artifactId} 89 | war 90 | 91 | /rest-server 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/RESTConfig.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | import javax.ws.rs.ApplicationPath; 10 | import javax.ws.rs.core.Application; 11 | 12 | @ApplicationPath("/") 13 | public class RESTConfig extends Application {} -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.xml.bind.annotation.XmlRootElement; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | @XmlRootElement 12 | public class Book { 13 | 14 | private String isbn; 15 | private String title; 16 | private String author; 17 | private Float price; 18 | 19 | public Book() { 20 | } 21 | 22 | public Book(String isbn, String title, String author, Float price){ 23 | this.isbn = isbn; 24 | this.title = title; 25 | this.author = author; 26 | this.price = price; 27 | } 28 | 29 | public Book(String isbn) { 30 | this.isbn = isbn; 31 | } 32 | 33 | public Book(String title, String author, Float price) { 34 | this.title = title; 35 | this.author = author; 36 | this.price = price; 37 | } 38 | 39 | public String getIsbn() { 40 | return isbn; 41 | } 42 | 43 | public void setIsbn(String isbn) { 44 | this.isbn = isbn; 45 | } 46 | 47 | public String getTitle() { 48 | return title; 49 | } 50 | 51 | public void setTitle(String title) { 52 | this.title = title; 53 | } 54 | 55 | public String getAuthor() { 56 | return author; 57 | } 58 | 59 | public void setAuthor(String author) { 60 | this.author = author; 61 | } 62 | 63 | public Float getPrice() { 64 | return price; 65 | } 66 | 67 | public void setPrice(Float price) { 68 | this.price = price; 69 | } 70 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/BookRepository.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.annotation.PostConstruct; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | 14 | public class BookRepository { 15 | 16 | @PostConstruct 17 | public void initiate(){ 18 | books.add(new Book("Java Fun","Alex Theedom", 10f)); 19 | books.add(new Book("Java 101","Alex Theedom", 10f)); 20 | books.add(new Book("Java Expert","Alex Theedom", 10f)); 21 | books.add(new Book("Java EE 8","Alex Theedom", 10f)); 22 | } 23 | 24 | private List books = new ArrayList<>(); 25 | 26 | public List getAllBooks() { 27 | return books; 28 | } 29 | 30 | public Book saveBook(Book book) { 31 | books.add(book); 32 | return book; 33 | } 34 | 35 | public Book updateBook(Book book) { 36 | // check book contains new data then update otherwise return 37 | books.add(book); 38 | return book; 39 | } 40 | 41 | public Book deleteBookByIsbn(String isbn) { 42 | int i = books.indexOf(new Book(isbn)); 43 | return books.remove(i); 44 | } 45 | 46 | public List searchBook(String keyword, int limit) { 47 | // Search DB for book title containing 'keyword' and return 48 | // result page of size 'limit' 49 | return new ArrayList<>(); 50 | } 51 | 52 | public List getAllNewBooks() { 53 | return new ArrayList<>(); 54 | } 55 | 56 | public List getBookBy(String author, String category, String language) { 57 | return new ArrayList<>(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/BookResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.inject.Inject; 4 | import javax.ws.rs.*; 5 | import javax.ws.rs.core.GenericEntity; 6 | import javax.ws.rs.core.MediaType; 7 | import javax.ws.rs.core.Response; 8 | import java.util.List; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | @Path("/books") 17 | public class BookResource { 18 | 19 | @Inject 20 | private BookRepository bookRepository; 21 | 22 | @GET 23 | @Path("all-books") 24 | @Produces(MediaType.APPLICATION_JSON) 25 | public Response getAllBooks() { 26 | List books = bookRepository.getAllBooks(); // queries database for all books 27 | GenericEntity> list = new GenericEntity>(books) { 28 | }; 29 | return Response.ok(list).build(); 30 | } 31 | 32 | @GET 33 | @Path("all-new-books") 34 | @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) 35 | // @Produces({"application/json", "application/xml"}) 36 | public Response getAllNewBooks() { 37 | return Response.ok( 38 | new GenericEntity>( 39 | bookRepository.getAllNewBooks() 40 | ) { 41 | }).build(); 42 | } 43 | 44 | @GET 45 | @Path("book-by") 46 | @Produces(MediaType.APPLICATION_JSON) 47 | public Response getBookBy(@MatrixParam("author") String author, 48 | @MatrixParam("category") String category, 49 | @MatrixParam("language") String language) { 50 | return Response.ok( 51 | new GenericEntity>( 52 | bookRepository.getBookBy(author, category, language) 53 | ) { 54 | }).build(); 55 | } 56 | 57 | @GET 58 | @Path("cart") 59 | @Produces(MediaType.APPLICATION_JSON) 60 | public Response getCart(@CookieParam("cartId") int cartId) { 61 | return Response.ok().build(); 62 | } 63 | 64 | @GET 65 | @Path("referrer") 66 | @Produces(MediaType.APPLICATION_JSON) 67 | public Response getReferrer(@HeaderParam("referer") String referrer) { 68 | return Response.ok(referrer).build(); 69 | } 70 | 71 | @POST 72 | @Path("save-book") 73 | @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) 74 | @Produces(MediaType.APPLICATION_JSON) 75 | public Response saveBook(Book book) { 76 | book = bookRepository.saveBook(book); 77 | return Response.ok(book).build(); 78 | } 79 | 80 | @POST 81 | @Path("save") 82 | @Produces(MediaType.APPLICATION_JSON) 83 | public Response saveBookF(@FormParam("title") String title, 84 | @FormParam("author") String author, 85 | @FormParam("price") Float price) { 86 | return Response.ok(bookRepository.saveBook(new Book(title, author, price))).build(); 87 | } 88 | 89 | @PUT 90 | @Consumes(MediaType.APPLICATION_JSON) 91 | @Produces(MediaType.APPLICATION_JSON) 92 | public Response updateBook(Book book) { 93 | book = bookRepository.updateBook(book); 94 | return Response.ok(book).build(); 95 | } 96 | 97 | @DELETE 98 | @Path("{isbn:}") 99 | @Produces(MediaType.APPLICATION_JSON) 100 | public Response deleteBook(@PathParam("isbn") String isbn) { 101 | Book book = bookRepository.deleteBookByIsbn(isbn); 102 | return Response.ok(book).build(); 103 | } 104 | 105 | @OPTIONS 106 | public Response preflight() { 107 | return Response.ok().header("Allow", true).build(); 108 | } 109 | 110 | @HEAD 111 | public Response headsUp() { 112 | return Response.ok().build(); 113 | } 114 | 115 | @GET 116 | @Produces(MediaType.APPLICATION_JSON) 117 | @Path("search") 118 | public Response searchBook(@QueryParam("keyword") String keyword, @QueryParam("limit") int limit) { 119 | List books = bookRepository.searchBook(keyword, limit); 120 | return Response.ok(new GenericEntity>(books) { 121 | }).build(); 122 | } 123 | 124 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/CalculatorResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.ws.rs.GET; 4 | import javax.ws.rs.Path; 5 | import javax.ws.rs.Produces; 6 | import javax.ws.rs.QueryParam; 7 | import javax.ws.rs.core.MediaType; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | @Path("/calculator") 16 | public class CalculatorResource { 17 | 18 | @GET 19 | @Path("add") 20 | @Produces(MediaType.APPLICATION_JSON) 21 | public Integer add(@QueryParam("x") int x, @QueryParam("y") int y) { 22 | return x + y; 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/HttpHeaderResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.ws.rs.GET; 4 | import javax.ws.rs.Path; 5 | import javax.ws.rs.PathParam; 6 | import javax.ws.rs.Produces; 7 | import javax.ws.rs.core.Context; 8 | import javax.ws.rs.core.HttpHeaders; 9 | import javax.ws.rs.core.MediaType; 10 | import javax.ws.rs.core.Response; 11 | 12 | /** 13 | * Source code github.com/readlearncode 14 | * 15 | * @author Alex Theedom www.readlearncode.com 16 | * @version 1.0 17 | */ 18 | @Path("/http-headers") 19 | public class HttpHeaderResource { 20 | 21 | // @Context 22 | // private HttpHeaders httpHeaders; 23 | // 24 | // @GET 25 | // @Produces(MediaType.APPLICATION_JSON) 26 | // public Response getAllHttpHeaders(){ 27 | // return Response.ok(httpHeaders.getRequestHeaders()).build(); 28 | // } 29 | 30 | // @GET 31 | // @Produces(MediaType.APPLICATION_JSON) 32 | // public Response getUserAgentHttpHeaders(final @HeaderParam("user-agent") String userAgent){ 33 | // return Response.ok(userAgent).build(); 34 | // } 35 | 36 | @GET 37 | @Produces(MediaType.APPLICATION_JSON) 38 | public Response getAllHttpHeaders(final @Context HttpHeaders httpHeaders){ 39 | return Response.ok(httpHeaders.getRequestHeaders()).build(); 40 | } 41 | 42 | @GET 43 | @Path("/{header-param}") 44 | @Produces(MediaType.APPLICATION_JSON) 45 | public Response getSpecifiedHeader(final @PathParam("header-param") String header_param, final @Context HttpHeaders httpHeaders){ 46 | return Response.ok(httpHeaders.getRequestHeader(header_param)).build(); 47 | } 48 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/HttpServletRequestResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.servlet.http.HttpServletRequest; 4 | import javax.ws.rs.GET; 5 | import javax.ws.rs.Path; 6 | import javax.ws.rs.Produces; 7 | import javax.ws.rs.core.Context; 8 | import javax.ws.rs.core.MediaType; 9 | import javax.ws.rs.core.Response; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | @Path("/remote-address") 18 | public class HttpServletRequestResource { 19 | 20 | @GET 21 | @Produces(MediaType.APPLICATION_JSON) 22 | public Response getRemoteAddress(final @Context HttpServletRequest httpServletRequest){ 23 | return Response.ok(httpServletRequest.getRemoteAddr()).build(); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/HttpServletResponseResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.servlet.ServletOutputStream; 4 | import javax.servlet.http.HttpServletResponse; 5 | import javax.ws.rs.GET; 6 | import javax.ws.rs.Path; 7 | import javax.ws.rs.Produces; 8 | import javax.ws.rs.core.Context; 9 | import javax.ws.rs.core.MediaType; 10 | import javax.ws.rs.core.Response; 11 | import java.io.IOException; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | @Path("/output") 20 | public class HttpServletResponseResource { 21 | 22 | @GET 23 | @Produces(MediaType.APPLICATION_JSON) 24 | public Response get(final @Context HttpServletResponse httpServletResponse) throws IOException { 25 | 26 | ServletOutputStream out = httpServletResponse.getOutputStream(); 27 | out.print("Hello"); 28 | out.flush(); 29 | 30 | return Response.ok().build(); 31 | } 32 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/RequestResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.ws.rs.GET; 4 | import javax.ws.rs.Path; 5 | import javax.ws.rs.PathParam; 6 | import javax.ws.rs.Produces; 7 | import javax.ws.rs.core.*; 8 | import java.util.Map; 9 | import java.util.concurrent.ConcurrentHashMap; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | @Path("request") 18 | public class RequestResource { 19 | 20 | private Map database = new ConcurrentHashMap<>(); 21 | 22 | { 23 | database.put(1, new Employee(10_000)); 24 | } 25 | 26 | class Employee { 27 | public Employee(float salary){ 28 | salary = salary; 29 | } 30 | public float salary; 31 | } 32 | @GET 33 | @Path("{id}") 34 | @Produces(MediaType.APPLICATION_JSON) 35 | public Response updateEmployee(@PathParam("id") int id, 36 | @Context Request request, 37 | Employee emp) { 38 | 39 | Employee employee = database.get(id); 40 | EntityTag tag = new EntityTag(Integer.toString(employee.hashCode())); 41 | Response.ResponseBuilder builder = request.evaluatePreconditions(tag); 42 | 43 | if (builder != null) { 44 | // Preconditions not met so return 45 | return builder.build(); 46 | } 47 | 48 | // Preconditions met so update employee 49 | employee.salary = emp.salary; 50 | 51 | return Response.noContent().build(); 52 | } 53 | 54 | 55 | 56 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/ResourceContextResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | 4 | import javax.ws.rs.GET; 5 | import javax.ws.rs.Path; 6 | import javax.ws.rs.Produces; 7 | import javax.ws.rs.container.ResourceContext; 8 | import javax.ws.rs.core.Context; 9 | import javax.ws.rs.core.MediaType; 10 | import javax.ws.rs.core.Response; 11 | import javax.ws.rs.core.UriInfo; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | @Path("/resource-context") 20 | public class ResourceContextResource { 21 | 22 | @GET 23 | @Path("/add") 24 | @Produces(MediaType.APPLICATION_JSON) 25 | public Response get(final @Context ResourceContext resourceContext, final @Context UriInfo uriInfo) { 26 | final CalculatorResource calculatorResource = resourceContext.getResource(CalculatorResource.class); 27 | int x = Integer.valueOf(uriInfo.getQueryParameters().getFirst("x")); 28 | int y = Integer.valueOf(uriInfo.getQueryParameters().getFirst("y")); 29 | return Response.ok(calculatorResource.add(x, y)).build(); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/SecurityContextResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.ws.rs.GET; 4 | import javax.ws.rs.Path; 5 | import javax.ws.rs.Produces; 6 | import javax.ws.rs.core.Context; 7 | import javax.ws.rs.core.MediaType; 8 | import javax.ws.rs.core.Response; 9 | import javax.ws.rs.core.SecurityContext; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | @Path("/security-context") 18 | public class SecurityContextResource { 19 | 20 | @GET 21 | @Produces(MediaType.APPLICATION_JSON) 22 | public Response sayHello(final @Context SecurityContext securityContext) { 23 | return Response.ok(securityContext.isUserInRole("guest")).build(); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/ServletConfigResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.servlet.ServletConfig; 4 | import javax.ws.rs.GET; 5 | import javax.ws.rs.Path; 6 | import javax.ws.rs.Produces; 7 | import javax.ws.rs.core.Context; 8 | import javax.ws.rs.core.MediaType; 9 | import javax.ws.rs.core.Response; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | @Path("servlet-config") 18 | public class ServletConfigResource { 19 | 20 | @GET 21 | @Produces(MediaType.APPLICATION_JSON) 22 | public Response getServletName(final @Context ServletConfig servletConfig){ 23 | return Response.ok(servletConfig.getServletName()).build(); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/ServletContextResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.servlet.ServletContext; 4 | import javax.ws.rs.GET; 5 | import javax.ws.rs.Path; 6 | import javax.ws.rs.Produces; 7 | import javax.ws.rs.core.Context; 8 | import javax.ws.rs.core.MediaType; 9 | import javax.ws.rs.core.Response; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | @Path("servlet-context") 18 | public class ServletContextResource { 19 | 20 | @GET 21 | @Produces(MediaType.APPLICATION_JSON) 22 | public Response getContextPath(final @Context ServletContext servletContext) { 23 | return Response.ok(servletContext.getContextPath()).build(); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/UriInfoResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver; 2 | 3 | import javax.ws.rs.GET; 4 | import javax.ws.rs.Path; 5 | import javax.ws.rs.Produces; 6 | import javax.ws.rs.core.Context; 7 | import javax.ws.rs.core.MediaType; 8 | import javax.ws.rs.core.Response; 9 | import javax.ws.rs.core.UriInfo; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | @Path("/uri-info") 18 | public class UriInfoResource { 19 | 20 | @Context 21 | private UriInfo uriInfo; 22 | 23 | /* These method paths clash and cannot be used together.*/ 24 | @GET 25 | @Path("/{search}") 26 | @Produces(MediaType.APPLICATION_JSON) 27 | public Response getQueryParameters(final @Context UriInfo uriInfo){ 28 | return Response.ok(uriInfo.getQueryParameters()).build(); 29 | } 30 | 31 | // @GET 32 | // @Path("/{path: .*}") 33 | // @Produces(MediaType.APPLICATION_JSON) 34 | // public Response getPathParameters(final @Context UriInfo uriInfo){ 35 | // return Response.ok(uriInfo.getPathParameters()).build(); 36 | // } 37 | 38 | 39 | 40 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/java/com/readlearncode/restserver/genericlist/BookResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.restserver.genericlist; 2 | 3 | import com.readlearncode.restserver.Book; 4 | import com.readlearncode.restserver.BookRepository; 5 | 6 | import javax.inject.Inject; 7 | import javax.ws.rs.*; 8 | import javax.ws.rs.core.GenericEntity; 9 | import javax.ws.rs.core.MediaType; 10 | import javax.ws.rs.core.Response; 11 | import java.util.List; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | @Path("/my-books") 20 | public class BookResource { 21 | 22 | @Inject 23 | private BookRepository bookRepository; 24 | 25 | @GET 26 | @Path("response") 27 | @Produces(MediaType.APPLICATION_JSON) 28 | public Response getAllBooksResponse() { 29 | List books = bookRepository.getAllBooks(); // queries database for all books 30 | GenericEntity> list = new GenericEntity>(books) { 31 | }; 32 | return Response.ok(list).build(); 33 | } 34 | 35 | @GET 36 | @Path("raw") 37 | @Produces(MediaType.APPLICATION_JSON) 38 | public List getAllBooksRaw() { 39 | List books = bookRepository.getAllBooks(); // queries database for all books 40 | return books; 41 | } 42 | 43 | @GET 44 | @Path("raw-response") 45 | @Produces(MediaType.APPLICATION_JSON) 46 | public Response getAllBooksRawResponse() { 47 | List books = bookRepository.getAllBooks(); // queries database for all books 48 | return Response.ok(books).build(); 49 | } 50 | } -------------------------------------------------------------------------------- /jax-rs-context/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | -------------------------------------------------------------------------------- /json-processing/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | readlearncode-articles 7 | com.readlearncode 8 | 1.0 9 | 10 | 11 | 4.0.0 12 | 13 | json-processing 14 | 15 | 16 | 1.1 17 | 18 | 19 | 20 | 21 | 22 | org.glassfish 23 | javax.json 24 | ${javax.json.version} 25 | provided 26 | 27 | 28 | 29 | 30 | junit 31 | junit 32 | test 33 | 34 | 35 | 36 | org.assertj 37 | assertj-core 38 | test 39 | 40 | 41 | 42 | 43 | 44 | json-p-1-1 45 | 46 | 47 | org.apache.maven.plugins 48 | maven-compiler-plugin 49 | ${maven-compiler-plugin.version} 50 | 51 | ${java.version} 52 | ${java.version} 53 | 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /json-processing/src/main/java/com/readlearncode/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import java.util.Objects; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | public class Book { 12 | 13 | private String id; 14 | 15 | private String title; 16 | 17 | private String author; 18 | 19 | public Book() { 20 | } 21 | 22 | public Book(String id, String title, String author) { 23 | this.id = id; 24 | this.title = title; 25 | this.author = author; 26 | } 27 | 28 | public String getId() { 29 | return id; 30 | } 31 | 32 | public void setId(String id) { 33 | this.id = id; 34 | } 35 | 36 | public String getTitle() { 37 | return title; 38 | } 39 | 40 | public void setTitle(String title) { 41 | this.title = title; 42 | } 43 | 44 | public String getAuthor() { 45 | return author; 46 | } 47 | 48 | public void setAuthor(String author) { 49 | this.author = author; 50 | } 51 | 52 | @Override 53 | public boolean equals(Object o) { 54 | if (this == o) return true; 55 | if (o == null || getClass() != o.getClass()) return false; 56 | Book book = (Book) o; 57 | return Objects.equals(id, book.id) && 58 | Objects.equals(title, book.title) && 59 | Objects.equals(author, book.author); 60 | } 61 | 62 | @Override 63 | public int hashCode() { 64 | return Objects.hash(id, title, author); 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return "Book{" + 70 | "id='" + id + '\'' + 71 | ", title='" + title + '\'' + 72 | ", author=" + author + 73 | '}'; 74 | } 75 | } -------------------------------------------------------------------------------- /json-processing/src/main/java/com/readlearncode/SimpleObjectModelExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import javax.json.Json; 4 | import javax.json.JsonObject; 5 | import javax.json.JsonReader; 6 | import java.io.StringReader; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | public class SimpleObjectModelExample { 15 | 16 | private String json = 17 | "{\"id\": 123456, \"title\": \"Fun with JSON-Processing\", \"published\": true}"; 18 | 19 | /** 20 | * Builds a JsonObject from a Stirng of JSON data. 21 | * 22 | * @return a JsonObject built from a String of JSON data 23 | */ 24 | public JsonObject loadJsonString() { 25 | 26 | JsonReader jsonReader = Json.createReader(new StringReader(json)); 27 | JsonObject jsonObject = jsonReader.readObject(); 28 | jsonReader.close(); 29 | 30 | return jsonObject; 31 | } 32 | } -------------------------------------------------------------------------------- /json-processing/src/main/java/com/readlearncode/SimpleStreamingModelExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import javax.json.Json; 4 | import javax.json.JsonObject; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | public class SimpleStreamingModelExample { 13 | 14 | public JsonObject buildJsonDocument() { 15 | 16 | JsonObject jsonObject = Json.createObjectBuilder() 17 | .add("id", 123456) 18 | .add("title", "Fun with JSON-Processing") 19 | .add("published", true) 20 | .build(); 21 | 22 | return jsonObject; 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /json-processing/src/test/java/com/readlearncode/SimpleObjectModelExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.JsonObject; 6 | 7 | import static org.assertj.core.api.Java6Assertions.assertThat; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | public class SimpleObjectModelExampleTest { 16 | 17 | @Test 18 | public void loadJsonString() { 19 | 20 | JsonObject jsonObject = new SimpleObjectModelExample().loadJsonString(); 21 | 22 | assertThat(jsonObject.getInt("id")).isEqualTo(123456); 23 | assertThat(jsonObject.getString("title")).isEqualTo("Fun with JSON-Processing"); 24 | assertThat(jsonObject.getBoolean("published")).isEqualTo(true); 25 | 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /json-processing/src/test/java/com/readlearncode/SimpleStreamingModelExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.JsonObject; 6 | 7 | import static org.assertj.core.api.Java6Assertions.assertThat; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | public class SimpleStreamingModelExampleTest { 16 | 17 | @Test 18 | public void buildJsonDocument() { 19 | JsonObject jsonObject = new SimpleStreamingModelExample().buildJsonDocument(); 20 | 21 | assertThat(jsonObject.getInt("id")).isEqualTo(123456); 22 | assertThat(jsonObject.getString("title")).isEqualTo("Fun with JSON-Processing"); 23 | assertThat(jsonObject.getBoolean("published")).isEqualTo(true); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.readlearncode 8 | readlearncode-articles 9 | 1.0 10 | pom 11 | 12 | 13 | jax-rs-context 14 | java-se 15 | json-processing 16 | 17 | 18 | 19 | UTF-8 20 | 3.6.0 21 | 1.8 22 | 23 | 24 | 25 | 26 | 27 | junit 28 | junit 29 | 4.12 30 | 31 | 32 | org.assertj 33 | assertj-core 34 | 3.8.0 35 | 36 | 37 | 38 | 39 | 40 | 41 | --------------------------------------------------------------------------------