Search in sources :

Example 36 with W3CDOMStreamWriter

use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cas by apereo.

the class WSFederationMetadataWriter method produceMetadataDocument.

/**
 * Produce metadata document.
 *
 * @param config the config
 * @return the document
 * @throws Exception the exception
 */
public static Document produceMetadataDocument(final CasConfigurationProperties config) throws Exception {
    val wsfedIdp = config.getAuthn().getWsfedIdp();
    val sts = wsfedIdp.getSts();
    val prop = CryptoUtils.getSecurityProperties(sts.getRealm().getKeystoreFile(), sts.getRealm().getKeystorePassword(), sts.getRealm().getKeystoreAlias());
    val crypto = CryptoFactory.getInstance(prop);
    val writer = new W3CDOMStreamWriter();
    writer.writeStartDocument(StandardCharsets.UTF_8.name(), "1.0");
    val referenceID = IDGenerator.generateID("_");
    writer.writeStartElement("md", "EntityDescriptor", SAML2_METADATA_NS);
    writer.writeAttribute("ID", referenceID);
    val idpEntityId = config.getServer().getPrefix().concat(WSFederationConstants.ENDPOINT_FEDERATION_REQUEST);
    writer.writeAttribute("entityID", idpEntityId);
    writer.writeNamespace("md", SAML2_METADATA_NS);
    writer.writeNamespace("fed", WS_FEDERATION_NS);
    writer.writeNamespace("wsa", WS_ADDRESSING_NS);
    writer.writeNamespace("auth", WS_FEDERATION_NS);
    writer.writeNamespace("xsi", SCHEMA_INSTANCE_NS);
    val stsUrl = config.getServer().getPrefix().concat(WSFederationConstants.BASE_ENDPOINT_STS).concat(wsfedIdp.getIdp().getRealmName());
    writeFederationMetadata(writer, idpEntityId, stsUrl, crypto);
    writer.writeEndElement();
    writer.writeEndDocument();
    writer.close();
    val out = DOM2Writer.nodeToString(writer.getDocument());
    LOGGER.trace(out);
    return SignatureUtils.signMetaInfo(crypto, null, sts.getRealm().getKeyPassword(), writer.getDocument(), referenceID);
}
Also used : lombok.val(lombok.val) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter)

Example 37 with W3CDOMStreamWriter

use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.

the class MappingBeanDefinitionParser method transformElement.

private Element transformElement(Element element) {
    W3CDOMStreamWriter domWriter = new W3CDOMStreamWriter();
    OutTransformWriter transformWriter = new OutTransformWriter(domWriter, transformMap);
    try {
        StaxUtils.copy(element, transformWriter);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
    return domWriter.getDocument().getDocumentElement();
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) OutTransformWriter(org.apache.cxf.staxutils.transform.OutTransformWriter) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 38 with W3CDOMStreamWriter

use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.

the class OutTransformWriterTest method testNamespaceConversionAndDefaultNS.

@Test
public void testNamespaceConversionAndDefaultNS() throws Exception {
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
    Map<String, String> outMap = new HashMap<>();
    outMap.put("{http://testbeans.com}testBean", "{http://testbeans.com/v2}testBean");
    outMap.put("{http://testbeans.com}bean", "{http://testbeans.com/v3}bean");
    OutTransformWriter transformWriter = new OutTransformWriter(writer, outMap, Collections.<String, String>emptyMap(), Collections.<String>emptyList(), false, "http://testbeans.com/v2");
    JAXBContext context = JAXBContext.newInstance(TestBean.class);
    Marshaller m = context.createMarshaller();
    m.marshal(new TestBean(), transformWriter);
    Element el = writer.getDocument().getDocumentElement();
    assertEquals("http://testbeans.com/v2", el.getNamespaceURI());
    assertTrue(StringUtils.isEmpty(el.getPrefix()));
    el = DOMUtils.getFirstElement(el);
    assertEquals("http://testbeans.com/v3", el.getNamespaceURI());
    assertFalse(StringUtils.isEmpty(el.getPrefix()));
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) Marshaller(javax.xml.bind.Marshaller) HashMap(java.util.HashMap) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Element(org.w3c.dom.Element) XmlElement(javax.xml.bind.annotation.XmlElement) JAXBContext(javax.xml.bind.JAXBContext) Test(org.junit.Test)

Example 39 with W3CDOMStreamWriter

use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.

the class EndpointReferenceUtils method convertToXML.

public static Source convertToXML(EndpointReferenceType epr) {
    try {
        Marshaller jm = getJAXBContextForEPR().createMarshaller();
        jm.setProperty(Marshaller.JAXB_FRAGMENT, true);
        QName qname = new QName("http://www.w3.org/2005/08/addressing", "EndpointReference");
        JAXBElement<EndpointReferenceType> jaxEle = new JAXBElement<>(qname, EndpointReferenceType.class, epr);
        W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
        jm.marshal(jaxEle, writer);
        return new DOMSource(writer.getDocument());
    } catch (JAXBException e) {
    // ignore
    }
    return null;
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) Marshaller(javax.xml.bind.Marshaller) DOMSource(javax.xml.transform.dom.DOMSource) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement)

Example 40 with W3CDOMStreamWriter

use of org.apache.cxf.staxutils.W3CDOMStreamWriter in project cxf by apache.

the class TunedDocumentLoader method loadDocument.

@Override
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {
    if (validationMode == XmlBeanDefinitionReader.VALIDATION_NONE) {
        SAXParserFactory parserFactory = namespaceAware ? nsasaxParserFactory : saxParserFactory;
        SAXParser parser = parserFactory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setEntityResolver(entityResolver);
        reader.setErrorHandler(errorHandler);
        SAXSource saxSource = new SAXSource(reader, inputSource);
        W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
        StaxUtils.copy(saxSource, writer);
        return writer.getDocument();
    }
    return super.loadDocument(inputSource, entityResolver, errorHandler, validationMode, namespaceAware);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SAXSource(javax.xml.transform.sax.SAXSource) SAXParser(javax.xml.parsers.SAXParser) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

W3CDOMStreamWriter (org.apache.cxf.staxutils.W3CDOMStreamWriter)60 Element (org.w3c.dom.Element)29 DOMSource (javax.xml.transform.dom.DOMSource)24 XMLStreamException (javax.xml.stream.XMLStreamException)15 Document (org.w3c.dom.Document)14 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 Node (org.w3c.dom.Node)9 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)8 Fault (org.apache.cxf.interceptor.Fault)8 XMLStreamReader (javax.xml.stream.XMLStreamReader)7 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)7 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)7 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)7 JAXBElement (javax.xml.bind.JAXBElement)6 JAXBException (javax.xml.bind.JAXBException)5 SOAPMessage (javax.xml.soap.SOAPMessage)5 SoapFault (org.apache.cxf.binding.soap.SoapFault)5 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4