├── .gitignore ├── Bean-Validation-2-0 ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── readlearncode │ │ ├── constraints │ │ ├── combined │ │ │ ├── Author.java │ │ │ ├── Binding.java │ │ │ └── Book.java │ │ ├── email │ │ │ ├── Customer.java │ │ │ └── Person.java │ │ ├── futureorpresent │ │ │ └── Dates.java │ │ ├── negative │ │ │ └── Numbers.java │ │ ├── negativeorzero │ │ │ └── Numbers.java │ │ ├── notblank │ │ │ └── Person.java │ │ ├── notempty │ │ │ └── Person.java │ │ ├── optional │ │ │ └── OptionalExample.java │ │ ├── pastorpresent │ │ │ └── Dates.java │ │ ├── positive │ │ │ └── Numbers.java │ │ ├── positiveorzero │ │ │ └── Numbers.java │ │ └── repeatable │ │ │ └── RepeatedConstraint.java │ │ ├── containervalidation │ │ ├── Book.java │ │ ├── Choice.java │ │ ├── ClientChoice.java │ │ ├── ClientFavourites.java │ │ ├── Favourite.java │ │ ├── ValidateChoice.java │ │ └── ValidateChoiceValidator.java │ │ └── hashtag100DaysOfJavaEE8 │ │ └── BeanValidationChallenge.java │ └── test │ └── java │ └── com │ └── readlearncode │ ├── constraints │ ├── combined │ │ └── BookTest.java │ ├── email │ │ ├── CustomerTest.java │ │ └── PersonTest.java │ ├── futureorpresent │ │ └── DatesTest.java │ ├── negative │ │ └── NumbersTest.java │ ├── negativeorzero │ │ └── NumbersTest.java │ ├── notblank │ │ └── PersonTest.java │ ├── notempty │ │ └── PersonTest.java │ ├── optional │ │ └── OptionalExampleTest.java │ ├── pastorpresent │ │ └── DatesTest.java │ ├── positive │ │ └── NumbersTest.java │ ├── positiveorzero │ │ └── NumbersTest.java │ └── repeatable │ │ └── RepeatedConstraintTest.java │ └── containervalidation │ ├── BookTest.java │ ├── ClientChoiceTest.java │ └── FavouriteTest.java ├── CDI-2-0 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── readlearncode │ │ ├── async │ │ ├── AuditEvent.java │ │ ├── AuditEventReciever.java │ │ └── AuditEventSender.java │ │ └── priority │ │ ├── AuditEventReciever1.java │ │ ├── AuditEventReciever2.java │ │ ├── AuditEventReciever3.java │ │ ├── AuditEventReciever4.java │ │ ├── AuditEventReciever5.java │ │ ├── AuditEventSender.java │ │ └── ObserverEvent.java │ └── webapp │ └── WEB-INF │ └── beans.xml ├── EJB3-2 ├── pom.xml ├── src │ └── main │ │ ├── java │ │ └── com │ │ │ └── readlearncode │ │ │ └── alternatives │ │ │ ├── LivePriceList.java │ │ │ ├── MockPriceList.java │ │ │ ├── PriceList.java │ │ │ └── ShoppingCart.java │ │ └── webapp │ │ ├── WEB-INF │ │ ├── beans.xml │ │ ├── faces-config.xml │ │ ├── glassfish-web.xml │ │ └── web.xml │ │ └── index.xhtml └── web │ └── WEB-INF │ └── web.xml ├── LICENSE ├── README.md ├── ejb2 ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── readlearncode │ ├── AuthFilter.java │ ├── EJB2 │ ├── Book.java │ ├── BookHomeRemote.java │ ├── BookRemote.java │ ├── BookShopBean.java │ ├── BookShopHomeRemote.java │ ├── BookShopRemote.java │ └── ejb-jar.xml │ ├── EJB3 │ ├── BookShop.java │ └── BookShopBean.java │ └── MyServlet.java ├── jax-rs-2-1 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── readlearncode │ │ ├── reactive │ │ ├── Book.java │ │ ├── BookRepository.java │ │ ├── BookResource.java │ │ ├── ClientAPIExample.java │ │ ├── RESTConfig.java │ │ └── ReactiveJaxRs.java │ │ └── sse │ │ ├── ServerSentEventsResource.java │ │ ├── SseClient.java │ │ └── SseResource.java │ └── webapp │ └── WEB-INF │ └── beans.xml ├── json-b-1-0 ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── readlearncode │ │ ├── JsonBindingExample.java │ │ ├── JsonData.java │ │ ├── devWorks │ │ ├── overview │ │ │ ├── BookDeserializer.java │ │ │ ├── BookSerializer.java │ │ │ ├── BookletAdapter.java │ │ │ ├── ComprehensiveExample.java │ │ │ ├── EnumExample.java │ │ │ ├── MagazineSerializer.java │ │ │ ├── MinimalExample.java │ │ │ ├── NestedClassExample.java │ │ │ └── domain │ │ │ │ ├── Author.java │ │ │ │ ├── Binding.java │ │ │ │ ├── Book.java │ │ │ │ ├── Booklet.java │ │ │ │ ├── Magazine.java │ │ │ │ └── OuterClass.java │ │ ├── part1 │ │ │ ├── arraysandcollections │ │ │ │ ├── ArraysAndCollections.java │ │ │ │ └── ScoreBoard.java │ │ │ ├── basictypes │ │ │ │ ├── Book.java │ │ │ │ ├── LexicographicalOrder.java │ │ │ │ └── SimpleBook.java │ │ │ ├── javatypes │ │ │ │ ├── BigIntegerExample.java │ │ │ │ ├── Binding.java │ │ │ │ ├── EmptyOptionalDefaultValueExample.java │ │ │ │ ├── EmptyOptionalExample.java │ │ │ │ ├── OptionalDoubleExample.java │ │ │ │ ├── OptionalExample.java │ │ │ │ ├── OptionalIntExample.java │ │ │ │ └── OptionalLongExample.java │ │ │ ├── overloaded │ │ │ │ ├── Author.java │ │ │ │ ├── Binding.java │ │ │ │ └── Book.java │ │ │ ├── simpleexample │ │ │ │ └── Book.java │ │ │ ├── treatmentofnulls │ │ │ │ ├── OptionalAndValueExample.java │ │ │ │ └── OptionalExample.java │ │ │ └── untypedmapping │ │ │ │ └── UntypedMappingExample.java │ │ └── part2 │ │ │ ├── adaptors │ │ │ ├── Author.java │ │ │ ├── Booklet.java │ │ │ ├── BookletAdapter.java │ │ │ └── FirstNameAdapter.java │ │ │ ├── customcreator │ │ │ ├── Author.java │ │ │ ├── Book.java │ │ │ └── Magazine.java │ │ │ ├── dateandnumberformat │ │ │ ├── Author.java │ │ │ ├── Book.java │ │ │ ├── Booklet.java │ │ │ └── Magazine.java │ │ │ ├── miscellaneous │ │ │ ├── Author.java │ │ │ ├── Book.java │ │ │ └── Magazine.java │ │ │ ├── propertname │ │ │ ├── Author.java │ │ │ ├── Book.java │ │ │ └── Magazine.java │ │ │ ├── propertyorder │ │ │ ├── Author.java │ │ │ ├── Book.java │ │ │ └── Magazine.java │ │ │ ├── serialiseranddeserialiser │ │ │ ├── Book.java │ │ │ ├── BookDeserializer.java │ │ │ └── BookSerializer.java │ │ │ ├── treatementofnulls │ │ │ ├── Author.java │ │ │ ├── Book.java │ │ │ ├── Booklet.java │ │ │ └── Magazine.java │ │ │ └── visibility │ │ │ ├── Author.java │ │ │ ├── Book.java │ │ │ └── Magazine.java │ │ └── domain │ │ ├── Author.java │ │ ├── Binding.java │ │ ├── Book.java │ │ ├── Booklet.java │ │ ├── Magazine.java │ │ └── Newspaper.java │ └── test │ └── java │ └── com │ └── readlearncode │ ├── JsonBindingExampleTest.java │ ├── devWorks │ ├── overview │ │ ├── BookDeserializerTest.java │ │ ├── BookSerializerTest.java │ │ ├── BookletAdapterTest.java │ │ ├── ComprehensiveExampleTest.java │ │ ├── EnumExampleTest.java │ │ ├── MagazineSerializerTest.java │ │ ├── MinimalExampleTest.java │ │ └── NestedClassExampleTest.java │ ├── part1 │ │ ├── arraysandcollections │ │ │ └── ArraysAndCollectionsTest.java │ │ ├── basictypes │ │ │ └── BasicTypeExampleTest.java │ │ ├── datetimetypes │ │ │ └── DateTimeExampleTest.java │ │ ├── javatypes │ │ │ └── StandardTypeExampleTest.java │ │ ├── jsonptypes │ │ │ └── JsonPTypesTest.java │ │ ├── overloaded │ │ │ ├── FromJsonExampleTest.java │ │ │ └── ToJsonExampleTest.java │ │ ├── simpleexample │ │ │ └── SimplestExampleTest.java │ │ ├── treatementofnulls │ │ │ └── NullsTest.java │ │ └── unmappedtypes │ │ │ └── ObjectToMapExampleTest.java │ └── part2 │ │ ├── adapter │ │ └── CustomiseAdapter.java │ │ ├── customcreator │ │ └── CustomCreatorTest.java │ │ ├── dateandnumberformat │ │ ├── CustomiseDateFormatTest.java │ │ └── CustomiseNumberFormatTest.java │ │ ├── miscellaneous │ │ └── MiscellaneousTest.java │ │ ├── propertname │ │ ├── CustomiseJsonbPropertyTest.java │ │ └── CustomisePropertyNamingStrategyTest.java │ │ ├── propertyorder │ │ └── CustomisePropertyOrderStrategyTest.java │ │ ├── serialiseranddeserialiser │ │ ├── BookDeserializerTest.java │ │ └── BookSerializerTest.java │ │ ├── treatementofnulls │ │ ├── CustomiseJsonbNillableTest.java │ │ ├── CustomiseNullValuesTest.java │ │ └── NullsTest.java │ │ └── visibility │ │ ├── CustomisePropertyVisibilityStrategyTest.java │ │ └── JsonbTransientTest.java │ └── hashtag100DaysOfJavaEE8 │ ├── DefaultNullTreatmentTest.java │ ├── PropertyNamingStrategyTest.java │ └── PropertyOrderStrategyTest.java ├── json-p-1-1 ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ │ └── readlearncode │ │ │ │ ├── Java8Integration.java │ │ │ │ ├── JsonExample.java │ │ │ │ ├── JsonMergeDiffExample.java │ │ │ │ ├── JsonMergePatchExample.java │ │ │ │ ├── JsonPatchExample.java │ │ │ │ └── JsonPointerExample.java │ │ └── resources │ │ │ └── jsondata-object.json │ └── test │ │ └── java │ │ └── com │ │ └── readlearncode │ │ ├── Java8IntegrationTest.java │ │ ├── JsonMergeDiffExampleTest.java │ │ ├── JsonMergePatchExampleTest.java │ │ ├── JsonPatchExampleTest.java │ │ ├── JsonPointerExampleTest.java │ │ └── hashtag100daysofjavaee8 │ │ └── challenges.java └── target │ └── classes │ └── jsondata-object.json ├── pom.xml ├── security-1-0 ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── readlearncode │ │ ├── httpauthenticationmechanism │ │ ├── AdminServlet.java │ │ ├── ApplicationConfig.java │ │ ├── CustomAuthenticationMechanism.java │ │ ├── LoginBean.java │ │ └── UserServlet.java │ │ ├── identitystore │ │ ├── AdminServlet.java │ │ ├── ApplicationConfig.java │ │ ├── LiteAuthenticationMechanism.java │ │ └── LiteWeightIdentityStore.java │ │ └── securitycontext │ │ ├── CustomPrincipal.java │ │ ├── HasAccessServlet.java │ │ ├── SecretServlet.java │ │ └── SecurityContextServlet.java │ └── webapp │ ├── WEB-INF │ ├── beans.xml │ ├── faces-config.xml │ ├── glassfish-web.xml │ └── web.xml │ └── login.xhtml └── servlets-4-0 ├── pom.xml └── src └── main ├── java └── servlet4 │ ├── ShowLogoServlet.java │ ├── hashtag100DaysOfJavaEE8 │ └── ServerPushExample.java │ ├── mapping │ └── ServletMapping.java │ └── pushbuilder │ ├── ObtainPushBuilder.java │ ├── PushCacheFilter.java │ └── SimplestExample.java └── webapp ├── WEB-INF ├── beans.xml ├── faces-config.xml ├── glassfish-web.xml └── web.xml ├── duke.jsp ├── duke.xhtml ├── index.jsp ├── resources ├── css │ ├── coffee-cup.css │ └── logo.css ├── images │ ├── java-ee-logo.png │ └── logo.jpg └── js │ └── logo.js ├── showlogo.jsp └── simplejsfpage.xhtml /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | .idea/ 24 | *.iml 25 | -------------------------------------------------------------------------------- /Bean-Validation-2-0/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Java-EE-8-Sampler 7 | com.readlearncode 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | Bean-Validation-2-0 13 | 14 | 15 | 16 | org.hibernate.validator 17 | hibernate-validator 18 | 6.0.7.Final 19 | 20 | 21 | org.glassfish 22 | javax.el 23 | 3.0.1-b09 24 | 25 | 26 | org.hibernate.validator 27 | hibernate-validator-cdi 28 | 6.0.7.Final 29 | 30 | 31 | 32 | 33 | Bean-Validation-2-0 34 | 35 | 36 | org.apache.maven.plugins 37 | maven-compiler-plugin 38 | ${maven-compiler-plugin.version} 39 | 40 | ${java.version} 41 | ${java.version} 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/combined/Binding.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.combined; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public enum Binding { 10 | 11 | HARD_BACK, SOFT_BACK 12 | } 13 | -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/email/Customer.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.email; 2 | 3 | import javax.validation.constraints.Email; 4 | import javax.validation.constraints.Pattern; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | public class Customer { 13 | 14 | private String firstName; 15 | 16 | private String secondName; 17 | 18 | @Email(regexp = ".*com # Allow only .com emails", flags = Pattern.Flag.COMMENTS) 19 | private String email; 20 | 21 | public String getFirstName() { 22 | return firstName; 23 | } 24 | 25 | public void setFirstName(String firstName) { 26 | this.firstName = firstName; 27 | } 28 | 29 | public String getSecondName() { 30 | return secondName; 31 | } 32 | 33 | public void setSecondName(String secondName) { 34 | this.secondName = secondName; 35 | } 36 | 37 | public String getEmail() { 38 | return email; 39 | } 40 | 41 | public void setEmail(String email) { 42 | this.email = email; 43 | } 44 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/email/Person.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.email; 2 | 3 | import javax.validation.constraints.Email; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | public class Person { 12 | 13 | private String firstName; 14 | 15 | private String secondName; 16 | 17 | @Email 18 | private String email; 19 | 20 | public String getFirstName() { 21 | return firstName; 22 | } 23 | 24 | public void setFirstName(String firstName) { 25 | this.firstName = firstName; 26 | } 27 | 28 | public String getSecondName() { 29 | return secondName; 30 | } 31 | 32 | public void setSecondName(String secondName) { 33 | this.secondName = secondName; 34 | } 35 | 36 | public String getEmail() { 37 | return email; 38 | } 39 | 40 | public void setEmail(String email) { 41 | this.email = email; 42 | } 43 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/futureorpresent/Dates.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.futureorpresent; 2 | 3 | import javax.validation.constraints.FutureOrPresent; 4 | import java.time.LocalDate; 5 | import java.time.Year; 6 | import java.util.Date; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | public class Dates { 15 | 16 | @FutureOrPresent 17 | private Date past; 18 | 19 | @FutureOrPresent 20 | private LocalDate now; 21 | 22 | @FutureOrPresent 23 | private Year future; 24 | 25 | public Date getPast() { 26 | return past; 27 | } 28 | 29 | public void setPast(Date past) { 30 | this.past = past; 31 | } 32 | 33 | public LocalDate getNow() { 34 | return now; 35 | } 36 | 37 | public void setNow(LocalDate now) { 38 | this.now = now; 39 | } 40 | 41 | public Year getFuture() { 42 | return future; 43 | } 44 | 45 | public void setFuture(Year future) { 46 | this.future = future; 47 | } 48 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/negative/Numbers.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.negative; 2 | 3 | import javax.validation.constraints.Negative; 4 | import java.math.BigDecimal; 5 | import java.util.concurrent.atomic.LongAdder; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class Numbers { 14 | 15 | @Negative 16 | private int aPrimitiveNumber; 17 | @Negative 18 | private BigDecimal aBigDecimal; 19 | @Negative 20 | private LongAdder aLongAdder; 21 | 22 | 23 | public int getAPrimitiveNumber() { 24 | return aPrimitiveNumber; 25 | } 26 | 27 | public void setAPrimitiveNumber(int aPrimitiveNumber) { 28 | this.aPrimitiveNumber = aPrimitiveNumber; 29 | } 30 | 31 | public BigDecimal getABigDecimal() { 32 | return aBigDecimal; 33 | } 34 | 35 | public void setABigDecimal(BigDecimal aBigDecimal) { 36 | this.aBigDecimal = aBigDecimal; 37 | } 38 | 39 | public LongAdder getALongAdder() { 40 | return aLongAdder; 41 | } 42 | 43 | public void setALongAdder(LongAdder aLongAdder) { 44 | this.aLongAdder = aLongAdder; 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/negativeorzero/Numbers.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.negativeorzero; 2 | 3 | import javax.validation.constraints.NegativeOrZero; 4 | import java.math.BigDecimal; 5 | import java.util.concurrent.atomic.LongAdder; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class Numbers { 14 | 15 | @NegativeOrZero 16 | private int aPrimitiveNumber; 17 | @NegativeOrZero 18 | private BigDecimal aBigDecimal; 19 | @NegativeOrZero 20 | private LongAdder aLongAdder; 21 | 22 | 23 | public int getAPrimitiveNumber() { 24 | return aPrimitiveNumber; 25 | } 26 | 27 | public void setAPrimitiveNumber(int aPrimitiveNumber) { 28 | this.aPrimitiveNumber = aPrimitiveNumber; 29 | } 30 | 31 | public BigDecimal getABigDecimal() { 32 | return aBigDecimal; 33 | } 34 | 35 | public void setABigDecimal(BigDecimal aBigDecimal) { 36 | this.aBigDecimal = aBigDecimal; 37 | } 38 | 39 | public LongAdder getALongAdder() { 40 | return aLongAdder; 41 | } 42 | 43 | public void setALongAdder(LongAdder aLongAdder) { 44 | this.aLongAdder = aLongAdder; 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/notblank/Person.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.notblank; 2 | 3 | import javax.validation.constraints.NotBlank; 4 | import java.util.List; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | public class Person { 13 | 14 | @NotBlank 15 | private String firstName; 16 | 17 | @NotBlank 18 | private String secondName; 19 | 20 | @NotBlank 21 | private String email; 22 | 23 | private List<@NotBlank String> cars; 24 | 25 | public String getFirstName() { 26 | return firstName; 27 | } 28 | 29 | public void setFirstName(String firstName) { 30 | this.firstName = firstName; 31 | } 32 | 33 | public String getSecondName() { 34 | return secondName; 35 | } 36 | 37 | public void setSecondName(String secondName) { 38 | this.secondName = secondName; 39 | } 40 | 41 | public String getEmail() { 42 | return email; 43 | } 44 | 45 | public void setEmail(String email) { 46 | this.email = email; 47 | } 48 | 49 | public List getCars() { 50 | return cars; 51 | } 52 | 53 | public void setCars(List cars) { 54 | this.cars = cars; 55 | } 56 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/notempty/Person.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.notempty; 2 | 3 | import javax.validation.constraints.NotEmpty; 4 | import java.util.List; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | public class Person { 13 | 14 | @NotEmpty 15 | private String firstName; 16 | 17 | @NotEmpty 18 | private String secondName; 19 | 20 | @NotEmpty 21 | private String email; 22 | 23 | @NotEmpty 24 | private List<@NotEmpty String> cars; 25 | 26 | public String getFirstName() { 27 | return firstName; 28 | } 29 | 30 | public void setFirstName(String firstName) { 31 | this.firstName = firstName; 32 | } 33 | 34 | public String getSecondName() { 35 | return secondName; 36 | } 37 | 38 | public void setSecondName(String secondName) { 39 | this.secondName = secondName; 40 | } 41 | 42 | public String getEmail() { 43 | return email; 44 | } 45 | 46 | public void setEmail(String email) { 47 | this.email = email; 48 | } 49 | 50 | public List getCars() { 51 | return cars; 52 | } 53 | 54 | public void setCars(List cars) { 55 | this.cars = cars; 56 | } 57 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/optional/OptionalExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.optional; 2 | 3 | import javax.validation.constraints.NotBlank; 4 | import javax.validation.constraints.NotNull; 5 | import javax.validation.constraints.Size; 6 | import java.util.Optional; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | public class OptionalExample { 15 | 16 | private Optional<@NotBlank @Size(min = 4, max = 10) String> text; 17 | 18 | @NotNull 19 | private Optional name; 20 | 21 | public Optional getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = Optional.ofNullable(name); 27 | } 28 | 29 | public Optional getText() { 30 | return text; 31 | } 32 | 33 | public void setText(String text) { 34 | this.text = Optional.of(text); 35 | } 36 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/pastorpresent/Dates.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.pastorpresent; 2 | 3 | import javax.validation.constraints.PastOrPresent; 4 | import java.time.LocalDate; 5 | import java.time.Year; 6 | import java.util.Date; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | public class Dates { 15 | 16 | @PastOrPresent 17 | private Date past; 18 | 19 | @PastOrPresent 20 | private LocalDate now; 21 | 22 | @PastOrPresent 23 | private Year future; 24 | 25 | public Date getPast() { 26 | return past; 27 | } 28 | 29 | public void setPast(Date past) { 30 | this.past = past; 31 | } 32 | 33 | public LocalDate getNow() { 34 | return now; 35 | } 36 | 37 | public void setNow(LocalDate now) { 38 | this.now = now; 39 | } 40 | 41 | public Year getFuture() { 42 | return future; 43 | } 44 | 45 | public void setFuture(Year future) { 46 | this.future = future; 47 | } 48 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/positive/Numbers.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.positive; 2 | 3 | import javax.validation.constraints.Positive; 4 | import java.math.BigDecimal; 5 | import java.util.concurrent.atomic.LongAdder; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class Numbers { 14 | 15 | @Positive 16 | private int aPrimitiveNumber; 17 | @Positive 18 | private BigDecimal aBigDecimal; 19 | @Positive 20 | private LongAdder aLongAdder; 21 | 22 | 23 | public int getAPrimitiveNumber() { 24 | return aPrimitiveNumber; 25 | } 26 | 27 | public void setAPrimitiveNumber(int aPrimitiveNumber) { 28 | this.aPrimitiveNumber = aPrimitiveNumber; 29 | } 30 | 31 | public BigDecimal getABigDecimal() { 32 | return aBigDecimal; 33 | } 34 | 35 | public void setABigDecimal(BigDecimal aBigDecimal) { 36 | this.aBigDecimal = aBigDecimal; 37 | } 38 | 39 | public LongAdder getALongAdder() { 40 | return aLongAdder; 41 | } 42 | 43 | public void setALongAdder(LongAdder aLongAdder) { 44 | this.aLongAdder = aLongAdder; 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/positiveorzero/Numbers.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.positiveorzero; 2 | 3 | import javax.validation.constraints.PositiveOrZero; 4 | import java.math.BigDecimal; 5 | import java.util.concurrent.atomic.LongAdder; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class Numbers { 14 | 15 | @PositiveOrZero 16 | private int aPrimitiveNumber; 17 | @PositiveOrZero 18 | private BigDecimal aBigDecimal; 19 | @PositiveOrZero 20 | private LongAdder aLongAdder; 21 | 22 | 23 | public int getAPrimitiveNumber() { 24 | return aPrimitiveNumber; 25 | } 26 | 27 | public void setAPrimitiveNumber(int aPrimitiveNumber) { 28 | this.aPrimitiveNumber = aPrimitiveNumber; 29 | } 30 | 31 | public BigDecimal getABigDecimal() { 32 | return aBigDecimal; 33 | } 34 | 35 | public void setABigDecimal(BigDecimal aBigDecimal) { 36 | this.aBigDecimal = aBigDecimal; 37 | } 38 | 39 | public LongAdder getALongAdder() { 40 | return aLongAdder; 41 | } 42 | 43 | public void setALongAdder(LongAdder aLongAdder) { 44 | this.aLongAdder = aLongAdder; 45 | } 46 | 47 | 48 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/constraints/repeatable/RepeatedConstraint.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.repeatable; 2 | 3 | import javax.validation.constraints.Pattern; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | public class RepeatedConstraint { 12 | 13 | @Pattern(regexp = ".*com") 14 | @Pattern(regexp = "^([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,}$") 15 | private String text; 16 | 17 | public String getText() { 18 | return text; 19 | } 20 | 21 | public void setText(String text) { 22 | this.text = text; 23 | } 24 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/containervalidation/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.containervalidation; 2 | 3 | import javax.validation.constraints.NotBlank; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | public class Book { 16 | 17 | private List<@NotBlank String> chapterTitles = new ArrayList<>(); 18 | 19 | private Map<@NotBlank String, @NotBlank String> authorChapter = new HashMap<>(); 20 | 21 | public List getChapterTitles() { 22 | return chapterTitles; 23 | } 24 | 25 | public void addChapterTitle(String chapterTitle) { 26 | chapterTitles.add(chapterTitle); 27 | } 28 | 29 | public Map getAuthorChapter() { 30 | return authorChapter; 31 | } 32 | 33 | public void addAuthorChapter(String author, String chapter) { 34 | authorChapter.put(author,chapter); 35 | } 36 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/containervalidation/Choice.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.containervalidation; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class Choice { 10 | 11 | private int id; 12 | 13 | private String name; 14 | 15 | public Choice(int id, String name) { 16 | this.id = id; 17 | this.name = name; 18 | } 19 | 20 | public int getId() { 21 | return id; 22 | } 23 | 24 | public void setId(int id) { 25 | this.id = id; 26 | } 27 | 28 | public String getName() { 29 | return name; 30 | } 31 | 32 | public void setName(String name) { 33 | this.name = name; 34 | } 35 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/containervalidation/ClientChoice.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.containervalidation; 2 | 3 | import javax.validation.constraints.NotBlank; 4 | import javax.validation.constraints.NotEmpty; 5 | import java.util.ArrayList; 6 | import java.util.HashMap; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | public class ClientChoice { 17 | 18 | private List<@ValidateChoice Choice> choices = new ArrayList<>(); 19 | 20 | private Map<@NotBlank String, @NotEmpty List<@ValidateChoice Choice>> clientChoices = new HashMap<>(); 21 | 22 | 23 | public void addChoices(Choice choice) { 24 | choices.add(choice); 25 | } 26 | 27 | public void addClientChoices(String name, List choices) { 28 | clientChoices.put(name, choices); 29 | } 30 | 31 | public List getChoices() { 32 | return choices; 33 | } 34 | 35 | public Map> getClientChoices() { 36 | return clientChoices; 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/containervalidation/ClientFavourites.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.containervalidation; 2 | 3 | import javax.validation.Valid; 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 | public class ClientFavourites { 14 | 15 | private List<@Valid Favourite> favourites = new ArrayList<>(); 16 | 17 | public List getFavourites() { 18 | return favourites; 19 | } 20 | 21 | public void addFavourites(Favourite favourite) { 22 | favourites.add(favourite); 23 | } 24 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/containervalidation/Favourite.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.containervalidation; 2 | 3 | import javax.validation.constraints.NotBlank; 4 | import javax.validation.constraints.Positive; 5 | import javax.validation.constraints.Size; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class Favourite { 14 | 15 | @Positive 16 | private int id; 17 | 18 | @NotBlank 19 | @Size(min = 4) 20 | private String name; 21 | 22 | public Favourite(int id, String name) { 23 | this.id = id; 24 | this.name = name; 25 | } 26 | 27 | public int getId() { 28 | return id; 29 | } 30 | 31 | public void setId(int id) { 32 | this.id = id; 33 | } 34 | 35 | public String getName() { 36 | return name; 37 | } 38 | 39 | public void setName(String name) { 40 | this.name = name; 41 | } 42 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/containervalidation/ValidateChoice.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.containervalidation; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | 10 | import javax.validation.Constraint; 11 | import javax.validation.Payload; 12 | import java.lang.annotation.Documented; 13 | import java.lang.annotation.Retention; 14 | import java.lang.annotation.Target; 15 | 16 | import static java.lang.annotation.ElementType.*; 17 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 18 | 19 | @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) 20 | @Retention(RUNTIME) 21 | @Documented 22 | @Constraint(validatedBy = {ValidateChoiceValidator.class}) 23 | public @interface ValidateChoice { 24 | 25 | String message() default ""; 26 | 27 | Class[] groups() default {}; 28 | 29 | Class[] payload() default {}; 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/containervalidation/ValidateChoiceValidator.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.containervalidation; 2 | 3 | import javax.validation.ConstraintValidator; 4 | import javax.validation.ConstraintValidatorContext; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | public class ValidateChoiceValidator implements ConstraintValidator { 13 | 14 | @Override 15 | public void initialize(ValidateChoice validateChoice) { 16 | } 17 | 18 | @Override 19 | public boolean isValid(Choice choice, ConstraintValidatorContext context) { 20 | 21 | if (choice.getName() != null && choice.getName().length() > 4) return true; 22 | 23 | return false; 24 | } 25 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/main/java/com/readlearncode/hashtag100DaysOfJavaEE8/BeanValidationChallenge.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.hashtag100DaysOfJavaEE8; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class BeanValidationChallenge { 10 | 11 | /* 12 | The Bean Validation 2.0 API introduces new constraint. 13 | Which of the following are among those new constraints? 14 | 15 | 16 | @NotBlank String id; // 1 17 | @EAN String barcode; // 2 18 | @Email String email; // 3 19 | @DurationMax int length; // 4 20 | @NegativeOrZero int daysToRelease; // 5 21 | @PastOrPresent Year released; // 6 22 | @CreditCardNumber String ccNumber; // 7 23 | 24 | 25 | 26 | 27 | 28 | Answer: 1, 3, 5, 6 29 | */ 30 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/test/java/com/readlearncode/constraints/email/CustomerTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.email; 2 | 3 | import org.junit.BeforeClass; 4 | import org.junit.Test; 5 | 6 | import javax.validation.ConstraintViolation; 7 | import javax.validation.Validation; 8 | import javax.validation.Validator; 9 | import java.util.Set; 10 | 11 | import static org.assertj.core.api.Java6Assertions.assertThat; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | public class CustomerTest { 20 | 21 | private static Validator validator; 22 | 23 | @BeforeClass 24 | public static void setUpValidator() { 25 | validator = Validation.buildDefaultValidatorFactory().getValidator(); 26 | } 27 | 28 | @Test 29 | public void givenBeanWithValidEmailConstraints_shouldValidate() { 30 | Customer customer = new Customer(); 31 | customer.setEmail("alex.theedom@example.com"); 32 | 33 | Set> constraintViolations = validator.validate(customer); 34 | assertThat(constraintViolations.size()).isEqualTo(0); 35 | } 36 | 37 | @Test 38 | public void givenBeanWithInvalidEmailConstraints_shouldNotValidate() { 39 | Customer customer = new Customer(); 40 | customer.setEmail("alex.theedom@example.co.uk"); 41 | 42 | Set> constraintViolations = validator.validate(customer); 43 | assertThat(constraintViolations.size()).isEqualTo(1); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/test/java/com/readlearncode/constraints/email/PersonTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.email; 2 | 3 | import org.junit.BeforeClass; 4 | import org.junit.Test; 5 | 6 | import javax.validation.ConstraintViolation; 7 | import javax.validation.Validation; 8 | import javax.validation.Validator; 9 | import java.util.Set; 10 | 11 | import static org.assertj.core.api.Java6Assertions.assertThat; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | public class PersonTest { 20 | 21 | private static Validator validator; 22 | 23 | @BeforeClass 24 | public static void setUpValidator() { 25 | validator = Validation.buildDefaultValidatorFactory().getValidator(); 26 | } 27 | 28 | @Test 29 | public void givenBeanWithValidEmailConstraints_shouldValidate() { 30 | Person person = new Person(); 31 | person.setEmail("alex.theedom@example.com"); 32 | 33 | Set> constraintViolations = validator.validate(person); 34 | assertThat(constraintViolations.size()).isEqualTo(0); 35 | } 36 | 37 | @Test 38 | public void givenBeanWithInvalidEmailConstraints_shouldNotValidate() { 39 | Person person = new Person(); 40 | person.setEmail("alex.theedom@example"); 41 | 42 | Set> constraintViolations = validator.validate(person); 43 | assertThat(constraintViolations.size()).isEqualTo(0); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/test/java/com/readlearncode/constraints/futureorpresent/DatesTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.futureorpresent; 2 | 3 | import org.junit.BeforeClass; 4 | import org.junit.Test; 5 | 6 | import javax.validation.ConstraintViolation; 7 | import javax.validation.Validation; 8 | import javax.validation.Validator; 9 | import java.time.LocalDate; 10 | import java.time.Year; 11 | import java.util.Date; 12 | import java.util.Set; 13 | 14 | import static org.assertj.core.api.Java6Assertions.assertThat; 15 | 16 | /** 17 | * Source code github.com/readlearncode 18 | * 19 | * @author Alex Theedom www.readlearncode.com 20 | * @version 1.0 21 | */ 22 | public class DatesTest { 23 | 24 | 25 | private static Validator validator; 26 | 27 | @BeforeClass 28 | public static void setUpValidator() { 29 | validator = Validation.buildDefaultValidatorFactory().getValidator(); 30 | } 31 | 32 | @Test 33 | public void givenBeanWithPresentAndFutureFields_shouldValidate() { 34 | 35 | Dates dates = new Dates(); 36 | 37 | dates.setNow(LocalDate.now()); 38 | dates.setFuture(Year.parse("2020")); 39 | 40 | Set> constraintViolations = validator.validate(dates); 41 | assertThat(constraintViolations.size()).isEqualTo(0); 42 | } 43 | 44 | 45 | @Test 46 | public void givenBeanWithPastFields_shouldNotValidate() { 47 | 48 | Dates dates = new Dates(); 49 | 50 | dates.setPast(new Date(2007-1900, 11, 24)); 51 | 52 | Set> constraintViolations = validator.validate(dates); 53 | assertThat(constraintViolations.size()).isEqualTo(1); 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/test/java/com/readlearncode/constraints/negative/NumbersTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.negative; 2 | 3 | import org.junit.BeforeClass; 4 | import org.junit.Test; 5 | 6 | import javax.validation.ConstraintViolation; 7 | import javax.validation.Validation; 8 | import javax.validation.Validator; 9 | import java.math.BigDecimal; 10 | import java.util.Set; 11 | import java.util.concurrent.atomic.LongAdder; 12 | 13 | import static org.assertj.core.api.Java6Assertions.assertThat; 14 | 15 | /** 16 | * Source code github.com/readlearncode 17 | * 18 | * @author Alex Theedom www.readlearncode.com 19 | * @version 1.0 20 | */ 21 | public class NumbersTest { 22 | 23 | private static Validator validator; 24 | 25 | @BeforeClass 26 | public static void setUpValidator() { 27 | validator = Validation.buildDefaultValidatorFactory().getValidator(); 28 | } 29 | 30 | @Test 31 | public void givenBeanWithNegativeFields_shouldValidate(){ 32 | 33 | Numbers numbers = new Numbers(); 34 | 35 | numbers.setABigDecimal(BigDecimal.valueOf(-5)); 36 | numbers.setAPrimitiveNumber(-5); 37 | 38 | LongAdder longAdder = new LongAdder(); 39 | longAdder.add(-5); 40 | numbers.setALongAdder(longAdder); 41 | 42 | Set> constraintViolations = validator.validate(numbers); 43 | assertThat(constraintViolations.size()).isEqualTo(0); 44 | } 45 | 46 | @Test 47 | public void givenBeanWithPositiveFields_shouldNotValidate() { 48 | Numbers numbers = new Numbers(); 49 | 50 | numbers.setABigDecimal(BigDecimal.valueOf(5)); 51 | numbers.setAPrimitiveNumber(5); 52 | 53 | LongAdder longAdder = new LongAdder(); 54 | longAdder.add(5); 55 | numbers.setALongAdder(longAdder); 56 | 57 | Set> constraintViolations = validator.validate(numbers); 58 | assertThat(constraintViolations.size()).isEqualTo(3); 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/test/java/com/readlearncode/constraints/pastorpresent/DatesTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.pastorpresent; 2 | 3 | import org.junit.BeforeClass; 4 | import org.junit.Test; 5 | 6 | import javax.validation.ConstraintViolation; 7 | import javax.validation.Validation; 8 | import javax.validation.Validator; 9 | import java.time.LocalDate; 10 | import java.time.Year; 11 | import java.util.Date; 12 | import java.util.Set; 13 | 14 | import static org.assertj.core.api.Java6Assertions.assertThat; 15 | 16 | /** 17 | * Source code github.com/readlearncode 18 | * 19 | * @author Alex Theedom www.readlearncode.com 20 | * @version 1.0 21 | */ 22 | public class DatesTest { 23 | 24 | private static Validator validator; 25 | 26 | @BeforeClass 27 | public static void setUpValidator() { 28 | validator = Validation.buildDefaultValidatorFactory().getValidator(); 29 | } 30 | 31 | @Test 32 | public void givenBeanWithPresentAndPastFields_shouldValidate() { 33 | 34 | Dates dates = new Dates(); 35 | 36 | dates.setPast(new Date(2007-1900, 11, 24)); 37 | dates.setNow(LocalDate.now()); 38 | //dates.setFuture(Year.parse("2020")); 39 | 40 | Set> constraintViolations = validator.validate(dates); 41 | assertThat(constraintViolations.size()).isEqualTo(0); 42 | } 43 | 44 | 45 | @Test 46 | public void givenBeanWithFutureFields_shouldNotValidate() { 47 | 48 | Dates dates = new Dates(); 49 | 50 | dates.setFuture(Year.parse("2020")); 51 | 52 | Set> constraintViolations = validator.validate(dates); 53 | assertThat(constraintViolations.size()).isEqualTo(1); 54 | } 55 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/test/java/com/readlearncode/constraints/repeatable/RepeatedConstraintTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.constraints.repeatable; 2 | 3 | import org.junit.BeforeClass; 4 | import org.junit.Test; 5 | 6 | import javax.validation.ConstraintViolation; 7 | import javax.validation.Validation; 8 | import javax.validation.Validator; 9 | import java.util.Set; 10 | 11 | import static org.assertj.core.api.Java6Assertions.assertThat; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | public class RepeatedConstraintTest { 20 | 21 | private static Validator validator; 22 | 23 | @BeforeClass 24 | public static void setUpValidator() { 25 | validator = Validation.buildDefaultValidatorFactory().getValidator(); 26 | } 27 | 28 | @Test 29 | public void givenBeanWithValidDomain_shouldValidate() { 30 | 31 | RepeatedConstraint repeatedConstraint = new RepeatedConstraint(); 32 | 33 | repeatedConstraint.setText("www.readlearncode.com"); 34 | 35 | Set> constraintViolations = validator.validate(repeatedConstraint); 36 | assertThat(constraintViolations.size()).isEqualTo(0); 37 | } 38 | 39 | 40 | @Test 41 | public void givenBeanWithInValidDomain_shouldNotValidate() { 42 | 43 | RepeatedConstraint repeatedConstraint = new RepeatedConstraint(); 44 | 45 | repeatedConstraint.setText("www.readlearncode.net"); 46 | 47 | Set> constraintViolations = validator.validate(repeatedConstraint); 48 | assertThat(constraintViolations.size()).isEqualTo(1); 49 | } 50 | } -------------------------------------------------------------------------------- /Bean-Validation-2-0/src/test/java/com/readlearncode/containervalidation/FavouriteTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.containervalidation; 2 | 3 | import org.junit.BeforeClass; 4 | import org.junit.Test; 5 | 6 | import javax.validation.ConstraintViolation; 7 | import javax.validation.Validation; 8 | import javax.validation.Validator; 9 | import java.util.Set; 10 | 11 | import static org.assertj.core.api.Java6Assertions.assertThat; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | public class FavouriteTest { 20 | 21 | private static Validator validator; 22 | 23 | @BeforeClass 24 | public static void setUpValidator() { 25 | validator = Validation.buildDefaultValidatorFactory().getValidator(); 26 | } 27 | 28 | @Test 29 | public void givenListWithConstraints_shouldNotValidate() { 30 | 31 | ClientFavourites clientFavourites = new ClientFavourites(); 32 | clientFavourites.addFavourites(new Favourite(1, "Cheese")); 33 | clientFavourites.addFavourites(new Favourite(2, "Ham")); 34 | clientFavourites.addFavourites(new Favourite(3, "Egg")); 35 | 36 | Set> constraintViolations = validator.validate(clientFavourites); 37 | assertThat(constraintViolations.size()).isEqualTo(2); 38 | 39 | } 40 | 41 | 42 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/java/com/readlearncode/async/AuditEvent.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.async; 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 AuditEvent { 12 | 13 | public enum Priority { HIGH, LOW } 14 | 15 | public AuditEvent() { 16 | } 17 | 18 | public AuditEvent(String message, Priority priority) { 19 | this.message = message; 20 | this.priority = priority; 21 | } 22 | 23 | private String message; 24 | private Priority priority = Priority.LOW; 25 | 26 | public String getMessage() { 27 | return message; 28 | } 29 | 30 | public void setMessage(String message) { 31 | this.message = message; 32 | } 33 | 34 | public Priority getPriority() { 35 | return priority; 36 | } 37 | 38 | public void setPriority(Priority priority) { 39 | this.priority = priority; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | AuditEvent that = (AuditEvent) o; 47 | return Objects.equals(message, that.message) && 48 | priority == that.priority; 49 | } 50 | 51 | @Override 52 | public int hashCode() { 53 | return Objects.hash(message, priority); 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "AuditEvent{" + 59 | "message='" + message + '\'' + 60 | ", async=" + priority + 61 | '}'; 62 | } 63 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/java/com/readlearncode/async/AuditEventReciever.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.async; 2 | 3 | import javax.enterprise.context.ApplicationScoped; 4 | import javax.enterprise.event.ObservesAsync; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | @ApplicationScoped 13 | public class AuditEventReciever { 14 | 15 | public void receiveAsync(@ObservesAsync AuditEvent auditEvent) { 16 | 17 | System.out.println(auditEvent.getPriority() + " " + auditEvent.getMessage()); 18 | 19 | try { 20 | // Simulate some amount of work 21 | Thread.sleep(2000); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/java/com/readlearncode/async/AuditEventSender.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.async; 2 | 3 | import javax.enterprise.event.Event; 4 | import javax.enterprise.event.NotificationOptions; 5 | import javax.inject.Inject; 6 | import java.util.concurrent.CompletionStage; 7 | import java.util.concurrent.ForkJoinPool; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | public class AuditEventSender { 16 | 17 | @Inject 18 | private Event event; 19 | 20 | public CompletionStage sendAsync(AuditEvent auditEvent) { 21 | System.out.println("Sending priority"); 22 | CompletionStage stage = event.fireAsync(auditEvent) 23 | .handle((event, ex) -> { 24 | if (event != null) { 25 | return event; 26 | } else { 27 | for (Throwable t : ex.getSuppressed()) {} 28 | return auditEvent; 29 | } 30 | }); 31 | 32 | return stage; 33 | } 34 | 35 | public void send(AuditEvent auditEvent) { 36 | event.fireAsync(auditEvent, NotificationOptions.ofExecutor(new ForkJoinPool(2))); 37 | } 38 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/java/com/readlearncode/priority/AuditEventReciever1.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.priority; 2 | 3 | import com.readlearncode.async.AuditEvent; 4 | 5 | import javax.annotation.Priority; 6 | import javax.enterprise.context.ApplicationScoped; 7 | import javax.enterprise.event.Observes; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | @ApplicationScoped 16 | public class AuditEventReciever1 { 17 | 18 | // Order: 2nd 19 | public void receive(@Observes @Priority(1000) AuditEvent auditEvent) { 20 | System.out.println("Priority: 1000 " + 21 | auditEvent.getPriority() + " " + auditEvent.getMessage()); 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/java/com/readlearncode/priority/AuditEventReciever2.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.priority; 2 | 3 | import com.readlearncode.async.AuditEvent; 4 | 5 | import javax.annotation.Priority; 6 | import javax.enterprise.context.ApplicationScoped; 7 | import javax.enterprise.event.Observes; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | @ApplicationScoped 16 | public class AuditEventReciever2 { 17 | 18 | // Order: 1st 19 | public void receive(@Observes @Priority(100) AuditEvent auditEvent) { 20 | System.out.println("Priority: 100 " + 21 | auditEvent.getPriority() + " " + auditEvent.getMessage()); 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/java/com/readlearncode/priority/AuditEventReciever3.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.priority; 2 | 3 | import com.readlearncode.async.AuditEvent; 4 | 5 | import javax.annotation.Priority; 6 | import javax.enterprise.context.ApplicationScoped; 7 | import javax.enterprise.event.Observes; 8 | 9 | import static javax.interceptor.Interceptor.Priority.APPLICATION; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | @ApplicationScoped 18 | public class AuditEventReciever3 { 19 | 20 | // Order: 3rd 21 | public void receive(@Observes @Priority(APPLICATION + 500) AuditEvent auditEvent) { 22 | System.out.println("Priority: " + (APPLICATION + 500) + " " + 23 | auditEvent.getPriority() + " " + auditEvent.getMessage()); 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/java/com/readlearncode/priority/AuditEventReciever4.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.priority; 2 | 3 | import com.readlearncode.async.AuditEvent; 4 | 5 | import javax.annotation.Priority; 6 | import javax.enterprise.context.ApplicationScoped; 7 | import javax.enterprise.event.ObservesAsync; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | @ApplicationScoped 16 | public class AuditEventReciever4 { 17 | 18 | // Order: Doesn't fire 19 | public void receive(@ObservesAsync @Priority(1) AuditEvent auditEvent) { 20 | System.out.println("Priority: no (ObservesAsync) " + 21 | auditEvent.getPriority() + " " + auditEvent.getMessage()); 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/java/com/readlearncode/priority/AuditEventReciever5.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.priority; 2 | 3 | import com.readlearncode.async.AuditEvent; 4 | 5 | import javax.annotation.Priority; 6 | import javax.enterprise.context.ApplicationScoped; 7 | import javax.enterprise.event.Observes; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | @ApplicationScoped 16 | public class AuditEventReciever5 { 17 | 18 | // Order: 4th 19 | public void receive(@Observes @Priority(5000) AuditEvent auditEvent) { 20 | System.out.println("Priority: 5000 " + 21 | auditEvent.getPriority() + " " + auditEvent.getMessage()); 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/java/com/readlearncode/priority/AuditEventSender.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.priority; 2 | 3 | import com.readlearncode.async.AuditEvent; 4 | 5 | import javax.annotation.PostConstruct; 6 | import javax.ejb.Startup; 7 | import javax.ejb.Singleton; 8 | import javax.enterprise.event.Event; 9 | import javax.inject.Inject; 10 | 11 | import static com.readlearncode.async.AuditEvent.Priority.HIGH; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | @Singleton 20 | @Startup 21 | public class AuditEventSender { 22 | 23 | @Inject 24 | private Event event; 25 | 26 | @PostConstruct 27 | public void send() { 28 | event.fire(new AuditEvent("Security Violation", HIGH)); 29 | } 30 | 31 | 32 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/java/com/readlearncode/priority/ObserverEvent.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.priority; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class ObserverEvent { 10 | 11 | /* 12 | The CDI 2.0 API introduces the @Priority annotation that allows 13 | the order of observer methods. 14 | 15 | When the fire method is called in what order are the observer 16 | methods executed. 17 | */ 18 | 19 | 20 | // // Order: 2nd 21 | // public void observe1(@Observes @Priority(1000) AuditEvent auditEvent) {} 22 | // 23 | // // Order: 1st 24 | // public void observe2(@Observes @Priority(100) AuditEvent auditEvent) {} 25 | // 26 | // // Order: 3rd 27 | // public void observe3(@Observes @Priority(APPLICATION + 500) AuditEvent auditEvent) {} 28 | // 29 | // // Order: Doesn't fire 30 | // public void observe4(@ObservesAsync @Priority(1) AuditEvent auditEvent) {} 31 | // 32 | // // Order: 4th 33 | // public void observe5(@Observes @Priority(5000) AuditEvent auditEvent) {} 34 | } -------------------------------------------------------------------------------- /CDI-2-0/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | -------------------------------------------------------------------------------- /EJB3-2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Java-EE-8-Sampler 7 | com.readlearncode 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | EJB3-2 13 | 14 | war 15 | -------------------------------------------------------------------------------- /EJB3-2/src/main/java/com/readlearncode/alternatives/LivePriceList.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.alternatives; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class LivePriceList implements PriceList { 10 | public String priceList() { 11 | // connect to price list webservice or database 12 | return "Live Price List"; 13 | } 14 | } -------------------------------------------------------------------------------- /EJB3-2/src/main/java/com/readlearncode/alternatives/MockPriceList.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.alternatives; 2 | 3 | import javax.enterprise.inject.Alternative; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | @Alternative 12 | public class MockPriceList implements PriceList { 13 | public String priceList() { 14 | // use hard code prices for testing 15 | return "Mock Price List"; 16 | } 17 | } -------------------------------------------------------------------------------- /EJB3-2/src/main/java/com/readlearncode/alternatives/PriceList.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.alternatives; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public interface PriceList { 10 | String priceList(); 11 | } 12 | -------------------------------------------------------------------------------- /EJB3-2/src/main/java/com/readlearncode/alternatives/ShoppingCart.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.alternatives; 2 | 3 | import javax.faces.bean.ManagedBean; 4 | import javax.inject.Inject; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | @ManagedBean 13 | public class ShoppingCart { 14 | 15 | @Inject 16 | private PriceList priceList; 17 | 18 | public String ObtainPriceList(){ 19 | return priceList.priceList(); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /EJB3-2/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | com.readlearncode.alternatives.MockPriceList 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /EJB3-2/src/main/webapp/WEB-INF/faces-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /EJB3-2/src/main/webapp/WEB-INF/glassfish-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | /ejb 6 | 7 | -------------------------------------------------------------------------------- /EJB3-2/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | Faces Servlet 9 | javax.faces.webapp.FacesServlet 10 | 1 11 | 12 | 13 | 14 | Faces Servlet 15 | *.xhtml 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /EJB3-2/src/main/webapp/index.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | Alternative Example 8 | 9 | 10 | The price list used: 11 | 12 | 13 | -------------------------------------------------------------------------------- /EJB3-2/web/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 ReadLearnCode 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java-EE-8-Sampler 2 | Code examples demonstrating the new capabilities of Java EE 8 3 | -------------------------------------------------------------------------------- /ejb2/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Java-EE-8-Sampler 7 | com.readlearncode 8 | 1.0 9 | 10 | 4.0.0 11 | 12 | ejb2 13 | 14 | 15 | 16 | jax-rs-2-1 17 | 18 | 19 | org.apache.maven.plugins 20 | maven-compiler-plugin 21 | 3.6.0 22 | 23 | ${java.version} 24 | ${java.version} 25 | 26 | 27 | 28 | 29 | org.apache.maven.plugins 30 | maven-war-plugin 31 | 3.1.0 32 | 33 | 34 | 35 | 36 | 37 | 38 | javax.ejb 39 | javax.ejb-api 40 | 3.2 41 | 42 | 43 | javax.servlet 44 | javax.servlet-api 45 | 4.0.0 46 | 47 | 48 | -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/AuthFilter.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import javax.servlet.*; 4 | import javax.servlet.annotation.WebFilter; 5 | import java.io.IOException; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | 14 | @WebFilter(filterName = "AuthFilter", urlPatterns = {"/user/*"}) 15 | public class AuthFilter implements Filter { 16 | 17 | public void init(FilterConfig filterConfig) throws ServletException {} 18 | 19 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 20 | chain.doFilter(request, response); 21 | } 22 | 23 | public void destroy() {} 24 | } 25 | -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/EJB2/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.EJB2; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class Book { 10 | } -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/EJB2/BookHomeRemote.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.EJB2; 2 | 3 | import javax.ejb.EJBHome; 4 | import javax.ejb.EJBObject; 5 | import javax.ejb.Handle; 6 | import javax.ejb.RemoveException; 7 | import java.rmi.RemoteException; 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 | public class BookHomeRemote implements BookShopRemote{ 17 | @Override 18 | public List listBooks() throws RemoteException { 19 | return null; 20 | } 21 | 22 | @Override 23 | public EJBHome getEJBHome() throws RemoteException { 24 | return null; 25 | } 26 | 27 | @Override 28 | public Object getPrimaryKey() throws RemoteException { 29 | return null; 30 | } 31 | 32 | @Override 33 | public void remove() throws RemoteException, RemoveException { 34 | 35 | } 36 | 37 | @Override 38 | public Handle getHandle() throws RemoteException { 39 | return null; 40 | } 41 | 42 | @Override 43 | public boolean isIdentical(EJBObject obj) throws RemoteException { 44 | return false; 45 | } 46 | } -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/EJB2/BookRemote.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.EJB2; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class BookRemote { 10 | } -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/EJB2/BookShopBean.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.EJB2; 2 | 3 | import javax.ejb.EJBException; 4 | import javax.ejb.SessionBean; 5 | import javax.ejb.SessionContext; 6 | import javax.naming.InitialContext; 7 | import javax.naming.NamingException; 8 | import javax.rmi.PortableRemoteObject; 9 | import java.rmi.RemoteException; 10 | import java.util.List; 11 | 12 | /** 13 | * Source code github.com/readlearncode 14 | * 15 | * @author Alex Theedom www.readlearncode.com 16 | * @version 1.0 17 | */ 18 | public class BookShopBean implements SessionBean { 19 | 20 | public List listBooks() throws RemoteException, NamingException { 21 | Object obj = new InitialContext().lookup("java:comp/env/ejb/BookHomeRemote"); 22 | BookHomeRemote home = (BookHomeRemote) PortableRemoteObject.narrow(obj, BookHomeRemote.class); 23 | return home.listBooks(); 24 | } 25 | 26 | 27 | @Override 28 | public void setSessionContext(SessionContext sessionContext) throws EJBException, RemoteException { 29 | 30 | } 31 | 32 | @Override 33 | public void ejbRemove() throws EJBException, RemoteException { 34 | 35 | } 36 | 37 | @Override 38 | public void ejbActivate() throws EJBException, RemoteException { 39 | 40 | } 41 | 42 | @Override 43 | public void ejbPassivate() throws EJBException, RemoteException { 44 | 45 | } 46 | } -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/EJB2/BookShopHomeRemote.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.EJB2; 2 | 3 | import javax.ejb.CreateException; 4 | import javax.ejb.EJBHome; 5 | import java.rmi.RemoteException; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public interface BookShopHomeRemote extends EJBHome { 14 | BookShopRemote create() throws RemoteException, CreateException; 15 | } 16 | -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/EJB2/BookShopRemote.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.EJB2; 2 | 3 | import javax.ejb.EJBObject; 4 | import java.rmi.RemoteException; 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 | public interface BookShopRemote extends EJBObject { 14 | List listBooks() throws RemoteException; 15 | } 16 | -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/EJB2/ejb-jar.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | BookShopEJB 8 | com.readlearncode.EJB2.BookShopHomeRemote 9 | com.readlearncode.EJB2.BookShopRemote 10 | com.readlearncode.EJB2.BookShopBean 11 | Stateless 12 | Container 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/EJB3/BookShop.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.EJB3; 2 | 3 | import com.readlearncode.EJB2.Book; 4 | 5 | import javax.ejb.Local; 6 | import javax.ejb.Remote; 7 | import javax.ejb.Stateless; 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | @Stateless 18 | @Remote 19 | @Local 20 | public class BookShop { 21 | 22 | public List listBooks() { 23 | return new ArrayList<>(); 24 | } 25 | } -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/EJB3/BookShopBean.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.EJB3; 2 | 3 | import com.readlearncode.EJB2.Book; 4 | 5 | import javax.ejb.Stateless; 6 | import javax.naming.InitialContext; 7 | import javax.naming.NamingException; 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 | @Stateless 17 | public class BookShopBean { 18 | 19 | public List listBooks() throws NamingException { 20 | Object obj = new InitialContext().lookup("java:comp/env/ejb/BookShop"); 21 | return ((BookShop) obj).listBooks(); 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /ejb2/src/main/java/com/readlearncode/MyServlet.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import javax.servlet.annotation.WebInitParam; 4 | import javax.servlet.annotation.WebServlet; 5 | import javax.servlet.http.HttpServlet; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | import java.io.IOException; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | @WebServlet(name = "FrontControllerServlet", urlPatterns = {"/*"}, 17 | initParams = {@WebInitParam(name = "defaultPage", value = "default.jsp")}) 18 | public class MyServlet extends HttpServlet { 19 | 20 | protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { 21 | String language = req.getHeader("Accept-Language"); 22 | resp.addHeader("X-Pingback", "http://www.example.org/xmlrpc/"); 23 | resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Access Forbidden"); 24 | } 25 | } -------------------------------------------------------------------------------- /jax-rs-2-1/src/main/java/com/readlearncode/reactive/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.reactive; 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-2-1/src/main/java/com/readlearncode/reactive/BookRepository.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.reactive; 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-2-1/src/main/java/com/readlearncode/reactive/BookResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.reactive; 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 | @Produces(MediaType.APPLICATION_JSON) 24 | public Response getAllBooks() { 25 | List books = bookRepository.getAllBooks(); 26 | GenericEntity> list = new GenericEntity>(books) { 27 | }; 28 | return Response.ok(list).build(); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /jax-rs-2-1/src/main/java/com/readlearncode/reactive/ClientAPIExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.reactive; 2 | 3 | import javax.ws.rs.client.ClientBuilder; 4 | import javax.ws.rs.client.WebTarget; 5 | import javax.ws.rs.sse.SseEventSource; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class ClientAPIExample { 14 | 15 | public void ClientAPIExample() { 16 | 17 | WebTarget target = ClientBuilder.newClient().target("http://localhost:8080/jax-rs-2-1/sse/"); 18 | try (SseEventSource source = SseEventSource.target(target).build()) { 19 | source.register(System.out::println); 20 | source.open(); 21 | } 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /jax-rs-2-1/src/main/java/com/readlearncode/reactive/RESTConfig.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.reactive; 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-2-1/src/main/java/com/readlearncode/reactive/ReactiveJaxRs.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.reactive; 2 | 3 | import javax.ws.rs.GET; 4 | import javax.ws.rs.Path; 5 | import javax.ws.rs.Produces; 6 | import javax.ws.rs.client.ClientBuilder; 7 | import javax.ws.rs.core.MediaType; 8 | import javax.ws.rs.core.Response; 9 | import java.util.concurrent.CompletionStage; 10 | import java.util.function.BiFunction; 11 | 12 | /** 13 | * Source code github.com/readlearncode 14 | * 15 | * @author Alex Theedom www.readlearncode.com 16 | * @version 1.0 17 | */ 18 | @Path("reactive") 19 | public class ReactiveJaxRs { 20 | 21 | BiFunction concat = (x, y) -> x.readEntity(String.class) + y.readEntity(String.class); 22 | 23 | @GET 24 | @Produces(MediaType.APPLICATION_JSON) 25 | public Response testReactive() { 26 | 27 | CompletionStage cs1 = ClientBuilder.newClient() 28 | .target("http://localhost:8080/jax-rs-2-1/books") 29 | .request() 30 | .rx() 31 | .get(); 32 | 33 | CompletionStage cs2 = ClientBuilder.newClient() 34 | .target("http://localhost:8080/jax-rs-2-1/books") 35 | .request() 36 | .rx() 37 | .get(); 38 | 39 | try { 40 | Thread.sleep(5000); 41 | } catch (InterruptedException e) { 42 | e.printStackTrace(); 43 | } 44 | 45 | cs1.thenCombine(cs2, (r1, r2) -> r1.readEntity(String.class) + r2.readEntity(String.class)).thenAccept(System.out::println); 46 | 47 | return Response.noContent().build(); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /jax-rs-2-1/src/main/java/com/readlearncode/sse/SseClient.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.sse; 2 | 3 | import javax.ws.rs.client.ClientBuilder; 4 | import javax.ws.rs.client.Entity; 5 | import javax.ws.rs.client.WebTarget; 6 | import javax.ws.rs.sse.SseEventSource; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | public class SseClient { 15 | 16 | public static WebTarget target; 17 | 18 | public static void main(String[] args) { 19 | target = ClientBuilder.newClient().target(args[0]); 20 | consumeEventsViaSubscription(); 21 | } 22 | 23 | private static void consumeEventsViaSubscription() { 24 | try (final SseEventSource eventSource = 25 | SseEventSource.target(target) 26 | .build()) { 27 | 28 | eventSource.register(System.out::println); 29 | eventSource.open(); 30 | 31 | for (int counter = 0; counter < 5; counter++) { 32 | target.request().post(Entity.text("message " + counter)); 33 | } 34 | 35 | Thread.sleep(1000); // make sure all the events have time to arrive 36 | } catch (InterruptedException e) { 37 | e.printStackTrace(); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /jax-rs-2-1/src/main/java/com/readlearncode/sse/SseResource.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.sse; 2 | 3 | import javax.annotation.PostConstruct; 4 | import javax.inject.Singleton; 5 | import javax.ws.rs.*; 6 | import javax.ws.rs.core.Context; 7 | import javax.ws.rs.core.MediaType; 8 | import javax.ws.rs.sse.Sse; 9 | import javax.ws.rs.sse.SseBroadcaster; 10 | import javax.ws.rs.sse.SseEventSink; 11 | 12 | /** 13 | * Source code github.com/readlearncode 14 | * 15 | * @author Alex Theedom www.readlearncode.com 16 | * @version 1.0 17 | */ 18 | @Path("/") 19 | @Singleton 20 | public class SseResource { 21 | 22 | @Context 23 | private Sse sse; 24 | 25 | private SseBroadcaster broadcaster; 26 | 27 | @PostConstruct 28 | public void initialise() { 29 | this.broadcaster = sse.newBroadcaster(); 30 | } 31 | 32 | @GET 33 | @Path("subscribe") 34 | @Produces(MediaType.SERVER_SENT_EVENTS) 35 | public void subscribe(@Context SseEventSink eventSink) { 36 | eventSink.send(sse.newEvent("You are subscribed")); 37 | broadcaster.register(eventSink); 38 | } 39 | 40 | @POST 41 | @Path("broadcast") 42 | @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 43 | public void broadcast(@FormParam("message") String message) { 44 | broadcaster.broadcast(sse.newEvent(message)); 45 | } 46 | } -------------------------------------------------------------------------------- /jax-rs-2-1/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/BookDeserializer.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Book; 4 | 5 | import javax.json.bind.serializer.DeserializationContext; 6 | import javax.json.bind.serializer.JsonbDeserializer; 7 | import javax.json.stream.JsonParser; 8 | import java.lang.reflect.Type; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | public class BookDeserializer implements JsonbDeserializer { 17 | 18 | @Override 19 | public Book deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { 20 | Book book = null; 21 | while (parser.hasNext()) { 22 | JsonParser.Event event = parser.next(); 23 | if (event == JsonParser.Event.KEY_NAME) { 24 | String className = parser.getString(); 25 | parser.next(); 26 | try { 27 | book = ctx.deserialize(Class.forName(className).asSubclass(Book.class), parser); 28 | } catch (ClassNotFoundException e) { 29 | e.printStackTrace(); 30 | } 31 | } 32 | } 33 | return book; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/BookSerializer.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Book; 4 | 5 | import javax.json.bind.serializer.JsonbSerializer; 6 | import javax.json.bind.serializer.SerializationContext; 7 | import javax.json.stream.JsonGenerator; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | public class BookSerializer implements JsonbSerializer { 16 | 17 | @Override 18 | public void serialize(Book book, JsonGenerator generator, SerializationContext ctx) { 19 | 20 | if (book != null) { 21 | generator.writeStartObject(); 22 | ctx.serialize(book.getClass().getName(), book, generator); 23 | generator.writeEnd(); 24 | } else { 25 | ctx.serialize(null, generator); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/BookletAdapter.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Author; 4 | import com.readlearncode.devWorks.overview.domain.Booklet; 5 | 6 | import javax.json.Json; 7 | import javax.json.JsonObject; 8 | import javax.json.bind.adapter.JsonbAdapter; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | public class BookletAdapter implements JsonbAdapter { 17 | 18 | @Override 19 | public JsonObject adaptToJson(Booklet booklet) { 20 | return Json.createObjectBuilder() 21 | .add("title", booklet.getTitle()) 22 | .add("firstName", booklet.getAuthor().getFirstName()) 23 | .add("lastName", booklet.getAuthor().getLastName()) 24 | .build(); 25 | } 26 | 27 | @Override 28 | public Booklet adaptFromJson(JsonObject json) { 29 | Booklet booklet = new Booklet("Fun with Java", "Alex", "Theedom"); 30 | booklet.setTitle(json.getString("title")); 31 | booklet.setAuthor(new Author(json.getString("firstName"), json.getString("lastName"))); 32 | return booklet; 33 | } 34 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/EnumExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Binding; 4 | 5 | import javax.json.bind.JsonbBuilder; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class EnumExample { 14 | 15 | public String enumSerialisation() { 16 | return JsonbBuilder.create().toJson(Binding.HARD_BACK.name()); 17 | } 18 | 19 | public class container { 20 | public Binding binding = Binding.HARD_BACK; 21 | } 22 | 23 | public String enumSerialisationInObject() { 24 | return JsonbBuilder.create().toJson(new container()); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/MagazineSerializer.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Magazine; 4 | 5 | import javax.json.bind.serializer.JsonbSerializer; 6 | import javax.json.bind.serializer.SerializationContext; 7 | import javax.json.stream.JsonGenerator; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | public class MagazineSerializer implements JsonbSerializer { 16 | 17 | @Override 18 | public void serialize(Magazine magazine, JsonGenerator generator, SerializationContext ctx) { 19 | 20 | if (magazine != null) { 21 | generator.writeStartObject(); 22 | ctx.serialize("name", magazine.getAuthor(), generator); 23 | generator.writeEnd(); 24 | } else { 25 | ctx.serialize(null, generator); 26 | } 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/MinimalExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Book; 4 | 5 | import javax.json.bind.Jsonb; 6 | import javax.json.bind.JsonbBuilder; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | public class MinimalExample { 15 | 16 | public String serializeBook() { 17 | Book book = new Book("SHDUJ-4532", "Fun with Java", "Alex Theedom"); 18 | Jsonb jsonb = JsonbBuilder.create(); 19 | String json = jsonb.toJson(book); 20 | return json; 21 | } 22 | 23 | public Book deserializeBook() { 24 | Book book = new Book("SHDUJ-4532", "Fun with Java", "Alex Theedom"); 25 | Jsonb jsonb = JsonbBuilder.create(); 26 | String json = jsonb.toJson(book); 27 | book = jsonb.fromJson(json, Book.class); 28 | return book; 29 | } 30 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/NestedClassExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.OuterClass; 4 | 5 | import javax.json.bind.Jsonb; 6 | import javax.json.bind.JsonbBuilder; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | public class NestedClassExample { 15 | 16 | public String serializeNestedClasses() { 17 | 18 | OuterClass.InnerClass innerClass = new OuterClass().new InnerClass(); 19 | 20 | Jsonb jsonb = JsonbBuilder.create(); 21 | String json = jsonb.toJson(innerClass); 22 | 23 | return json; 24 | } 25 | 26 | public OuterClass.InnerClass deserialiseNestedClasses() { 27 | 28 | String json = "{\"name\":\"Inner Class\"}"; 29 | 30 | OuterClass.InnerClass innerClass = 31 | JsonbBuilder.create().fromJson(json, OuterClass.InnerClass.class); 32 | 33 | return innerClass; 34 | } 35 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/domain/Author.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview.domain; 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 Author { 12 | 13 | private String firstName; 14 | private String lastName; 15 | 16 | public Author() { 17 | } 18 | 19 | public Author(String firstName, String lastName) { 20 | this.firstName = firstName; 21 | this.lastName = lastName; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public void setFirstName(String firstName) { 29 | this.firstName = firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | public void setLastName(String lastName) { 37 | this.lastName = lastName; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Author author = (Author) o; 45 | return Objects.equals(firstName, author.firstName) && 46 | Objects.equals(lastName, author.lastName); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(firstName, lastName); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Author{" + 57 | "firstName='" + firstName + '\'' + 58 | ", lastName='" + lastName + '\'' + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/domain/Binding.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview.domain; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public enum Binding { 10 | HARD_BACK { 11 | public String toString() { 12 | return "Hard Back"; 13 | } 14 | }, SOFT_BACK { 15 | public String toString() { 16 | return "Soft Back"; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/domain/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview.domain; 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-b-1-0/src/main/java/com/readlearncode/devWorks/overview/domain/Booklet.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview.domain; 2 | 3 | import com.readlearncode.devWorks.overview.BookletAdapter; 4 | 5 | import javax.json.bind.annotation.JsonbTypeAdapter; 6 | import java.util.Objects; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | @JsonbTypeAdapter(BookletAdapter.class) 15 | public class Booklet { 16 | 17 | private String title; 18 | 19 | private Author author; 20 | 21 | public Booklet(String title, String firstName, String lastName) { 22 | this.title = title; 23 | this.author = new Author(firstName, lastName); 24 | } 25 | 26 | public String getTitle() { 27 | return title; 28 | } 29 | 30 | public void setTitle(String title) { 31 | this.title = title; 32 | } 33 | 34 | public Author getAuthor() { 35 | return author; 36 | } 37 | 38 | public void setAuthor(Author author) { 39 | this.author = author; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | Booklet booklet = (Booklet) o; 47 | return Objects.equals(title, booklet.title) && 48 | Objects.equals(author, booklet.author); 49 | } 50 | 51 | @Override 52 | public int hashCode() { 53 | return Objects.hash(title, author); 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "Booklet{" + 59 | "title='" + title + '\'' + 60 | ", author=" + author + 61 | '}'; 62 | } 63 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/overview/domain/OuterClass.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview.domain; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class OuterClass { 10 | public OuterClass() {} 11 | public String name = "OuterClass"; 12 | public class InnerClass { 13 | public InnerClass() {} 14 | public String name = "Inner Class"; 15 | } 16 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/arraysandcollections/ArraysAndCollections.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.arraysandcollections; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.Set; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class ArraysAndCollections { 14 | 15 | private Map map; 16 | 17 | private List list; 18 | 19 | private Set set; 20 | 21 | private int[] ints = new int[5]; 22 | 23 | private int[][] ints2d = new int[5][]; 24 | 25 | 26 | public Map getMap() { 27 | return map; 28 | } 29 | 30 | public void setMap(Map map) { 31 | this.map = map; 32 | } 33 | 34 | public List getList() { 35 | return list; 36 | } 37 | 38 | public void setList(List list) { 39 | this.list = list; 40 | } 41 | 42 | public Set getSet() { 43 | return set; 44 | } 45 | 46 | public void setSet(Set set) { 47 | this.set = set; 48 | } 49 | 50 | public int[] getInts() { 51 | return ints; 52 | } 53 | 54 | public void setInts(int[] ints) { 55 | this.ints = ints; 56 | } 57 | 58 | public int[][] getInts2d() { 59 | return ints2d; 60 | } 61 | 62 | public void setInts2d(int[][] ints2d) { 63 | this.ints2d = ints2d; 64 | } 65 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/arraysandcollections/ScoreBoard.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.arraysandcollections; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | public class ScoreBoard { 13 | 14 | public Map scores = new HashMap() {{ 15 | put("John", 12); 16 | put("Jane", 34); 17 | }}; 18 | 19 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/basictypes/LexicographicalOrder.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.basictypes; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class LexicographicalOrder { 10 | 11 | public String dog = "Labradoodle"; 12 | public String animal = "Cat"; 13 | public String bread = "Chiapata"; 14 | public String car = "Ford"; 15 | 16 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/basictypes/SimpleBook.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.basictypes; 2 | 3 | import java.util.Objects; 4 | import java.util.concurrent.atomic.AtomicInteger; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | public class SimpleBook { 13 | 14 | private AtomicInteger version; 15 | 16 | public SimpleBook() { 17 | } 18 | 19 | public SimpleBook(AtomicInteger version) { 20 | this.version = version; 21 | } 22 | 23 | public AtomicInteger getVersion() { 24 | return version; 25 | } 26 | 27 | public void setVersion(AtomicInteger version) { 28 | this.version = version; 29 | } 30 | 31 | @Override 32 | public boolean equals(Object o) { 33 | if (this == o) return true; 34 | if (o == null || getClass() != o.getClass()) return false; 35 | SimpleBook that = (SimpleBook) o; 36 | return 37 | Objects.equals(version, that.version); 38 | } 39 | 40 | @Override 41 | public int hashCode() { 42 | return Objects.hash(version); 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return "OptionalExample{" + 48 | ", version=" + version + 49 | '}'; 50 | } 51 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/javatypes/BigIntegerExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.javatypes; 2 | 3 | import java.math.BigInteger; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | public class BigIntegerExample { 12 | 13 | public BigInteger bigInteger = new BigInteger("10"); 14 | 15 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/javatypes/Binding.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.javatypes; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public enum Binding { 10 | HARD_BACK { 11 | public String toString() { 12 | return "Hard Back"; 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/javatypes/EmptyOptionalDefaultValueExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.javatypes; 2 | 3 | import java.util.Optional; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | public class EmptyOptionalDefaultValueExample { 12 | 13 | private Optional value = Optional.of(10); 14 | 15 | public EmptyOptionalDefaultValueExample() { 16 | } 17 | 18 | public EmptyOptionalDefaultValueExample(Integer value) { 19 | if(value == null){ 20 | this.value = Optional.empty(); 21 | } else { 22 | this.value = Optional.of(value); 23 | } 24 | 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/javatypes/EmptyOptionalExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.javatypes; 2 | 3 | import java.util.Optional; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | public class EmptyOptionalExample { 12 | 13 | public Optional value; 14 | 15 | public EmptyOptionalExample() { 16 | } 17 | 18 | public EmptyOptionalExample(Integer value) { 19 | if(value == null){ 20 | this.value = Optional.empty(); 21 | } else { 22 | this.value = Optional.of(value); 23 | } 24 | 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/javatypes/OptionalDoubleExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.javatypes; 2 | 3 | import java.util.OptionalDouble; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | public class OptionalDoubleExample { 12 | 13 | public OptionalDouble value; 14 | 15 | public OptionalDoubleExample() { 16 | } 17 | 18 | public OptionalDoubleExample(Double value) { 19 | this.value = OptionalDouble.of(value); 20 | } 21 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/javatypes/OptionalExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.javatypes; 2 | 3 | import java.util.Optional; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | public class OptionalExample { 12 | 13 | public Optional value; 14 | 15 | public OptionalExample() { 16 | } 17 | 18 | public OptionalExample(Integer value) { 19 | this.value = Optional.of(value); 20 | } 21 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/javatypes/OptionalIntExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.javatypes; 2 | 3 | import java.util.OptionalInt; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | public class OptionalIntExample { 12 | 13 | public OptionalInt value; 14 | 15 | public OptionalIntExample() { 16 | } 17 | 18 | public OptionalIntExample(Integer value) { 19 | this.value = OptionalInt.of(value); 20 | } 21 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/javatypes/OptionalLongExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.javatypes; 2 | 3 | import java.util.OptionalLong; 4 | 5 | /** 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | public class OptionalLongExample { 12 | 13 | public OptionalLong value; 14 | 15 | public OptionalLongExample() { 16 | } 17 | 18 | public OptionalLongExample(Long value) { 19 | this.value = OptionalLong.of(value); 20 | } 21 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/overloaded/Author.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.overloaded; 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 Author { 12 | 13 | private String firstName; 14 | private String lastName; 15 | 16 | public Author() { 17 | } 18 | 19 | public Author(String firstName, String lastName) { 20 | this.firstName = firstName; 21 | this.lastName = lastName; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public void setFirstName(String firstName) { 29 | this.firstName = firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | public void setLastName(String lastName) { 37 | this.lastName = lastName; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Author author = (Author) o; 45 | return Objects.equals(firstName, author.firstName) && 46 | Objects.equals(lastName, author.lastName); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(firstName, lastName); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Author{" + 57 | "firstName='" + firstName + '\'' + 58 | ", lastName='" + lastName + '\'' + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/overloaded/Binding.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.overloaded; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public enum Binding { 10 | 11 | HARD_BACK, SOFT_BACK; 12 | } 13 | -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/simpleexample/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.simpleexample; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class Book { 10 | 11 | public String title; 12 | 13 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/treatmentofnulls/OptionalAndValueExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.treatmentofnulls; 2 | 3 | import javax.json.bind.annotation.JsonbNillable; 4 | import java.util.Optional; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | @JsonbNillable 13 | public class OptionalAndValueExample { 14 | 15 | public String name; 16 | 17 | public Optional value; 18 | 19 | public OptionalAndValueExample() { 20 | } 21 | 22 | public OptionalAndValueExample(Integer value) { 23 | this.value = Optional.ofNullable(value); 24 | } 25 | 26 | public OptionalAndValueExample(Integer value, String name) { 27 | this.value = Optional.ofNullable(value); 28 | this.name = name; 29 | } 30 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/treatmentofnulls/OptionalExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.treatmentofnulls; 2 | 3 | import javax.json.bind.annotation.JsonbNillable; 4 | import java.util.Optional; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | @JsonbNillable 13 | public class OptionalExample { 14 | 15 | public Optional value; 16 | 17 | public OptionalExample() { 18 | } 19 | 20 | public OptionalExample(Integer value) { 21 | this.value = Optional.ofNullable(value); 22 | } 23 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part1/untypedmapping/UntypedMappingExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.untypedmapping; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public class UntypedMappingExample { 10 | 11 | 12 | 13 | 14 | 15 | 16 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/adaptors/Booklet.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.adaptors; 2 | 3 | import com.readlearncode.devWorks.overview.BookletAdapter; 4 | 5 | import javax.json.bind.annotation.JsonbTypeAdapter; 6 | import java.util.Objects; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | @JsonbTypeAdapter(BookletAdapter.class) 15 | public class Booklet { 16 | 17 | private String title; 18 | 19 | private Author author; 20 | 21 | public Booklet(String title, Author author) { 22 | this.title = title; 23 | this.author = author; 24 | } 25 | 26 | public String getTitle() { 27 | return title; 28 | } 29 | 30 | public void setTitle(String title) { 31 | this.title = title; 32 | } 33 | 34 | public Author getAuthor() { 35 | return author; 36 | } 37 | 38 | public void setAuthor(Author author) { 39 | this.author = author; 40 | } 41 | 42 | @Override 43 | public boolean equals(Object o) { 44 | if (this == o) return true; 45 | if (o == null || getClass() != o.getClass()) return false; 46 | Booklet booklet = (Booklet) o; 47 | return Objects.equals(title, booklet.title) && 48 | Objects.equals(author, booklet.author); 49 | } 50 | 51 | @Override 52 | public int hashCode() { 53 | return Objects.hash(title, author); 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "Booklet{" + 59 | "title='" + title + '\'' + 60 | ", author=" + author + 61 | '}'; 62 | } 63 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/adaptors/BookletAdapter.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.adaptors; 2 | 3 | import javax.json.Json; 4 | import javax.json.JsonObject; 5 | import javax.json.bind.adapter.JsonbAdapter; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class BookletAdapter implements JsonbAdapter { 14 | 15 | @Override 16 | public JsonObject adaptToJson(Booklet booklet) { 17 | return Json.createObjectBuilder() 18 | .add("title", booklet.getTitle()) 19 | .add("firstName", booklet.getAuthor().getFirstName()) 20 | .add("lastName", booklet.getAuthor().getLastName()) 21 | .build(); 22 | } 23 | 24 | @Override 25 | public Booklet adaptFromJson(JsonObject json) { 26 | Booklet booklet = new Booklet( 27 | json.getString("title"), 28 | new Author(json.getString("firstName"), json.getString("lastName"))); 29 | return booklet; 30 | } 31 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/adaptors/FirstNameAdapter.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.adaptors; 2 | 3 | import javax.json.Json; 4 | import javax.json.JsonValue; 5 | import javax.json.bind.adapter.JsonbAdapter; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class FirstNameAdapter implements JsonbAdapter { 14 | 15 | @Override 16 | public JsonValue adaptToJson(String firstName) { 17 | return Json.createValue(firstName.subSequence(0,1).toString()); 18 | } 19 | 20 | @Override 21 | public String adaptFromJson(JsonValue json) { 22 | return json.toString(); 23 | } 24 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/customcreator/Author.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.customcreator; 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 Author { 12 | 13 | private String firstName; 14 | private String lastName; 15 | 16 | public Author() { 17 | } 18 | 19 | public Author(String firstName, String lastName) { 20 | this.firstName = firstName; 21 | this.lastName = lastName; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public void setFirstName(String firstName) { 29 | this.firstName = firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | public void setLastName(String lastName) { 37 | this.lastName = lastName; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Author author = (Author) o; 45 | return Objects.equals(firstName, author.firstName) && 46 | Objects.equals(lastName, author.lastName); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(firstName, lastName); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Author{" + 57 | "firstName='" + firstName + '\'' + 58 | ", lastName='" + lastName + '\'' + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/customcreator/Magazine.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.customcreator; 2 | 3 | 4 | import javax.json.bind.annotation.JsonbCreator; 5 | import javax.json.bind.annotation.JsonbProperty; 6 | import java.util.Objects; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | public class Magazine { 15 | 16 | private String title; 17 | 18 | private Author authorName; 19 | 20 | @JsonbCreator 21 | public Magazine(@JsonbProperty("bookTitle") String title, 22 | @JsonbProperty("firstName") String firstName, 23 | @JsonbProperty("surname") String lastName) { 24 | this.title = title; 25 | this.authorName = new Author(firstName, lastName); 26 | } 27 | 28 | public Magazine() { 29 | } 30 | 31 | public String getTitle() { 32 | return title; 33 | } 34 | 35 | public void setTitle(String title) { 36 | this.title = title; 37 | } 38 | 39 | public Author getAuthorName() { 40 | return authorName; 41 | } 42 | 43 | public void setAuthorName(Author authorName) { 44 | this.authorName = authorName; 45 | } 46 | 47 | @Override 48 | public boolean equals(Object o) { 49 | if (this == o) return true; 50 | if (o == null || getClass() != o.getClass()) return false; 51 | Magazine magazine = (Magazine) o; 52 | return 53 | Objects.equals(title, magazine.title) && 54 | Objects.equals(authorName, magazine.authorName); 55 | } 56 | 57 | @Override 58 | public int hashCode() { 59 | return Objects.hash(title, authorName); 60 | } 61 | 62 | @Override 63 | public String toString() { 64 | return "Magazine{" + 65 | "title='" + title + '\'' + 66 | ", authorName=" + authorName + 67 | '}'; 68 | } 69 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/dateandnumberformat/Author.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.dateandnumberformat; 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 Author { 12 | 13 | private String firstName; 14 | private String lastName; 15 | 16 | public Author() { 17 | } 18 | 19 | public Author(String firstName, String lastName) { 20 | this.firstName = firstName; 21 | this.lastName = lastName; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public void setFirstName(String firstName) { 29 | this.firstName = firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | public void setLastName(String lastName) { 37 | this.lastName = lastName; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Author author = (Author) o; 45 | return Objects.equals(firstName, author.firstName) && 46 | Objects.equals(lastName, author.lastName); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(firstName, lastName); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Author{" + 57 | "firstName='" + firstName + '\'' + 58 | ", lastName='" + lastName + '\'' + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/miscellaneous/Author.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.miscellaneous; 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 Author { 12 | 13 | private String firstName; 14 | private String lastName; 15 | 16 | public Author() { 17 | } 18 | 19 | public Author(String firstName, String lastName) { 20 | this.firstName = firstName; 21 | this.lastName = lastName; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public void setFirstName(String firstName) { 29 | this.firstName = firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | public void setLastName(String lastName) { 37 | this.lastName = lastName; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Author author = (Author) o; 45 | return Objects.equals(firstName, author.firstName) && 46 | Objects.equals(lastName, author.lastName); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(firstName, lastName); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Author{" + 57 | "firstName='" + firstName + '\'' + 58 | ", lastName='" + lastName + '\'' + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/miscellaneous/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.miscellaneous; 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-b-1-0/src/main/java/com/readlearncode/devWorks/part2/propertname/Author.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.propertname; 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 Author { 12 | 13 | private String firstName; 14 | private String lastName; 15 | 16 | public Author() { 17 | } 18 | 19 | public Author(String firstName, String lastName) { 20 | this.firstName = firstName; 21 | this.lastName = lastName; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public void setFirstName(String firstName) { 29 | this.firstName = firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | public void setLastName(String lastName) { 37 | this.lastName = lastName; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Author author = (Author) o; 45 | return Objects.equals(firstName, author.firstName) && 46 | Objects.equals(lastName, author.lastName); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(firstName, lastName); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Author{" + 57 | "firstName='" + firstName + '\'' + 58 | ", lastName='" + lastName + '\'' + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/propertname/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.propertname; 2 | 3 | import javax.json.bind.annotation.JsonbProperty; 4 | import java.util.Objects; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | public class Book { 13 | 14 | @JsonbProperty("isbn") 15 | private String id; 16 | 17 | private String title; 18 | 19 | private String author; 20 | 21 | public Book() { 22 | } 23 | 24 | public Book(String id, String title, String author) { 25 | this.id = id; 26 | this.title = title; 27 | this.author = author; 28 | } 29 | 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | public void setId(String id) { 35 | this.id = id; 36 | } 37 | 38 | @JsonbProperty("bookTitle") 39 | public String getTitle() { 40 | return title; 41 | } 42 | 43 | public void setTitle(String title) { 44 | this.title = title; 45 | } 46 | 47 | public String getAuthor() { 48 | return author; 49 | } 50 | 51 | @JsonbProperty("authorName") 52 | public void setAuthor(String author) { 53 | this.author = author; 54 | } 55 | 56 | @Override 57 | public boolean equals(Object o) { 58 | if (this == o) return true; 59 | if (o == null || getClass() != o.getClass()) return false; 60 | Book book = (Book) o; 61 | return Objects.equals(id, book.id) && 62 | Objects.equals(title, book.title) && 63 | Objects.equals(author, book.author); 64 | } 65 | 66 | @Override 67 | public int hashCode() { 68 | return Objects.hash(id, title, author); 69 | } 70 | 71 | @Override 72 | public String toString() { 73 | return "Book{" + 74 | "id='" + id + '\'' + 75 | ", title='" + title + '\'' + 76 | ", author=" + author + 77 | '}'; 78 | } 79 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/propertyorder/Author.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.propertyorder; 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 Author { 12 | 13 | private String firstName; 14 | private String lastName; 15 | 16 | public Author() { 17 | } 18 | 19 | public Author(String firstName, String lastName) { 20 | this.firstName = firstName; 21 | this.lastName = lastName; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public void setFirstName(String firstName) { 29 | this.firstName = firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | public void setLastName(String lastName) { 37 | this.lastName = lastName; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Author author = (Author) o; 45 | return Objects.equals(firstName, author.firstName) && 46 | Objects.equals(lastName, author.lastName); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(firstName, lastName); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Author{" + 57 | "firstName='" + firstName + '\'' + 58 | ", lastName='" + lastName + '\'' + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/propertyorder/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.propertyorder; 2 | 3 | import javax.json.bind.annotation.JsonbPropertyOrder; 4 | import java.util.Objects; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | @JsonbPropertyOrder(value = {"id", "title", "author"}) 13 | public class Book { 14 | 15 | private String id; 16 | 17 | private String title; 18 | 19 | private String author; 20 | 21 | public Book() { 22 | } 23 | 24 | public Book(String id, String title, String author) { 25 | this.id = id; 26 | this.title = title; 27 | this.author = author; 28 | } 29 | 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | public void setId(String id) { 35 | this.id = id; 36 | } 37 | 38 | public String getTitle() { 39 | return title; 40 | } 41 | 42 | public void setTitle(String title) { 43 | this.title = title; 44 | } 45 | 46 | public String getAuthor() { 47 | return author; 48 | } 49 | 50 | public void setAuthor(String author) { 51 | this.author = author; 52 | } 53 | 54 | @Override 55 | public boolean equals(Object o) { 56 | if (this == o) return true; 57 | if (o == null || getClass() != o.getClass()) return false; 58 | Book book = (Book) o; 59 | return Objects.equals(id, book.id) && 60 | Objects.equals(title, book.title) && 61 | Objects.equals(author, book.author); 62 | } 63 | 64 | @Override 65 | public int hashCode() { 66 | return Objects.hash(id, title, author); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "Book{" + 72 | "id='" + id + '\'' + 73 | ", title='" + title + '\'' + 74 | ", author=" + author + 75 | '}'; 76 | } 77 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/serialiseranddeserialiser/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.serialiseranddeserialiser; 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-b-1-0/src/main/java/com/readlearncode/devWorks/part2/serialiseranddeserialiser/BookDeserializer.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.serialiseranddeserialiser; 2 | 3 | 4 | import javax.json.bind.serializer.DeserializationContext; 5 | import javax.json.bind.serializer.JsonbDeserializer; 6 | import javax.json.stream.JsonParser; 7 | import java.lang.reflect.Type; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | public class BookDeserializer implements JsonbDeserializer { 16 | 17 | @Override 18 | public String deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { 19 | while (parser.hasNext()) { 20 | JsonParser.Event event = parser.next(); 21 | if (event == JsonParser.Event.KEY_NAME) { 22 | String keyName = parser.getString(); 23 | if (keyName.equals("id")) { 24 | return ctx.deserialize(String.class, parser); 25 | } 26 | } 27 | parser.next(); 28 | } 29 | return ""; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/serialiseranddeserialiser/BookSerializer.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.serialiseranddeserialiser; 2 | 3 | 4 | import javax.json.bind.serializer.JsonbSerializer; 5 | import javax.json.bind.serializer.SerializationContext; 6 | import javax.json.stream.JsonGenerator; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | public class BookSerializer implements JsonbSerializer { 15 | 16 | @Override 17 | public void serialize(Book book, JsonGenerator generator, SerializationContext ctx) { 18 | generator.writeStartObject(); 19 | generator.write("id", "QWE-123-RTS"); 20 | generator.write("title", book.getTitle()); 21 | generator.write("firstName", book.getAuthor().split(" ")[0]); 22 | generator.write("lastName", book.getAuthor().split(" ")[1]); 23 | generator.writeEnd(); 24 | } 25 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/treatementofnulls/Author.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.treatementofnulls; 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 Author { 12 | 13 | private String firstName; 14 | private String lastName; 15 | 16 | public Author() { 17 | } 18 | 19 | public Author(String firstName, String lastName) { 20 | this.firstName = firstName; 21 | this.lastName = lastName; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public void setFirstName(String firstName) { 29 | this.firstName = firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | public void setLastName(String lastName) { 37 | this.lastName = lastName; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Author author = (Author) o; 45 | return Objects.equals(firstName, author.firstName) && 46 | Objects.equals(lastName, author.lastName); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(firstName, lastName); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Author{" + 57 | "firstName='" + firstName + '\'' + 58 | ", lastName='" + lastName + '\'' + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/treatementofnulls/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.treatementofnulls; 2 | 3 | import javax.json.bind.annotation.JsonbNillable; 4 | import java.util.Objects; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | @JsonbNillable 13 | public class Book { 14 | 15 | private String id; 16 | 17 | private String title; 18 | 19 | private String author; 20 | 21 | public Book() { 22 | } 23 | 24 | public Book(String id, String title, String author) { 25 | this.id = id; 26 | this.title = title; 27 | this.author = author; 28 | } 29 | 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | public void setId(String id) { 35 | this.id = id; 36 | } 37 | 38 | public String getTitle() { 39 | return title; 40 | } 41 | 42 | public void setTitle(String title) { 43 | this.title = title; 44 | } 45 | 46 | public String getAuthor() { 47 | return author; 48 | } 49 | 50 | public void setAuthor(String author) { 51 | this.author = author; 52 | } 53 | 54 | @Override 55 | public boolean equals(Object o) { 56 | if (this == o) return true; 57 | if (o == null || getClass() != o.getClass()) return false; 58 | Book book = (Book) o; 59 | return Objects.equals(id, book.id) && 60 | Objects.equals(title, book.title) && 61 | Objects.equals(author, book.author); 62 | } 63 | 64 | @Override 65 | public int hashCode() { 66 | return Objects.hash(id, title, author); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "Book{" + 72 | "id='" + id + '\'' + 73 | ", title='" + title + '\'' + 74 | ", author=" + author + 75 | '}'; 76 | } 77 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/treatementofnulls/Booklet.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.treatementofnulls; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Author; 4 | 5 | import java.util.Objects; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class Booklet { 14 | 15 | private String title; 16 | 17 | private Author author; 18 | 19 | public Booklet(String title, String firstName, String lastName) { 20 | this.title = title; 21 | this.author = new Author(firstName, lastName); 22 | } 23 | 24 | public String getTitle() { 25 | return title; 26 | } 27 | 28 | public void setTitle(String title) { 29 | this.title = title; 30 | } 31 | 32 | public com.readlearncode.devWorks.overview.domain.Author getAuthor() { 33 | return author; 34 | } 35 | 36 | public void setAuthor(Author author) { 37 | this.author = author; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Booklet booklet = (Booklet) o; 45 | return Objects.equals(title, booklet.title) && 46 | Objects.equals(author, booklet.author); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(title, author); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Booklet{" + 57 | "title='" + title + '\'' + 58 | ", author=" + author + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/visibility/Author.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.visibility; 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 Author { 12 | 13 | private String firstName; 14 | private String lastName; 15 | 16 | public Author() { 17 | } 18 | 19 | public Author(String firstName, String lastName) { 20 | this.firstName = firstName; 21 | this.lastName = lastName; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public void setFirstName(String firstName) { 29 | this.firstName = firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | public void setLastName(String lastName) { 37 | this.lastName = lastName; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Author author = (Author) o; 45 | return Objects.equals(firstName, author.firstName) && 46 | Objects.equals(lastName, author.lastName); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(firstName, lastName); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Author{" + 57 | "firstName='" + firstName + '\'' + 58 | ", lastName='" + lastName + '\'' + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/devWorks/part2/visibility/Book.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.visibility; 2 | 3 | import javax.json.bind.annotation.JsonbTransient; 4 | import java.util.Objects; 5 | 6 | /** 7 | * Source code github.com/readlearncode 8 | * 9 | * @author Alex Theedom www.readlearncode.com 10 | * @version 1.0 11 | */ 12 | public class Book { 13 | 14 | @JsonbTransient 15 | private String id; 16 | 17 | private String title; 18 | 19 | private String author; 20 | 21 | public Book() { 22 | } 23 | 24 | public Book(String id, String title, String author) { 25 | this.id = id; 26 | this.title = title; 27 | this.author = author; 28 | } 29 | 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | public void setId(String id) { 35 | this.id = id; 36 | } 37 | 38 | public String getTitle() { 39 | return title; 40 | } 41 | 42 | public void setTitle(String title) { 43 | this.title = title; 44 | } 45 | 46 | public String getAuthor() { 47 | return author; 48 | } 49 | 50 | public void setAuthor(String author) { 51 | this.author = author; 52 | } 53 | 54 | @Override 55 | public boolean equals(Object o) { 56 | if (this == o) return true; 57 | if (o == null || getClass() != o.getClass()) return false; 58 | Book book = (Book) o; 59 | return Objects.equals(id, book.id) && 60 | Objects.equals(title, book.title) && 61 | Objects.equals(author, book.author); 62 | } 63 | 64 | @Override 65 | public int hashCode() { 66 | return Objects.hash(id, title, author); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "Book{" + 72 | "id='" + id + '\'' + 73 | ", title='" + title + '\'' + 74 | ", author=" + author + 75 | '}'; 76 | } 77 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/domain/Author.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.domain; 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 Author { 12 | 13 | private String firstName; 14 | private String lastName; 15 | 16 | public Author() { 17 | } 18 | 19 | public Author(String firstName, String lastName) { 20 | this.firstName = firstName; 21 | this.lastName = lastName; 22 | } 23 | 24 | public String getFirstName() { 25 | return firstName; 26 | } 27 | 28 | public void setFirstName(String firstName) { 29 | this.firstName = firstName; 30 | } 31 | 32 | public String getLastName() { 33 | return lastName; 34 | } 35 | 36 | public void setLastName(String lastName) { 37 | this.lastName = lastName; 38 | } 39 | 40 | @Override 41 | public boolean equals(Object o) { 42 | if (this == o) return true; 43 | if (o == null || getClass() != o.getClass()) return false; 44 | Author author = (Author) o; 45 | return Objects.equals(firstName, author.firstName) && 46 | Objects.equals(lastName, author.lastName); 47 | } 48 | 49 | @Override 50 | public int hashCode() { 51 | return Objects.hash(firstName, lastName); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "Author{" + 57 | "firstName='" + firstName + '\'' + 58 | ", lastName='" + lastName + '\'' + 59 | '}'; 60 | } 61 | } -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/domain/Binding.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.domain; 2 | 3 | /** 4 | * Source code github.com/readlearncode 5 | * 6 | * @author Alex Theedom www.readlearncode.com 7 | * @version 1.0 8 | */ 9 | public enum Binding { 10 | 11 | HARD_BACK, SOFT_BACK 12 | } 13 | -------------------------------------------------------------------------------- /json-b-1-0/src/main/java/com/readlearncode/domain/Booklet.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.domain; 2 | 3 | import javax.json.bind.annotation.*; 4 | import javax.json.bind.config.PropertyOrderStrategy; 5 | import java.util.Objects; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | @JsonbNillable 14 | @JsonbPropertyOrder(PropertyOrderStrategy.REVERSE) 15 | public class Booklet { 16 | 17 | private String title; 18 | 19 | @JsonbProperty("cost") 20 | @JsonbNumberFormat("#0.00") 21 | private Float price; 22 | 23 | private Author author; 24 | 25 | @JsonbTransient 26 | public String getTitle() { 27 | return title; 28 | } 29 | 30 | @JsonbTransient 31 | public void setTitle(String title) { 32 | this.title = title; 33 | } 34 | 35 | public Float getPrice() { 36 | return price; 37 | } 38 | 39 | public void setPrice(Float price) { 40 | this.price = price; 41 | } 42 | 43 | public Author getAuthor() { 44 | return author; 45 | } 46 | 47 | public void setAuthor(Author author) { 48 | this.author = author; 49 | } 50 | 51 | @Override 52 | public boolean equals(Object o) { 53 | if (this == o) return true; 54 | if (o == null || getClass() != o.getClass()) return false; 55 | Booklet booklet = (Booklet) o; 56 | return Objects.equals(title, booklet.title) && 57 | Objects.equals(price, booklet.price) && 58 | Objects.equals(author, booklet.author); 59 | } 60 | 61 | @Override 62 | public int hashCode() { 63 | return Objects.hash(title, price, author); 64 | } 65 | 66 | @Override 67 | public String toString() { 68 | return "Booklet{" + 69 | "title='" + title + '\'' + 70 | ", price=" + price + 71 | ", author=" + author + 72 | '}'; 73 | } 74 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/overview/BookDeserializerTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Book; 4 | import org.junit.Test; 5 | 6 | import javax.json.bind.Jsonb; 7 | import javax.json.bind.JsonbBuilder; 8 | import javax.json.bind.JsonbConfig; 9 | 10 | import static org.assertj.core.api.Java6Assertions.assertThat; 11 | 12 | /** 13 | * Source code github.com/readlearncode 14 | * 15 | * @author Alex Theedom www.readlearncode.com 16 | * @version 1.0 17 | */ 18 | public class BookDeserializerTest { 19 | 20 | @Test 21 | public void givenJSON_shouldDeserializeToBook() { 22 | Book expectedBook = new Book("SHDUJ-4532", "Fun with Java", "Alex Theedom"); 23 | String json = "{\"com.readlearncode.devWorks.overview.domain.Book\":{\"author\":\"Alex Theedom\",\"id\":\"SHDUJ-4532\",\"title\":\"Fun with Java\"}}"; 24 | JsonbConfig config = new JsonbConfig().withDeserializers(new BookDeserializer()); 25 | Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(config).build(); 26 | Book actualBook = jsonb.fromJson(json, Book.class); 27 | 28 | assertThat(actualBook).isEqualTo(expectedBook); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/overview/BookSerializerTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Book; 4 | import org.junit.Test; 5 | 6 | import javax.json.bind.Jsonb; 7 | import javax.json.bind.JsonbBuilder; 8 | import javax.json.bind.JsonbConfig; 9 | 10 | import static org.assertj.core.api.Java6Assertions.assertThat; 11 | 12 | /** 13 | * Source code github.com/readlearncode 14 | * 15 | * @author Alex Theedom www.readlearncode.com 16 | * @version 1.0 17 | */ 18 | public class BookSerializerTest { 19 | 20 | @Test 21 | public void givenSerialize_shouldSerialiseBook() { 22 | Book book = new Book("SHDUJ-4532", "Fun with Java", "Alex Theedom"); 23 | String expectedJson = "{\"com.readlearncode.devWorks.overview.domain.Book\":{\"author\":\"Alex Theedom\",\"id\":\"SHDUJ-4532\",\"title\":\"Fun with Java\"}}"; 24 | JsonbConfig config = new JsonbConfig().withSerializers(new BookSerializer()); 25 | Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(config).build(); 26 | String actualJson = jsonb.toJson(book); 27 | assertThat(actualJson).isEqualTo(expectedJson); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/overview/BookletAdapterTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Booklet; 4 | import org.junit.Test; 5 | 6 | import javax.json.bind.JsonbBuilder; 7 | import javax.json.bind.JsonbConfig; 8 | 9 | import static org.assertj.core.api.Java6Assertions.assertThat; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | public class BookletAdapterTest { 18 | 19 | @Test 20 | public void givenJSON_shouldUseAdapterToDeserialise() { 21 | String actualJson = "{\"title\":\"Fun with Java\",\"firstName\":\"Alex\",\"lastName\":\"Theedom\"}"; 22 | Booklet actualBooklet = new Booklet("Fun with Java", "Alex", "Theedom"); 23 | JsonbConfig config = new JsonbConfig().withAdapters(new BookletAdapter()); 24 | Booklet expectedBooklet = JsonbBuilder.create(config).fromJson(actualJson, Booklet.class); 25 | 26 | assertThat(actualBooklet).isEqualTo(expectedBooklet); 27 | } 28 | 29 | @Test 30 | public void givenBookObject_shouldUseAdapterToSerialise() { 31 | String expectedJson = "{\"title\":\"Fun with Java\",\"firstName\":\"Alex\",\"lastName\":\"Theedom\"}"; 32 | Booklet booklet = new Booklet("Fun with Java", "Alex", "Theedom"); 33 | JsonbConfig config = new JsonbConfig().withAdapters(new BookletAdapter()); 34 | String actualJson = JsonbBuilder.create(config).toJson(booklet); 35 | 36 | assertThat(actualJson).isEqualTo(expectedJson); 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/overview/EnumExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import org.junit.Ignore; 4 | import org.junit.Test; 5 | 6 | import static org.assertj.core.api.Java6Assertions.assertThat; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | public class EnumExampleTest { 15 | 16 | @Test 17 | public void enumSerialisationInObject() { 18 | String expectedJson = "{\"binding\":\"Hard Back\"}"; 19 | EnumExample enumExample = new EnumExample(); 20 | String actualJson = enumExample.enumSerialisationInObject(); 21 | assertThat(actualJson).isEqualTo(expectedJson); 22 | } 23 | 24 | @Test @Ignore 25 | public void givenEnum_shouldThrownExceptionWhenSerialised() { 26 | // assertThatThrownBy(() -> 27 | new EnumExample().enumSerialisation(); 28 | // ).isInstanceOf(javax.json.bind.JsonbException.class); 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/overview/MagazineSerializerTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.Author; 4 | import com.readlearncode.devWorks.overview.domain.Magazine; 5 | import org.junit.Test; 6 | 7 | import javax.json.bind.Jsonb; 8 | import javax.json.bind.JsonbBuilder; 9 | import javax.json.bind.JsonbConfig; 10 | 11 | import static org.assertj.core.api.Java6Assertions.assertThat; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | public class MagazineSerializerTest { 20 | 21 | @Test 22 | public void givenSerialize_shouldSerialiseMagazine() { 23 | 24 | Magazine magazine = new Magazine(); 25 | magazine.setId("1234-QWERT"); 26 | magazine.setTitle("Fun with Java"); 27 | magazine.setAuthor(new Author("Alex","Theedom")); 28 | 29 | String expectedJson = "{\"name\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"}}"; 30 | 31 | JsonbConfig config = new JsonbConfig().withSerializers(new MagazineSerializer()); 32 | Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(config).build(); 33 | String actualJson = jsonb.toJson(magazine); 34 | assertThat(actualJson).isEqualTo(expectedJson); 35 | 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/overview/MinimalExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | 4 | import com.readlearncode.devWorks.overview.domain.Book; 5 | import org.junit.Test; 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 MinimalExampleTest { 16 | 17 | @Test 18 | public void givenBookInstance_shouldSerialiseToJSONString() { 19 | String expectedJson = "{\"author\":\"Alex Theedom\",\"id\":\"SHDUJ-4532\",\"title\":\"Fun with Java\"}"; 20 | 21 | MinimalExample minimalExample = new MinimalExample(); 22 | String actualJson = minimalExample.serializeBook(); 23 | 24 | assertThat(actualJson).isEqualTo(expectedJson); 25 | } 26 | 27 | @Test 28 | public void givenJSONString_shouldDeserializeToBookObject() { 29 | Book expectedBook = new Book("SHDUJ-4532", "Fun with Java", "Alex Theedom"); 30 | 31 | MinimalExample minimalExample = new MinimalExample(); 32 | Book actualBook = minimalExample.deserializeBook(); 33 | 34 | assertThat(actualBook).isEqualTo(expectedBook); 35 | } 36 | 37 | 38 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/overview/NestedClassExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.overview; 2 | 3 | import com.readlearncode.devWorks.overview.domain.OuterClass; 4 | import org.junit.Ignore; 5 | import org.junit.Test; 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 NestedClassExampleTest { 16 | 17 | @Test 18 | public void givenNestedInnerClass_shouldSerializeNestedClasses() { 19 | String expectedJson = "{\"name\":\"Inner Class\"}"; 20 | 21 | NestedClassExample nestedClassExample = new NestedClassExample(); 22 | String actualJson = nestedClassExample.serializeNestedClasses(); 23 | 24 | assertThat(actualJson).isEqualTo(expectedJson); 25 | } 26 | 27 | @Test @Ignore 28 | public void givenJson_shouldDeserialiseToNestedClass() { 29 | 30 | OuterClass.InnerClass expectedInner = new OuterClass().new InnerClass(); 31 | 32 | NestedClassExample nestedClassExample = new NestedClassExample(); 33 | OuterClass.InnerClass actualInnerClass = nestedClassExample.deserialiseNestedClasses(); 34 | 35 | assertThat(actualInnerClass).isEqualTo(expectedInner); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part1/datetimetypes/DateTimeExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.datetimetypes; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.Jsonb; 6 | import javax.json.bind.JsonbBuilder; 7 | import java.text.ParseException; 8 | import java.text.SimpleDateFormat; 9 | import java.time.Duration; 10 | import java.util.Date; 11 | 12 | import static org.assertj.core.api.Java6Assertions.assertThat; 13 | 14 | /** 15 | * Source code github.com/readlearncode 16 | * 17 | * @author Alex Theedom www.readlearncode.com 18 | * @version 1.0 19 | */ 20 | public class DateTimeExampleTest { 21 | 22 | private Jsonb jsonb = JsonbBuilder.create(); 23 | 24 | @Test 25 | public void givenDate_shouldParseAndSerialise() throws ParseException { 26 | SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 27 | Date date = sdf.parse("25/12/2018"); 28 | String json = jsonb.toJson(date); 29 | 30 | assertThat("\"2018-12-25T00:00:00Z[UTC]\"").isEqualTo(json); 31 | } 32 | 33 | @Test 34 | public void givenDuration_shouldSerialiseAsDuration() throws ParseException { 35 | String json = jsonb.toJson(Duration.ofHours(4).plusMinutes(3).plusSeconds(2)); 36 | 37 | assertThat("\"PT4H3M2S\"").isEqualTo(json); 38 | } 39 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part1/jsonptypes/JsonPTypesTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.jsonptypes; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.Json; 6 | import javax.json.JsonArray; 7 | import javax.json.JsonValue; 8 | import javax.json.bind.Jsonb; 9 | import javax.json.bind.JsonbBuilder; 10 | 11 | import static org.assertj.core.api.Java6Assertions.assertThat; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | public class JsonPTypesTest { 20 | 21 | private Jsonb jsonb = JsonbBuilder.create(); 22 | 23 | @Test 24 | public void givenJsonArray_shouldSerialiseToJsonArray() { 25 | 26 | String expectedJson = "[\"John\",null]"; 27 | 28 | JsonArray value = Json.createArrayBuilder() 29 | .add(Json.createValue("John")) 30 | .add(JsonValue.NULL) 31 | .build(); 32 | 33 | String actualJson = jsonb.toJson(value); 34 | 35 | assertThat(actualJson).isEqualTo(expectedJson); 36 | } 37 | 38 | 39 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part1/simpleexample/SimplestExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.simpleexample; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.JsonbBuilder; 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 SimplestExampleTest { 16 | 17 | @Test 18 | public void givenBookObject_shouldSerialise() { 19 | String expectedJson = "{\"title\":\"Fun with Java\"}"; 20 | 21 | Book book = new Book(); 22 | book.title = "Fun with Java"; 23 | 24 | String actualJson = JsonbBuilder.create().toJson(book); 25 | 26 | assertThat(actualJson).isEqualTo(expectedJson); 27 | 28 | /* 29 | { 30 | "title": "Fun with Java" 31 | } 32 | */ 33 | } 34 | 35 | @Test 36 | public void givenBookJson_shouldDeserialiseToBookObject(){ 37 | 38 | Book expectedBook = new Book(); 39 | expectedBook.title = "Fun with Java"; 40 | 41 | String json = "{\"title\":\"Fun with Java\"}"; 42 | 43 | Book actualBook = JsonbBuilder.create().fromJson(json, Book.class); 44 | 45 | assertThat(actualBook.title).isEqualTo(expectedBook.title); 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part1/treatementofnulls/NullsTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.treatementofnulls; 2 | 3 | import com.readlearncode.devWorks.part1.treatmentofnulls.OptionalAndValueExample; 4 | import com.readlearncode.devWorks.part1.treatmentofnulls.OptionalExample; 5 | import org.junit.Test; 6 | 7 | import javax.json.bind.Jsonb; 8 | import javax.json.bind.JsonbBuilder; 9 | import java.util.Optional; 10 | 11 | import static org.assertj.core.api.Java6Assertions.assertThat; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | public class NullsTest { 20 | 21 | private Jsonb jsonb = JsonbBuilder.create(); 22 | 23 | @Test 24 | public void givenIntegerJSON_shouldDeserialiseToOptionalOfInteger() { 25 | 26 | String json = jsonb.toJson(new OptionalExample(null)); 27 | 28 | assertThat(jsonb.fromJson(json, OptionalExample.class)) 29 | .hasFieldOrPropertyWithValue("value", Optional.ofNullable(null)); 30 | } 31 | 32 | 33 | @Test 34 | public void givenEmptyJson_shouldNotPreserveRoundTripEquivalence() { 35 | assertThat( 36 | jsonb.fromJson("{}", OptionalAndValueExample.class 37 | ) 38 | ).hasFieldOrPropertyWithValue("value", null); 39 | } 40 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part1/unmappedtypes/ObjectToMapExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part1.unmappedtypes; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.JsonbBuilder; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | 9 | import static org.assertj.core.api.Java6Assertions.assertThat; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | public class ObjectToMapExampleTest { 18 | 19 | @Test 20 | public void givenJSONObject_shouldDeserialiseIntoMap() { 21 | 22 | String json = "{\"title\":\"Fun with Java\",\"price\":24.99,\"issue\":null}"; 23 | 24 | Object o = JsonbBuilder.create().fromJson(json, Map.class); 25 | 26 | assertThat(o).isInstanceOf(new HashMap().getClass()); 27 | } 28 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/customcreator/CustomCreatorTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.customcreator; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.JsonbBuilder; 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 CustomCreatorTest { 16 | 17 | @Test 18 | public void givenCustomCreatorFactoryMethod_shouldUseCustomCreator() { 19 | String json = "{\"author\":\"Alex Theedom\",\"title\":\"Fun with JSON-B\",\"id\":\"ABC-123-XYZ\"}"; 20 | Book book = JsonbBuilder.create().fromJson(json, Book.class); 21 | assertThat(book.getId()).isEqualTo("ABC-123-XYZ"); 22 | assertThat(book.getTitle()).isEqualTo("Fun with JSON-B"); 23 | assertThat(book.getAuthor()).isEqualTo("Alex Theedom"); 24 | } 25 | 26 | @Test 27 | public void givenCustomCreatorConstructor_shouldUseCustomCreator() { 28 | String json = "{\"surname\":\"Theedom\",\"bookTitle\":\"Fun with JSON-B\",\"firstName\":\"Alex\"}"; 29 | Magazine magazine = JsonbBuilder.create().fromJson(json, Magazine.class); 30 | assertThat(magazine.getTitle()).isEqualTo("Fun with JSON-B"); 31 | assertThat(magazine.getAuthorName().getFirstName()).isEqualTo("Alex"); 32 | assertThat(magazine.getAuthorName().getLastName()).isEqualTo("Theedom"); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/dateandnumberformat/CustomiseNumberFormatTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.dateandnumberformat; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.JsonbBuilder; 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 CustomiseNumberFormatTest { 16 | 17 | @Test 18 | public void givenJsonbNumberFormatOnPrice_shouldFormatNumber() { 19 | String expectedJson = "{\"author\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"},\"price\":\"19.00\",\"title\":\"Fun with JSON-B\"}"; 20 | Booklet booklet = new Booklet("Fun with JSON-B", "Alex", "Theedom", 19); 21 | String actualJson = JsonbBuilder.create().toJson(booklet); 22 | assertThat(actualJson).isEqualTo(expectedJson); 23 | } 24 | 25 | 26 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/miscellaneous/MiscellaneousTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.miscellaneous; 2 | 3 | 4 | import org.junit.Test; 5 | 6 | import javax.json.bind.JsonbBuilder; 7 | import javax.json.bind.JsonbConfig; 8 | import java.util.Locale; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | public class MiscellaneousTest { 17 | 18 | @Test 19 | public void characterEncoding(){ 20 | JsonbConfig jsonbConfig = new JsonbConfig().withEncoding("UTF-32"); 21 | } 22 | 23 | @Test 24 | public void local(){ 25 | JsonbConfig jsonbConfig = new JsonbConfig().withLocale(Locale.CANADA); 26 | } 27 | 28 | @Test 29 | public void prettyJsonFormat(){ 30 | JsonbConfig jsonbConfig = new JsonbConfig().withFormatting(true); 31 | Book book = new Book("ABC-123-XYZ", "Fun with JSON-B", "Alex Theedom"); 32 | String actualJson = JsonbBuilder.create(jsonbConfig).toJson(book); 33 | System.out.println(actualJson); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/propertname/CustomiseJsonbPropertyTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.propertname; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.JsonbBuilder; 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 CustomiseJsonbPropertyTest { 16 | 17 | @Test 18 | public void givenFieldAnnotateWithJsonbProperty_shouldChangeOfField() { 19 | Book book = new Book("ABC-123-XYZ", "Fun with JSON-B", "Alex Theedom"); 20 | String actualJson = JsonbBuilder.create().toJson(book); 21 | assertThat(actualJson).contains("isbn"); 22 | } 23 | 24 | @Test 25 | public void givenGetterAnnotateWithJsonbProperty_shouldChangeOfJsonProperty() { 26 | Book book = new Book("ABC-123-XYZ", "Fun with JSON-B", "Alex Theedom"); 27 | String actualJson = JsonbBuilder.create().toJson(book); 28 | assertThat(actualJson).contains("bookTitle"); 29 | } 30 | 31 | @Test 32 | public void givenSetterAnnotateWithJsonbProperty_shouldDeserialiseJsonProperty() { 33 | String json = "{\"authorName\":\"Alex Theedom\",\"bookTitle\":\"Fun with JSON-B\",\"isbn\":\"ABC-123-XYZ\"}"; 34 | Book book = JsonbBuilder.create().fromJson(json, Book.class); 35 | assertThat(book.getAuthor()).contains("Alex Theedom"); 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/serialiseranddeserialiser/BookDeserializerTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.serialiseranddeserialiser; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.Jsonb; 6 | import javax.json.bind.JsonbBuilder; 7 | import javax.json.bind.JsonbConfig; 8 | 9 | import static org.assertj.core.api.Java6Assertions.assertThat; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | public class BookDeserializerTest { 18 | 19 | @Test 20 | public void givenJSON_shouldDeserializeToBook() { 21 | String json = "{\"id\":\"QWE-123-RTS\",\"lastName\":\"Theedom\",\"firstName\":\"Alex\",\"title\":\"Fun with Java\"}"; 22 | JsonbConfig config = new JsonbConfig().withDeserializers(new BookDeserializer()); 23 | Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(config).build(); 24 | String id = jsonb.fromJson(json, String.class); 25 | assertThat(id).isEqualTo("QWE-123-RTS"); 26 | } 27 | 28 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/serialiseranddeserialiser/BookSerializerTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.serialiseranddeserialiser; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.Jsonb; 6 | import javax.json.bind.JsonbBuilder; 7 | import javax.json.bind.JsonbConfig; 8 | 9 | import static org.assertj.core.api.Java6Assertions.assertThat; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | public class BookSerializerTest { 18 | 19 | @Test 20 | public void givenSerialize_shouldSerialiseBook() { 21 | Book book = new Book("SHDUJ-4532", "Fun with Java", "Alex Theedom"); 22 | String expectedJson = "{\"id\":\"QWE-123-RTS\",\"title\":\"Fun with Java\",\"firstName\":\"Alex\",\"lastName\":\"Theedom\"}"; 23 | JsonbConfig config = new JsonbConfig().withSerializers(new BookSerializer()); 24 | Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(config).build(); 25 | String actualJson = jsonb.toJson(book); 26 | assertThat(actualJson).isEqualTo(expectedJson); 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/treatementofnulls/CustomiseNullValuesTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.treatementofnulls; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.JsonbBuilder; 6 | import javax.json.bind.JsonbConfig; 7 | 8 | import static org.assertj.core.api.Java6Assertions.assertThat; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | public class CustomiseNullValuesTest { 17 | 18 | @Test 19 | public void givenNullValuesCustomisation_shouldPreserverNulls(){ 20 | String expectedJson = "{\"author\":{\"firstName\":null,\"lastName\":null},\"title\":\"Fun with Java EE\"}"; 21 | Booklet booklet = new Booklet("Fun with Java EE", null, null); 22 | JsonbConfig jsonbConfig = new JsonbConfig().withNullValues(true); 23 | String actualJson = JsonbBuilder.create(jsonbConfig).toJson(booklet); 24 | assertThat(actualJson).isEqualTo(expectedJson); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/treatementofnulls/NullsTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.treatementofnulls; 2 | 3 | import com.readlearncode.devWorks.part1.treatmentofnulls.OptionalAndValueExample; 4 | import com.readlearncode.devWorks.part1.treatmentofnulls.OptionalExample; 5 | import org.junit.Test; 6 | 7 | import javax.json.bind.Jsonb; 8 | import javax.json.bind.JsonbBuilder; 9 | import java.util.Optional; 10 | 11 | import static org.assertj.core.api.Java6Assertions.assertThat; 12 | 13 | /** 14 | * Source code github.com/readlearncode 15 | * 16 | * @author Alex Theedom www.readlearncode.com 17 | * @version 1.0 18 | */ 19 | public class NullsTest { 20 | 21 | private Jsonb jsonb = JsonbBuilder.create(); 22 | 23 | @Test 24 | public void givenIntegerJSON_shouldDeserialiseToOptionalOfInteger() { 25 | 26 | String json = jsonb.toJson(new OptionalExample(null)); 27 | 28 | assertThat(jsonb.fromJson(json, OptionalExample.class)) 29 | .hasFieldOrPropertyWithValue("value", Optional.ofNullable(null)); 30 | } 31 | 32 | 33 | @Test 34 | public void givenEmptyJson_shouldNotPreserveRoundTripEquivalence() { 35 | assertThat( 36 | jsonb.fromJson("{}", OptionalAndValueExample.class 37 | ) 38 | ).hasFieldOrPropertyWithValue("value", null); 39 | } 40 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/visibility/CustomisePropertyVisibilityStrategyTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.visibility; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.JsonbBuilder; 6 | import javax.json.bind.JsonbConfig; 7 | import javax.json.bind.config.PropertyVisibilityStrategy; 8 | import java.lang.reflect.Field; 9 | import java.lang.reflect.Method; 10 | import java.lang.reflect.Modifier; 11 | 12 | import static org.assertj.core.api.Java6Assertions.assertThat; 13 | 14 | /** 15 | * Source code github.com/readlearncode 16 | * 17 | * @author Alex Theedom www.readlearncode.com 18 | * @version 1.0 19 | */ 20 | public class CustomisePropertyVisibilityStrategyTest { 21 | 22 | PropertyVisibilityStrategy vis = new PropertyVisibilityStrategy() { 23 | @Override 24 | public boolean isVisible(Field field) { 25 | return Modifier.isProtected(field.getModifiers()); 26 | } 27 | 28 | @Override 29 | public boolean isVisible(Method method) { 30 | return !method.getName().contains("AlternativeTitle"); 31 | } 32 | }; 33 | 34 | @Test 35 | public void givenPropertyVisibilityStrategy_shouldChangeVisibility() { 36 | 37 | Magazine magazine = new Magazine("Fun with JSON binding", new Author("Alex", "Theedom"), "Fun with JSON-B"); 38 | 39 | String expectedJson = "{\"authorName\":{\"firstName\":\"Alex\",\"lastName\":\"Theedom\"},\"title\":\"Fun with JSON binding\"}"; 40 | JsonbConfig jsonbConfig = new JsonbConfig() 41 | .withPropertyVisibilityStrategy(vis); 42 | 43 | String actualJson = JsonbBuilder.create(jsonbConfig).toJson(magazine); 44 | 45 | assertThat(actualJson).isEqualTo(expectedJson); 46 | } 47 | 48 | 49 | } -------------------------------------------------------------------------------- /json-b-1-0/src/test/java/com/readlearncode/devWorks/part2/visibility/JsonbTransientTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.devWorks.part2.visibility; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.bind.JsonbBuilder; 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 JsonbTransientTest { 16 | 17 | @Test 18 | public void whenIDFieldIsAnnotatedJsonbTransient_shouldNotBeSerialised(){ 19 | 20 | Book book = new Book("ABC-123-XYZ", "Fun with JSON-B", "Alex Theedom"); 21 | String actualJson = JsonbBuilder.create().toJson(book); 22 | assertThat(actualJson).doesNotContain("id"); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /json-p-1-1/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | json-p-1-1 7 | war 8 | 9 | 10 | com.readlearncode 11 | Java-EE-8-Sampler 12 | 1.0 13 | 14 | 15 | json-p-1-1 16 | 17 | 18 | 1.1 19 | 20 | 21 | 22 | 23 | 24 | org.glassfish 25 | javax.json 26 | ${javax.json.version} 27 | provided 28 | 29 | 30 | 31 | 32 | 33 | json-p-1-1 34 | 35 | 36 | org.apache.maven.plugins 37 | maven-compiler-plugin 38 | ${maven-compiler-plugin.version} 39 | 40 | ${java.version} 41 | ${java.version} 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /json-p-1-1/src/main/java/com/readlearncode/Java8Integration.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import javax.json.JsonArray; 4 | import javax.json.JsonObject; 5 | import javax.json.JsonString; 6 | import javax.json.stream.JsonCollectors; 7 | import java.util.List; 8 | import java.util.stream.Collectors; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | public class Java8Integration extends JsonExample { 17 | 18 | private JsonObject jsonObject = loadJsonObject(); 19 | 20 | public List filterJsonArrayToList() { 21 | List topics = jsonObject.getJsonArray("topics").stream() 22 | .filter(jsonValue -> ((JsonString) jsonValue).getString().startsWith("C")) 23 | .map(jsonValue -> ((JsonString) jsonValue).getString()) 24 | .collect(Collectors.toList()); 25 | return topics; 26 | } 27 | 28 | public JsonArray filterJsonArrayToJsonArray() { 29 | JsonArray topics = jsonObject.getJsonArray("topics").stream() 30 | .filter(jsonValue -> ((JsonString) jsonValue).getString().startsWith("C")) 31 | .collect(JsonCollectors.toJsonArray()); 32 | return topics; 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /json-p-1-1/src/main/java/com/readlearncode/JsonExample.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.IOException; 7 | import java.io.InputStream; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | public class JsonExample { 16 | 17 | public static JsonObject loadJsonObject() { 18 | try (InputStream is = JsonPointerExample.class.getResourceAsStream("/jsondata-object.json"); 19 | JsonReader jsonReader = Json.createReader(is)) { 20 | return jsonReader.readObject(); 21 | } catch (IOException e) { 22 | return null; 23 | } 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /json-p-1-1/src/main/java/com/readlearncode/JsonMergeDiffExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import javax.json.Json; 4 | import javax.json.JsonMergePatch; 5 | import javax.json.JsonValue; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class JsonMergeDiffExample extends JsonExample { 14 | 15 | public JsonMergePatch createMergePatch(){ 16 | JsonValue source = Json.createValue("{\"colour\":\"blue\"}"); 17 | JsonValue target = Json.createValue("{\"colour\":\"red\"}"); 18 | JsonMergePatch jsonMergePatch = Json.createMergeDiff(source, target); 19 | return jsonMergePatch; 20 | } 21 | } -------------------------------------------------------------------------------- /json-p-1-1/src/main/java/com/readlearncode/JsonMergePatchExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import javax.json.Json; 4 | import javax.json.JsonMergePatch; 5 | import javax.json.JsonValue; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class JsonMergePatchExample extends JsonExample { 14 | 15 | public JsonValue changeValue() { 16 | JsonValue source = Json.createValue("{\"colour\":\"blue\"}"); 17 | JsonValue patch = Json.createValue("{\"colour\":\"red\"}"); 18 | JsonMergePatch jsonMergePatch = Json.createMergePatch(patch); 19 | return jsonMergePatch.apply(source); 20 | } 21 | 22 | public JsonValue addValue() { 23 | JsonValue source = Json.createValue("{\"colour\":\"blue\"}"); 24 | JsonValue patch = Json.createValue("{\"blue\":\"light\"}"); 25 | JsonMergePatch jsonMergePatch = Json.createMergePatch(patch); 26 | return jsonMergePatch.apply(source); 27 | } 28 | 29 | public JsonValue deleteValue() { 30 | JsonValue source = Json.createValue("{\"colour\":\"blue\"}"); 31 | JsonValue patch = Json.createValue("{\"colour\":null}"); 32 | JsonMergePatch jsonMergePatch = Json.createMergePatch(patch); 33 | return jsonMergePatch.apply(source); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /json-p-1-1/src/main/java/com/readlearncode/JsonPointerExample.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import javax.json.*; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class JsonPointerExample { 14 | 15 | private JsonObject jsonObject = loadJsonObject(); 16 | 17 | public String find() { 18 | JsonPointer pointer = Json.createPointer("/topics/1"); 19 | JsonString jsonValue = (JsonString) pointer.getValue(jsonObject); 20 | return jsonValue.getString(); 21 | } 22 | 23 | public String replace() { 24 | JsonPointer pointer = Json.createPointer("/topics/1"); 25 | JsonObject newJsonObject = pointer.replace(jsonObject, Json.createValue("Big Data")); 26 | JsonString jsonValue = (JsonString) pointer.getValue(newJsonObject); 27 | return jsonValue.getString(); 28 | } 29 | 30 | public String add(){ 31 | JsonPointer pointer = Json.createPointer("/topics/0"); 32 | JsonObject newJsonObject = pointer.add(jsonObject,Json.createValue("Java EE")); 33 | JsonString jsonValue = (JsonString) pointer.getValue(newJsonObject); 34 | return jsonValue.getString(); 35 | } 36 | 37 | private static JsonObject loadJsonObject() { 38 | try (InputStream is = JsonPointerExample.class.getResourceAsStream("/jsondata-object.json"); 39 | JsonReader jsonReader = Json.createReader(is)) { 40 | return jsonReader.readObject(); 41 | } catch (IOException e){ 42 | return null; 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /json-p-1-1/src/main/resources/jsondata-object.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "IBM developerWorks", 3 | "URL": "https://www.ibm.com/developerworks/", 4 | "topics": [ 5 | "Cognitive", 6 | "Cloud", 7 | "Data", 8 | "IoT", 9 | "Java" 10 | ], 11 | "series": [ 12 | "Sample Code...", 13 | "5 things you didn't know...", 14 | "Introduction to..." 15 | ], 16 | "notes": null, 17 | "live": true, 18 | "visitors": 100000000 19 | } -------------------------------------------------------------------------------- /json-p-1-1/src/test/java/com/readlearncode/Java8IntegrationTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.Json; 6 | import javax.json.JsonArray; 7 | import java.util.List; 8 | 9 | import static org.assertj.core.api.Java6Assertions.assertThat; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | public class Java8IntegrationTest { 18 | 19 | @Test 20 | public void givenJsonArray_shouldFilterAllTopicsStartingCToList() throws Exception { 21 | Java8Integration java8Integration = new Java8Integration(); 22 | List topics = java8Integration.filterJsonArrayToList(); 23 | 24 | assertThat(topics).contains("Cloud"); 25 | assertThat(topics).contains("Cognitive"); 26 | } 27 | 28 | @Test 29 | public void givenJsonArray_shouldFilterAllTopicsStartingCToJsonArray() throws Exception { 30 | Java8Integration java8Integration = new Java8Integration(); 31 | JsonArray topics = java8Integration.filterJsonArrayToJsonArray(); 32 | 33 | assertThat(topics.contains(Json.createValue("Cloud"))).isTrue(); 34 | assertThat(topics.contains(Json.createValue("Cognitive"))).isTrue(); 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /json-p-1-1/src/test/java/com/readlearncode/JsonMergeDiffExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.JsonMergePatch; 6 | import javax.json.JsonString; 7 | 8 | import static org.assertj.core.api.Java6Assertions.assertThat; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | public class JsonMergeDiffExampleTest { 17 | 18 | @Test 19 | public void givenSourceAndTarget_shouldCreateMergePatch() { 20 | JsonMergeDiffExample jsonMergeDiffExample = new JsonMergeDiffExample(); 21 | JsonMergePatch mergePatch = jsonMergeDiffExample.createMergePatch(); 22 | JsonString jsonString = (JsonString) mergePatch.toJsonValue(); 23 | 24 | assertThat(jsonString.getString()).isEqualToIgnoringCase("{\"colour\":\"red\"}"); 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /json-p-1-1/src/test/java/com/readlearncode/JsonMergePatchExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import org.junit.Ignore; 4 | import org.junit.Test; 5 | 6 | import javax.json.JsonString; 7 | import javax.json.JsonValue; 8 | 9 | import static org.assertj.core.api.Java6Assertions.assertThat; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | public class JsonMergePatchExampleTest { 18 | 19 | @Test 20 | public void givenPatch_sourceValueChanges() throws Exception { 21 | JsonMergePatchExample jsonMergePatchExample = new JsonMergePatchExample(); 22 | JsonValue result = jsonMergePatchExample.changeValue(); 23 | 24 | assertThat(((JsonString) result).getString()).isEqualToIgnoringCase("{\"colour\":\"red\"}"); 25 | } 26 | 27 | @Test 28 | @Ignore // Behaviour is not as specified in https://tools.ietf.org/html/rfc7386 29 | public void givenPatch_addNewJsonToSource() throws Exception { 30 | JsonMergePatchExample jsonMergePatchExample = new JsonMergePatchExample(); 31 | JsonValue result = jsonMergePatchExample.addValue(); 32 | 33 | assertThat(((JsonString) result).getString()).isEqualToIgnoringCase("{\"colour\":\"blue\",\"blue\":\"light\"}"); 34 | } 35 | 36 | @Test 37 | @Ignore // Behaviour is not as specified in https://tools.ietf.org/html/rfc7386 38 | public void givenPatch_deleteValue() throws Exception { 39 | JsonMergePatchExample jsonMergePatchExample = new JsonMergePatchExample(); 40 | JsonValue result = jsonMergePatchExample.deleteValue(); 41 | 42 | assertThat(((JsonString) result).getString()).isEqualToIgnoringCase("{}"); 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /json-p-1-1/src/test/java/com/readlearncode/JsonPointerExampleTest.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.assertj.core.api.Java6Assertions.assertThat; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | public class JsonPointerExampleTest { 14 | 15 | @Test 16 | public void givenPointerToTopic_shouldReturnTopic() { 17 | JsonPointerExample jsonPointerExample = new JsonPointerExample(); 18 | String topic = jsonPointerExample.find(); 19 | assertThat(topic).isEqualToIgnoringCase("Cloud"); 20 | } 21 | 22 | @Test 23 | public void givenPointerToTopic_shouldReplaceTopic() { 24 | JsonPointerExample jsonPointerExample = new JsonPointerExample(); 25 | String topic = jsonPointerExample.replace(); 26 | assertThat(topic).isEqualToIgnoringCase("Big Data"); 27 | } 28 | 29 | @Test 30 | public void givenPointerToArrayElement_shouldInsertTopicInToList() { 31 | JsonPointerExample jsonPointerExample = new JsonPointerExample(); 32 | String topic = jsonPointerExample.add(); 33 | assertThat(topic).isEqualToIgnoringCase("Java EE"); 34 | } 35 | } -------------------------------------------------------------------------------- /json-p-1-1/src/test/java/com/readlearncode/hashtag100daysofjavaee8/challenges.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.hashtag100daysofjavaee8; 2 | 3 | import org.junit.Test; 4 | 5 | import javax.json.*; 6 | import java.io.StringReader; 7 | 8 | import static org.assertj.core.api.Java6Assertions.assertThat; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | public class challenges { 17 | 18 | @Test 19 | public void givenObservers_shouldCallInPriorityOrder() { 20 | 21 | JsonValue target = Json.createValue("{\"colour\":\"blue\",\"size\":10}"); 22 | JsonValue patch = Json.createValue("{\"colour\":\"red\"}"); 23 | JsonValue result = Json.createMergePatch(patch).apply(target); 24 | 25 | System.out.println(result); 26 | 27 | assertThat(((JsonString) result).getString()).isEqualToIgnoringCase("{\"colour\":\"red\"}"); 28 | } 29 | 30 | 31 | @Test 32 | public void givenATarget_JSONPointerShouldAddAndRemoveElementValue() { 33 | 34 | /* Target JSON document 35 | { 36 | "name": "Duke", 37 | "likes": [ 38 | "Java", 39 | "Coffee" 40 | ] 41 | } 42 | */ 43 | 44 | String target = "{\"name\":\"Duke\",\"likes\":[\"Java\",\"Coffee\"]}"; 45 | JsonObject jsonObject = Json.createReader(new StringReader(target)).readObject(); 46 | JsonPointer pointer = Json.createPointer("/likes/0"); 47 | jsonObject = pointer.add(jsonObject, Json.createValue("Java EE 8")); 48 | pointer = Json.createPointer("/likes/2"); 49 | JsonObject newJsonObject = pointer.replace(jsonObject, Json.createValue("Ice Cream")); 50 | 51 | // What is the shape of the new JSON Object? 52 | 53 | assertThat(newJsonObject.toString()).isEqualToIgnoringCase("{\"name\":\"Duke\",\"likes\":[\"Java EE 8\",\"Java\",\"Ice Cream\"]}"); 54 | } 55 | 56 | 57 | } -------------------------------------------------------------------------------- /json-p-1-1/target/classes/jsondata-object.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "IBM developerWorks", 3 | "URL": "https://www.ibm.com/developerworks/", 4 | "topics": [ 5 | "Cognitive", 6 | "Cloud", 7 | "Data", 8 | "IoT", 9 | "Java" 10 | ], 11 | "series": [ 12 | "Sample Code...", 13 | "5 things you didn't know...", 14 | "Introduction to..." 15 | ], 16 | "notes": null, 17 | "live": true, 18 | "visitors": 100000000 19 | } -------------------------------------------------------------------------------- /security-1-0/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | Java-EE-8-Sampler 7 | com.readlearncode 8 | 1.0 9 | 10 | 11 | 4.0.0 12 | 13 | security-1-0 14 | 15 | 16 | security-1-0 17 | 18 | 19 | org.apache.maven.plugins 20 | maven-compiler-plugin 21 | ${maven-compiler-plugin.version} 22 | 23 | ${java.version} 24 | ${java.version} 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /security-1-0/src/main/java/com/readlearncode/httpauthenticationmechanism/AdminServlet.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.httpauthenticationmechanism; 2 | 3 | import javax.annotation.security.DeclareRoles; 4 | import javax.security.enterprise.authentication.mechanism.http.CustomFormAuthenticationMechanismDefinition; 5 | import javax.security.enterprise.authentication.mechanism.http.LoginToContinue; 6 | import javax.servlet.annotation.HttpConstraint; 7 | import javax.servlet.annotation.ServletSecurity; 8 | import javax.servlet.annotation.WebServlet; 9 | import javax.servlet.http.HttpServlet; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | @CustomFormAuthenticationMechanismDefinition( 18 | loginToContinue = @LoginToContinue( 19 | loginPage = "/login.do" 20 | ) 21 | ) 22 | @WebServlet("/admin") 23 | @DeclareRoles({"admin", "user", "demo"}) 24 | @ServletSecurity(@HttpConstraint(rolesAllowed = "admin")) 25 | public class AdminServlet extends HttpServlet { 26 | 27 | // Servlet Code 28 | 29 | } 30 | -------------------------------------------------------------------------------- /security-1-0/src/main/java/com/readlearncode/httpauthenticationmechanism/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.httpauthenticationmechanism; 2 | 3 | import javax.enterprise.context.ApplicationScoped; 4 | import javax.security.enterprise.authentication.mechanism.http.FormAuthenticationMechanismDefinition; 5 | import javax.security.enterprise.authentication.mechanism.http.LoginToContinue; 6 | 7 | /** 8 | * Source code github.com/readlearncode 9 | * 10 | * @author Alex Theedom www.readlearncode.com 11 | * @version 1.0 12 | */ 13 | @FormAuthenticationMechanismDefinition( 14 | loginToContinue = @LoginToContinue( 15 | loginPage = "/login-servlet", 16 | errorPage = "/login-servlet-fail", 17 | useForwardToLoginExpression = "${appConfigs.forward}" 18 | ) 19 | ) 20 | @ApplicationScoped 21 | public class ApplicationConfig { 22 | 23 | // Config Code 24 | 25 | } 26 | -------------------------------------------------------------------------------- /security-1-0/src/main/java/com/readlearncode/httpauthenticationmechanism/CustomAuthenticationMechanism.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.httpauthenticationmechanism; 2 | 3 | import javax.enterprise.context.ApplicationScoped; 4 | import javax.inject.Inject; 5 | import javax.security.enterprise.AuthenticationStatus; 6 | import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism; 7 | import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext; 8 | import javax.security.enterprise.authentication.mechanism.http.RememberMe; 9 | import javax.security.enterprise.identitystore.IdentityStoreHandler; 10 | import javax.servlet.http.HttpServletRequest; 11 | import javax.servlet.http.HttpServletResponse; 12 | 13 | @RememberMe( 14 | cookieMaxAgeSeconds = 3600 15 | ) 16 | @ApplicationScoped 17 | public class CustomAuthenticationMechanism implements HttpAuthenticationMechanism { 18 | 19 | @Inject 20 | private IdentityStoreHandler idStoreHandler; 21 | 22 | @Override 23 | public AuthenticationStatus validateRequest(HttpServletRequest req, HttpServletResponse res, HttpMessageContext msg) { 24 | return msg.doNothing(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /security-1-0/src/main/java/com/readlearncode/httpauthenticationmechanism/UserServlet.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.httpauthenticationmechanism; 2 | 3 | import javax.annotation.security.DeclareRoles; 4 | import javax.security.enterprise.authentication.mechanism.http.BasicAuthenticationMechanismDefinition; 5 | import javax.servlet.annotation.HttpConstraint; 6 | import javax.servlet.annotation.ServletSecurity; 7 | import javax.servlet.annotation.WebServlet; 8 | import javax.servlet.http.HttpServlet; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | @BasicAuthenticationMechanismDefinition(realmName = "user-realm") 17 | @WebServlet("/user") 18 | @DeclareRoles({"admin", "user", "demo"}) 19 | @ServletSecurity(@HttpConstraint(rolesAllowed = "user")) 20 | public class UserServlet extends HttpServlet { 21 | 22 | // Servlet Code 23 | 24 | } 25 | -------------------------------------------------------------------------------- /security-1-0/src/main/java/com/readlearncode/identitystore/AdminServlet.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.identitystore; 2 | 3 | import javax.annotation.security.DeclareRoles; 4 | import javax.security.enterprise.identitystore.LdapIdentityStoreDefinition; 5 | import javax.servlet.annotation.WebServlet; 6 | import javax.servlet.http.HttpServlet; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | @LdapIdentityStoreDefinition( 15 | url = "ldap://localhost:33389/", 16 | callerBaseDn = "ou=caller,dc=jsr375,dc=net", 17 | groupSearchBase = "ou=group,dc=jsr375,dc=net" 18 | ) 19 | @DeclareRoles({"admin", "user", "demo"}) 20 | @WebServlet("/admin") 21 | public class AdminServlet extends HttpServlet { 22 | 23 | // Servlet Code 24 | 25 | } 26 | -------------------------------------------------------------------------------- /security-1-0/src/main/java/com/readlearncode/identitystore/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.identitystore; 2 | 3 | import javax.enterprise.context.ApplicationScoped; 4 | import javax.inject.Named; 5 | import javax.security.enterprise.identitystore.DatabaseIdentityStoreDefinition; 6 | import javax.security.enterprise.identitystore.PasswordHash; 7 | 8 | /** 9 | * Source code github.com/readlearncode 10 | * 11 | * @author Alex Theedom www.readlearncode.com 12 | * @version 1.0 13 | */ 14 | @DatabaseIdentityStoreDefinition( 15 | dataSourceLookup = "${'java:global/permissions_db'}", 16 | callerQuery = "#{'select password from caller where name = ?'}", 17 | groupsQuery = "select group_name from caller_groups where caller_name = ?", 18 | hashAlgorithm = PasswordHash.class, 19 | priority = 10 20 | ) 21 | @ApplicationScoped 22 | @Named 23 | public class ApplicationConfig { 24 | 25 | // Config code 26 | 27 | } 28 | -------------------------------------------------------------------------------- /security-1-0/src/main/java/com/readlearncode/identitystore/LiteAuthenticationMechanism.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | package com.readlearncode.identitystore; 4 | 5 | 6 | import javax.enterprise.context.ApplicationScoped; 7 | import javax.inject.Inject; 8 | import javax.security.enterprise.AuthenticationStatus; 9 | import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism; 10 | import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext; 11 | import javax.security.enterprise.credential.UsernamePasswordCredential; 12 | import javax.security.enterprise.identitystore.CredentialValidationResult; 13 | import javax.security.enterprise.identitystore.IdentityStoreHandler; 14 | import javax.servlet.http.HttpServletRequest; 15 | import javax.servlet.http.HttpServletResponse; 16 | 17 | import static javax.security.enterprise.identitystore.CredentialValidationResult.Status.VALID; 18 | 19 | /** 20 | * Source code github.com/readlearncode 21 | * 22 | * @author Alex Theedom www.readlearncode.com 23 | * @version 1.0 24 | */ 25 | @ApplicationScoped 26 | public class LiteAuthenticationMechanism implements HttpAuthenticationMechanism { 27 | 28 | @Inject 29 | private IdentityStoreHandler idStoreHandler; 30 | 31 | @Override 32 | public AuthenticationStatus validateRequest(HttpServletRequest req, HttpServletResponse res, HttpMessageContext context) { 33 | 34 | CredentialValidationResult result = idStoreHandler.validate( 35 | new UsernamePasswordCredential( 36 | req.getParameter("name"), req.getParameter("password"))); 37 | 38 | if (result.getStatus() == VALID) { 39 | return context.notifyContainerAboutLogin(result); 40 | } else { 41 | return context.responseUnauthorized(); 42 | } 43 | 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /security-1-0/src/main/java/com/readlearncode/identitystore/LiteWeightIdentityStore.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.identitystore; 2 | 3 | import javax.enterprise.context.ApplicationScoped; 4 | import javax.security.enterprise.credential.UsernamePasswordCredential; 5 | import javax.security.enterprise.identitystore.CredentialValidationResult; 6 | import javax.security.enterprise.identitystore.IdentityStore; 7 | import java.util.HashSet; 8 | 9 | import static java.util.Arrays.asList; 10 | import static javax.security.enterprise.identitystore.CredentialValidationResult.INVALID_RESULT; 11 | 12 | /** 13 | * Source code github.com/readlearncode 14 | * 15 | * @author Alex Theedom www.readlearncode.com 16 | * @version 1.0 17 | */ 18 | @ApplicationScoped 19 | public class LiteWeightIdentityStore implements IdentityStore { 20 | 21 | public CredentialValidationResult validate(UsernamePasswordCredential userCredential) { 22 | 23 | if (userCredential.compareTo("admin", "pwd1")) { 24 | return new CredentialValidationResult("admin", new HashSet<>(asList("admin", "user", "demo"))); 25 | } 26 | 27 | return INVALID_RESULT; 28 | } 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /security-1-0/src/main/java/com/readlearncode/securitycontext/SecretServlet.java: -------------------------------------------------------------------------------- 1 | package com.readlearncode.securitycontext; 2 | 3 | import javax.annotation.security.DeclareRoles; 4 | import javax.servlet.annotation.HttpConstraint; 5 | import javax.servlet.annotation.ServletSecurity; 6 | import javax.servlet.annotation.WebServlet; 7 | import javax.servlet.http.HttpServlet; 8 | 9 | /** 10 | * Source code github.com/readlearncode 11 | * 12 | * @author Alex Theedom www.readlearncode.com 13 | * @version 1.0 14 | */ 15 | 16 | @WebServlet("/secretServlet") 17 | @DeclareRoles({"admin", "user", "demo"}) 18 | @ServletSecurity(@HttpConstraint(rolesAllowed = "admin")) 19 | public class SecretServlet extends HttpServlet { } -------------------------------------------------------------------------------- /security-1-0/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/security-1-0/src/main/webapp/WEB-INF/beans.xml -------------------------------------------------------------------------------- /security-1-0/src/main/webapp/WEB-INF/faces-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /security-1-0/src/main/webapp/WEB-INF/glassfish-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /security-1-0/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | javax.faces.PROJECT_STAGE 11 | Development 12 | 13 | 14 | 15 | 16 | primefaces.THEME 17 | ui-lightness 18 | 19 | 20 | 21 | Faces Servlet 22 | javax.faces.webapp.FacesServlet 23 | 1 24 | 25 | 26 | 27 | Faces Servlet 28 | *.xhtml 29 | 30 | 31 | 32 | 60 33 | 34 | 35 | 36 | index.xhtml 37 | index.html 38 | 39 | 40 | -------------------------------------------------------------------------------- /security-1-0/src/main/webapp/login.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 11 | 12 |

13 | Login to continue 14 |

15 | 16 |
17 |

18 | Username 19 | 20 |

21 |

22 | Password 23 | 24 |

25 |

26 | 27 |

28 |
29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /servlets-4-0/src/main/java/servlet4/ShowLogoServlet.java: -------------------------------------------------------------------------------- 1 | package servlet4; 2 | 3 | import javax.servlet.ServletException; 4 | import javax.servlet.annotation.WebServlet; 5 | import javax.servlet.http.HttpServlet; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | import java.io.IOException; 9 | 10 | /** 11 | * A simple POC use of the Server Push feature. 12 | *

13 | * Source code github.com/readlearncode 14 | * 15 | * @author Alex Theedom www.readlearncode.com 16 | * @version 1.0 17 | */ 18 | @WebServlet("/showlogoservlet") 19 | public class ShowLogoServlet extends HttpServlet { 20 | 21 | @Override 22 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 23 | 24 | request.newPushBuilder().path("resources/images/logo.jpg").push(); 25 | 26 | getServletContext() 27 | .getRequestDispatcher("/showlogo.jsp") 28 | .forward(request, response); 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /servlets-4-0/src/main/java/servlet4/hashtag100DaysOfJavaEE8/ServerPushExample.java: -------------------------------------------------------------------------------- 1 | package servlet4.hashtag100DaysOfJavaEE8; 2 | 3 | /** 4 | * A simple POC use of the Server Push feature. 5 | * 6 | * Source code github.com/readlearncode 7 | * 8 | * @author Alex Theedom www.readlearncode.com 9 | * @version 1.0 10 | */ 11 | 12 | 13 | /* 14 | Servlets 4.0 introduces support for HTTP/2 feature 15 | ServerPush. What happens when a request is made to the 16 | URI /duke over an insecure connection. i.e. over HTTP 17 | connection? 18 | */ 19 | 20 | // @WebServlet("/duke") 21 | // public class ServerPushExample extends HttpServlet { 22 | // @Override 23 | // protected void doGet(HttpServletRequest request, 24 | // HttpServletResponse response) 25 | // throws ServletException, IOException { 26 | // 27 | // request.newPushBuilder() 28 | // .path("resources/images/java-ee-logo.png") 29 | // .push(); 30 | // 31 | // getServletContext() 32 | // .getRequestDispatcher("/duke.jsp") 33 | // .forward(request, response); 34 | // } 35 | // } 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /servlets-4-0/src/main/java/servlet4/mapping/ServletMapping.java: -------------------------------------------------------------------------------- 1 | package servlet4.mapping; 2 | 3 | import javax.servlet.annotation.WebServlet; 4 | import javax.servlet.http.HttpServlet; 5 | import javax.servlet.http.HttpServletMapping; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | import java.io.IOException; 9 | 10 | /** 11 | * Source code github.com/readlearncode 12 | * 13 | * @author Alex Theedom www.readlearncode.com 14 | * @version 1.0 15 | */ 16 | @WebServlet({"/path/", "/path/to/*", "*.ext", "/path/file.ext"}) 17 | public class ServletMapping extends HttpServlet { 18 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 19 | HttpServletMapping servletMapping = request.getHttpServletMapping(); 20 | response.getWriter() 21 | .append("") 22 | .append("Value Matched: ").append(servletMapping.getMatchValue()) 23 | .append("
") 24 | .append("Pattern Used: ").append(servletMapping.getPattern()) 25 | .append("
") 26 | .append("Mapping Matched: ").append(servletMapping.getMappingMatch().name()) 27 | .append("
") 28 | .append("Servlet Name: ").append(servletMapping.getServletName()) 29 | .append("
") 30 | .append(""); 31 | } 32 | } -------------------------------------------------------------------------------- /servlets-4-0/src/main/java/servlet4/pushbuilder/ObtainPushBuilder.java: -------------------------------------------------------------------------------- 1 | package servlet4.pushbuilder; 2 | 3 | import javax.servlet.ServletException; 4 | import javax.servlet.annotation.WebServlet; 5 | import javax.servlet.http.HttpServlet; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | import javax.servlet.http.PushBuilder; 9 | import java.io.IOException; 10 | 11 | /** 12 | * Source code github.com/readlearncode 13 | * 14 | * @author Alex Theedom www.readlearncode.com 15 | * @version 1.0 16 | */ 17 | @WebServlet("/obtainpushbuilder") 18 | public class ObtainPushBuilder extends HttpServlet { 19 | 20 | @Override 21 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22 | 23 | PushBuilder pushBuilder = request.newPushBuilder(); 24 | 25 | if (pushBuilder != null) { 26 | pushBuilder.path("images/hero-banner.jpg").push(); 27 | pushBuilder.path("css/menu.css").push(); 28 | pushBuilder.path("js/marquee.js").push(); 29 | } 30 | 31 | getServletContext().getRequestDispatcher("/duke.jsp").forward(request, response); 32 | 33 | } 34 | } -------------------------------------------------------------------------------- /servlets-4-0/src/main/java/servlet4/pushbuilder/SimplestExample.java: -------------------------------------------------------------------------------- 1 | package servlet4.pushbuilder; 2 | 3 | import javax.servlet.ServletException; 4 | import javax.servlet.annotation.WebServlet; 5 | import javax.servlet.http.HttpServlet; 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | import java.io.IOException; 9 | 10 | /** 11 | * A simple POC use of the Server Push feature. 12 | * 13 | * Source code github.com/readlearncode 14 | * 15 | * @author Alex Theedom www.readlearncode.com 16 | * @version 1.0 17 | */ 18 | @WebServlet("/duke") 19 | public class SimplestExample extends HttpServlet { 20 | 21 | @Override 22 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 23 | 24 | request.newPushBuilder() 25 | .path("resources/images/java-ee-logo.png") 26 | .push(); 27 | 28 | getServletContext().getRequestDispatcher("/duke.jsp").forward(request, response); 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/WEB-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/WEB-INF/faces-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/WEB-INF/glassfish-web.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | /Servlet4Push 6 | 7 | -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | Servlet4Push 10 | /* 11 | GET 12 | 13 | 14 | 15 | CONFIDENTIAL 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | Faces Servlet 32 | javax.faces.webapp.FacesServlet 33 | 1 34 | 35 | 36 | 37 | Faces Servlet 38 | *.xhtml 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/duke.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | 3 | 4 | Servlet 4.0 ServerPush Example 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/duke.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | 9 | 10 | JSF 2.3 ServerPush Example 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" %> 2 | 3 | 4 | Servlet 4.0 5 | 6 | 7 | <%--

Welcome to the Server Push example.

--%> 8 | <%--

Open the network tab and click here.

--%> 9 | <%--

Open the network tab and click here.

--%> 10 |

Welcome to the Runtime Discovery of URL Mappings example.

11 | 12 |

Activate the ServletMapping servlet with:

13 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/resources/css/coffee-cup.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/servlets-4-0/src/main/webapp/resources/css/coffee-cup.css -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/resources/css/logo.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/servlets-4-0/src/main/webapp/resources/css/logo.css -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/resources/images/java-ee-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/servlets-4-0/src/main/webapp/resources/images/java-ee-logo.png -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/resources/images/logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/servlets-4-0/src/main/webapp/resources/images/logo.jpg -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/resources/js/logo.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/readlearncode/Java-EE-8-Sampler/403bafbb48fff8feb01a6e2156fb221bd526b2d2/servlets-4-0/src/main/webapp/resources/js/logo.js -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/showlogo.jsp: -------------------------------------------------------------------------------- 1 | <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 | 3 | 4 | Servlet 4.0 ServerPush Example 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /servlets-4-0/src/main/webapp/simplejsfpage.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | JSF 2.3 ServerPush Example 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | --------------------------------------------------------------------------------