Search in sources :

Example 1 with PrimitiveXNode

use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.

the class XNodeProcessorUtil method parseProtectedType.

public static <T> void parseProtectedType(ProtectedDataType<T> protectedType, MapXNode xmap, PrismContext prismContext, ParsingContext pc) throws SchemaException {
    RootXNode xEncryptedData = xmap.getEntryAsRoot(ProtectedDataType.F_ENCRYPTED_DATA);
    if (xEncryptedData != null) {
        if (!(xEncryptedData.getSubnode() instanceof MapXNode)) {
            throw new SchemaException("Cannot parse encryptedData from " + xEncryptedData);
        }
        EncryptedDataType encryptedDataType = prismContext.parserFor(xEncryptedData).context(pc).parseRealValue(EncryptedDataType.class);
        protectedType.setEncryptedData(encryptedDataType);
    } else {
        // Check for legacy EncryptedData
        RootXNode xLegacyEncryptedData = xmap.getEntryAsRoot(ProtectedDataType.F_XML_ENC_ENCRYPTED_DATA);
        if (xLegacyEncryptedData != null) {
            if (!(xLegacyEncryptedData.getSubnode() instanceof MapXNode)) {
                throw new SchemaException("Cannot parse EncryptedData from " + xEncryptedData);
            }
            RootXNode xConvertedEncryptedData = (RootXNode) xLegacyEncryptedData.cloneTransformKeys(in -> {
                String elementName = StringUtils.uncapitalize(in.getLocalPart());
                if (elementName.equals("type")) {
                    return null;
                }
                return new QName(null, elementName);
            });
            EncryptedDataType encryptedDataType = prismContext.parserFor(xConvertedEncryptedData).context(pc).parseRealValue(EncryptedDataType.class);
            protectedType.setEncryptedData(encryptedDataType);
            if (protectedType instanceof ProtectedStringType) {
                transformEncryptedValue(protectedType, prismContext);
            }
        }
    }
    RootXNode xHashedData = xmap.getEntryAsRoot(ProtectedDataType.F_HASHED_DATA);
    if (xHashedData != null) {
        if (!(xHashedData.getSubnode() instanceof MapXNode)) {
            throw new SchemaException("Cannot parse hashedData from " + xHashedData);
        }
        HashedDataType hashedDataType = prismContext.parserFor(xHashedData).context(pc).parseRealValue(HashedDataType.class);
        protectedType.setHashedData(hashedDataType);
    }
    // protected data empty..check for clear value
    if (protectedType.isEmpty()) {
        XNode xClearValue = xmap.get(ProtectedDataType.F_CLEAR_VALUE);
        if (xClearValue == null) {
            //TODO: try to use common namespace (only to be compatible with previous versions)
            //FIXME maybe add some warning, info...
            xClearValue = xmap.get(new QName(ProtectedDataType.F_CLEAR_VALUE.getLocalPart()));
        }
        if (xClearValue == null) {
            return;
        }
        if (!(xClearValue instanceof PrimitiveXNode)) {
            //this is maybe not good..
            throw new SchemaException("Cannot parse clear value from " + xClearValue);
        }
        // TODO: clearValue
        T clearValue = (T) ((PrimitiveXNode) xClearValue).getParsedValue(DOMUtil.XSD_STRING, String.class);
        protectedType.setClearValue(clearValue);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) HashedDataType(com.evolveum.prism.xml.ns._public.types_3.HashedDataType) Field(java.lang.reflect.Field) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) DOMUtil(com.evolveum.midpoint.util.DOMUtil) ParsingContext(com.evolveum.midpoint.prism.ParsingContext) Protector(com.evolveum.midpoint.prism.crypto.Protector) PrismContext(com.evolveum.midpoint.prism.PrismContext) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) XmlValue(javax.xml.bind.annotation.XmlValue) QName(javax.xml.namespace.QName) ProtectedDataType(com.evolveum.prism.xml.ns._public.types_3.ProtectedDataType) ProtectedStringType(com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType) EncryptedDataType(com.evolveum.prism.xml.ns._public.types_3.EncryptedDataType) SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) EncryptedDataType(com.evolveum.prism.xml.ns._public.types_3.EncryptedDataType) QName(javax.xml.namespace.QName) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) ProtectedStringType(com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType) HashedDataType(com.evolveum.prism.xml.ns._public.types_3.HashedDataType)

Example 2 with PrimitiveXNode

use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.

the class SearchFilterType method parseFromXNode.

public void parseFromXNode(XNode xnode, PrismContext prismContext) throws SchemaException {
    if (xnode == null || xnode.isEmpty()) {
        this.filterClauseXNode = null;
        this.description = null;
    } else {
        if (!(xnode instanceof MapXNode)) {
            throw new SchemaException("Cannot parse filter from " + xnode);
        }
        MapXNode xmap = (MapXNode) xnode;
        XNode xdesc = xmap.get(SearchFilterType.F_DESCRIPTION);
        if (xdesc != null) {
            if (xdesc instanceof PrimitiveXNode<?>) {
                String desc = ((PrimitiveXNode<String>) xdesc).getParsedValue(DOMUtil.XSD_STRING, String.class);
                setDescription(desc);
            } else {
                throw new SchemaException("Description must have a primitive value");
            }
        }
        MapXNode xfilter = new MapXNode();
        for (Entry<QName, XNode> entry : xmap.entrySet()) {
            if (!QNameUtil.match(entry.getKey(), SearchFilterType.F_DESCRIPTION) && !QNameUtil.match(entry.getKey(), new QName("condition"))) {
                xfilter.put(entry.getKey(), entry.getValue());
            }
        }
        if (xfilter.size() > 1) {
            throw new SchemaException("Filter clause has more than one item: " + xfilter);
        }
        this.filterClauseXNode = xfilter;
        QueryConvertor.parseFilterPreliminarily(xfilter, prismContext);
    }
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) QName(javax.xml.namespace.QName) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode)

Example 3 with PrimitiveXNode

use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.

the class TestDomParser method testParseUserToXNode.

@Test
public void testParseUserToXNode() throws Exception {
    final String TEST_NAME = "testParseUserToXNode";
    displayTestTitle(TEST_NAME);
    // GIVEN
    DomLexicalProcessor parser = createParser();
    // WHEN
    XNode xnode = parser.read(getFile(USER_JACK_FILE_BASENAME), ParsingContext.createDefault());
    // THEN
    System.out.println("Parsed XNode:");
    System.out.println(xnode.debugDump());
    RootXNode root = getAssertXNode("root node", xnode, RootXNode.class);
    MapXNode rootMap = getAssertXNode("root subnode", root.getSubnode(), MapXNode.class);
    PrimitiveXNode<String> xname = getAssertXMapSubnode("root map", rootMap, UserType.F_NAME, PrimitiveXNode.class);
    // TODO: assert value
    ListXNode xass = getAssertXMapSubnode("root map", rootMap, UserType.F_ASSIGNMENT, ListXNode.class);
    assertEquals("assignment size", 2, xass.size());
    // TODO: asserts
    MapXNode xextension = getAssertXMapSubnode("root map", rootMap, UserType.F_EXTENSION, MapXNode.class);
}
Also used : DomLexicalProcessor(com.evolveum.midpoint.prism.lex.dom.DomLexicalProcessor) ListXNode(com.evolveum.midpoint.prism.xnode.ListXNode) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) ListXNode(com.evolveum.midpoint.prism.xnode.ListXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) Test(org.testng.annotations.Test)

Example 4 with PrimitiveXNode

use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.

the class DomLexicalWriter method serializeSubnode.

private void serializeSubnode(XNode xsubnode, Element parentElement, QName elementName) throws SchemaException {
    if (xsubnode == null) {
        return;
    }
    if (xsubnode instanceof RootXNode) {
        Element element = createElement(elementName, parentElement);
        appendCommentIfPresent(element, xsubnode);
        parentElement.appendChild(element);
        serializeSubnode(((RootXNode) xsubnode).getSubnode(), element, ((RootXNode) xsubnode).getRootElementName());
    } else if (xsubnode instanceof MapXNode) {
        Element element = createElement(elementName, parentElement);
        appendCommentIfPresent(element, xsubnode);
        if (xsubnode.isExplicitTypeDeclaration() && xsubnode.getTypeQName() != null) {
            DOMUtil.setXsiType(element, setQNamePrefixExplicitIfNeeded(xsubnode.getTypeQName()));
        }
        parentElement.appendChild(element);
        serializeMap((MapXNode) xsubnode, element);
    } else if (xsubnode instanceof PrimitiveXNode<?>) {
        PrimitiveXNode<?> xprim = (PrimitiveXNode<?>) xsubnode;
        if (xprim.isAttribute()) {
            serializePrimitiveElementOrAttribute(xprim, parentElement, elementName, true);
        } else {
            serializePrimitiveElementOrAttribute(xprim, parentElement, elementName, false);
        }
    } else if (xsubnode instanceof ListXNode) {
        ListXNode xlist = (ListXNode) xsubnode;
        if (xlist.isHeterogeneousList()) {
            Element element = createElement(elementName, parentElement);
            serializeExplicitList(xlist, element);
            parentElement.appendChild(element);
        } else {
            for (XNode xsubsubnode : xlist) {
                serializeSubnode(xsubsubnode, parentElement, elementName);
            }
        }
    } else if (xsubnode instanceof SchemaXNode) {
        serializeSchema((SchemaXNode) xsubnode, parentElement);
    } else {
        throw new IllegalArgumentException("Unknown subnode " + xsubnode);
    }
}
Also used : SchemaXNode(com.evolveum.midpoint.prism.xnode.SchemaXNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) ListXNode(com.evolveum.midpoint.prism.xnode.ListXNode) Element(org.w3c.dom.Element) SchemaXNode(com.evolveum.midpoint.prism.xnode.SchemaXNode) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) ListXNode(com.evolveum.midpoint.prism.xnode.ListXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode)

Example 5 with PrimitiveXNode

use of com.evolveum.midpoint.prism.xnode.PrimitiveXNode in project midpoint by Evolveum.

the class DomLexicalWriter method serializeInternal.

@NotNull
private Element serializeInternal(@NotNull RootXNode rootxnode, Element parentElement) throws SchemaException {
    QName rootElementName = rootxnode.getRootElementName();
    Element topElement = createElement(rootElementName, parentElement);
    if (parentElement != null) {
        parentElement.appendChild(topElement);
    }
    QName typeQName = rootxnode.getTypeQName();
    if (typeQName != null && rootxnode.isExplicitTypeDeclaration()) {
        DOMUtil.setXsiType(topElement, setQNamePrefixExplicitIfNeeded(typeQName));
    }
    XNode subnode = rootxnode.getSubnode();
    if (subnode instanceof PrimitiveXNode) {
        serializePrimitiveElementOrAttribute((PrimitiveXNode) subnode, topElement, rootElementName, false);
        return DOMUtil.getFirstChildElement(topElement);
    }
    if (subnode instanceof MapXNode) {
        // at this point we can put frequently used namespaces (e.g. c, t, q, ri) into the document to eliminate their use
        // on many places inside the doc (MID-2198)
        DOMUtil.setNamespaceDeclarations(topElement, getNamespacePrefixMapper().getNamespacesDeclaredByDefault());
        serializeMap((MapXNode) subnode, topElement);
    } else if (subnode.isHeterogeneousList()) {
        DOMUtil.setNamespaceDeclarations(topElement, getNamespacePrefixMapper().getNamespacesDeclaredByDefault());
        serializeExplicitList((ListXNode) subnode, topElement);
    } else {
        throw new SchemaException("Sub-root xnode is not a map nor an explicit list, cannot serialize to XML (it is " + subnode + ")");
    }
    addDefaultNamespaceDeclaration(topElement);
    return topElement;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) ListXNode(com.evolveum.midpoint.prism.xnode.ListXNode) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) SchemaXNode(com.evolveum.midpoint.prism.xnode.SchemaXNode) RootXNode(com.evolveum.midpoint.prism.xnode.RootXNode) XNode(com.evolveum.midpoint.prism.xnode.XNode) PrimitiveXNode(com.evolveum.midpoint.prism.xnode.PrimitiveXNode) ListXNode(com.evolveum.midpoint.prism.xnode.ListXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PrimitiveXNode (com.evolveum.midpoint.prism.xnode.PrimitiveXNode)19 QName (javax.xml.namespace.QName)13 MapXNode (com.evolveum.midpoint.prism.xnode.MapXNode)10 XNode (com.evolveum.midpoint.prism.xnode.XNode)9 RootXNode (com.evolveum.midpoint.prism.xnode.RootXNode)8 ListXNode (com.evolveum.midpoint.prism.xnode.ListXNode)5 RawType (com.evolveum.prism.xml.ns._public.types_3.RawType)5 Test (org.testng.annotations.Test)5 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)4 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 ItemPathType (com.evolveum.prism.xml.ns._public.types_3.ItemPathType)4 SchemaXNode (com.evolveum.midpoint.prism.xnode.SchemaXNode)3 JAXBElement (javax.xml.bind.JAXBElement)3 Element (org.w3c.dom.Element)3 PrismContext (com.evolveum.midpoint.prism.PrismContext)2 DomLexicalProcessor (com.evolveum.midpoint.prism.lex.dom.DomLexicalProcessor)2 MappingType (com.evolveum.midpoint.xml.ns._public.common.common_3.MappingType)2 AbstractModelIntegrationTest (com.evolveum.midpoint.model.test.AbstractModelIntegrationTest)1 ParsingContext (com.evolveum.midpoint.prism.ParsingContext)1