Search in sources :

Example 1 with ParsingContentHandler

use of org.apache.jackrabbit.commons.xml.ParsingContentHandler in project jackrabbit-oak by apache.

the class WorkspaceImpl method importXML.

@Override
public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, RepositoryException {
    ensureIsAlive();
    try {
        ContentHandler handler = getImportContentHandler(parentAbsPath, uuidBehavior);
        new ParsingContentHandler(handler).parse(in);
    } catch (SAXException e) {
        Throwable exception = e.getException();
        if (exception instanceof RepositoryException) {
            throw (RepositoryException) exception;
        } else if (exception instanceof IOException) {
            throw (IOException) exception;
        } else if (exception instanceof CommitFailedException) {
            throw ((CommitFailedException) exception).asRepositoryException();
        } else {
            throw new InvalidSerializedDataException("XML parse error", e);
        }
    } finally {
        // JCR-2903
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) ContentHandler(org.xml.sax.ContentHandler) ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) SAXException(org.xml.sax.SAXException)

Example 2 with ParsingContentHandler

use of org.apache.jackrabbit.commons.xml.ParsingContentHandler in project jackrabbit by apache.

the class UserImporterTest method doImport.

private void doImport(NodeImpl target, String xml, int uuidBehavior, int importBehavior) throws IOException, SAXException, RepositoryException {
    InputStream in = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    SessionImporter importer = new SessionImporter(target, sImpl, uuidBehavior, new PseudoConfig(importBehavior));
    ImportHandler ih = new ImportHandler(importer, sImpl);
    new ParsingContentHandler(ih).parse(in);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) ImportHandler(org.apache.jackrabbit.core.xml.ImportHandler) SessionImporter(org.apache.jackrabbit.core.xml.SessionImporter)

Example 3 with ParsingContentHandler

use of org.apache.jackrabbit.commons.xml.ParsingContentHandler in project jackrabbit by apache.

the class AccessControlImporterTest method testImportWithDefaultImporter.

/**
 * With the default importer that isn't able to deal with ACEs the
 * policy will be created but any ACEs will be ignored.
 *
 * @throws Exception
 */
public void testImportWithDefaultImporter() throws Exception {
    NodeImpl target = (NodeImpl) testRootNode;
    try {
        InputStream in = new ByteArrayInputStream(XML_POLICY_TREE.getBytes("UTF-8"));
        SessionImporter importer = new SessionImporter(target, sImpl, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW, null);
        ImportHandler ih = new ImportHandler(importer, sImpl);
        new ParsingContentHandler(ih).parse(in);
        assertTrue(target.hasNode("test"));
        String path = target.getNode("test").getPath();
        AccessControlManager acMgr = sImpl.getAccessControlManager();
        AccessControlPolicy[] policies = acMgr.getPolicies(path);
        assertEquals(1, policies.length);
        assertTrue(policies[0] instanceof JackrabbitAccessControlList);
        AccessControlEntry[] entries = ((JackrabbitAccessControlList) policies[0]).getAccessControlEntries();
        assertEquals(0, entries.length);
    } finally {
        superuser.refresh(false);
    }
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) JackrabbitAccessControlManager(org.apache.jackrabbit.api.security.JackrabbitAccessControlManager) AccessControlPolicy(javax.jcr.security.AccessControlPolicy) NodeImpl(org.apache.jackrabbit.core.NodeImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) JackrabbitAccessControlEntry(org.apache.jackrabbit.api.security.JackrabbitAccessControlEntry) AccessControlEntry(javax.jcr.security.AccessControlEntry) JackrabbitAccessControlList(org.apache.jackrabbit.api.security.JackrabbitAccessControlList) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 4 with ParsingContentHandler

use of org.apache.jackrabbit.commons.xml.ParsingContentHandler in project jackrabbit by apache.

the class AccessControlImporterTest method testImportPrincipalBasedACL.

/**
 * Imports a principal-based ACL containing a single entry mist fail with
 * the default configuration.
 *
 * @throws Exception
 */
public void testImportPrincipalBasedACL() throws Exception {
    JackrabbitAccessControlManager acMgr = (JackrabbitAccessControlManager) sImpl.getAccessControlManager();
    if (acMgr.getApplicablePolicies(EveryonePrincipal.getInstance()).length > 0 || acMgr.getPolicies(EveryonePrincipal.getInstance()).length > 0) {
        // test expects that only resource-based acl is supported
        throw new NotExecutableException();
    }
    PrincipalManager pmgr = sImpl.getPrincipalManager();
    if (!pmgr.hasPrincipal(SecurityConstants.ADMINISTRATORS_NAME)) {
        UserManager umgr = sImpl.getUserManager();
        umgr.createGroup(new PrincipalImpl(SecurityConstants.ADMINISTRATORS_NAME));
        if (!umgr.isAutoSave()) {
            sImpl.save();
        }
        if (pmgr.hasPrincipal(SecurityConstants.ADMINISTRATORS_NAME)) {
            throw new NotExecutableException();
        }
    }
    NodeImpl target;
    NodeImpl root = (NodeImpl) sImpl.getRootNode();
    if (!root.hasNode(AccessControlConstants.N_ACCESSCONTROL)) {
        target = root.addNode(AccessControlConstants.N_ACCESSCONTROL, AccessControlConstants.NT_REP_ACCESS_CONTROL, null);
    } else {
        target = root.getNode(AccessControlConstants.N_ACCESSCONTROL);
        if (!target.isNodeType(AccessControlConstants.NT_REP_ACCESS_CONTROL)) {
            target.setPrimaryType(sImpl.getJCRName(AccessControlConstants.NT_REP_ACCESS_CONTROL));
        }
    }
    try {
        InputStream in = new ByteArrayInputStream(XML_AC_TREE.getBytes("UTF-8"));
        SessionImporter importer = new SessionImporter(target, sImpl, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW, new PseudoConfig());
        ImportHandler ih = new ImportHandler(importer, sImpl);
        new ParsingContentHandler(ih).parse(in);
        fail("Default config only allows resource-based ACL -> protected import must fail");
    } catch (SAXException e) {
        if (e.getException() instanceof ConstraintViolationException) {
        // success
        } else {
            throw e;
        }
    } finally {
        superuser.refresh(false);
    }
}
Also used : JackrabbitAccessControlManager(org.apache.jackrabbit.api.security.JackrabbitAccessControlManager) PrincipalManager(org.apache.jackrabbit.api.security.principal.PrincipalManager) NodeImpl(org.apache.jackrabbit.core.NodeImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) SAXException(org.xml.sax.SAXException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) ByteArrayInputStream(java.io.ByteArrayInputStream) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) PrincipalImpl(org.apache.jackrabbit.core.security.principal.PrincipalImpl)

Example 5 with ParsingContentHandler

use of org.apache.jackrabbit.commons.xml.ParsingContentHandler in project jackrabbit by apache.

the class AccessControlImporterTest method createPolicyNode.

private NodeImpl createPolicyNode(NodeImpl target) throws Exception {
    try {
        InputStream in = new ByteArrayInputStream(XML_POLICY_ONLY.getBytes("UTF-8"));
        SessionImporter importer = new SessionImporter(target, sImpl, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
        ImportHandler ih = new ImportHandler(importer, sImpl);
        new ParsingContentHandler(ih).parse(in);
        return (NodeImpl) target.getNode("test/rep:policy");
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        superuser.refresh(false);
        if (superuser.nodeExists("/test")) {
            NodeIterator it = superuser.getRootNode().getNodes("test");
            while (it.hasNext()) {
                it.nextNode().remove();
            }
        }
        superuser.save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) NodeImpl(org.apache.jackrabbit.core.NodeImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException)

Aggregations

ParsingContentHandler (org.apache.jackrabbit.commons.xml.ParsingContentHandler)18 ByteArrayInputStream (java.io.ByteArrayInputStream)14 InputStream (java.io.InputStream)14 NodeImpl (org.apache.jackrabbit.core.NodeImpl)12 JackrabbitAccessControlManager (org.apache.jackrabbit.api.security.JackrabbitAccessControlManager)10 AccessControlManager (javax.jcr.security.AccessControlManager)9 AccessControlPolicy (javax.jcr.security.AccessControlPolicy)9 AccessControlEntry (javax.jcr.security.AccessControlEntry)8 JackrabbitAccessControlEntry (org.apache.jackrabbit.api.security.JackrabbitAccessControlEntry)8 JackrabbitAccessControlList (org.apache.jackrabbit.api.security.JackrabbitAccessControlList)8 SAXException (org.xml.sax.SAXException)7 IOException (java.io.IOException)6 InvalidSerializedDataException (javax.jcr.InvalidSerializedDataException)4 RepositoryException (javax.jcr.RepositoryException)4 ContentHandler (org.xml.sax.ContentHandler)4 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)3 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)3 AccessControlList (javax.jcr.security.AccessControlList)2 AccessControlPolicyIterator (javax.jcr.security.AccessControlPolicyIterator)2 ToXmlContentHandler (org.apache.jackrabbit.commons.xml.ToXmlContentHandler)2