Search in sources :

Example 51 with AegisType

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

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

the class BaeldungIntegrationTest method marshalCourseRepo.

private void marshalCourseRepo(CourseRepo courseRepo) throws Exception {
    AegisWriter<XMLStreamWriter> 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();
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) QName(javax.xml.namespace.QName) FileOutputStream(java.io.FileOutputStream)

Example 53 with AegisType

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

the class NoDefaultConstructorBeanKeyType method readObject.

@Override
public Object readObject(MessageReader reader, Context context) throws DatabindingException {
    BeanTypeInfo inf = getTypeInfo();
    try {
        String key = null;
        // Read child elements
        while (reader.hasMoreElementReaders()) {
            MessageReader childReader = reader.getNextElementReader();
            if (childReader.isXsiNil()) {
                childReader.readToEnd();
                continue;
            }
            QName name = childReader.getName();
            AegisType defaultType = inf.getType(name);
            AegisType type = TypeUtil.getReadType(childReader.getXMLStreamReader(), context.getGlobalContext(), defaultType);
            if (type != null) {
                String value = (String) type.readObject(childReader, context);
                if ("key".equals(name.getLocalPart())) {
                    key = value;
                }
            } else {
                childReader.readToEnd();
            }
        }
        return new NoDefaultConstructorBeanKeyImpl(key);
    } catch (IllegalArgumentException e) {
        throw new DatabindingException("Illegal argument. " + e.getMessage(), e);
    }
}
Also used : BeanTypeInfo(org.apache.cxf.aegis.type.basic.BeanTypeInfo) DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) NoDefaultConstructorBeanKeyImpl(org.apache.cxf.aegis.custom.service.NoDefaultConstructorBeanKeyImpl) MessageReader(org.apache.cxf.aegis.xml.MessageReader)

Example 54 with AegisType

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

the class TrailingBlocks method writeBlocks.

/**
 * Writes all of the unmarshalled objects in the MarshalRegistry.
 *
 * @param writer the stream to write the objects
 * @param context the marshal context
 * @return a list containing the object instances written
 */
public List<Object> writeBlocks(MessageWriter writer, Context context) {
    List<Object> blocks = new ArrayList<>();
    for (Object instance : MarshalRegistry.get(context)) {
        // determine instance type
        AegisType type = objectType.determineType(context, instance.getClass());
        if (type == null) {
            TypeMapping tm = context.getTypeMapping();
            if (tm == null) {
                tm = objectType.getTypeMapping();
            }
            type = tm.getTypeCreator().createType(instance.getClass());
            tm.register(type);
        }
        // create an new element for the instance
        MessageWriter cwriter = writer.getElementWriter(type.getSchemaType());
        // write the id attribute
        String id = MarshalRegistry.get(context).getInstanceId(instance);
        SoapEncodingUtil.writeId(cwriter, id);
        // write the instance
        objectType.writeObject(instance, cwriter, context);
        blocks.add(instance);
        // close the element
        cwriter.close();
    }
    return blocks;
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType) ArrayList(java.util.ArrayList) TypeMapping(org.apache.cxf.aegis.type.TypeMapping) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter)

Example 55 with AegisType

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

the class Java5TypeCreator method createCollectionType.

@Override
public AegisType createCollectionType(TypeClassInfo info) {
    Type type = info.getType();
    Type componentType = getComponentType(type, 0);
    if (componentType != null) {
        return createCollectionTypeFromGeneric(info);
    }
    return nextCreator.createCollectionType(info);
}
Also used : BeanType(org.apache.cxf.aegis.type.basic.BeanType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AegisType(org.apache.cxf.aegis.type.AegisType)

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