Search in sources :

Example 16 with AegisContext

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);
}
Also used : AegisContext(org.apache.cxf.aegis.AegisContext) XMLTypeCreator(org.apache.cxf.aegis.type.XMLTypeCreator) TypeCreationOptions(org.apache.cxf.aegis.type.TypeCreationOptions) Before(org.junit.Before)

Example 17 with AegisContext

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();
}
Also used : IndentingXMLStreamWriter(javanet.staxutils.IndentingXMLStreamWriter) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) AegisContext(org.apache.cxf.aegis.AegisContext) AegisType(org.apache.cxf.aegis.type.AegisType) IndentingXMLStreamWriter(javanet.staxutils.IndentingXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) FileOutputStream(java.io.FileOutputStream) Zoo(org.apache.cxf.demo.aegis.types.Zoo) HashSet(java.util.HashSet)

Example 18 with AegisContext

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);
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) AegisType(org.apache.cxf.aegis.type.AegisType) AegisContext(org.apache.cxf.aegis.AegisContext) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 19 with AegisContext

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;
    }
}
Also used : MediaType(javax.ws.rs.core.MediaType) Type(java.lang.reflect.Type) AegisContext(org.apache.cxf.aegis.AegisContext) HashSet(java.util.HashSet)

Example 20 with AegisContext

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);
    }
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) AegisContext(org.apache.cxf.aegis.AegisContext) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

AegisContext (org.apache.cxf.aegis.AegisContext)40 Test (org.junit.Test)20 QName (javax.xml.namespace.QName)15 AegisType (org.apache.cxf.aegis.type.AegisType)15 HashSet (java.util.HashSet)13 XMLStreamReader (javax.xml.stream.XMLStreamReader)8 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)8 TypeMapping (org.apache.cxf.aegis.type.TypeMapping)8 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)6 Context (org.apache.cxf.aegis.Context)6 TypeCreationOptions (org.apache.cxf.aegis.type.TypeCreationOptions)6 XmlSchema (org.apache.ws.commons.schema.XmlSchema)5 Before (org.junit.Before)5 StringWriter (java.io.StringWriter)4 AegisDatabinding (org.apache.cxf.aegis.databinding.AegisDatabinding)4 XmlMappedAttributeBean (org.apache.cxf.aegis.services.XmlMappedAttributeBean)4 Type (java.lang.reflect.Type)3 ArrayList (java.util.ArrayList)3 SimpleBean (org.apache.cxf.aegis.services.SimpleBean)3 DefaultTypeMapping (org.apache.cxf.aegis.type.DefaultTypeMapping)3