org.opensaml.xml.parse.XMLParserException Java Examples

The following examples show how to use org.opensaml.xml.parse.XMLParserException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Decrypter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parse the specified input stream in a DOM DocumentFragment, owned by the specified Document.
 * 
 * @param input the InputStream to parse
 * @param owningDocument the Document which will own the returned DocumentFragment
 * @return a DocumentFragment
 * @throws DecryptionException thrown if there is an error parsing the input stream
 */
private DocumentFragment parseInputStream(InputStream input, Document owningDocument) throws DecryptionException {
    // Since Xerces currently seems not to handle parsing into a DocumentFragment
    // without a bit hackery, use this to simulate, so we can keep the API
    // the way it hopefully will look in the future. Obviously this only works for
    // input streams containing valid XML instances, not fragments.

    Document newDocument = null;
    try {
        newDocument = parserPool.parse(input);
    } catch (XMLParserException e) {
        log.error("Error parsing decrypted input stream", e);
        throw new DecryptionException("Error parsing input stream", e);
    }

    Element element = newDocument.getDocumentElement();
    owningDocument.adoptNode(element);

    DocumentFragment container = owningDocument.createDocumentFragment();
    container.appendChild(element);

    return container;
}
 
Example #2
Source File: AbstractXMLObjectMarshaller.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Prepares the given DOM caching XMLObject for adoption into another document. If the XMLObject has a parent then
 * all visible namespaces used by the given XMLObject and its descendants are declared within that subtree and the
 * parent's DOM is invalidated.
 * 
 * @param domCachingObject the XMLObject to prepare for adoption
 * 
 * @throws MarshallingException thrown if a namespace within the XMLObject's DOM subtree can not be resolved.
 */
private void prepareForAdoption(XMLObject domCachingObject) throws MarshallingException {
    if (domCachingObject.getParent() != null) {
        log.trace("Rooting all visible namespaces of XMLObject {} before adding it to new parent Element",
                domCachingObject.getElementQName());
        try {
            XMLHelper.rootNamespaces(domCachingObject.getDOM());
        } catch (XMLParserException e) {
            String errorMsg =
                    "Unable to root namespaces of cached DOM element, " + domCachingObject.getElementQName();
            log.error(errorMsg, e);
            throw new MarshallingException(errorMsg, e);
        }

        log.trace("Release DOM of XMLObject parent");
        domCachingObject.releaseParentDOM(true);
    }
}
 
Example #3
Source File: XMLConfigurator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Loads a configuration file from an input stream.
 * 
 * @param configurationStream configuration stream
 * 
 * @throws ConfigurationException thrown if the given configuration is invalid or can not be read
 */
public void load(InputStream configurationStream) throws ConfigurationException {
    try {
        Document configuration = parserPool.parse(configurationStream);
        load(configuration);
    } catch (XMLParserException e) {
        log.error("Invalid configuration file", e);
        throw new ConfigurationException("Unable to create DocumentBuilder", e);
    }

}
 
Example #4
Source File: XMLObjectHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unmarshall a Document from an InputSteam.
 * 
 * @param parserPool the ParserPool instance to use
 * @param inputStream the InputStream to unmarshall
 * @return the unmarshalled XMLObject
 * @throws XMLParserException if there is a problem parsing the input data
 * @throws UnmarshallingException if there is a problem unmarshalling the parsed DOM
 */
public static XMLObject unmarshallFromInputStream(ParserPool parserPool, InputStream inputStream)
        throws XMLParserException, UnmarshallingException {
    Logger log = getLogger();
    log.debug("Parsing InputStream into DOM document");

    Document messageDoc = parserPool.parse(inputStream);
    Element messageElem = messageDoc.getDocumentElement();

    if (log.isTraceEnabled()) {
        log.trace("Resultant DOM message was:");
        log.trace(XMLHelper.nodeToString(messageElem));
    }

    log.debug("Unmarshalling DOM parsed from InputStream");
    Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem);
    if (unmarshaller == null) {
        log.error("Unable to unmarshall InputStream, no unmarshaller registered for element "
                + XMLHelper.getNodeQName(messageElem));
        throw new UnmarshallingException(
                "Unable to unmarshall InputStream, no unmarshaller registered for element "
                        + XMLHelper.getNodeQName(messageElem));
    }

    XMLObject message = unmarshaller.unmarshall(messageElem);

    log.debug("InputStream succesfully unmarshalled");
    return message;
}
 
Example #5
Source File: XMLObjectHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Unmarshall a Document from a Reader.
 * 
 * @param parserPool the ParserPool instance to use
 * @param reader the Reader to unmarshall
 * @return the unmarshalled XMLObject
 * @throws XMLParserException if there is a problem parsing the input data
 * @throws UnmarshallingException if there is a problem unmarshalling the parsed DOM
 */
public static XMLObject unmarshallFromReader(ParserPool parserPool, Reader reader)
        throws XMLParserException, UnmarshallingException {
    Logger log = getLogger();
    log.debug("Parsing Reader into DOM document");
    

    Document messageDoc = parserPool.parse(reader);
    Element messageElem = messageDoc.getDocumentElement();

    if (log.isTraceEnabled()) {
        log.trace("Resultant DOM message was:");
        log.trace(XMLHelper.nodeToString(messageElem));
    }

    log.debug("Unmarshalling DOM parsed from Reader");
    Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(messageElem);
    if (unmarshaller == null) {
        log.error("Unable to unmarshall Reader, no unmarshaller registered for element "
                + XMLHelper.getNodeQName(messageElem));
        throw new UnmarshallingException(
                "Unable to unmarshall Reader, no unmarshaller registered for element "
                        + XMLHelper.getNodeQName(messageElem));
    }

    XMLObject message = unmarshaller.unmarshall(messageElem);

    log.debug("Reader succesfully unmarshalled");
    return message;
}
 
Example #6
Source File: AbstractXMLObjectMarshaller.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/** {@inheritDoc} */
public Element marshall(XMLObject xmlObject) throws MarshallingException {
    try {
        Document document = Configuration.getParserPool().newDocument();
        return marshall(xmlObject, document);
    } catch (XMLParserException e) {
        throw new MarshallingException("Unable to create Document to place marshalled elements in", e);
    }
}
 
Example #7
Source File: SAMLConfigurer.java    From spring-security-saml-dsl with MIT License 5 votes vote down vote up
private StaticBasicParserPool staticBasicParserPool() {
	StaticBasicParserPool parserPool = new StaticBasicParserPool();
	try {
		parserPool.initialize();
	} catch (XMLParserException e) {
		e.printStackTrace();
	}
	return parserPool;
}
 
Example #8
Source File: Shibboleth3ConfService.java    From oxTrust with MIT License 5 votes vote down vote up
/**
 * @param stream
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 * @return GluuErrorHandler
 * @throws XMLParserException 
 */
public GluuErrorHandler validateMetadata(String metadataPath)
		throws ParserConfigurationException, SAXException, IOException, XMLParserException {
	if (samlSchema == null) {
		final List<String> validationLog = new ArrayList<String>();
		validationLog.add(GluuErrorHandler.SCHEMA_CREATING_ERROR_MESSAGE);
		validationLog.add("Failed to load SAML schema");
		return new GluuErrorHandler(false, true, validationLog);
	}
	
	try (InputStream stream = documentStoreService.readDocumentAsStream(metadataPath)) {
		return XMLValidator.validateMetadata(stream, samlSchema);
	}
}
 
Example #9
Source File: XMLHelper.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Recursively called function that ensures all the visibly used namespaces referenced by the given Element or its
 * descendants are declared if they don't appear in the list of already resolved namespaces.
 * 
 * @param domElement the Element
 * @param upperNamespaceSearchBound the "root" element of the fragment where namespaces may be rooted
 * 
 * @throws XMLParserException thrown if a namespace prefix is encountered that can't be resolved to a namespace URI
 */
private static void rootNamespaces(Element domElement, Element upperNamespaceSearchBound) throws XMLParserException {
    String namespaceURI = null;
    String namespacePrefix = domElement.getPrefix();

    // Check if the namespace for this element is already declared on this element
    boolean nsDeclaredOnElement = false;
    if (namespacePrefix == null) {
        nsDeclaredOnElement = domElement.hasAttributeNS(null, XMLConstants.XMLNS_PREFIX);
    } else {
        nsDeclaredOnElement = domElement.hasAttributeNS(XMLConstants.XMLNS_NS, namespacePrefix);
    }

    if (!nsDeclaredOnElement) {
        // Namspace for element was not declared on the element itself, see if the namespace is declared on
        // an ancestral element within the subtree where namespaces much be rooted
        namespaceURI = lookupNamespaceURI(domElement, upperNamespaceSearchBound, namespacePrefix);

        if (namespaceURI == null) {
            // Namespace for the element is not declared on any ancestral nodes within the subtree where namespaces
            // must be rooted. Resolve the namespace from ancestors outside that subtree.
            namespaceURI = lookupNamespaceURI(upperNamespaceSearchBound, null, namespacePrefix);
            if (namespaceURI != null) {
                // Namespace resolved outside the subtree where namespaces must be declared so declare the namespace
                // on this element (within the subtree).
                appendNamespaceDeclaration(domElement, namespaceURI, namespacePrefix);
            } else {
                // Namespace couldn't be resolved from any ancestor. If the namespace prefix is null then the
                // element is simply in the undeclared default document namespace, which is fine. If it isn't null
                // then a namespace prefix, that hasn't properly been declared, is being used.
                if (namespacePrefix != null) {
                    throw new XMLParserException("Unable to resolve namespace prefix " + namespacePrefix
                            + " found on element " + getNodeQName(domElement));
                }
            }
        }
    }

    // Make sure all the attribute URIs are rooted here or have been rooted in an ancestor
    NamedNodeMap attributes = domElement.getAttributes();
    Node attributeNode;
    for (int i = 0; i < attributes.getLength(); i++) {
        namespacePrefix = null;
        namespaceURI = null;
        attributeNode = attributes.item(i);

        // Shouldn't need this check, but just to be safe, we have it
        if (attributeNode.getNodeType() != Node.ATTRIBUTE_NODE) {
            continue;
        }

        namespacePrefix = attributeNode.getPrefix();
        if (!DatatypeHelper.isEmpty(namespacePrefix)) {
            // If it's the "xmlns" prefix then it is the namespace declaration,
            // don't try to look it up and redeclare it
            if (namespacePrefix.equals(XMLConstants.XMLNS_PREFIX)
                    || namespacePrefix.equals(XMLConstants.XML_PREFIX)) {
                continue;
            }

            // check to see if the namespace for the prefix has already been defined within the XML fragment
            namespaceURI = lookupNamespaceURI(domElement, upperNamespaceSearchBound, namespacePrefix);
            if (namespaceURI == null) {
                namespaceURI = lookupNamespaceURI(upperNamespaceSearchBound, null, namespacePrefix);
                if (namespaceURI == null) {
                    throw new XMLParserException("Unable to resolve namespace prefix " + namespacePrefix
                            + " found on attribute " + getNodeQName(attributeNode) + " found on element "
                            + getNodeQName(domElement));
                }

                appendNamespaceDeclaration(domElement, namespaceURI, namespacePrefix);
            }
        }
    }

    // Now for the child elements, we pass a copy of the resolved namespace list in order to
    // maintain proper scoping of namespaces.
    Element childNode = getFirstChildElement(domElement);
    while (childNode != null) {
        rootNamespaces(childNode, upperNamespaceSearchBound);
        childNode = getNextSiblingElement(childNode);
    }
}
 
Example #10
Source File: DefaultBootstrap.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Initializes the default global parser pool instance.
 * 
 * <p>
 * The ParserPool configured by default here is an instance of
 * {@link StaticBasicParserPool}, with a maxPoolSize property of 50 
 * and all other properties with default values.
 * </p>
 * 
 * <p>
 * If a deployment wishes to use a different parser pool implementation,
 * or one configured with different characteristics, they may either override this method,
 * or simply configure a different ParserPool after bootstrapping via 
 * {@link Configuration#setParserPool(org.opensaml.xml.parse.ParserPool)}.
 * </p>
 * 
 * @throws ConfigurationException thrown if there is a problem initializing the parser pool
 */
protected static void initializeParserPool() throws ConfigurationException {
    StaticBasicParserPool pp = new StaticBasicParserPool();
    pp.setMaxPoolSize(50);
    try {
        pp.initialize();
    } catch (XMLParserException e) {
        throw new ConfigurationException("Error initializing parser pool", e);
    }
    Configuration.setParserPool(pp);
}
 
Example #11
Source File: XMLHelper.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Ensures that all the visibly used namespaces referenced by the given Element or its descendants are declared by
 * the given Element or one of its descendants.
 * 
 * <strong>NOTE:</strong> This is a very costly operation.
 * 
 * @param domElement the element to act as the root of the namespace declarations
 * 
 * @throws XMLParserException thrown if a namespace prefix is encountered that can't be resolved to a namespace URI
 */
public static void rootNamespaces(Element domElement) throws XMLParserException {
    rootNamespaces(domElement, domElement);
}