Search in sources :

Example 86 with AegisType

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

the class XMLStreamDataWriter method write.

public void write(Object obj, MessagePartInfo part, XMLStreamWriter output) {
    AegisType type = databinding.getType(part);
    if (type == null) {
        type = databinding.getTypeFromClass(obj.getClass());
    }
    if (type == null) {
        throw new Fault(new Message("NO_MESSAGE_FOR_PART", LOG, part));
    }
    Context context = new Context(databinding.getAegisContext());
    context.setAttachments(attachments);
    type = TypeUtil.getWriteType(databinding.getAegisContext(), obj, type);
    /*
         * We arrive here with a 'type' of the inner type if isWriteOuter is null.
         * However, in that case, the original type is available.
         */
    AegisType outerType = null;
    if (part != null) {
        outerType = part.getProperty("org.apache.cxf.aegis.outerType", AegisType.class);
    }
    try {
        if (obj == null) {
            if (part.getXmlSchema() instanceof XmlSchemaElement && ((XmlSchemaElement) part.getXmlSchema()).getMinOccurs() == 0) {
                // skip writing minOccurs=0 stuff if obj is null
                return;
            } else if (type.isNillable()) {
                ElementWriter writer = new ElementWriter(output);
                MessageWriter w2 = writer.getElementWriter(part.getConcreteName());
                w2.writeXsiNil();
                w2.close();
                return;
            }
        }
        ElementWriter writer = new ElementWriter(output);
        // outerType is only != null for a flat array.
        if (outerType == null) {
            MessageWriter w2 = writer.getElementWriter(part != null ? part.getConcreteName() : type.getSchemaType());
            type.writeObject(obj, w2, context);
            w2.close();
        } else {
            // it has better be an array (!)
            ArrayType aType = (ArrayType) outerType;
            // the part has to have a name or we can't do this.
            aType.writeObject(obj, writer, context, part.getConcreteName());
        }
    } catch (DatabindingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Context(org.apache.cxf.aegis.Context) ArrayType(org.apache.cxf.aegis.type.basic.ArrayType) DatabindingException(org.apache.cxf.aegis.DatabindingException) Message(org.apache.cxf.common.i18n.Message) AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) Fault(org.apache.cxf.interceptor.Fault) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter)

Example 87 with AegisType

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

the class ArrayType method getComponentType.

/**
 * Get the <code>AegisType</code> of the elements in the array.
 *
 * @return
 */
public AegisType getComponentType() {
    Class<?> compType = getTypeClass().getComponentType();
    AegisType type;
    if (componentName == null) {
        type = getTypeMapping().getType(compType);
    } else {
        type = getTypeMapping().getType(componentName);
        // below instead.
        if (type == null) {
            LOG.finest("Couldn't find array component type " + componentName + ". Creating one instead.");
        }
    }
    if (type == null) {
        type = getTypeMapping().getTypeCreator().createType(compType);
        getTypeMapping().register(type);
    }
    return type;
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType)

Example 88 with AegisType

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

the class ArrayType method writeSchema.

@Override
public void writeSchema(XmlSchema root) {
    if (isFlat()) {
        // there is no extra level of type.
        return;
    }
    if (hasDefinedArray(root)) {
        return;
    }
    XmlSchemaComplexType complex = new XmlSchemaComplexType(root, true);
    complex.setName(getSchemaType().getLocalPart());
    XmlSchemaSequence seq = new XmlSchemaSequence();
    complex.setParticle(seq);
    AegisType componentType = getComponentType();
    XmlSchemaElement element = new XmlSchemaElement(root, false);
    element.setName(componentType.getSchemaType().getLocalPart());
    element.setSchemaTypeName(componentType.getSchemaType());
    seq.getItems().add(element);
    if (componentType.isNillable()) {
        element.setNillable(true);
    }
    element.setMinOccurs(getMinOccurs());
    element.setMaxOccurs(getMaxOccurs());
}
Also used : XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType)

Example 89 with AegisType

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

the class BeanType method readObject.

/**
 * {@inheritDoc}
 */
@Override
public Object readObject(MessageReader reader, Context context) throws DatabindingException {
    BeanTypeInfo inf = getTypeInfo();
    try {
        Class<?> clazz = getTypeClass();
        Object object;
        // the target for properties; either the object or the proxy handler
        Object target;
        if (isInterface) {
            String impl = context.getGlobalContext().getBeanImplementationMap().get(clazz);
            if (impl == null) {
                InvocationHandler handler = new InterfaceInvocationHandler();
                object = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, handler);
                target = handler;
            } else {
                try {
                    clazz = ClassLoaderUtils.loadClass(impl, getClass());
                    object = clazz.newInstance();
                    target = object;
                } catch (ClassNotFoundException e) {
                    throw new DatabindingException("Could not find implementation class " + impl + " for class " + clazz.getName());
                }
            }
        } else if (isException) {
            object = createFromFault(context);
            target = object;
        } else {
            object = clazz.newInstance();
            target = object;
        }
        // Read attributes
        while (reader.hasMoreAttributeReaders()) {
            MessageReader childReader = reader.getNextAttributeReader();
            QName name = childReader.getName();
            AegisType type = inf.getType(name);
            if (type != null) {
                Object writeObj = type.readObject(childReader, context);
                writeProperty(name, target, writeObj, clazz, inf);
            }
        }
        // Read child elements
        while (reader.hasMoreElementReaders()) {
            MessageReader childReader = reader.getNextElementReader();
            QName name = childReader.getName();
            // Find the BeanTypeInfo that contains a property for the element name
            BeanTypeInfo propertyTypeInfo = getBeanTypeInfoWithProperty(name);
            // Get the AegisType for the property
            AegisType type = getElementType(name, propertyTypeInfo, childReader, context);
            if (type != null) {
                if (!childReader.isXsiNil()) {
                    Object writeObj;
                    if (type.isFlatArray()) {
                        ArrayType aType = (ArrayType) type;
                        PropertyDescriptor desc = inf.getPropertyDescriptorFromMappedName(name);
                        boolean isList = List.class.isAssignableFrom(desc.getPropertyType());
                        writeObj = aType.readObject(childReader, name, context, !isList);
                    } else {
                        writeObj = type.readObject(childReader, context);
                    }
                    writeProperty(name, target, writeObj, clazz, propertyTypeInfo);
                } else {
                    if (!alwaysAllowNillables() && !propertyTypeInfo.isNillable(name)) {
                        throw new DatabindingException(name.getLocalPart() + " is nil, but not nillable.");
                    }
                    childReader.readToEnd();
                }
            } else {
                childReader.readToEnd();
            }
        }
        return object;
    } catch (IllegalAccessException e) {
        throw new DatabindingException("Illegal access. " + e.getMessage(), e);
    } catch (InstantiationException e) {
        throw new DatabindingException("Couldn't instantiate class. " + e.getMessage(), e);
    } catch (SecurityException e) {
        throw new DatabindingException("Illegal access. " + e.getMessage(), e);
    } catch (IllegalArgumentException e) {
        throw new DatabindingException("Illegal argument. " + e.getMessage(), e);
    } catch (InvocationTargetException e) {
        throw new DatabindingException("Could not create class: " + e.getMessage(), e);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) MessageReader(org.apache.cxf.aegis.xml.MessageReader) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException) DatabindingException(org.apache.cxf.aegis.DatabindingException)

Example 90 with AegisType

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

the class AegisDatabinding method initialize.

/**
 * {@inheritDoc} Set up the data binding for a service.
 */
public void initialize(Service s) {
    // We want to support some compatibility configuration properties.
    if (aegisContext == null) {
        aegisContext = new AegisContext();
        Object val = s.get("mtom-enabled");
        if ("true".equals(val) || Boolean.TRUE.equals(val) || mtomEnabled) {
            setMtomEnabled(true);
            aegisContext.setMtomEnabled(true);
        }
        if (mtomUseXmime) {
            aegisContext.setMtomUseXmime(true);
        }
        Map<Class<?>, String> implMap = new HashMap<>();
        // now for a really annoying case, the .implementation objects.
        for (String key : s.keySet()) {
            if (key.endsWith(".implementation")) {
                String className = key.substring(0, key.length() - ".implementation".length());
                try {
                    String implClassName = (String) s.get(key);
                    implMap.put(ClassLoaderUtils.loadClass(className, getClass()), implClassName);
                } catch (ClassNotFoundException e) {
                    Message message = new Message("MAPPED_CLASS_NOT_FOUND", LOG, className, key);
                    LOG.warning(message.toString());
                    continue;
                }
            }
        }
        if (overrideTypes != null) {
            aegisContext.setRootClassNames(overrideTypes);
        }
        if (configuration != null) {
            aegisContext.setTypeCreationOptions(configuration);
        }
        if (!implMap.isEmpty()) {
            aegisContext.setBeanImplementationMap(implMap);
        }
    }
    aegisContext.setMappingNamespaceURI(s.getServiceInfos().get(0).getInterface().getName().getNamespaceURI());
    aegisContext.initialize();
    this.service = s;
    s.getInInterceptors().add(new StaxSchemaValidationInInterceptor());
    Set<AegisType> deps = new HashSet<>();
    for (ServiceInfo info : s.getServiceInfos()) {
        for (OperationInfo opInfo : info.getInterface().getOperations()) {
            if (opInfo.isUnwrappedCapable()) {
                initializeOperation(s, aegisContext.getTypeMapping(), opInfo.getUnwrappedOperation(), deps);
            } else {
                initializeOperation(s, aegisContext.getTypeMapping(), opInfo, deps);
            }
        }
    }
    Collection<AegisType> additional = aegisContext.getRootTypes();
    if (additional != null) {
        for (AegisType t : additional) {
            if (!deps.contains(t)) {
                deps.add(t);
            }
            addDependencies(deps, t);
        }
    }
    createSchemas(s, deps);
    for (ServiceInfo info : s.getServiceInfos()) {
        for (OperationInfo opInfo : info.getInterface().getOperations()) {
            if (opInfo.isUnwrappedCapable()) {
                initializeOperationTypes(info, opInfo.getUnwrappedOperation());
            } else {
                initializeOperationTypes(info, opInfo);
            }
        }
    }
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) Message(org.apache.cxf.common.i18n.Message) HashMap(java.util.HashMap) AegisType(org.apache.cxf.aegis.type.AegisType) AegisContext(org.apache.cxf.aegis.AegisContext) StaxSchemaValidationInInterceptor(org.apache.cxf.staxutils.validation.StaxSchemaValidationInInterceptor) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) HashSet(java.util.HashSet)

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