Search in sources :

Example 56 with W3CDOMStreamWriter

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

the class ElementDataWriter method write.

public void write(Object obj, MessagePartInfo part, Element output) {
    AegisType type = databinding.getType(part);
    if (type == null) {
        throw new Fault(new Message("NO_MESSAGE_FOR_PART", LOG));
    }
    Context context = new Context(databinding.getAegisContext());
    context.setAttachments(attachments);
    type = TypeUtil.getWriteType(databinding.getAegisContext(), obj, type);
    try {
        W3CDOMStreamWriter domWriter = new W3CDOMStreamWriter(output);
        ElementWriter writer = new ElementWriter(domWriter);
        MessageWriter w2 = writer.getElementWriter(part.getConcreteName());
        if (type.isNillable() && type.isWriteOuter() && obj == null) {
            w2.writeXsiNil();
            w2.close();
            return;
        }
        type.writeObject(obj, w2, context);
        w2.close();
    } catch (DatabindingException e) {
        throw new RuntimeException(e);
    }
}
Also used : Context(org.apache.cxf.aegis.Context) DatabindingException(org.apache.cxf.aegis.DatabindingException) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) Message(org.apache.cxf.common.i18n.Message) AegisType(org.apache.cxf.aegis.type.AegisType) MessageWriter(org.apache.cxf.aegis.xml.MessageWriter) Fault(org.apache.cxf.interceptor.Fault) ElementWriter(org.apache.cxf.aegis.xml.stax.ElementWriter)

Example 57 with W3CDOMStreamWriter

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

the class JaxWsEndpointImpl method buildWsdlExtensibilities.

private void buildWsdlExtensibilities(BindingInfo bindingInfo) {
    Addressing addressing = getAddressing();
    if (addressing != null) {
        ExtensionRegistry extensionRegistry = getBus().getExtension(WSDLManager.class).getExtensionRegistry();
        try {
            ExtensibilityElement el = extensionRegistry.createExtension(javax.wsdl.Binding.class, JAXWSAConstants.WSAW_USINGADDRESSING_QNAME);
            el.setRequired(addressing.required());
            bindingInfo.addExtensor(el);
            StringBuilder polRefId = new StringBuilder(bindingInfo.getName().getLocalPart());
            polRefId.append("_WSAM_Addressing_Policy");
            UnknownExtensibilityElement uel = new UnknownExtensibilityElement();
            W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
            writer.writeStartElement("wsp", "PolicyReference", URI_POLICY_NS);
            writer.writeAttribute("URI", "#" + polRefId.toString());
            writer.writeEndElement();
            Element pr = writer.getDocument().getDocumentElement();
            uel.setElement(pr);
            uel.setElementType(DOMUtils.getElementQName(pr));
            bindingInfo.addExtensor(uel);
            writer = new W3CDOMStreamWriter();
            writer.writeStartElement("wsp", "Policy", URI_POLICY_NS);
            writer.writeAttribute("wsu", URI_WSU_NS, "Id", polRefId.toString());
            writer.writeStartElement("wsam", "Addressing", JAXWSAConstants.NS_WSAM);
            if (!addressing.required()) {
                writer.writeAttribute("wsp", URI_POLICY_NS, "Optional", "true");
            }
            writer.writeStartElement("wsp", "Policy", URI_POLICY_NS);
            String s = getAddressingRequirement(addressing);
            if (s != null) {
                writer.writeEmptyElement("wsam", s, JAXWSAConstants.NS_WSAM);
            }
            writer.writeEndElement();
            writer.writeEndElement();
            writer.writeEndElement();
            pr = writer.getDocument().getDocumentElement();
            uel = new UnknownExtensibilityElement();
            uel.setElement(pr);
            uel.setElementType(DOMUtils.getElementQName(pr));
            if (bindingInfo.getService().getDescription() == null) {
                DescriptionInfo description = new DescriptionInfo();
                description.setName(bindingInfo.getService().getName());
                bindingInfo.getService().setDescription(description);
            }
            bindingInfo.getService().getDescription().addExtensor(uel);
        } catch (Exception e) {
            // ignore
            e.printStackTrace();
        }
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) UnknownExtensibilityElement(javax.wsdl.extensions.UnknownExtensibilityElement) UnknownExtensibilityElement(javax.wsdl.extensions.UnknownExtensibilityElement) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Element(org.w3c.dom.Element) WSDLManager(org.apache.cxf.wsdl.WSDLManager) DescriptionInfo(org.apache.cxf.service.model.DescriptionInfo) Addressing(javax.xml.ws.soap.Addressing) UnknownExtensibilityElement(javax.wsdl.extensions.UnknownExtensibilityElement) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) WebServiceException(javax.xml.ws.WebServiceException) EndpointException(org.apache.cxf.endpoint.EndpointException) ExtensionRegistry(javax.wsdl.extensions.ExtensionRegistry)

Example 58 with W3CDOMStreamWriter

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

the class ProviderImpl method createW3CEndpointReference.

// CHECKSTYLE:OFF - spec requires a bunch of params
public W3CEndpointReference createW3CEndpointReference(String address, QName interfaceName, QName serviceName, QName portName, List<Element> metadata, String wsdlDocumentLocation, List<Element> referenceParameters, List<Element> elements, Map<QName, String> attributes) {
    // CHECKSTYLE:ON
    if (serviceName != null && portName != null && wsdlDocumentLocation != null && interfaceName == null) {
        Bus bus = BusFactory.getThreadDefaultBus();
        WSDLManager wsdlManager = bus.getExtension(WSDLManager.class);
        try {
            Definition def = wsdlManager.getDefinition(wsdlDocumentLocation);
            interfaceName = def.getService(serviceName).getPort(portName.getLocalPart()).getBinding().getPortType().getQName();
        } catch (Exception e) {
        // do nothing
        }
    }
    if (serviceName == null && portName == null && address == null) {
        throw new IllegalStateException("Address in an EPR cannot be null, " + " when serviceName or portName is null");
    }
    try {
        final W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
        writer.setPrefix(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.NS_WSA);
        writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_ERF_NAME, JAXWSAConstants.NS_WSA);
        writer.writeNamespace(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.NS_WSA);
        writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_ADDRESS_NAME, JAXWSAConstants.NS_WSA);
        address = address == null ? "" : address;
        writer.writeCharacters(address);
        writer.writeEndElement();
        if (referenceParameters != null) {
            writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_REFERENCEPARAMETERS_NAME, JAXWSAConstants.NS_WSA);
            for (Element ele : referenceParameters) {
                StaxUtils.writeElement(ele, writer, true);
            }
            writer.writeEndElement();
        }
        if (wsdlDocumentLocation != null || interfaceName != null || serviceName != null || (metadata != null && !metadata.isEmpty())) {
            writer.writeStartElement(JAXWSAConstants.WSA_PREFIX, JAXWSAConstants.WSA_METADATA_NAME, JAXWSAConstants.NS_WSA);
            writer.writeNamespace(JAXWSAConstants.WSAW_PREFIX, JAXWSAConstants.NS_WSAW);
            writer.writeNamespace(JAXWSAConstants.WSAM_PREFIX, JAXWSAConstants.NS_WSAM);
            if (wsdlDocumentLocation != null) {
                boolean includeLocationOnly = false;
                org.apache.cxf.message.Message message = PhaseInterceptorChain.getCurrentMessage();
                if (message != null) {
                    includeLocationOnly = MessageUtils.getContextualBoolean(message, "org.apache.cxf.wsa.metadata.wsdlLocationOnly", false);
                }
                String attrubuteValue = serviceName != null && !includeLocationOnly ? serviceName.getNamespaceURI() + " " + wsdlDocumentLocation : wsdlDocumentLocation;
                writer.writeNamespace(JAXWSAConstants.WSDLI_PFX, JAXWSAConstants.NS_WSDLI);
                writer.writeAttribute(JAXWSAConstants.WSDLI_PFX, JAXWSAConstants.NS_WSDLI, JAXWSAConstants.WSDLI_WSDLLOCATION, attrubuteValue);
            }
            if (interfaceName != null) {
                writer.writeStartElement(JAXWSAConstants.WSAM_PREFIX, JAXWSAConstants.WSAM_INTERFACE_NAME, JAXWSAConstants.NS_WSAM);
                String portTypePrefix = interfaceName.getPrefix();
                if (portTypePrefix == null || portTypePrefix.isEmpty()) {
                    portTypePrefix = "ns1";
                }
                writer.writeNamespace(portTypePrefix, interfaceName.getNamespaceURI());
                writer.writeCharacters(portTypePrefix + ":" + interfaceName.getLocalPart());
                writer.writeEndElement();
            }
            if (serviceName != null) {
                String serviceNamePrefix = (serviceName.getPrefix() == null || serviceName.getPrefix().length() == 0) ? "ns2" : serviceName.getPrefix();
                writer.writeStartElement(JAXWSAConstants.WSAM_PREFIX, JAXWSAConstants.WSAM_SERVICENAME_NAME, JAXWSAConstants.NS_WSAM);
                if (portName != null) {
                    writer.writeAttribute(JAXWSAConstants.WSAM_ENDPOINT_NAME, portName.getLocalPart());
                }
                writer.writeNamespace(serviceNamePrefix, serviceName.getNamespaceURI());
                writer.writeCharacters(serviceNamePrefix + ":" + serviceName.getLocalPart());
                writer.writeEndElement();
            }
            if (wsdlDocumentLocation != null) {
                writer.writeStartElement(WSDLConstants.WSDL_PREFIX, WSDLConstants.QNAME_DEFINITIONS.getLocalPart(), WSDLConstants.NS_WSDL11);
                writer.writeNamespace(WSDLConstants.WSDL_PREFIX, WSDLConstants.NS_WSDL11);
                writer.writeStartElement(WSDLConstants.WSDL_PREFIX, WSDLConstants.QNAME_IMPORT.getLocalPart(), WSDLConstants.QNAME_IMPORT.getNamespaceURI());
                if (serviceName != null) {
                    writer.writeAttribute(WSDLConstants.ATTR_NAMESPACE, serviceName.getNamespaceURI());
                }
                writer.writeAttribute(WSDLConstants.ATTR_LOCATION, wsdlDocumentLocation);
                writer.writeEndElement();
                writer.writeEndElement();
            }
            if (metadata != null) {
                for (Element e : metadata) {
                    StaxUtils.writeElement(e, writer, true);
                }
            }
            writer.writeEndElement();
        }
        if (elements != null) {
            for (Element e : elements) {
                StaxUtils.writeElement(e, writer, true);
            }
        }
        writer.writeEndElement();
        writer.flush();
        try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction<W3CEndpointReference>() {

                public W3CEndpointReference run() throws Exception {
                    Unmarshaller unmarshaller = getJAXBContext().createUnmarshaller();
                    try {
                        return (W3CEndpointReference) unmarshaller.unmarshal(writer.getDocument());
                    } finally {
                        JAXBUtils.closeUnmarshaller(unmarshaller);
                    }
                }
            });
        } catch (PrivilegedActionException pae) {
            Exception e = pae.getException();
            if (e instanceof JAXBException) {
                throw (JAXBException) e;
            }
            throw new SecurityException(e);
        }
    } catch (Exception e) {
        throw new WebServiceException(new Message("ERROR_UNMARSHAL_ENDPOINTREFERENCE", LOG).toString(), e);
    }
}
Also used : Bus(org.apache.cxf.Bus) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) WebServiceException(javax.xml.ws.WebServiceException) Message(org.apache.cxf.common.i18n.Message) PrivilegedActionException(java.security.PrivilegedActionException) Element(org.w3c.dom.Element) JAXBException(javax.xml.bind.JAXBException) Definition(javax.wsdl.Definition) XMLStreamException(javax.xml.stream.XMLStreamException) PrivilegedActionException(java.security.PrivilegedActionException) JAXBException(javax.xml.bind.JAXBException) WebServiceException(javax.xml.ws.WebServiceException) W3CEndpointReference(javax.xml.ws.wsaddressing.W3CEndpointReference) WSDLManager(org.apache.cxf.wsdl.WSDLManager) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 59 with W3CDOMStreamWriter

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

the class STSRESTTest method testValidateJWTAndIssueSAML.

@org.junit.Test
public void testValidateJWTAndIssueSAML() throws Exception {
    WebClient client = webClient().path("jwt").accept(MediaType.TEXT_PLAIN);
    // 1. Get a token via GET
    String token = client.get(String.class);
    assertNotNull(token);
    // 2. Now validate it in the STS using POST
    client = webClient().query("action", "validate").type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);
    // Create RequestSecurityToken
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
    writer.writeStartElement("wst", "RequestSecurityToken", WST_NS_05_12);
    writer.writeStartElement("wst", "RequestType", WST_NS_05_12);
    writer.writeCharacters(WST_NS_05_12 + "/Validate");
    writer.writeEndElement();
    writer.writeStartElement("wst", "TokenType", WST_NS_05_12);
    writer.writeCharacters(SAML2_TOKEN_TYPE);
    writer.writeEndElement();
    writer.writeStartElement("wst", "ValidateTarget", WST_NS_05_12);
    writer.writeStartElement("TokenWrapper");
    writer.writeCharacters(token);
    writer.writeEndElement();
    writer.writeEndElement();
    writer.writeEndElement();
    RequestSecurityTokenResponseType securityResponse = client.post(new DOMSource(writer.getDocument().getDocumentElement()), RequestSecurityTokenResponseType.class);
    assertTrue(getValidationStatus(securityResponse));
    // Check the token
    validateSAMLSecurityTokenResponse(securityResponse, true);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 60 with W3CDOMStreamWriter

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

the class STSRESTTest method testIssueSAML2TokenViaPOST.

@org.junit.Test
public void testIssueSAML2TokenViaPOST() throws Exception {
    WebClient client = webClient().type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);
    // Create RequestSecurityToken
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
    writer.writeStartElement("wst", "RequestSecurityToken", WST_NS_05_12);
    writer.writeStartElement("wst", "RequestType", WST_NS_05_12);
    writer.writeCharacters(WST_NS_05_12 + "/Issue");
    writer.writeEndElement();
    writer.writeStartElement("wst", "TokenType", WST_NS_05_12);
    writer.writeCharacters(SAML2_TOKEN_TYPE);
    writer.writeEndElement();
    writer.writeEndElement();
    RequestSecurityTokenResponseType securityResponse = client.post(new DOMSource(writer.getDocument().getDocumentElement()), RequestSecurityTokenResponseType.class);
    validateSAMLSecurityTokenResponse(securityResponse, true);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) WebClient(org.apache.cxf.jaxrs.client.WebClient)

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