use of org.apache.cxf.aegis.AegisContext in project cxf by apache.
the class ConfigurationTest method setUp.
@Before
public void setUp() throws Exception {
super.setUp();
AegisContext context = new AegisContext();
config = new TypeCreationOptions();
context.setTypeCreationOptions(config);
context.initialize();
XMLTypeCreator creator = new XMLTypeCreator();
creator.setConfiguration(config);
Java5TypeCreator next = new Java5TypeCreator();
next.setConfiguration(config);
creator.setNextCreator(next);
tm = (DefaultTypeMapping) context.getTypeMapping();
tm.setTypeCreator(creator);
}
use of org.apache.cxf.aegis.AegisContext in project cxf by apache.
the class WriteZoo method go.
private void go() throws Exception {
AegisContext context;
context = new AegisContext();
context.setWriteXsiTypes(true);
Set<java.lang.reflect.Type> rootClasses = new HashSet<java.lang.reflect.Type>();
rootClasses.add(Zoo.class);
context.setRootClasses(rootClasses);
context.initialize();
AegisWriter<XMLStreamWriter> writer = context.createXMLStreamWriter();
FileOutputStream output = new FileOutputStream(outputPathname);
XMLStreamWriter xmlWriter = outputFactory.createXMLStreamWriter(output);
IndentingXMLStreamWriter indentWriter = new IndentingXMLStreamWriter(xmlWriter);
Zoo zoo = populateZoo();
AegisType aegisType = context.getTypeMapping().getType(zoo.getClass());
writer.write(zoo, new QName("urn:aegis:demo", "zoo"), false, indentWriter, aegisType);
xmlWriter.close();
output.close();
}
use of org.apache.cxf.aegis.AegisContext in project cxf by apache.
the class AegisElementProvider method readFrom.
public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType m, MultivaluedMap<String, String> headers, InputStream is) throws IOException {
if (genericType == null) {
genericType = type;
}
if (type == null) {
type = messyCastToRawType(genericType);
}
AegisContext context = getAegisContext(type, genericType);
AegisType typeToRead = context.getTypeMapping().getType(genericType);
AegisReader<XMLStreamReader> aegisReader = context.createXMLStreamReader();
XMLStreamReader xmlStreamReader = null;
try {
xmlStreamReader = createStreamReader(typeToRead, is);
return type.cast(aegisReader.read(xmlStreamReader, typeToRead));
} catch (Exception e) {
throw ExceptionUtils.toBadRequestException(e, null);
} finally {
try {
StaxUtils.close(xmlStreamReader);
} catch (XMLStreamException e) {
throw ExceptionUtils.toBadRequestException(e, null);
}
}
}
use of org.apache.cxf.aegis.AegisContext in project cxf by apache.
the class AbstractAegisProvider method getClassContext.
private AegisContext getClassContext(Type reflectionType) {
synchronized (classContexts) {
AegisContext context = classContexts.get(reflectionType);
if (context == null) {
context = new AegisContext();
context.setWriteXsiTypes(writeXsiType);
context.setReadXsiTypes(readXsiType);
Set<java.lang.reflect.Type> rootClasses = new HashSet<java.lang.reflect.Type>();
rootClasses.add(reflectionType);
context.setRootClasses(rootClasses);
context.initialize();
classContexts.put(reflectionType, context);
}
return context;
}
}
use of org.apache.cxf.aegis.AegisContext in project cxf by apache.
the class AegisElementProvider method writeTo.
public void writeTo(T obj, Class<?> type, Type genericType, Annotation[] anns, MediaType m, MultivaluedMap<String, Object> headers, OutputStream os) throws IOException {
if (type == null) {
type = obj.getClass();
}
if (genericType == null) {
genericType = type;
}
AegisContext context = getAegisContext(type, genericType);
AegisType aegisType = context.getTypeMapping().getType(genericType);
AegisWriter<XMLStreamWriter> aegisWriter = context.createXMLStreamWriter();
try {
String enc = HttpUtils.getSetEncoding(m, headers, StandardCharsets.UTF_8.name());
XMLStreamWriter xmlStreamWriter = createStreamWriter(aegisType.getSchemaType(), enc, os);
// use type qname as element qname?
xmlStreamWriter.writeStartDocument();
aegisWriter.write(obj, aegisType.getSchemaType(), false, xmlStreamWriter, aegisType);
xmlStreamWriter.writeEndDocument();
xmlStreamWriter.close();
} catch (Exception e) {
throw ExceptionUtils.toInternalServerErrorException(e, null);
}
}
Aggregations