Search in sources :

Example 81 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class EnumTypeTest method testAutoCreation.

@Test
public void testAutoCreation() throws Exception {
    AegisType type = tm.getTypeCreator().createType(SmallEnum.class);
    assertTrue(type instanceof EnumType);
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) Test(org.junit.Test) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest)

Example 82 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class XFireXmlParamTypeTest method testType.

@Test
public void testType() throws Exception {
    Method m = CustomTypeService.class.getMethod("doFoo", new Class[] { String.class });
    AegisType type = creator.createType(m, 0);
    tm.register(type);
    assertTrue(type instanceof CustomStringType);
    assertEquals(new QName("urn:xfire:foo", "custom"), type.getSchemaType());
    type = creator.createType(m, -1);
    tm.register(type);
    assertTrue(type instanceof CustomStringType);
    assertEquals(new QName("urn:xfire:foo", "custom"), type.getSchemaType());
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) Method(java.lang.reflect.Method) Test(org.junit.Test) AbstractAegisTest(org.apache.cxf.aegis.AbstractAegisTest)

Example 83 with AegisType

use of org.apache.cxf.aegis.type.AegisType 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)

Example 84 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class AegisContext method processRootTypes.

/**
 * Examine a list of override classes, and register all of them.
 */
private void processRootTypes() {
    rootTypes = new HashSet<>();
    // app may have already supplied classes.
    if (rootClasses == null) {
        rootClasses = new HashSet<>();
    }
    rootTypeQNames = new HashSet<>();
    if (this.rootClassNames != null) {
        for (String typeName : rootClassNames) {
            try {
                rootClasses.add(ClassLoaderUtils.loadClass(typeName, TypeUtil.class));
            } catch (ClassNotFoundException e) {
                throw new DatabindingException("Could not find override type class: " + typeName, e);
            }
        }
    }
    // This is a list of AegisType rather than Class so that it can set up for generic collections.
    // When we see a generic, we process both the generic outer class and each parameter class.
    // This is not the same thing as allowing mappings of arbitrary x<q> types.
    Set<Class<?>> rootMappableClassSet = rootMappableClasses();
    /*
         * First loop: process non-Class roots, creating full types for them
         * and registering them.
         */
    for (java.lang.reflect.Type reflectType : rootClasses) {
        if (!(reflectType instanceof Class)) {
            // if it's not a Class, it can't be mapped from Class to type in the mapping.
            // so we create
            AegisType aegisType = typeMapping.getTypeCreator().createType(reflectType);
            typeMapping.register(aegisType);
            // note: we don't handle arbitrary generics, so no BeanType
            // check here.
            rootTypeQNames.add(aegisType.getSchemaType());
        }
    }
    /*
         * Second loop: process Class roots, including those derived from
         * generic types, creating when not in the default mappings.
         */
    for (Class<?> c : rootMappableClassSet) {
        AegisType t = typeMapping.getType(c);
        if (t == null) {
            t = typeMapping.getTypeCreator().createType(c);
            typeMapping.register(t);
        }
        rootTypeQNames.add(t.getSchemaType());
        if (t instanceof BeanType) {
            BeanType bt = (BeanType) t;
            bt.getTypeInfo().setExtension(true);
            rootTypes.add(bt);
        }
    }
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) BeanType(org.apache.cxf.aegis.type.basic.BeanType) TypeUtil(org.apache.cxf.aegis.type.TypeUtil)

Example 85 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class ElementDataWriter method write.

public void write(Object obj, MessagePartInfo part, Element output) {
    AegisType type = databinding.getType(part);
    if (type == null) {
        throw new Fault(new Message("NO_MESSAGE_FOR_PART", LOG));
    }
    Context context = new Context(databinding.getAegisContext());
    context.setAttachments(attachments);
    type = TypeUtil.getWriteType(databinding.getAegisContext(), obj, type);
    try {
        W3CDOMStreamWriter domWriter = new W3CDOMStreamWriter(output);
        ElementWriter writer = new ElementWriter(domWriter);
        MessageWriter w2 = writer.getElementWriter(part.getConcreteName());
        if (type.isNillable() && type.isWriteOuter() && obj == null) {
            w2.writeXsiNil();
            w2.close();
            return;
        }
        type.writeObject(obj, w2, context);
        w2.close();
    } catch (DatabindingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Context(org.apache.cxf.aegis.Context) DatabindingException(org.apache.cxf.aegis.DatabindingException) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) Message(org.apache.cxf.common.i18n.Message) AegisType(org.apache.cxf.aegis.type.AegisType) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) Fault(org.apache.cxf.interceptor.Fault) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter)

Aggregations

AegisType (org.apache.cxf.aegis.type.AegisType)97 QName (javax.xml.namespace.QName)43 Test (org.junit.Test)40 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)35 DatabindingException (org.apache.cxf.aegis.DatabindingException)18 AegisContext (org.apache.cxf.aegis.AegisContext)16 TypeMapping (org.apache.cxf.aegis.type.TypeMapping)11 Element (org.w3c.dom.Element)11 Context (org.apache.cxf.aegis.Context)10 BeanType (org.apache.cxf.aegis.type.basic.BeanType)10 Method (java.lang.reflect.Method)9 MessageReader (org.apache.cxf.aegis.xml.MessageReader)9 TypeCreationOptions (org.apache.cxf.aegis.type.TypeCreationOptions)8 BeanTypeInfo (org.apache.cxf.aegis.type.basic.BeanTypeInfo)8 CollectionType (org.apache.cxf.aegis.type.collection.CollectionType)8 HashSet (java.util.HashSet)7 MapType (org.apache.cxf.aegis.type.collection.MapType)7 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)7 ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)7 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)5