Search in sources :

Example 36 with BindingProvider

use of javax.xml.ws.BindingProvider in project cxf by apache.

the class UsernameOnBehalfOfCachingTest method testAppliesToCaching.

/**
 * Test caching the issued token when the STSClient is deployed in an intermediary
 */
@org.junit.Test
public void testAppliesToCaching() throws Exception {
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = UsernameOnBehalfOfCachingTest.class.getResource("cxf-client.xml");
    Bus bus = bf.createBus(busFile.toString());
    BusFactory.setDefaultBus(bus);
    BusFactory.setThreadDefaultBus(bus);
    URL wsdl = UsernameOnBehalfOfCachingTest.class.getResource("DoubleIt.wsdl");
    Service service = Service.create(wsdl, SERVICE_QNAME);
    QName portQName = new QName(NAMESPACE, "DoubleItOBOAsymmetricSAML2BearerPort4");
    DoubleItPortType port = service.getPort(portQName, DoubleItPortType.class);
    updateAddressPort(port, PORT);
    TokenTestUtils.updateSTSPort((BindingProvider) port, STSPORT2);
    // Disable storing tokens per-proxy
    ((BindingProvider) port).getRequestContext().put(SecurityConstants.CACHE_ISSUED_TOKEN_IN_ENDPOINT, "false");
    // Make a successful invocation
    ((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
    BindingProvider p = (BindingProvider) port;
    p.getRequestContext().put(SecurityConstants.STS_APPLIES_TO, "http://localhost:" + PORT + "/doubleit/services/doubleitasymmetricnew");
    doubleIt(port, 25);
    // Make a successful invocation
    ((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "bob");
    p.getRequestContext().put(SecurityConstants.STS_APPLIES_TO, "http://localhost:" + PORT + "/doubleit/services/doubleitasymmetricnew2");
    doubleIt(port, 25);
    // Change the STSClient so that it can no longer find the STS
    clearSTSClient(p);
    // Make a successful invocation - should work as token is cached
    ((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
    p.getRequestContext().put(SecurityConstants.STS_APPLIES_TO, "http://localhost:" + PORT + "/doubleit/services/doubleitasymmetricnew");
    doubleIt(port, 25);
    // Make a successful invocation - should work as token is cached
    ((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "bob");
    p.getRequestContext().put(SecurityConstants.STS_APPLIES_TO, "http://localhost:" + PORT + "/doubleit/services/doubleitasymmetricnew2");
    doubleIt(port, 25);
    // Change appliesTo - should fail
    ((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
    p.getRequestContext().put(SecurityConstants.STS_APPLIES_TO, "http://localhost:" + PORT + "/doubleit/services/doubleitasymmetricnew2");
    try {
        doubleIt(port, 30);
        fail("Failure expected");
    } catch (Exception ex) {
    // 
    }
    ((java.io.Closeable) port).close();
    bus.shutdown(true);
}
Also used : Bus(org.apache.cxf.Bus) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) DoubleItPortType(org.example.contract.doubleit.DoubleItPortType) BindingProvider(javax.xml.ws.BindingProvider) URL(java.net.URL) BusException(org.apache.cxf.BusException) EndpointException(org.apache.cxf.endpoint.EndpointException)

Example 37 with BindingProvider

use of javax.xml.ws.BindingProvider in project cxf by apache.

the class Client method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        System.out.println("Please specify the WSDL file.");
        System.exit(1);
    }
    URL wsdlURL;
    File wsdlFile = new File(args[0]);
    if (wsdlFile.exists()) {
        wsdlURL = wsdlFile.toURI().toURL();
    } else {
        wsdlURL = new URL(args[0]);
    }
    System.out.println(wsdlURL);
    TestMtomService tms = new TestMtomService(wsdlURL, SERVICE_NAME);
    TestMtomPortType port = (TestMtomPortType) tms.getPort(PORT_NAME, TestMtomPortType.class);
    Binding binding = ((BindingProvider) port).getBinding();
    ((SOAPBinding) binding).setMTOMEnabled(true);
    URL fileURL = Client.class.getResource("/me.bmp");
    File aFile = new File(new URI(fileURL.toString()));
    long fileSize = aFile.length();
    System.out.println("Filesize of me.bmp image is: " + fileSize);
    System.out.println("\nStarting MTOM Test using basic byte array:");
    Holder<String> name = new Holder<String>("Sam");
    Holder<byte[]> param = new Holder<byte[]>();
    param.value = new byte[(int) fileSize];
    InputStream in = fileURL.openStream();
    int len = in.read(param.value);
    while (len < fileSize) {
        len += in.read(param.value, len, (int) (fileSize - len));
    }
    System.out.println("--Sending the me.bmp image to server");
    System.out.println("--Sending a name value of " + name.value);
    port.testByteArray(name, param);
    System.out.println("--Received byte[] back from server, returned size is " + param.value.length);
    System.out.println("--Returned string value is " + name.value);
    Image image = ImageIO.read(new ByteArrayInputStream(param.value));
    System.out.println("--Loaded image from byte[] successfully, hashCode=" + image.hashCode());
    System.out.println("Successfully ran MTOM/byte array demo");
    System.out.println("\nStarting MTOM test with DataHandler:");
    name.value = "Bob";
    Holder<DataHandler> handler = new Holder<DataHandler>();
    handler.value = new DataHandler(fileURL);
    System.out.println("--Sending the me.bmp image to server");
    System.out.println("--Sending a name value of " + name.value);
    port.testDataHandler(name, handler);
    InputStream mtomIn = handler.value.getInputStream();
    fileSize = 0;
    for (int i = mtomIn.read(); i != -1; i = mtomIn.read()) {
        fileSize++;
    }
    System.out.println("--Received DataHandler back from server, " + "returned size is " + fileSize);
    System.out.println("--Returned string value is " + name.value);
    System.out.println("Successfully ran MTOM/DataHandler demo");
    System.exit(0);
}
Also used : Binding(javax.xml.ws.Binding) SOAPBinding(javax.xml.ws.soap.SOAPBinding) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Holder(javax.xml.ws.Holder) SOAPBinding(javax.xml.ws.soap.SOAPBinding) BindingProvider(javax.xml.ws.BindingProvider) DataHandler(javax.activation.DataHandler) TestMtomPortType(org.apache.cxf.mime.TestMtomPortType) Image(java.awt.Image) TestMtomService(org.apache.cxf.mime.TestMtomService) URI(java.net.URI) URL(java.net.URL) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File)

Example 38 with BindingProvider

use of javax.xml.ws.BindingProvider in project cxf by apache.

the class JaxWsClientThreadTest method testRequestContextThreadSafety.

@Test
public void testRequestContextThreadSafety() throws Throwable {
    URL url = getClass().getResource("/wsdl/hello_world.wsdl");
    javax.xml.ws.Service s = javax.xml.ws.Service.create(url, serviceName);
    final Greeter greeter = s.getPort(portName, Greeter.class);
    final InvocationHandler handler = Proxy.getInvocationHandler(greeter);
    ((BindingProvider) handler).getRequestContext().put(JaxWsClientProxy.THREAD_LOCAL_REQUEST_CONTEXT, Boolean.TRUE);
    Map<String, Object> requestContext = ((BindingProvider) handler).getRequestContext();
    String address = (String) requestContext.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
    final Throwable[] errorHolder = new Throwable[1];
    Runnable r = new Runnable() {

        public void run() {
            try {
                final String protocol = "http-" + Thread.currentThread().getId();
                for (int i = 0; i < 10; i++) {
                    String threadSpecificaddress = protocol + "://localhost:80/" + i;
                    Map<String, Object> requestContext = ((BindingProvider) handler).getRequestContext();
                    requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, threadSpecificaddress);
                    assertEquals("we get what we set", threadSpecificaddress, requestContext.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY));
                    try {
                        greeter.greetMe("Hi");
                    } catch (WebServiceException expected) {
                        // expected.getCause().printStackTrace();
                        MalformedURLException mue = (MalformedURLException) expected.getCause();
                        if (mue == null || mue.getMessage() == null) {
                            throw expected;
                        }
                        assertTrue("protocol contains thread id from context", mue.getMessage().indexOf(protocol) != 0);
                    }
                    requestContext.remove(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
                    assertTrue("property is null", requestContext.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY) == null);
                }
            } catch (Throwable t) {
                // capture assert failures
                errorHolder[0] = t;
            }
        }
    };
    final int numThreads = 5;
    Thread[] threads = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        threads[i] = new Thread(r);
    }
    for (int i = 0; i < numThreads; i++) {
        threads[i].start();
    }
    for (int i = 0; i < numThreads; i++) {
        threads[i].join();
    }
    if (errorHolder[0] != null) {
        throw errorHolder[0];
    }
    // main thread contextValues are un changed
    assertTrue("address from existing context has not changed", address.equals(requestContext.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY)));
    // get the latest values
    ((ClientImpl.EchoContext) ((WrappedMessageContext) requestContext).getWrappedMap()).reload();
    assertTrue("address is different", !address.equals(requestContext.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY)));
    // verify value reflects what other threads were doing
    assertTrue("property is null from last thread execution", requestContext.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY) == null);
}
Also used : MalformedURLException(java.net.MalformedURLException) WebServiceException(javax.xml.ws.WebServiceException) Service(javax.xml.ws.Service) BindingProvider(javax.xml.ws.BindingProvider) InvocationHandler(java.lang.reflect.InvocationHandler) URL(java.net.URL) Greeter(org.apache.hello_world_soap_http.Greeter) Test(org.junit.Test) AbstractCXFTest(org.apache.cxf.test.AbstractCXFTest)

Example 39 with BindingProvider

use of javax.xml.ws.BindingProvider in project cxf by apache.

the class ClientServerWebSocketTest method testFaults.

@Test
public void testFaults() throws Exception {
    URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
    assertNotNull(wsdl);
    SOAPService service = new SOAPService(wsdl, serviceName);
    ExecutorService ex = Executors.newFixedThreadPool(1);
    service.setExecutor(ex);
    assertNotNull(service);
    String noSuchCodeFault = "NoSuchCodeLitFault";
    String badRecordFault = "BadRecordLitFault";
    Greeter greeter = service.getPort(portName, Greeter.class);
    updateGreeterAddress(greeter, PORT);
    for (int idx = 0; idx < 2; idx++) {
        try {
            greeter.testDocLitFault(noSuchCodeFault);
            fail("Should have thrown NoSuchCodeLitFault exception");
        } catch (NoSuchCodeLitFault nslf) {
            assertNotNull(nslf.getFaultInfo());
            assertNotNull(nslf.getFaultInfo().getCode());
        }
        try {
            greeter.testDocLitFault(badRecordFault);
            fail("Should have thrown BadRecordLitFault exception");
        } catch (BadRecordLitFault brlf) {
            BindingProvider bp = (BindingProvider) greeter;
            Map<String, Object> responseContext = bp.getResponseContext();
            String contentType = (String) responseContext.get(Message.CONTENT_TYPE);
            assertEquals("text/xml; charset=utf-8", contentType.toLowerCase());
            Integer responseCode = (Integer) responseContext.get(Message.RESPONSE_CODE);
            assertEquals(500, responseCode.intValue());
            assertNotNull(brlf.getFaultInfo());
            assertEquals("BadRecordLitFault", brlf.getFaultInfo());
        }
    }
}
Also used : SOAPService(org.apache.hello_world_soap_http.SOAPService) BadRecordLitFault(org.apache.hello_world_soap_http.BadRecordLitFault) NoSuchCodeLitFault(org.apache.hello_world_soap_http.NoSuchCodeLitFault) Greeter(org.apache.hello_world_soap_http.Greeter) ExecutorService(java.util.concurrent.ExecutorService) BindingProvider(javax.xml.ws.BindingProvider) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 40 with BindingProvider

use of javax.xml.ws.BindingProvider in project cxf by apache.

the class ClientServerWebSocketTest method testBasicConnection.

@Test
public void testBasicConnection() throws Exception {
    SOAPService service = new SOAPService();
    Greeter greeter = service.getPort(portName, Greeter.class);
    updateGreeterAddress(greeter, PORT);
    try {
        String reply = greeter.greetMe("test");
        assertNotNull("no response received from service", reply);
        assertEquals("Hello test", reply);
        reply = greeter.sayHi();
        assertNotNull("no response received from service", reply);
        assertEquals("Bonjour", reply);
    } catch (UndeclaredThrowableException ex) {
        throw (Exception) ex.getCause();
    }
    BindingProvider bp = (BindingProvider) greeter;
    Map<String, Object> responseContext = bp.getResponseContext();
    Integer responseCode = (Integer) responseContext.get(Message.RESPONSE_CODE);
    assertEquals(200, responseCode.intValue());
}
Also used : SOAPService(org.apache.hello_world_soap_http.SOAPService) Greeter(org.apache.hello_world_soap_http.Greeter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) BindingProvider(javax.xml.ws.BindingProvider) Test(org.junit.Test)

Aggregations

BindingProvider (javax.xml.ws.BindingProvider)147 URL (java.net.URL)87 Test (org.junit.Test)74 Service (javax.xml.ws.Service)65 QName (javax.xml.namespace.QName)41 WebServiceException (javax.xml.ws.WebServiceException)24 Greeter (org.apache.hello_world_soap_http.Greeter)23 DoubleItPortType (org.example.contract.doubleit.DoubleItPortType)18 Bus (org.apache.cxf.Bus)17 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)15 InvocationHandler (java.lang.reflect.InvocationHandler)14 SOAPService (org.apache.hello_world_soap_http.SOAPService)14 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)12 STSClient (org.apache.cxf.ws.security.trust.STSClient)11 Greeter (org.apache.cxf.greeter_control.Greeter)10 GreeterService (org.apache.cxf.greeter_control.GreeterService)10 SOAPBinding (javax.xml.ws.soap.SOAPBinding)9 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)9 Client (org.apache.cxf.endpoint.Client)9 HTTPConduit (org.apache.cxf.transport.http.HTTPConduit)8