Search in sources :

Example 21 with XPathUtils

use of org.apache.cxf.helpers.XPathUtils in project cxf by apache.

the class PolicyAnnotationTest method testAnnotationsInterfaceAsClass.

@org.junit.Test
public void testAnnotationsInterfaceAsClass() throws Exception {
    Bus bus = BusFactory.getDefaultBus();
    JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
    factory.setBus(bus);
    factory.setServiceBean(new TestImpl());
    factory.setServiceClass(TestInterface.class);
    factory.setStart(false);
    List<String> tp = Arrays.asList("http://schemas.xmlsoap.org/soap/http", "http://schemas.xmlsoap.org/wsdl/http/", "http://schemas.xmlsoap.org/wsdl/soap/http", "http://www.w3.org/2003/05/soap/bindings/HTTP/", "http://cxf.apache.org/transports/http/configuration", "http://cxf.apache.org/bindings/xformat");
    LocalTransportFactory f = new LocalTransportFactory();
    f.getUriPrefixes().add("http");
    f.setTransportIds(tp);
    Server s = factory.create();
    try {
        ServiceWSDLBuilder builder = new ServiceWSDLBuilder(bus, s.getEndpoint().getService().getServiceInfos());
        Definition def = builder.build();
        WSDLWriter wsdlWriter = bus.getExtension(WSDLManager.class).getWSDLFactory().newWSDLWriter();
        def.setExtensionRegistry(bus.getExtension(WSDLManager.class).getExtensionRegistry());
        Element wsdl = wsdlWriter.getDocument(def).getDocumentElement();
        Map<String, String> ns = new HashMap<>();
        ns.put("wsdl", WSDLConstants.NS_WSDL11);
        ns.put("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
        ns.put("wsp", Constants.URI_POLICY_13_NS);
        XPathUtils xpu = new XPathUtils(ns);
        // org.apache.cxf.helpers.XMLUtils.printDOM(wsdl);
        check(xpu, wsdl, "/wsdl:definitions/wsdl:service/wsdl:port", "TestInterfacePortPortPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:portType/", "TestInterfacePortTypePolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:portType/wsdl:operation/", "echoIntPortTypeOpPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:portType/wsdl:operation/wsdl:input", "echoIntPortTypeOpInputPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:portType/wsdl:operation/wsdl:output", "echoIntPortTypeOpOutputPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:binding/", "TestInterfaceServiceSoapBindingBindingPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:binding/wsdl:operation/", "echoIntBindingOpPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:binding/wsdl:operation/wsdl:input", "echoIntBindingOpInputPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:binding/wsdl:operation/wsdl:output", "echoIntBindingOpOutputPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:service/", "TestInterfaceServiceServicePolicy");
        assertEquals(1, xpu.getValueList("/wsdl:definitions/wsdl:binding/wsdl:operation/" + "wsp:PolicyReference[@URI='#echoIntBindingOpPolicy']", wsdl).getLength());
    } finally {
        bus.shutdown(true);
    }
}
Also used : Bus(org.apache.cxf.Bus) Server(org.apache.cxf.endpoint.Server) HashMap(java.util.HashMap) LocalTransportFactory(org.apache.cxf.transport.local.LocalTransportFactory) Element(org.w3c.dom.Element) Definition(javax.wsdl.Definition) WSDLWriter(javax.wsdl.xml.WSDLWriter) ServiceWSDLBuilder(org.apache.cxf.wsdl11.ServiceWSDLBuilder) XPathUtils(org.apache.cxf.helpers.XPathUtils) JaxWsServerFactoryBean(org.apache.cxf.jaxws.JaxWsServerFactoryBean)

Example 22 with XPathUtils

use of org.apache.cxf.helpers.XPathUtils in project cxf by apache.

the class PolicyAnnotationTest method testAnnotations.

@org.junit.Test
public void testAnnotations() throws Exception {
    Bus bus = BusFactory.getDefaultBus();
    JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
    factory.setBus(bus);
    factory.setServiceBean(new TestImpl());
    factory.setStart(false);
    List<String> tp = Arrays.asList("http://schemas.xmlsoap.org/soap/http", "http://schemas.xmlsoap.org/wsdl/http/", "http://schemas.xmlsoap.org/wsdl/soap/http", "http://www.w3.org/2003/05/soap/bindings/HTTP/", "http://cxf.apache.org/transports/http/configuration", "http://cxf.apache.org/bindings/xformat");
    LocalTransportFactory f = new LocalTransportFactory();
    f.getUriPrefixes().add("http");
    f.setTransportIds(tp);
    Server s = factory.create();
    try {
        ServiceWSDLBuilder builder = new ServiceWSDLBuilder(bus, s.getEndpoint().getService().getServiceInfos());
        Definition def = builder.build();
        WSDLWriter wsdlWriter = bus.getExtension(WSDLManager.class).getWSDLFactory().newWSDLWriter();
        def.setExtensionRegistry(bus.getExtension(WSDLManager.class).getExtensionRegistry());
        Element wsdl = wsdlWriter.getDocument(def).getDocumentElement();
        Map<String, String> ns = new HashMap<>();
        ns.put("wsdl", WSDLConstants.NS_WSDL11);
        ns.put("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
        ns.put("wsp", Constants.URI_POLICY_13_NS);
        XPathUtils xpu = new XPathUtils(ns);
        // org.apache.cxf.helpers.XMLUtils.printDOM(wsdl);
        check(xpu, wsdl, "/wsdl:definitions/wsdl:service/wsdl:port", "TestImplPortPortPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:portType/", "TestInterfacePortTypePolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:portType/wsdl:operation/", "echoIntPortTypeOpPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:portType/wsdl:operation/wsdl:input", "echoIntPortTypeOpInputPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:portType/wsdl:operation/wsdl:output", "echoIntPortTypeOpOutputPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:binding/", "TestImplServiceSoapBindingBindingPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:binding/wsdl:operation/", "echoIntBindingOpPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:binding/wsdl:operation/wsdl:input", "echoIntBindingOpInputPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:binding/wsdl:operation/wsdl:output", "echoIntBindingOpOutputPolicy");
        check(xpu, wsdl, "/wsdl:definitions/wsdl:service/", "TestImplServiceServicePolicy");
        assertEquals(1, xpu.getValueList("/wsdl:definitions/wsdl:binding/wsdl:operation/" + "wsp:PolicyReference[@URI='#echoIntBindingOpPolicy']", wsdl).getLength());
        EndpointPolicy policy = bus.getExtension(PolicyEngine.class).getServerEndpointPolicy(s.getEndpoint().getEndpointInfo(), null, null);
        assertNotNull(policy);
        assertEquals(1, policy.getChosenAlternative().size());
    } finally {
        bus.shutdown(true);
    }
}
Also used : Bus(org.apache.cxf.Bus) Server(org.apache.cxf.endpoint.Server) HashMap(java.util.HashMap) LocalTransportFactory(org.apache.cxf.transport.local.LocalTransportFactory) Element(org.w3c.dom.Element) Definition(javax.wsdl.Definition) PolicyEngine(org.apache.cxf.ws.policy.PolicyEngine) WSDLWriter(javax.wsdl.xml.WSDLWriter) ServiceWSDLBuilder(org.apache.cxf.wsdl11.ServiceWSDLBuilder) EndpointPolicy(org.apache.cxf.ws.policy.EndpointPolicy) XPathUtils(org.apache.cxf.helpers.XPathUtils) JaxWsServerFactoryBean(org.apache.cxf.jaxws.JaxWsServerFactoryBean)

Example 23 with XPathUtils

use of org.apache.cxf.helpers.XPathUtils in project cxf by apache.

the class PolicyAnnotationTest method testAnnotationImplNoInterfacePolicies.

@org.junit.Test
public void testAnnotationImplNoInterfacePolicies() throws Exception {
    Bus bus = BusFactory.getDefaultBus();
    JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
    factory.setBus(bus);
    factory.setServiceBean(new TestImplWithPoliciesNoInterface());
    factory.setStart(false);
    List<String> tp = Arrays.asList("http://schemas.xmlsoap.org/soap/http", "http://schemas.xmlsoap.org/wsdl/http/", "http://schemas.xmlsoap.org/wsdl/soap/http", "http://www.w3.org/2003/05/soap/bindings/HTTP/", "http://cxf.apache.org/transports/http/configuration", "http://cxf.apache.org/bindings/xformat");
    LocalTransportFactory f = new LocalTransportFactory();
    f.getUriPrefixes().add("http");
    f.setTransportIds(tp);
    Server s = factory.create();
    try {
        ServiceWSDLBuilder builder = new ServiceWSDLBuilder(bus, s.getEndpoint().getService().getServiceInfos());
        Definition def = builder.build();
        WSDLWriter wsdlWriter = bus.getExtension(WSDLManager.class).getWSDLFactory().newWSDLWriter();
        def.setExtensionRegistry(bus.getExtension(WSDLManager.class).getExtensionRegistry());
        Element wsdl = wsdlWriter.getDocument(def).getDocumentElement();
        Map<String, String> ns = new HashMap<>();
        ns.put("wsdl", WSDLConstants.NS_WSDL11);
        ns.put("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
        ns.put("wsp", Constants.URI_POLICY_13_NS);
        XPathUtils xpu = new XPathUtils(ns);
        // org.apache.cxf.helpers.XMLUtils.printDOM(wsdl);
        assertEquals(1, xpu.getValueList("/wsdl:definitions/wsdl:binding/" + "wsp:PolicyReference[@URI='#TestImplWithPoliciesNoInterfaceServiceSoapBindingBindingPolicy']", wsdl).getLength());
        final EndpointPolicy policy = bus.getExtension(PolicyEngine.class).getServerEndpointPolicy(s.getEndpoint().getEndpointInfo(), null, null);
        assertNotNull(policy);
    } finally {
        bus.shutdown(true);
    }
}
Also used : Bus(org.apache.cxf.Bus) Server(org.apache.cxf.endpoint.Server) HashMap(java.util.HashMap) LocalTransportFactory(org.apache.cxf.transport.local.LocalTransportFactory) Element(org.w3c.dom.Element) Definition(javax.wsdl.Definition) PolicyEngine(org.apache.cxf.ws.policy.PolicyEngine) WSDLWriter(javax.wsdl.xml.WSDLWriter) ServiceWSDLBuilder(org.apache.cxf.wsdl11.ServiceWSDLBuilder) EndpointPolicy(org.apache.cxf.ws.policy.EndpointPolicy) XPathUtils(org.apache.cxf.helpers.XPathUtils) JaxWsServerFactoryBean(org.apache.cxf.jaxws.JaxWsServerFactoryBean)

Example 24 with XPathUtils

use of org.apache.cxf.helpers.XPathUtils in project cxf by apache.

the class Soap12FaultInInterceptor method unmarshalFault.

public static SoapFault unmarshalFault(SoapMessage message, XMLStreamReader reader) {
    String exMessage;
    QName faultCode = null;
    List<QName> subCodes = null;
    final String role;
    final String node;
    Element detail = null;
    final String lang;
    Map<String, String> ns = new HashMap<>();
    ns.put("s", Soap12.SOAP_NAMESPACE);
    XPathUtils xu = new XPathUtils(ns);
    try {
        Node mainNode = message.getContent(Node.class);
        Node fault;
        if (reader instanceof W3CDOMStreamReader) {
            W3CDOMStreamReader dr = (W3CDOMStreamReader) reader;
            fault = dr.getCurrentElement();
            dr.consumeFrame();
        } else if (mainNode != null) {
            Node bodyNode = (Node) xu.getValue("//s:Body", mainNode, XPathConstants.NODE);
            StaxUtils.readDocElements(bodyNode.getOwnerDocument(), bodyNode, new FragmentStreamReader(reader), false, false);
            fault = (Element) xu.getValue("//s:Fault", bodyNode, XPathConstants.NODE);
        } else {
            fault = StaxUtils.read(new FragmentStreamReader(reader));
        }
        fault = DOMUtils.getDomElement(fault);
        Element el = (Element) xu.getValue("//s:Fault/s:Code/s:Value", fault, XPathConstants.NODE);
        if (el != null) {
            faultCode = DOMUtils.createQName(el.getTextContent(), el);
        }
        el = (Element) xu.getValue("//s:Fault/s:Code/s:Subcode", fault, XPathConstants.NODE);
        if (el != null) {
            subCodes = new LinkedList<>();
            NodeList vlist = el.getElementsByTagNameNS(Soap12.SOAP_NAMESPACE, "Value");
            for (int i = 0; i < vlist.getLength(); i++) {
                Node v = vlist.item(i);
                subCodes.add(DOMUtils.createQName(v.getTextContent(), v));
            }
        }
        exMessage = (String) xu.getValue("//s:Fault/s:Reason/s:Text/text()", fault, XPathConstants.STRING);
        lang = (String) xu.getValue("//s:Fault/s:Reason/s:Text/@xml:lang", fault, XPathConstants.STRING);
        Node detailNode = (Node) xu.getValue("//s:Fault/s:Detail", fault, XPathConstants.NODE);
        if (detailNode != null) {
            detail = (Element) detailNode;
        }
        role = (String) xu.getValue("//s:Fault/s:Role/text()", fault, XPathConstants.STRING);
        node = (String) xu.getValue("//s:Fault/s:Node/text()", fault, XPathConstants.STRING);
    } catch (XMLStreamException e) {
        throw new SoapFault("Could not parse message.", e, message.getVersion().getSender());
    }
    // if the fault's content is invalid and fautlCode is not found, blame the receiver
    if (faultCode == null) {
        faultCode = Soap12.getInstance().getReceiver();
        exMessage = new Message("INVALID_FAULT", LOG).toString();
    }
    SoapFault fault = new SoapFault(exMessage, faultCode);
    fault.setSubCodes(subCodes);
    fault.setDetail(detail);
    fault.setRole(role);
    fault.setNode(node);
    fault.setLang(lang);
    return fault;
}
Also used : SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.common.i18n.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) XMLStreamException(javax.xml.stream.XMLStreamException) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) XPathUtils(org.apache.cxf.helpers.XPathUtils) FragmentStreamReader(org.apache.cxf.staxutils.FragmentStreamReader)

Example 25 with XPathUtils

use of org.apache.cxf.helpers.XPathUtils in project cxf by apache.

the class FragmentDialectLanguageQName method getResourceFragment.

@Override
public Object getResourceFragment(final Representation representation, ExpressionType expression) {
    String expressionStr = getXPathFromQNameExpression(expression);
    // Evaluate XPath
    XPathUtils xu = new XPathUtils(new NamespaceContext() {

        @Override
        public String getNamespaceURI(String prefix) {
            if (prefix != null && !prefix.isEmpty()) {
                Element resource = (Element) representation.getAny();
                return resource.getAttribute("xmlns:" + prefix);
            }
            return null;
        }

        @Override
        public String getPrefix(String s) {
            throw new UnsupportedOperationException();
        }

        @Override
        public Iterator<String> getPrefixes(String s) {
            throw new UnsupportedOperationException();
        }
    });
    Node resource = (Node) representation.getAny();
    if (resource == null) {
        // Returns empty NodeList
        return new NodeList() {

            @Override
            public Node item(int i) {
                return null;
            }

            @Override
            public int getLength() {
                return 0;
            }
        };
    }
    return xu.getValueList(expressionStr, resource);
}
Also used : XPathUtils(org.apache.cxf.helpers.XPathUtils) NamespaceContext(javax.xml.namespace.NamespaceContext) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Iterator(java.util.Iterator)

Aggregations

XPathUtils (org.apache.cxf.helpers.XPathUtils)39 HashMap (java.util.HashMap)33 Document (org.w3c.dom.Document)26 Test (org.junit.Test)24 Element (org.w3c.dom.Element)22 Node (org.w3c.dom.Node)15 File (java.io.File)11 Bus (org.apache.cxf.Bus)9 Definition (javax.wsdl.Definition)7 QName (javax.xml.namespace.QName)7 StringReader (java.io.StringReader)6 WSDLWriter (javax.wsdl.xml.WSDLWriter)6 Server (org.apache.cxf.endpoint.Server)6 ServiceWSDLBuilder (org.apache.cxf.wsdl11.ServiceWSDLBuilder)6 URL (java.net.URL)5 InputStream (java.io.InputStream)4 Source (javax.xml.transform.Source)4 JaxWsServerFactoryBean (org.apache.cxf.jaxws.JaxWsServerFactoryBean)4 ServiceInfo (org.apache.cxf.service.model.ServiceInfo)4 LocalTransportFactory (org.apache.cxf.transport.local.LocalTransportFactory)4