Skip to content

Commit 1d692a9

Browse files
committed
Apache CXF Aegis data bindings
1 parent 21e996b commit 1d692a9

8 files changed

Lines changed: 211 additions & 0 deletions

File tree

apache-cxf/cxf-aegis/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>cxf-aegis</artifactId>
5+
<parent>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>apache-cxf</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<properties>
11+
<cxf.version>3.1.8</cxf.version>
12+
</properties>
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.apache.cxf</groupId>
16+
<artifactId>cxf-rt-databinding-aegis</artifactId>
17+
<version>${cxf.version}</version>
18+
</dependency>
19+
</dependencies>
20+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.cxf.aegis;
2+
3+
import java.util.Date;
4+
5+
public class Course {
6+
private int id;
7+
private String name;
8+
private String instructor;
9+
private Date enrolmentDate;
10+
11+
public int getId() {
12+
return id;
13+
}
14+
15+
public void setId(int id) {
16+
this.id = id;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public void setName(String name) {
24+
this.name = name;
25+
}
26+
27+
public String getInstructor() {
28+
return instructor;
29+
}
30+
31+
public void setInstructor(String instructor) {
32+
this.instructor = instructor;
33+
}
34+
35+
public Date getEnrolmentDate() {
36+
return enrolmentDate;
37+
}
38+
39+
public void setEnrolmentDate(Date enrolmentDate) {
40+
this.enrolmentDate = enrolmentDate;
41+
}
42+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.cxf.aegis;
2+
3+
import java.util.Map;
4+
5+
public interface CourseRepo {
6+
String getGreeting();
7+
void setGreeting(String greeting);
8+
Map<Integer, Course> getCourses();
9+
void setCourses(Map<Integer, Course> courses);
10+
void addCourse(Course course);
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.cxf.aegis;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class CourseRepoImpl implements CourseRepo {
7+
private String greeting;
8+
private Map<Integer, Course> courses = new HashMap<>();
9+
10+
@Override
11+
public String getGreeting() {
12+
return greeting;
13+
}
14+
15+
@Override
16+
public void setGreeting(String greeting) {
17+
this.greeting = greeting;
18+
}
19+
20+
@Override
21+
public Map<Integer, Course> getCourses() {
22+
return courses;
23+
}
24+
25+
@Override
26+
public void setCourses(Map<Integer, Course> courses) {
27+
this.courses = courses;
28+
}
29+
30+
@Override
31+
public void addCourse(Course course) {
32+
courses.put(course.getId(), course);
33+
}
34+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<mappings>
2+
<mapping>
3+
<property name="instructor" ignore="true"/>
4+
</mapping>
5+
</mappings>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<mappings xmlns:ns="http://courserepo.baeldung.com">
2+
<mapping name="ns:Baeldung">
3+
<property name="greeting" style="attribute"/>
4+
</mapping>
5+
</mappings>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.baeldung.cxf.aegis;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
6+
import org.junit.Test;
7+
8+
import java.io.FileInputStream;
9+
import java.io.FileOutputStream;
10+
import java.lang.reflect.Type;
11+
import java.util.Date;
12+
import java.util.HashMap;
13+
import java.util.HashSet;
14+
import java.util.Map;
15+
import java.util.Set;
16+
import javax.xml.namespace.QName;
17+
import javax.xml.stream.XMLInputFactory;
18+
import javax.xml.stream.XMLOutputFactory;
19+
import javax.xml.stream.XMLStreamReader;
20+
import javax.xml.stream.XMLStreamWriter;
21+
22+
import org.apache.cxf.aegis.AegisContext;
23+
import org.apache.cxf.aegis.AegisReader;
24+
import org.apache.cxf.aegis.AegisWriter;
25+
import org.apache.cxf.aegis.type.AegisType;
26+
27+
public class BaeldungTest {
28+
private AegisContext context;
29+
private String fileName = "baeldung.xml";
30+
31+
@Test
32+
public void whenMarshalingAndUnmarshalingCourseRepo_thenCorrect() throws Exception {
33+
initializeContext();
34+
CourseRepo inputRepo = initCourseRepo();
35+
marshalCourseRepo(inputRepo);
36+
CourseRepo outputRepo = unmarshalCourseRepo();
37+
Course restCourse = outputRepo.getCourses().get(1);
38+
Course securityCourse = outputRepo.getCourses().get(2);
39+
assertEquals("Welcome to Beldung!", outputRepo.getGreeting());
40+
assertEquals("REST with Spring", restCourse.getName());
41+
assertEquals(new Date(1234567890000L), restCourse.getEnrolmentDate());
42+
assertNull(restCourse.getInstructor());
43+
assertEquals("Learn Spring Security", securityCourse.getName());
44+
assertEquals(new Date(1456789000000L), securityCourse.getEnrolmentDate());
45+
assertNull(securityCourse.getInstructor());
46+
}
47+
48+
private void initializeContext() {
49+
context = new AegisContext();
50+
Set<Type> rootClasses = new HashSet<Type>();
51+
rootClasses.add(CourseRepo.class);
52+
context.setRootClasses(rootClasses);
53+
Map<Class<?>, String> beanImplementationMap = new HashMap<>();
54+
beanImplementationMap.put(CourseRepoImpl.class, "CourseRepo");
55+
context.setBeanImplementationMap(beanImplementationMap);
56+
context.setWriteXsiTypes(true);
57+
context.initialize();
58+
}
59+
60+
private CourseRepoImpl initCourseRepo() {
61+
Course restCourse = new Course();
62+
restCourse.setId(1);
63+
restCourse.setName("REST with Spring");
64+
restCourse.setInstructor("Eugen");
65+
restCourse.setEnrolmentDate(new Date(1234567890000L));
66+
Course securityCourse = new Course();
67+
securityCourse.setId(2);
68+
securityCourse.setName("Learn Spring Security");
69+
securityCourse.setInstructor("Eugen");
70+
securityCourse.setEnrolmentDate(new Date(1456789000000L));
71+
CourseRepoImpl courseRepo = new CourseRepoImpl();
72+
courseRepo.setGreeting("Welcome to Beldung!");
73+
courseRepo.addCourse(restCourse);
74+
courseRepo.addCourse(securityCourse);
75+
return courseRepo;
76+
}
77+
78+
private void marshalCourseRepo(CourseRepo courseRepo) throws Exception {
79+
AegisWriter<XMLStreamWriter> writer = context.createXMLStreamWriter();
80+
AegisType aegisType = context.getTypeMapping().getType(CourseRepo.class);
81+
XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream(fileName));
82+
writer.write(courseRepo, new QName("http://aegis.cxf.baeldung.com", "baeldung"), false, xmlWriter, aegisType);
83+
xmlWriter.close();
84+
}
85+
86+
private CourseRepo unmarshalCourseRepo() throws Exception {
87+
AegisReader<XMLStreamReader> reader = context.createXMLStreamReader();
88+
XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(fileName));
89+
CourseRepo courseRepo = (CourseRepo) reader.read(xmlReader, context.getTypeMapping().getType(CourseRepo.class));
90+
xmlReader.close();
91+
return courseRepo;
92+
}
93+
}

apache-cxf/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<module>cxf-introduction</module>
1010
<module>cxf-spring</module>
1111
<module>cxf-jaxrs-implementation</module>
12+
<module>cxf-aegis</module>
1213
</modules>
1314
<dependencies>
1415
<dependency>

0 commit comments

Comments
 (0)