diff --git a/apache-cxf/cxf-aegis/pom.xml b/apache-cxf/cxf-aegis/pom.xml new file mode 100644 index 0000000000..b436b03305 --- /dev/null +++ b/apache-cxf/cxf-aegis/pom.xml @@ -0,0 +1,20 @@ + + 4.0.0 + cxf-aegis + + com.baeldung + apache-cxf + 0.0.1-SNAPSHOT + + + 3.1.8 + + + + org.apache.cxf + cxf-rt-databinding-aegis + ${cxf.version} + + + diff --git a/apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/Course.java b/apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/Course.java new file mode 100644 index 0000000000..b8004f1ba7 --- /dev/null +++ b/apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/Course.java @@ -0,0 +1,42 @@ +package com.baeldung.cxf.aegis; + +import java.util.Date; + +public class Course { + private int id; + private String name; + private String instructor; + private Date enrolmentDate; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getInstructor() { + return instructor; + } + + public void setInstructor(String instructor) { + this.instructor = instructor; + } + + public Date getEnrolmentDate() { + return enrolmentDate; + } + + public void setEnrolmentDate(Date enrolmentDate) { + this.enrolmentDate = enrolmentDate; + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/CourseRepo.java b/apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/CourseRepo.java new file mode 100644 index 0000000000..a125fb1f85 --- /dev/null +++ b/apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/CourseRepo.java @@ -0,0 +1,11 @@ +package com.baeldung.cxf.aegis; + +import java.util.Map; + +public interface CourseRepo { + String getGreeting(); + void setGreeting(String greeting); + Map getCourses(); + void setCourses(Map courses); + void addCourse(Course course); +} \ No newline at end of file diff --git a/apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/CourseRepoImpl.java b/apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/CourseRepoImpl.java new file mode 100644 index 0000000000..e483feed58 --- /dev/null +++ b/apache-cxf/cxf-aegis/src/main/java/com/baeldung/cxf/aegis/CourseRepoImpl.java @@ -0,0 +1,34 @@ +package com.baeldung.cxf.aegis; + +import java.util.HashMap; +import java.util.Map; + +public class CourseRepoImpl implements CourseRepo { + private String greeting; + private Map courses = new HashMap<>(); + + @Override + public String getGreeting() { + return greeting; + } + + @Override + public void setGreeting(String greeting) { + this.greeting = greeting; + } + + @Override + public Map getCourses() { + return courses; + } + + @Override + public void setCourses(Map courses) { + this.courses = courses; + } + + @Override + public void addCourse(Course course) { + courses.put(course.getId(), course); + } +} \ No newline at end of file diff --git a/apache-cxf/cxf-aegis/src/main/resources/com/baeldung/cxf/aegis/Course.aegis.xml b/apache-cxf/cxf-aegis/src/main/resources/com/baeldung/cxf/aegis/Course.aegis.xml new file mode 100644 index 0000000000..53c6c2fc45 --- /dev/null +++ b/apache-cxf/cxf-aegis/src/main/resources/com/baeldung/cxf/aegis/Course.aegis.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/apache-cxf/cxf-aegis/src/main/resources/com/baeldung/cxf/aegis/CourseRepo.aegis.xml b/apache-cxf/cxf-aegis/src/main/resources/com/baeldung/cxf/aegis/CourseRepo.aegis.xml new file mode 100644 index 0000000000..7e13678b51 --- /dev/null +++ b/apache-cxf/cxf-aegis/src/main/resources/com/baeldung/cxf/aegis/CourseRepo.aegis.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/apache-cxf/cxf-aegis/src/test/java/com/baeldung/cxf/aegis/BaeldungTest.java b/apache-cxf/cxf-aegis/src/test/java/com/baeldung/cxf/aegis/BaeldungTest.java new file mode 100644 index 0000000000..559de037a9 --- /dev/null +++ b/apache-cxf/cxf-aegis/src/test/java/com/baeldung/cxf/aegis/BaeldungTest.java @@ -0,0 +1,93 @@ +package com.baeldung.cxf.aegis; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.lang.reflect.Type; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import org.apache.cxf.aegis.AegisContext; +import org.apache.cxf.aegis.AegisReader; +import org.apache.cxf.aegis.AegisWriter; +import org.apache.cxf.aegis.type.AegisType; + +public class BaeldungTest { + private AegisContext context; + private String fileName = "baeldung.xml"; + + @Test + public void whenMarshalingAndUnmarshalingCourseRepo_thenCorrect() throws Exception { + initializeContext(); + CourseRepo inputRepo = initCourseRepo(); + marshalCourseRepo(inputRepo); + CourseRepo outputRepo = unmarshalCourseRepo(); + Course restCourse = outputRepo.getCourses().get(1); + Course securityCourse = outputRepo.getCourses().get(2); + assertEquals("Welcome to Beldung!", outputRepo.getGreeting()); + assertEquals("REST with Spring", restCourse.getName()); + assertEquals(new Date(1234567890000L), restCourse.getEnrolmentDate()); + assertNull(restCourse.getInstructor()); + assertEquals("Learn Spring Security", securityCourse.getName()); + assertEquals(new Date(1456789000000L), securityCourse.getEnrolmentDate()); + assertNull(securityCourse.getInstructor()); + } + + private void initializeContext() { + context = new AegisContext(); + Set rootClasses = new HashSet(); + rootClasses.add(CourseRepo.class); + context.setRootClasses(rootClasses); + Map, String> beanImplementationMap = new HashMap<>(); + beanImplementationMap.put(CourseRepoImpl.class, "CourseRepo"); + context.setBeanImplementationMap(beanImplementationMap); + context.setWriteXsiTypes(true); + context.initialize(); + } + + private CourseRepoImpl initCourseRepo() { + Course restCourse = new Course(); + restCourse.setId(1); + restCourse.setName("REST with Spring"); + restCourse.setInstructor("Eugen"); + restCourse.setEnrolmentDate(new Date(1234567890000L)); + Course securityCourse = new Course(); + securityCourse.setId(2); + securityCourse.setName("Learn Spring Security"); + securityCourse.setInstructor("Eugen"); + securityCourse.setEnrolmentDate(new Date(1456789000000L)); + CourseRepoImpl courseRepo = new CourseRepoImpl(); + courseRepo.setGreeting("Welcome to Beldung!"); + courseRepo.addCourse(restCourse); + courseRepo.addCourse(securityCourse); + return courseRepo; + } + + private void marshalCourseRepo(CourseRepo courseRepo) throws Exception { + AegisWriter writer = context.createXMLStreamWriter(); + AegisType aegisType = context.getTypeMapping().getType(CourseRepo.class); + XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(fileName)); + writer.write(courseRepo, new QName("http://aegis.cxf.baeldung.com", "baeldung"), false, xmlWriter, aegisType); + xmlWriter.close(); + } + + private CourseRepo unmarshalCourseRepo() throws Exception { + AegisReader reader = context.createXMLStreamReader(); + XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(fileName)); + CourseRepo courseRepo = (CourseRepo) reader.read(xmlReader, context.getTypeMapping().getType(CourseRepo.class)); + xmlReader.close(); + return courseRepo; + } +} \ No newline at end of file diff --git a/apache-cxf/pom.xml b/apache-cxf/pom.xml index af7949bb6c..b382ee6506 100644 --- a/apache-cxf/pom.xml +++ b/apache-cxf/pom.xml @@ -9,6 +9,7 @@ cxf-introduction cxf-spring cxf-jaxrs-implementation + cxf-aegis