Search in sources :

Example 16 with XPathUtils

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

the class GenericExceptionTest method testGenericException.

@Test
public void testGenericException() throws Exception {
    String address = "http://localhost:" + PORT + "/generic";
    URL wsdlURL = new URL(address + "?wsdl");
    // check wsdl element
    InputStream ins = wsdlURL.openStream();
    Document doc = StaxUtils.read(ins);
    Map<String, String> ns = new HashMap<>();
    ns.put("xsd", "http://www.w3.org/2001/XMLSchema");
    ns.put("wsdl", "http://schemas.xmlsoap.org/wsdl/");
    ns.put("tns", "http://cxf.apache.org/test/HelloService");
    XPathUtils xpu = new XPathUtils(ns);
    Node nd = xpu.getValueNode("//xsd:complexType[@name='objectWithGenerics']", doc);
    assertNotNull(nd);
    assertNotNull(xpu.getValueNode("//xsd:element[@name='a']", nd));
    assertNotNull(xpu.getValueNode("//xsd:element[@name='b']", nd));
    Service service = Service.create(wsdlURL, serviceName);
    service.addPort(new QName("http://cxf.apache.org/test/HelloService", "HelloPort"), SOAPBinding.SOAP11HTTP_BINDING, address);
    GenericsEcho port = service.getPort(new QName("http://cxf.apache.org/test/HelloService", "HelloPort"), GenericsEcho.class);
    try {
        port.echo("test");
        fail("Exception is expected");
    } catch (GenericsException e) {
        ObjectWithGenerics<Boolean, Integer> genericObj = e.getObj();
        assertTrue(genericObj.getA());
        assertEquals(100, genericObj.getB().intValue());
    }
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) Service(javax.xml.ws.Service) Document(org.w3c.dom.Document) URL(java.net.URL) XPathUtils(org.apache.cxf.helpers.XPathUtils) Test(org.junit.Test)

Example 17 with XPathUtils

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

the class AbstractWSATestBase method getLogValue.

protected String getLogValue(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);
    return xpathu.getValueString(xpath, doc.getDocumentElement());
}
Also used : HashMap(java.util.HashMap) XPathUtils(org.apache.cxf.helpers.XPathUtils) StringReader(java.io.StringReader) Document(org.w3c.dom.Document)

Example 18 with XPathUtils

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

the class X509TokenTest method testAsymmetricIssuerSerialDispatch.

@org.junit.Test
public void testAsymmetricIssuerSerialDispatch() 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<Source> disp = service.createDispatch(portQName, Source.class, Mode.PAYLOAD);
    updateAddressPort(disp, test.getPort());
    if (test.isStreaming()) {
        SecurityTestUtil.enableStreaming(disp);
    }
    // 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
    QName wsdlOperationQName = new QName(NAMESPACE, "DoubleIt");
    disp.getRequestContext().put(MessageContext.WSDL_OPERATION, wsdlOperationQName);
    String req = "<ns2:DoubleIt xmlns:ns2=\"http://www.example.org/schema/DoubleIt\">" + "<numberToDouble>25</numberToDouble></ns2:DoubleIt>";
    Source source = new StreamSource(new StringReader(req));
    source = disp.invoke(source);
    Node nd = StaxUtils.read(source);
    if (nd instanceof Document) {
        nd = ((Document) nd).getDocumentElement();
    }
    XPathUtils xp = new XPathUtils(Collections.singletonMap("ns2", "http://www.example.org/schema/DoubleIt"));
    Object o = xp.getValue("//ns2:DoubleItResponse/doubledNumber", nd, XPathConstants.STRING);
    assertEquals(StaxUtils.toString(nd), "50", o);
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) Service(javax.xml.ws.Service) Document(org.w3c.dom.Document) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) XPathUtils(org.apache.cxf.helpers.XPathUtils) StringReader(java.io.StringReader)

Example 19 with XPathUtils

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

the class Soap12ClientServerTest method testSayHiSoap12ToSoap11.

@Test
public void testSayHiSoap12ToSoap11() throws Exception {
    HttpURLConnection httpConnection = getHttpConnection("http://localhost:" + PORT + "/SoapContext/Soap11Port/sayHi");
    httpConnection.setDoOutput(true);
    InputStream reqin = Soap12ClientServerTest.class.getResourceAsStream("sayHiSOAP12Req.xml");
    assertNotNull("could not load test data", reqin);
    httpConnection.setRequestMethod("POST");
    httpConnection.addRequestProperty("Content-Type", "text/xml;charset=utf-8");
    OutputStream reqout = httpConnection.getOutputStream();
    IOUtils.copy(reqin, reqout);
    reqout.close();
    assertEquals(500, httpConnection.getResponseCode());
    InputStream respin = httpConnection.getErrorStream();
    assertNotNull(respin);
    // we expect a soap 1.1 fault from the soap 1.1 test service that does not support soap 1.2
    assertEquals("text/xml;charset=utf-8", stripSpaces(httpConnection.getContentType().toLowerCase()));
    Document doc = StaxUtils.read(respin);
    assertNotNull(doc);
    Map<String, String> ns = new HashMap<>();
    ns.put("soap11", Soap11.SOAP_NAMESPACE);
    XPathUtils xu = new XPathUtils(ns);
    Node fault = (Node) xu.getValue("/soap11:Envelope/soap11:Body/soap11:Fault", doc, XPathConstants.NODE);
    assertNotNull(fault);
    String codev = (String) xu.getValue("//faultcode/text()", fault, XPathConstants.STRING);
    assertNotNull(codev);
    assertTrue("VersionMismatch expected", codev.endsWith("VersionMismatch"));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HashMap(java.util.HashMap) InputStream(java.io.InputStream) XPathUtils(org.apache.cxf.helpers.XPathUtils) OutputStream(java.io.OutputStream) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 20 with XPathUtils

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

the class CxfMtomProducerPayloadModeTest method testProducer.

@SuppressWarnings("unchecked")
@Test
public void testProducer() throws Exception {
    if (MtomTestHelper.isAwtHeadless(logger, null)) {
        return;
    }
    // START SNIPPET: producer
    Exchange exchange = context.createProducerTemplate().send("direct:testEndpoint", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            List<Source> elements = new ArrayList<Source>();
            elements.add(new DOMSource(StaxUtils.read(new StringReader(MtomTestHelper.REQ_MESSAGE)).getDocumentElement()));
            CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null);
            exchange.getIn().setBody(body);
            exchange.getIn().addAttachment(MtomTestHelper.REQ_PHOTO_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.REQ_PHOTO_DATA, "application/octet-stream")));
            exchange.getIn().addAttachment(MtomTestHelper.REQ_IMAGE_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.requestJpeg, "image/jpeg")));
        }
    });
    // process response 
    CxfPayload<SoapHeader> out = exchange.getOut().getBody(CxfPayload.class);
    Assert.assertEquals(1, out.getBody().size());
    Map<String, String> ns = new HashMap<String, String>();
    ns.put("ns", MtomTestHelper.SERVICE_TYPES_NS);
    ns.put("xop", MtomTestHelper.XOP_NS);
    XPathUtils xu = new XPathUtils(ns);
    Element oute = new XmlConverter().toDOMElement(out.getBody().get(0));
    Element ele = (Element) xu.getValue("//ns:DetailResponse/ns:photo/xop:Include", oute, XPathConstants.NODE);
    // skip "cid:"
    String photoId = ele.getAttribute("href").substring(4);
    ele = (Element) xu.getValue("//ns:DetailResponse/ns:image/xop:Include", oute, XPathConstants.NODE);
    // skip "cid:"
    String imageId = ele.getAttribute("href").substring(4);
    DataHandler dr = exchange.getOut().getAttachment(decodingReference(photoId));
    Assert.assertEquals("application/octet-stream", dr.getContentType());
    MtomTestHelper.assertEquals(MtomTestHelper.RESP_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));
    dr = exchange.getOut().getAttachment(decodingReference(imageId));
    Assert.assertEquals("image/jpeg", dr.getContentType());
    BufferedImage image = ImageIO.read(dr.getInputStream());
    Assert.assertEquals(560, image.getWidth());
    Assert.assertEquals(300, image.getHeight());
// END SNIPPET: producer
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Processor(org.apache.camel.Processor) CxfPayload(org.apache.camel.component.cxf.CxfPayload) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) DataHandler(javax.activation.DataHandler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DOMSource(javax.xml.transform.dom.DOMSource) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Source(javax.xml.transform.Source) BufferedImage(java.awt.image.BufferedImage) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) Exchange(org.apache.camel.Exchange) XPathUtils(org.apache.cxf.helpers.XPathUtils) StringReader(java.io.StringReader) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Test(org.junit.Test)

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