Search in sources :

Example 1 with InvalidSerializedDataException

use of javax.jcr.InvalidSerializedDataException 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 InvalidSerializedDataException

use of javax.jcr.InvalidSerializedDataException in project jackrabbit-oak by apache.

the class SysViewImportHandler method endElement.

@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    // check element name
    ImportState state = stack.peek();
    if (namespaceURI.equals(NamespaceConstants.NAMESPACE_SV) && "node".equals(localName)) {
        // sv:node element
        if (!state.started) {
            // need to start & end current node
            processNode(state, true, true);
            state.started = true;
        } else {
            // need to end current node
            processNode(state, false, true);
        }
        // pop current state from stack
        stack.pop();
    } else if (namespaceURI.equals(NamespaceConstants.NAMESPACE_SV) && "property".equals(localName)) {
        // have been collected and create node as necessary primaryType
        if (isSystemProperty("primaryType")) {
            BufferedStringValue val = currentPropValues.get(0);
            String s = null;
            try {
                s = val.retrieve();
                state.nodeTypeName = new NameInfo(s).getRepoQualifiedName();
            } catch (IOException e) {
                throw new SAXException(new InvalidSerializedDataException("illegal node type name: " + s, e));
            } catch (RepositoryException e) {
                throw new SAXException(new InvalidSerializedDataException("illegal node type name: " + s, e));
            }
        } else if (isSystemProperty("mixinTypes")) {
            if (state.mixinNames == null) {
                state.mixinNames = new ArrayList<String>(currentPropValues.size());
            }
            for (BufferedStringValue val : currentPropValues) {
                String s = null;
                try {
                    s = val.retrieve();
                    state.mixinNames.add(new NameInfo(s).getRepoQualifiedName());
                } catch (IOException ioe) {
                    throw new SAXException("error while retrieving value", ioe);
                } catch (RepositoryException e) {
                    throw new SAXException(new InvalidSerializedDataException("illegal mixin type name: " + s, e));
                }
            }
        } else if (isSystemProperty("uuid")) {
            BufferedStringValue val = currentPropValues.get(0);
            try {
                state.uuid = val.retrieve();
            } catch (IOException ioe) {
                throw new SAXException("error while retrieving value", ioe);
            }
        } else {
            if (currentPropMultipleStatus == PropInfo.MultipleStatus.UNKNOWN && currentPropValues.size() != 1) {
                currentPropMultipleStatus = PropInfo.MultipleStatus.MULTIPLE;
            }
            PropInfo prop = new PropInfo(currentPropName == null ? null : currentPropName.getRepoQualifiedName(), currentPropType, currentPropValues, currentPropMultipleStatus);
            state.props.add(prop);
        }
        // reset temp fields
        currentPropValues.clear();
    } else if (namespaceURI.equals(NamespaceConstants.NAMESPACE_SV) && "value".equals(localName)) {
        // sv:value element
        currentPropValues.add(currentPropValue);
        // reset temp fields
        currentPropValue = null;
    } else {
        throw new SAXException(new InvalidSerializedDataException("invalid element in system view xml document: " + localName));
    }
}
Also used : InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) RepositoryException(javax.jcr.RepositoryException) PropInfo(org.apache.jackrabbit.oak.spi.xml.PropInfo) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 3 with InvalidSerializedDataException

use of javax.jcr.InvalidSerializedDataException in project sling by apache.

the class JcrXmlImporter method importJcrXml.

/**
     * Import the XML file as JCR system or document view import. If the XML
     * file is not a valid system or document view export/import file,
     * <code>false</code> is returned.
     *
     * @param parent        The parent node below which to import
     * @param name          the name of the import resource
     * @param contentStream The XML content to import
     * @param replace       Whether or not to replace the subtree at name if the
     *                      node exists.
     * @return <code>true</code> if the import succeeds, <code>false</code> if the import fails due to XML format errors.
     * @throws IOException If an IO error occurrs reading the XML file.
     */
protected Node importJcrXml(Node parent, String name, InputStream contentStream, boolean replace) throws IOException {
    try {
        final String nodeName = (name.endsWith(EXT_JCR_XML)) ? name.substring(0, name.length() - EXT_JCR_XML.length()) : name;
        // ensure the name is not empty
        if (nodeName.length() == 0) {
            throw new IOException("Node name must not be empty (or extension only)");
        }
        // check for existence/replacement
        if (parent.hasNode(nodeName)) {
            Node existingNode = parent.getNode(nodeName);
            if (replace) {
                logger.debug("importJcrXml: Removing existing node at {}", nodeName);
                existingNode.remove();
            } else {
                logger.debug("importJcrXml: Node {} for XML already exists, nothing to to", nodeName);
                return existingNode;
            }
        }
        final int uuidBehavior;
        if (replace) {
            uuidBehavior = IMPORT_UUID_COLLISION_REPLACE_EXISTING;
        } else {
            uuidBehavior = IMPORT_UUID_CREATE_NEW;
        }
        Session session = parent.getSession();
        session.importXML(parent.getPath(), contentStream, uuidBehavior);
        // additionally check whether the expected child node exists
        return (parent.hasNode(nodeName)) ? parent.getNode(nodeName) : null;
    } catch (InvalidSerializedDataException isde) {
        // the xml might not be System or Document View export, fall back to old-style XML reading
        logger.info("importJcrXml: XML does not seem to be system or document view; cause: {}", isde.toString());
        return null;
    } catch (RepositoryException re) {
        // any other repository related issue...
        logger.info("importJcrXml: Repository issue loading XML; cause: {}", re.toString());
        return null;
    }
}
Also used : Node(javax.jcr.Node) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) Session(javax.jcr.Session)

Example 4 with InvalidSerializedDataException

use of javax.jcr.InvalidSerializedDataException in project jackrabbit-oak by apache.

the class SysViewImportHandler method startElement.

// -------------------------------------------------------< ContentHandler >
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    // check element name
    if (namespaceURI.equals(NamespaceConstants.NAMESPACE_SV) && "node".equals(localName)) {
        // sv:node element
        // node name (value of sv:name attribute)
        String svName = getAttribute(atts, NamespaceConstants.NAMESPACE_SV, "name");
        if (svName == null) {
            throw new SAXException(new InvalidSerializedDataException("missing mandatory sv:name attribute of element sv:node"));
        }
        if (!stack.isEmpty()) {
            // process current node first
            ImportState current = stack.peek();
            // need to start current node
            if (!current.started) {
                processNode(current, true, false);
                current.started = true;
            }
        }
        // push new ImportState instance onto the stack
        ImportState state = new ImportState();
        try {
            state.nodeName = new NameInfo(svName).getRepoQualifiedName();
        } catch (RepositoryException e) {
            throw new SAXException(new InvalidSerializedDataException("illegal node name: " + svName, e));
        }
        stack.push(state);
    } else if (namespaceURI.equals(NamespaceConstants.NAMESPACE_SV) && "property".equals(localName)) {
        // sv:property element
        // reset temp fields
        currentPropValues.clear();
        // property name (value of sv:name attribute)
        String svName = getAttribute(atts, NamespaceConstants.NAMESPACE_SV, "name");
        if (svName == null) {
            throw new SAXException(new InvalidSerializedDataException("missing mandatory sv:name attribute of element sv:property"));
        }
        try {
            currentPropName = new NameInfo(svName);
        } catch (RepositoryException e) {
            throw new SAXException(new InvalidSerializedDataException("illegal property name: " + svName, e));
        }
        // property type (sv:type attribute)
        String type = getAttribute(atts, NamespaceConstants.NAMESPACE_SV, "type");
        if (type == null) {
            throw new SAXException(new InvalidSerializedDataException("missing mandatory sv:type attribute of element sv:property"));
        }
        try {
            currentPropType = PropertyType.valueFromName(type);
        } catch (IllegalArgumentException e) {
            throw new SAXException(new InvalidSerializedDataException("Unknown property type: " + type, e));
        }
        // 'multi-value' hint (sv:multiple attribute)
        String multiple = getAttribute(atts, NamespaceConstants.NAMESPACE_SV, "multiple");
        if (multiple != null) {
            currentPropMultipleStatus = PropInfo.MultipleStatus.MULTIPLE;
        } else {
            currentPropMultipleStatus = PropInfo.MultipleStatus.UNKNOWN;
        }
    } else if (namespaceURI.equals(NamespaceConstants.NAMESPACE_SV) && "value".equals(localName)) {
        // sv:value element
        boolean base64 = currentPropType == PropertyType.BINARY || "xs:base64Binary".equals(atts.getValue("xsi:type"));
        currentPropValue = new BufferedStringValue(sessionContext.getValueFactory(), currentNamePathMapper(), base64);
    } else {
        throw new SAXException(new InvalidSerializedDataException("Unexpected element in system view xml document: {" + namespaceURI + '}' + localName));
    }
}
Also used : InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) RepositoryException(javax.jcr.RepositoryException) SAXException(org.xml.sax.SAXException)

Example 5 with InvalidSerializedDataException

use of javax.jcr.InvalidSerializedDataException in project jackrabbit by apache.

the class SerializationTest method testInvalidXmlThrowsInvalidSerializedDataException.

/**
 * Tests whether importing an invalid XML file throws a InvalidSerializedDataException.
 * The file used here is more or less garbage.
 */
public void testInvalidXmlThrowsInvalidSerializedDataException() throws RepositoryException, IOException {
    String data = "<this is not a <valid> <xml> file/>";
    ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes());
    try {
        session.importXML(treeComparator.targetFolder, in, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
        fail("Importing a invalid XML file should throw a InvalidSerializedDataException.");
    } catch (InvalidSerializedDataException e) {
    // ok
    }
    in = new ByteArrayInputStream(data.getBytes());
    try {
        workspace.importXML(treeComparator.targetFolder, in, 0);
        fail("Importing a invalid XML file should throw a InvalidSerializedDataException.");
    } catch (InvalidSerializedDataException e) {
    // ok
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException)

Aggregations

InvalidSerializedDataException (javax.jcr.InvalidSerializedDataException)14 SAXException (org.xml.sax.SAXException)12 IOException (java.io.IOException)10 RepositoryException (javax.jcr.RepositoryException)8 NamespaceException (javax.jcr.NamespaceException)5 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)5 ParsingContentHandler (org.apache.jackrabbit.commons.xml.ParsingContentHandler)4 ContentHandler (org.xml.sax.ContentHandler)4 Name (org.apache.jackrabbit.spi.Name)3 Node (javax.jcr.Node)2 ToXmlContentHandler (org.apache.jackrabbit.commons.xml.ToXmlContentHandler)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 AccessControlException (java.security.AccessControlException)1 AccessDeniedException (javax.jcr.AccessDeniedException)1 InvalidItemStateException (javax.jcr.InvalidItemStateException)1 ItemExistsException (javax.jcr.ItemExistsException)1 ItemNotFoundException (javax.jcr.ItemNotFoundException)1 LoginException (javax.jcr.LoginException)1 NoSuchWorkspaceException (javax.jcr.NoSuchWorkspaceException)1