Search in sources :

Example 6 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 7 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 8 with InvalidSerializedDataException

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

the class SysViewImportHandler method endElement.

/**
     * {@inheritDoc}
     */
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    Name name = NameFactoryImpl.getInstance().create(namespaceURI, localName);
    // check element name
    ImportState state = stack.peek();
    if (name.equals(NameConstants.SV_NODE)) {
        // 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 (name.equals(NameConstants.SV_PROPERTY)) {
        // have been collected and create node as necessary
        if (currentPropName.equals(NameConstants.JCR_PRIMARYTYPE)) {
            BufferedStringValue val = currentPropValues.get(0);
            String s = null;
            try {
                s = val.retrieve();
                state.nodeTypeName = resolver.getQName(s);
            } catch (IOException ioe) {
                throw new SAXException("error while retrieving value", ioe);
            } catch (NameException e) {
                throw new SAXException(new InvalidSerializedDataException("illegal node type name: " + s, e));
            } catch (NamespaceException e) {
                throw new SAXException(new InvalidSerializedDataException("illegal node type name: " + s, e));
            }
        } else if (currentPropName.equals(NameConstants.JCR_MIXINTYPES)) {
            if (state.mixinNames == null) {
                state.mixinNames = new ArrayList<Name>(currentPropValues.size());
            }
            for (BufferedStringValue val : currentPropValues) {
                String s = null;
                try {
                    s = val.retrieve();
                    Name mixin = resolver.getQName(s);
                    state.mixinNames.add(mixin);
                } catch (IOException ioe) {
                    throw new SAXException("error while retrieving value", ioe);
                } catch (NameException e) {
                    throw new SAXException(new InvalidSerializedDataException("illegal mixin type name: " + s, e));
                } catch (NamespaceException e) {
                    throw new SAXException(new InvalidSerializedDataException("illegal mixin type name: " + s, e));
                }
            }
        } else if (currentPropName.equals(NameConstants.JCR_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 == MultipleStatus.UNKNOWN && currentPropValues.size() != 1) {
                currentPropMultipleStatus = MultipleStatus.MULTIPLE;
            }
            PropInfo prop = new PropInfo(currentPropName, currentPropType, currentPropValues.toArray(new TextValue[currentPropValues.size()]), currentPropMultipleStatus);
            state.props.add(prop);
        }
        // reset temp fields
        currentPropValues.clear();
    } else if (name.equals(NameConstants.SV_VALUE)) {
        // 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 : NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) NamespaceException(javax.jcr.NamespaceException) IOException(java.io.IOException) Name(org.apache.jackrabbit.spi.Name) SAXException(org.xml.sax.SAXException)

Example 9 with InvalidSerializedDataException

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

the class AbstractSession method importXML.

/**
     * Parses the given input stream as an XML document and processes the
     * SAX events using the {@link ContentHandler} returned by
     * {@link Session#getImportContentHandler(String, int)}.
     *
     * @param parentAbsPath passed through
     * @param in input stream to be parsed as XML and imported
     * @param uuidBehavior passed through
     * @throws IOException if an I/O error occurs
     * @throws InvalidSerializedDataException if an XML parsing error occurs
     * @throws RepositoryException if a repository error occurs
     */
public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, InvalidSerializedDataException, RepositoryException {
    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 {
            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) ContentHandler(org.xml.sax.ContentHandler) ParsingContentHandler(org.apache.jackrabbit.commons.xml.ParsingContentHandler) ToXmlContentHandler(org.apache.jackrabbit.commons.xml.ToXmlContentHandler) SAXException(org.xml.sax.SAXException)

Example 10 with InvalidSerializedDataException

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

the class SessionImpl method importXML.

/**
     * @see javax.jcr.Session#importXML(String, java.io.InputStream, int)
     */
@Override
public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, PathNotFoundException, ItemExistsException, ConstraintViolationException, VersionException, InvalidSerializedDataException, LockException, RepositoryException {
    // NOTE: checks are performed by 'getImportContentHandler'
    ImportHandler handler = (ImportHandler) getImportContentHandler(parentAbsPath, uuidBehavior);
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
        SAXParser parser = factory.newSAXParser();
        parser.parse(new InputSource(in), handler);
    } catch (SAXException se) {
        // check for wrapped repository exception
        Exception e = se.getException();
        if (e != null && e instanceof RepositoryException) {
            throw (RepositoryException) e;
        } else {
            String msg = "failed to parse XML stream";
            log.debug(msg);
            throw new InvalidSerializedDataException(msg, se);
        }
    } catch (ParserConfigurationException e) {
        throw new RepositoryException("SAX parser configuration error", e);
    } finally {
        // JCR-2903
        in.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) ImportHandler(org.apache.jackrabbit.jcr2spi.xml.ImportHandler) SAXParser(javax.xml.parsers.SAXParser) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) RepositoryException(javax.jcr.RepositoryException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ItemExistsException(javax.jcr.ItemExistsException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NoSuchWorkspaceException(javax.jcr.NoSuchWorkspaceException) VersionException(javax.jcr.version.VersionException) InvalidSerializedDataException(javax.jcr.InvalidSerializedDataException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemNotFoundException(javax.jcr.ItemNotFoundException) LoginException(javax.jcr.LoginException) SAXException(org.xml.sax.SAXException) AccessControlException(java.security.AccessControlException) AccessDeniedException(javax.jcr.AccessDeniedException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) LockException(javax.jcr.lock.LockException) NamespaceException(javax.jcr.NamespaceException) IOException(java.io.IOException) UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

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