Search in sources :

Example 1 with HexBinaryAdapter

use of javax.xml.bind.annotation.adapters.HexBinaryAdapter in project cxf by apache.

the class JAXBEncoderDecoder method unmarshall.

public static Object unmarshall(Unmarshaller u, Object source, MessagePartInfo part, boolean unwrap) {
    Class<?> clazz = part != null ? (Class<?>) part.getTypeClass() : null;
    if (clazz != null && Exception.class.isAssignableFrom(clazz) && part != null && Boolean.TRUE.equals(part.getProperty(JAXBDataBinding.class.getName() + ".CUSTOM_EXCEPTION"))) {
        return unmarshallException(u, source, part);
    }
    QName elName = part != null ? part.getConcreteName() : null;
    if (clazz != null && clazz.isArray() && part != null && part.getXmlSchema() instanceof XmlSchemaElement) {
        XmlSchemaElement el = (XmlSchemaElement) part.getXmlSchema();
        if (el.getSchemaType() instanceof XmlSchemaSimpleType && ((XmlSchemaSimpleType) el.getSchemaType()).getContent() instanceof XmlSchemaSimpleTypeList) {
            Object obj = unmarshall(u, source, elName, null, unwrap);
            if (clazz.isArray() && obj instanceof List) {
                return ((List<?>) obj).toArray((Object[]) Array.newInstance(clazz.getComponentType(), ((List<?>) obj).size()));
            }
            return obj;
        } else if (part.getMessageInfo().getOperation().isUnwrapped() && el.getMaxOccurs() != 1) {
            // must read ourselves....
            List<Object> ret = unmarshallArray(u, source, elName, clazz.getComponentType(), createList(part));
            Object o = ret;
            if (!isList(part)) {
                if (isSet(part)) {
                    o = createSet(part, ret);
                } else if (clazz.getComponentType().isPrimitive()) {
                    o = java.lang.reflect.Array.newInstance(clazz.getComponentType(), ret.size());
                    for (int x = 0; x < ret.size(); x++) {
                        Array.set(o, x, ret.get(x));
                    }
                } else {
                    o = ret.toArray((Object[]) Array.newInstance(clazz.getComponentType(), ret.size()));
                }
            }
            return o;
        }
    } else if (byte[].class == clazz && part != null && part.getTypeQName() != null && part.getTypeQName().getLocalPart().equals("hexBinary")) {
        String obj = (String) unmarshall(u, source, elName, String.class, unwrap);
        return new HexBinaryAdapter().unmarshal(obj);
    } else if (part != null && u.getSchema() != null && !(part.getXmlSchema() instanceof XmlSchemaElement)) {
        // Validating RPC/Lit, make sure we don't try a root element name thing
        source = updateSourceWithXSIType(source, part.getTypeQName());
    }
    Object o = unmarshall(u, source, elName, clazz, unwrap);
    if (o != null && o.getClass().isArray() && isList(part)) {
        List<Object> ret = createList(part);
        ret.addAll(Arrays.asList((Object[]) o));
        o = ret;
    }
    return o;
}
Also used : XmlSchemaSimpleTypeList(org.apache.ws.commons.schema.XmlSchemaSimpleTypeList) HexBinaryAdapter(javax.xml.bind.annotation.adapters.HexBinaryAdapter) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) XmlSchemaSimpleTypeList(org.apache.ws.commons.schema.XmlSchemaSimpleTypeList) List(java.util.List) ArrayList(java.util.ArrayList) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) PrivilegedActionException(java.security.PrivilegedActionException)

Example 2 with HexBinaryAdapter

use of javax.xml.bind.annotation.adapters.HexBinaryAdapter in project hadoop by apache.

the class OfflineImageReconstructor method xattrsXmlToProto.

private INodeSection.XAttrFeatureProto.Builder xattrsXmlToProto(Node xattrs) throws IOException {
    INodeSection.XAttrFeatureProto.Builder bld = INodeSection.XAttrFeatureProto.newBuilder();
    while (true) {
        Node xattr = xattrs.removeChild(INODE_SECTION_XATTR);
        if (xattr == null) {
            break;
        }
        INodeSection.XAttrCompactProto.Builder b = INodeSection.XAttrCompactProto.newBuilder();
        String ns = xattr.removeChildStr(INODE_SECTION_NS);
        if (ns == null) {
            throw new IOException("<xattr> had no <ns> entry.");
        }
        int nsIdx = XAttrProtos.XAttrProto.XAttrNamespaceProto.valueOf(ns).ordinal();
        String name = xattr.removeChildStr(SECTION_NAME);
        String valStr = xattr.removeChildStr(INODE_SECTION_VAL);
        byte[] val = null;
        if (valStr == null) {
            String valHex = xattr.removeChildStr(INODE_SECTION_VAL_HEX);
            if (valHex == null) {
                throw new IOException("<xattr> had no <val> or <valHex> entry.");
            }
            val = new HexBinaryAdapter().unmarshal(valHex);
        } else {
            val = valStr.getBytes("UTF8");
        }
        b.setValue(ByteString.copyFrom(val));
        // The XAttrCompactProto name field uses a fairly complex format
        // to encode both the string table ID of the xattr name and the
        // namespace ID.  See the protobuf file for details.
        int nameId = registerStringId(name);
        int encodedName = (nameId << XATTR_NAME_OFFSET) | ((nsIdx & XATTR_NAMESPACE_MASK) << XATTR_NAMESPACE_OFFSET) | (((nsIdx >> 2) & XATTR_NAMESPACE_EXT_MASK) << XATTR_NAMESPACE_EXT_OFFSET);
        b.setName(encodedName);
        xattr.verifyNoRemainingKeys("xattr");
        bld.addXAttrs(b);
    }
    xattrs.verifyNoRemainingKeys("xattrs");
    return bld;
}
Also used : HexBinaryAdapter(javax.xml.bind.annotation.adapters.HexBinaryAdapter) ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException)

Example 3 with HexBinaryAdapter

use of javax.xml.bind.annotation.adapters.HexBinaryAdapter in project cxf by apache.

the class ConfigurerImplTest method testConfigureSimpleNoMatchingBean.

@Test
public void testConfigureSimpleNoMatchingBean() {
    SimpleBean sb = new SimpleBean("unknown");
    BusApplicationContext ac = new BusApplicationContext("/org/apache/cxf/configuration/spring/test-beans.xml", false);
    ConfigurerImpl configurer = new ConfigurerImpl(ac);
    configurer.configureBean(sb);
    assertEquals("Unexpected value for attribute stringAttr", "hello", sb.getStringAttr());
    assertTrue("Unexpected value for attribute booleanAttr", sb.getBooleanAttr());
    assertEquals("Unexpected value for attribute integerAttr", BigInteger.ONE, sb.getIntegerAttr());
    assertEquals("Unexpected value for attribute intAttr", Integer.valueOf(2), sb.getIntAttr());
    assertEquals("Unexpected value for attribute longAttr", Long.valueOf(3L), sb.getLongAttr());
    assertEquals("Unexpected value for attribute shortAttr", Short.valueOf((short) 4), sb.getShortAttr());
    assertEquals("Unexpected value for attribute decimalAttr", new BigDecimal("5"), sb.getDecimalAttr());
    assertEquals("Unexpected value for attribute floatAttr", Float.valueOf(6F), sb.getFloatAttr());
    assertEquals("Unexpected value for attribute doubleAttr", Double.valueOf(7.0D), sb.getDoubleAttr());
    assertEquals("Unexpected value for attribute byteAttr", Byte.valueOf((byte) 8), sb.getByteAttr());
    QName qn = sb.getQnameAttr();
    assertEquals("Unexpected value for attribute qnameAttrNoDefault", "schema", qn.getLocalPart());
    assertEquals("Unexpected value for attribute qnameAttrNoDefault", "http://www.w3.org/2001/XMLSchema", qn.getNamespaceURI());
    byte[] expected = DatatypeConverter.parseBase64Binary("abcd");
    byte[] val = sb.getBase64BinaryAttr();
    assertEquals("Unexpected value for attribute base64BinaryAttrNoDefault", expected.length, val.length);
    for (int i = 0; i < expected.length; i++) {
        assertEquals("Unexpected value for attribute base64BinaryAttrNoDefault", expected[i], val[i]);
    }
    expected = new HexBinaryAdapter().unmarshal("aaaa");
    val = sb.getHexBinaryAttr();
    assertEquals("Unexpected value for attribute hexBinaryAttrNoDefault", expected.length, val.length);
    for (int i = 0; i < expected.length; i++) {
        assertEquals("Unexpected value for attribute hexBinaryAttrNoDefault", expected[i], val[i]);
    }
    assertEquals("Unexpected value for attribute unsignedIntAttrNoDefault", Long.valueOf(9L), sb.getUnsignedIntAttr());
    assertEquals("Unexpected value for attribute unsignedShortAttrNoDefault", Integer.valueOf(10), sb.getUnsignedShortAttr());
    assertEquals("Unexpected value for attribute unsignedByteAttrNoDefault", Short.valueOf((short) 11), sb.getUnsignedByteAttr());
}
Also used : BusApplicationContext(org.apache.cxf.bus.spring.BusApplicationContext) HexBinaryAdapter(javax.xml.bind.annotation.adapters.HexBinaryAdapter) QName(javax.xml.namespace.QName) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 4 with HexBinaryAdapter

use of javax.xml.bind.annotation.adapters.HexBinaryAdapter in project cxf by apache.

the class JAXBEncoderDecoder method marshall.

public static void marshall(Marshaller marshaller, Object elValue, MessagePartInfo part, Object source) {
    try {
        // The Marshaller.JAXB_FRAGMENT will tell the Marshaller not to
        // generate the xml declaration.
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
    } catch (javax.xml.bind.PropertyException e) {
    // intentionally empty.
    }
    Class<?> cls = null;
    if (part != null) {
        cls = part.getTypeClass();
    }
    if (cls == null) {
        cls = null != elValue ? elValue.getClass() : null;
    }
    if (cls != null && cls.isArray() && elValue instanceof Collection) {
        Collection<?> col = (Collection<?>) elValue;
        elValue = col.toArray((Object[]) Array.newInstance(cls.getComponentType(), col.size()));
    }
    try {
        Object mObj = elValue;
        QName elName = null;
        if (part != null) {
            elName = part.getConcreteName();
        }
        if (null != elName) {
            if (part != null && part.getXmlSchema() instanceof XmlSchemaElement) {
                XmlSchemaElement el = (XmlSchemaElement) part.getXmlSchema();
                if (mObj.getClass().isArray() && el.getSchemaType() instanceof XmlSchemaSimpleType && ((XmlSchemaSimpleType) el.getSchemaType()).getContent() instanceof XmlSchemaSimpleTypeList) {
                    mObj = Arrays.asList((Object[]) mObj);
                    writeObject(marshaller, source, newJAXBElement(elName, cls, mObj));
                } else if (part.getMessageInfo().getOperation().isUnwrapped() && (mObj.getClass().isArray() || mObj instanceof List) && el.getMaxOccurs() != 1) {
                    writeArrayObject(marshaller, source, elName, mObj);
                } else {
                    writeObject(marshaller, source, newJAXBElement(elName, cls, mObj));
                }
            } else if (byte[].class == cls && part.getTypeQName() != null && part.getTypeQName().getLocalPart().equals("hexBinary")) {
                mObj = new HexBinaryAdapter().marshal((byte[]) mObj);
                writeObject(marshaller, source, newJAXBElement(elName, String.class, mObj));
            } else if (mObj instanceof JAXBElement) {
                writeObject(marshaller, source, mObj);
            } else if (marshaller.getSchema() != null) {
                // force xsi:type so types can be validated instead of trying to
                // use the RPC/lit element names that aren't in the schema
                writeObject(marshaller, source, newJAXBElement(elName, Object.class, mObj));
            } else {
                writeObject(marshaller, source, newJAXBElement(elName, cls, mObj));
            }
        } else {
            writeObject(marshaller, source, mObj);
        }
    } catch (Fault ex) {
        throw ex;
    } catch (Exception ex) {
        if (ex instanceof javax.xml.bind.MarshalException) {
            javax.xml.bind.MarshalException marshalEx = (javax.xml.bind.MarshalException) ex;
            Message faultMessage = new Message("MARSHAL_ERROR", LOG, marshalEx.getLinkedException().getMessage());
            throw new Fault(faultMessage, ex);
        }
        throw new Fault(new Message("MARSHAL_ERROR", LOG, ex.getMessage()), ex);
    }
}
Also used : XmlSchemaSimpleTypeList(org.apache.ws.commons.schema.XmlSchemaSimpleTypeList) HexBinaryAdapter(javax.xml.bind.annotation.adapters.HexBinaryAdapter) Message(org.apache.cxf.common.i18n.Message) QName(javax.xml.namespace.QName) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) Fault(org.apache.cxf.interceptor.Fault) JAXBElement(javax.xml.bind.JAXBElement) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) PrivilegedActionException(java.security.PrivilegedActionException) XmlSchemaSimpleType(org.apache.ws.commons.schema.XmlSchemaSimpleType) Collection(java.util.Collection) XmlSchemaSimpleTypeList(org.apache.ws.commons.schema.XmlSchemaSimpleTypeList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

HexBinaryAdapter (javax.xml.bind.annotation.adapters.HexBinaryAdapter)4 QName (javax.xml.namespace.QName)3 PrivilegedActionException (java.security.PrivilegedActionException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 JAXBException (javax.xml.bind.JAXBException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 XmlSchemaElement (org.apache.ws.commons.schema.XmlSchemaElement)2 XmlSchemaSimpleType (org.apache.ws.commons.schema.XmlSchemaSimpleType)2 XmlSchemaSimpleTypeList (org.apache.ws.commons.schema.XmlSchemaSimpleTypeList)2 ByteString (com.google.protobuf.ByteString)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 Collection (java.util.Collection)1 JAXBElement (javax.xml.bind.JAXBElement)1 BusApplicationContext (org.apache.cxf.bus.spring.BusApplicationContext)1 Message (org.apache.cxf.common.i18n.Message)1 Fault (org.apache.cxf.interceptor.Fault)1 Test (org.junit.Test)1