Search in sources :

Example 36 with XPathUtils

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

the class DocLiteralInInterceptorTest method testUnmarshalSourceDataWrapped.

@Test
public void testUnmarshalSourceDataWrapped() throws Exception {
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(getClass().getResourceAsStream("resources/docLitWrappedReq.xml"));
    assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag());
    XMLStreamReader filteredReader = new PartialXMLStreamReader(reader, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"));
    // advance the xml reader to the message parts
    StaxUtils.read(filteredReader);
    assertEquals(XMLStreamConstants.START_ELEMENT, reader.nextTag());
    Message m = new MessageImpl();
    // request to keep the document as wrapped
    m.put(DocLiteralInInterceptor.KEEP_PARAMETERS_WRAPPER, true);
    Exchange exchange = new ExchangeImpl();
    Service service = control.createMock(Service.class);
    exchange.put(Service.class, service);
    EasyMock.expect(service.getDataBinding()).andReturn(new SourceDataBinding()).anyTimes();
    EasyMock.expect(service.size()).andReturn(0).anyTimes();
    EasyMock.expect(service.isEmpty()).andReturn(true).anyTimes();
    Endpoint endpoint = control.createMock(Endpoint.class);
    exchange.put(Endpoint.class, endpoint);
    // wrapped
    OperationInfo operationInfo = new OperationInfo();
    MessageInfo messageInfo = new MessageInfo(operationInfo, Type.INPUT, new QName(NS, "foo"));
    messageInfo.addMessagePart(new MessagePartInfo(new QName(NS, "personId"), null));
    messageInfo.addMessagePart(new MessagePartInfo(new QName(NS, "ssn"), null));
    messageInfo.getMessagePart(0).setConcreteName(new QName(NS, "personId"));
    messageInfo.getMessagePart(1).setConcreteName(new QName(NS, "ssn"));
    operationInfo.setInput("inputName", messageInfo);
    // wrapper
    OperationInfo operationInfoWrapper = new OperationInfo();
    MessageInfo messageInfoWrapper = new MessageInfo(operationInfo, Type.INPUT, new QName(NS, "foo"));
    messageInfoWrapper.addMessagePart(new MessagePartInfo(new QName(NS, "GetPerson"), null));
    messageInfoWrapper.getMessagePart(0).setConcreteName(new QName(NS, "GetPerson"));
    operationInfoWrapper.setInput("inputName", messageInfoWrapper);
    operationInfoWrapper.setUnwrappedOperation(operationInfo);
    ServiceInfo serviceInfo = control.createMock(ServiceInfo.class);
    EasyMock.expect(serviceInfo.getName()).andReturn(new QName("http://foo.com", "service")).anyTimes();
    InterfaceInfo interfaceInfo = control.createMock(InterfaceInfo.class);
    EasyMock.expect(serviceInfo.getInterface()).andReturn(interfaceInfo).anyTimes();
    EasyMock.expect(interfaceInfo.getName()).andReturn(new QName("http://foo.com", "interface")).anyTimes();
    BindingInfo bindingInfo = new BindingInfo(serviceInfo, "");
    BindingOperationInfo boi = new BindingOperationInfo(bindingInfo, operationInfoWrapper);
    exchange.put(BindingOperationInfo.class, boi);
    EndpointInfo endpointInfo = control.createMock(EndpointInfo.class);
    BindingInfo binding = control.createMock(BindingInfo.class);
    EasyMock.expect(endpoint.getEndpointInfo()).andReturn(endpointInfo).anyTimes();
    EasyMock.expect(endpointInfo.getBinding()).andReturn(binding).anyTimes();
    EasyMock.expect(binding.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(endpointInfo.getProperties()).andReturn(new HashMap<String, Object>()).anyTimes();
    EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
    EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
    EasyMock.expect(endpointInfo.getService()).andReturn(serviceInfo).anyTimes();
    EasyMock.expect(endpointInfo.getName()).andReturn(new QName("http://foo.com", "endpoint")).anyTimes();
    EasyMock.expect(endpointInfo.getProperty("URI", URI.class)).andReturn(new URI("dummy")).anyTimes();
    List<OperationInfo> operations = new ArrayList<>();
    EasyMock.expect(interfaceInfo.getOperations()).andReturn(operations).anyTimes();
    m.setExchange(exchange);
    m.put(Message.SCHEMA_VALIDATION_ENABLED, false);
    m.setContent(XMLStreamReader.class, reader);
    control.replay();
    new DocLiteralInInterceptor().handleMessage(m);
    MessageContentsList params = (MessageContentsList) m.getContent(List.class);
    // we expect a wrapped document
    assertEquals(1, params.size());
    Map<String, String> ns = new HashMap<>();
    ns.put("ns", NS);
    XPathUtils xu = new XPathUtils(ns);
    assertEquals("hello", xu.getValueString("//ns:GetPerson/ns:personId", ((DOMSource) params.get(0)).getNode().getFirstChild()));
    assertEquals("1234", xu.getValueString("//ns:GetPerson/ns:ssn", ((DOMSource) params.get(0)).getNode().getFirstChild()));
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SourceDataBinding(org.apache.cxf.databinding.source.SourceDataBinding) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) URI(java.net.URI) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) BindingInfo(org.apache.cxf.service.model.BindingInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) ArrayList(java.util.ArrayList) List(java.util.List) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) MessageInfo(org.apache.cxf.service.model.MessageInfo) Exchange(org.apache.cxf.message.Exchange) XPathUtils(org.apache.cxf.helpers.XPathUtils) InterfaceInfo(org.apache.cxf.service.model.InterfaceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 37 with XPathUtils

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

the class JAXRSClientServerResourceCreatedSpringProviderTest method testPetStoreWadl.

@Test
public void testPetStoreWadl() throws Exception {
    List<Element> resourceEls = getWadlResourcesInfo("http://localhost:" + PORT + "/webapp/pets", "http://localhost:" + PORT + "/webapp/pets/", 1);
    checkPetStoreInfo(resourceEls.get(0));
    Element el = (Element) resourceEls.get(0).getParentNode().getParentNode();
    Map<String, String> namespaces = new HashMap<>();
    namespaces.put("xsd", "http://www.w3.org/2001/XMLSchema");
    namespaces.put("ns", "http://pets");
    namespaces.put("wadl", "http://wadl.dev.java.net/2009/02");
    XPathUtils xpu = new XPathUtils(namespaces);
    assertValidType(xpu, "//xsd:element[@name='elstatus']/@type", "petStoreStatusElement", el);
    assertValidType(xpu, "//xsd:schema[@targetNamespace='http://pets']/xsd:element[@name='status']/@type", "status", el);
    assertValidType(xpu, "//xsd:element[@name='statusType']/@type", "statusType", el);
    assertValidType(xpu, "//xsd:element[@name='statusImpl1']/@type", "petStoreStatusImpl1", el);
    assertValidType(xpu, "//xsd:element[@name='statusImpl1']/@substitutionGroup", "statusType", el);
    assertValidType(xpu, "//xsd:element[@name='statusImpl2']/@type", "petStoreStatusImpl2", el);
    assertValidType(xpu, "//xsd:element[@name='statusImpl2']/@substitutionGroup", "statusType", el);
    assertValid(xpu, "//wadl:representation[@element='prefix1:status']", el);
    assertValid(xpu, "//wadl:representation[@element='prefix1:elstatus']", el);
    assertValid(xpu, "//wadl:representation[@element='prefix1:statuses']", el);
    assertValid(xpu, "//wadl:representation[@element='prefix1:statusType']", el);
}
Also used : HashMap(java.util.HashMap) XPathUtils(org.apache.cxf.helpers.XPathUtils) Element(org.w3c.dom.Element) Test(org.junit.Test)

Example 38 with XPathUtils

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

the class X509TokenTest method testAsymmetricIssuerSerialDispatchMessage.

@org.junit.Test
public void testAsymmetricIssuerSerialDispatchMessage() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = X509TokenTest.class.getResource("client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    URL wsdl = X509TokenTest.class.getResource("DoubleItX509.wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItAsymmetricIssuerSerialOperationPort");
    Dispatch<SOAPMessage> disp = service.createDispatch(portQName, SOAPMessage.class, Mode.MESSAGE);
    updateAddressPort(disp, test.getPort());
    if (test.isStreaming()) {
        SecurityTestUtil.enableStreaming(disp);
    }
    Document xmlDocument = DOMUtils.newDocument();
    final String ns = "http://www.example.org/schema/DoubleIt";
    Element requestElement = xmlDocument.createElementNS(ns, "tns:DoubleIt");
    requestElement.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:tns", ns);
    Element dataElement = xmlDocument.createElementNS(null, "numberToDouble");
    dataElement.appendChild(xmlDocument.createTextNode("25"));
    requestElement.appendChild(dataElement);
    xmlDocument.appendChild(requestElement);
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage request = factory.createMessage();
    request.getSOAPBody().appendChild(request.getSOAPPart().adoptNode(requestElement));
    // We need to set the wsdl operation name here, or otherwise the policy layer won't pick
    // up the security policy attached at the operation level
    // this can be done in one of three ways:
    // 1) set the WSDL_OPERATION context property
    // QName wsdlOperationQName = new QName(NAMESPACE, "DoubleIt");
    // disp.getRequestContext().put(MessageContext.WSDL_OPERATION, wsdlOperationQName);
    // 2) Set the "find.dispatch.operation" to TRUE to have  CXF explicitly try and determine it from the payload
    disp.getRequestContext().put("find.dispatch.operation", Boolean.TRUE);
    // 3) Turn on WS-Addressing as that will force #2
    // TODO - add code for this, really is adding WS-Addressing feature to the createDispatch call above
    SOAPMessage resp = disp.invoke(request);
    Node nd = resp.getSOAPBody().getFirstChild();
    XPathUtils xp = new XPathUtils(Collections.singletonMap("ns2", ns));
    Object o = xp.getValue("//ns2:DoubleItResponse/doubledNumber", DOMUtils.getDomElement(nd), XPathConstants.STRING);
    assertEquals(StaxUtils.toString(nd), "50", o);
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) MessageFactory(javax.xml.soap.MessageFactory) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Service(javax.xml.ws.Service) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) XPathUtils(org.apache.cxf.helpers.XPathUtils)

Example 39 with XPathUtils

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

the class AbstractWSATestBase method assertLogNotContains.

protected void assertLogNotContains(String log, String xpath) throws XMLStreamException {
    String s = log.substring(log.indexOf("Payload: ") + 9);
    Document doc = StaxUtils.read(new StringReader(s));
    Map<String, String> ns = new HashMap<>();
    ns.put("wsa", "http://www.w3.org/2005/08/addressing");
    ns.put("soap", "http://schemas.xmlsoap.org/soap/envelope/");
    XPathUtils xpathu = new XPathUtils(ns);
    assertNull(xpathu.getValueNode(xpath, doc.getDocumentElement()));
}
Also used : HashMap(java.util.HashMap) XPathUtils(org.apache.cxf.helpers.XPathUtils) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

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