Java Code Examples for org.apache.cxf.endpoint.Client#invoke()

The following examples show how to use org.apache.cxf.endpoint.Client#invoke() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApplicationTests.java    From SpringBootBucket with MIT License 6 votes vote down vote up
/**
 * 方式2. 动态调用方式
 */
@Test
public void cl2() {
    // 创建动态客户端
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient(wsdlAddress);
    // 需要密码的情况需要加上用户名和密码
    // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
    Object[] objects;
    try {
        // invoke("方法名",参数1,参数2,参数3....);
        objects = client.invoke("sayHello", "Leftso");
        System.out.println("返回类型:" + objects[0].getClass());
        System.out.println("返回数据:" + objects[0]);
    } catch (java.lang.Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: ApplicationTests.java    From SpringBootBucket with MIT License 6 votes vote down vote up
/**
 * 方式3. 动态调用方式,返回对象User
 */
@Test
public void cl3() {
    // 创建动态客户端
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient(wsdlAddress);
    Object[] objects;
    try {
        // invoke("方法名",参数1,参数2,参数3....);
        objects = client.invoke("getUser", "张三");
        System.out.println("返回类型:" + objects[0].getClass());
        System.out.println("返回数据:" + objects[0]);
        User user = (User) objects[0];
        System.out.println("返回对象User.name=" + user.getName());
    } catch (java.lang.Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: ClientServerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testDynamicClientFactory() throws Exception {
    URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
    assertNotNull(wsdl);
    String wsdlUrl = null;
    wsdlUrl = wsdl.toURI().toString();

    DynamicClientFactory dcf = DynamicClientFactory.newInstance();
    Client client = dcf.createClient(wsdlUrl, serviceName, portName);
    updateAddressPort(client, PORT);
    client.invoke("greetMe", "test");
    Object[] result = client.invoke("sayHi");
    assertNotNull("no response received from service", result);
    assertEquals("Bonjour", result[0]);

    client = dcf.createClient(wsdlUrl, serviceName, portName);
    new LoggingFeature().initialize(client, client.getBus());
    updateAddressPort(client, PORT);
    client.invoke("greetMe", "test");
    result = client.invoke("sayHi");
    assertNotNull("no response received from service", result);
    assertEquals("Bonjour", result[0]);
}
 
Example 4
Source File: SOAPServiceTest.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
    * Tests WSDL generation from a URL.
    *
    * This is similar to another KEW test but it is good to have it as part of the KSB tests.  Note that the
    * {@link Client} modifies the current thread's class loader.
    *
    * @throws Exception for any errors connecting to the client
    */
@Test
public void testWsdlGeneration() throws Exception {
	ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();

       try {
           JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
           Client client = dcf.createClient(new URI(getWsdlUrl()).toString());
           client.getInInterceptors().add(new LoggingInInterceptor());
           client.getOutInterceptors().add(new LoggingOutInterceptor());
           Object[] results = client.invoke("echo", "testing");
           assertNotNull(results);
           assertEquals(1, results.length);
           assertEquals("testing", results[0]);
       } finally {
           Thread.currentThread().setContextClassLoader(originalClassLoader);
       }
}
 
Example 5
Source File: JaxWsDynamicClientTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvocation() throws Exception {
    JaxWsDynamicClientFactory dcf =
        JaxWsDynamicClientFactory.newInstance();
    URL wsdlURL = new URL("http://localhost:" + PORT + "/NoBodyParts/NoBodyPartsService?wsdl");
    Client client = dcf.createClient(wsdlURL);
    byte[] bucketOfBytes =
        IOUtils.readBytesFromStream(getClass().getResourceAsStream("/wsdl/no_body_parts.wsdl"));
    Operation1 parameters = new Operation1();
    parameters.setOptionString("opt-ion");
    parameters.setTargetType("tar-get");
    Object[] rparts = client.invoke("operation1", parameters, bucketOfBytes);
    Operation1Response r = (Operation1Response)rparts[0];
    assertEquals(digest(bucketOfBytes), r.getStatus());

    ClientCallback callback = new ClientCallback();
    client.invoke(callback, "operation1", parameters, bucketOfBytes);
    rparts = callback.get();
    r = (Operation1Response)rparts[0];
    assertEquals(digest(bucketOfBytes), r.getStatus());
}
 
Example 6
Source File: JAXWSClientMetricsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void usingClientProxyStopIsCalledForUnsupportedOperation() throws Exception {
    final JaxWsClientFactoryBean factory = new JaxWsClientFactoryBean();
    factory.setAddress("local://services/Book");
    factory.setServiceClass(IBookWebService.class);
    factory.setFeatures(Arrays.asList(new MetricsFeature(provider)));
    
    try {
        final Client client = factory.create();
        expectedException.expect(UncheckedException.class);
        client.invoke("getBooks");
    } finally {
        Mockito.verifyNoInteractions(endpointContext);
        Mockito.verifyNoInteractions(operationContext);
        Mockito.verifyNoInteractions(resourceContext);
    }
}
 
Example 7
Source File: WebserviceclientApplication.java    From spring-boot-study with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    SpringApplication.run(WebserviceclientApplication.class, args);

    JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance();

    Client client=dcflient.createClient("http://localhost:8080/ws/user?wsdl");
    try{
        Object[] objects=client.invoke("getUserById","1");
        System.out.println("getUserById 调用结果:"+objects[0].toString());

        Object[] objectall=client.invoke("getUsers");
        System.out.println("getUsers调用部分结果:"+objectall[0].toString());

    }catch (Exception e){
        e.printStackTrace();
    }
}
 
Example 8
Source File: JAXWSClientMetricsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void usingClientProxyStopIsCalledWhenServerReturnsResponse() throws Exception {
    final JaxWsClientFactoryBean factory = new JaxWsClientFactoryBean();
    factory.setAddress("local://services/Book");
    factory.setServiceClass(IBookWebService.class);
    factory.setFeatures(Arrays.asList(new MetricsFeature(provider)));
    
    try {
        final Client client = factory.create();
        String response = (String)client.invoke("getBook", 10)[0];
        assertEquals("All your bases belong to us.", response);
    } finally {
        Mockito.verify(operationContext, times(1)).start(any(Exchange.class));
        Mockito.verify(operationContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
        Mockito.verify(endpointContext, times(1)).start(any(Exchange.class));
        Mockito.verify(endpointContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
        Mockito.verifyNoInteractions(resourceContext);
    }
}
 
Example 9
Source File: JAXWSClientMetricsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void usingClientProxyStopIsCalledWhenServerReturnsFault() throws Exception {
    final JaxWsClientFactoryBean factory = new JaxWsClientFactoryBean();
    factory.setAddress("local://services/Book");
    factory.setServiceClass(IBookWebService.class);
    factory.setFeatures(Arrays.asList(new MetricsFeature(provider)));
    
    try {
        final Client client = factory.create();
        expectedException.expect(SoapFault.class);
        client.invoke("getBook", 11);
    } finally {
        Mockito.verify(operationContext, times(1)).start(any(Exchange.class));
        Mockito.verify(operationContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
        Mockito.verify(endpointContext, times(1)).start(any(Exchange.class));
        Mockito.verify(endpointContext, times(1)).stop(anyLong(), anyLong(), anyLong(), any(Exchange.class));
        Mockito.verifyNoInteractions(resourceContext);
    }
}
 
Example 10
Source File: ClientServerPartialWsdlTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testCXF4676partial2() throws Exception {
    DynamicClientFactory dcf = DynamicClientFactory.newInstance();
    Client client = dcf.createClient("http://localhost:"
        + PORT + "/AddNumbersImplPartial2Service?wsdl", serviceName2, portName2);
    updateAddressPort(client, PORT);
    Object[] result = client.invoke("addTwoNumbers", 10, 20);
    assertNotNull("no response received from service", result);
    assertEquals(30, result[0]);

}
 
Example 11
Source File: JaxWsDynamicClientTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testArgfiles() throws Exception {
    System.setProperty("org.apache.cxf.common.util.Compiler-fork", "true");
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient(new URL("http://localhost:"
                                             + PORT1 + "/ArrayService?wsdl"));

    String[] values = new String[] {"foobar", "something" };
    List<String> list = Arrays.asList(values);

    client.getOutInterceptors().add(new LoggingOutInterceptor());
    client.getInInterceptors().add(new LoggingInInterceptor());
    client.invoke("init", list);
}
 
Example 12
Source File: JaxWsDynamicClientTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testArrayList() throws Exception {
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient(new URL("http://localhost:"
                                             + PORT1 + "/ArrayService?wsdl"));

    String[] values = new String[] {"foobar", "something" };
    List<String> list = Arrays.asList(values);

    client.getOutInterceptors().add(new LoggingOutInterceptor());
    client.getInInterceptors().add(new LoggingInInterceptor());
    client.invoke("init", list);
}
 
Example 13
Source File: BeanPostProcessorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void verifyServices() throws Exception {
    JaxWsClientFactoryBean cf = new JaxWsClientFactoryBean();
    cf.setAddress("local://services/Alger");
    cf.setServiceClass(IWebServiceRUs.class);
    Client client = cf.create();
    String response = (String)client.invoke("consultTheOracle")[0];
    assertEquals("All your bases belong to us.", response);
    Service service = WebServiceRUs.getService();
    assertEquals(JAXBDataBinding.class, service.getDataBinding().getClass());
}
 
Example 14
Source File: ClientServerPartialWsdlTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testCXF4676Partial1() throws Exception {
    DynamicClientFactory dcf = DynamicClientFactory.newInstance();
    Client client = dcf.createClient("http://localhost:"
        + PORT + "/AddNumbersImplPartial1Service?wsdl", serviceName1, portName1);
    updateAddressPort(client, PORT);
    Object[] result = client.invoke("addTwoNumbers", 10, 20);
    assertNotNull("no response received from service", result);
    assertEquals(30, result[0]);
}
 
Example 15
Source File: WSRMWithWSSecurityPolicyTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testContextProperty() throws Exception {
    try (ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("org/apache/cxf/systest/ws/rm/sec/client-policy.xml")) {
        Bus bus = (Bus)context.getBean("bus");
        BusFactory.setDefaultBus(bus);
        BusFactory.setThreadDefaultBus(bus);
        Greeter greeter = (Greeter)context.getBean("GreeterCombinedClientNoProperty");
        Client client = ClientProxy.getClient(greeter);
        QName operationQName = new QName("http://cxf.apache.org/greeter_control", "greetMe");
        BindingOperationInfo boi = client.getEndpoint().getBinding().getBindingInfo().getOperation(operationQName);
        Map<String, Object> invocationContext = new HashMap<>();
        Map<String, Object> requestContext = new HashMap<>();
        Map<String, Object> responseContext = new HashMap<>();
        invocationContext.put(Client.REQUEST_CONTEXT, requestContext);
        invocationContext.put(Client.RESPONSE_CONTEXT, responseContext);

        requestContext.put(SecurityConstants.USERNAME, "Alice");
        requestContext.put(SecurityConstants.CALLBACK_HANDLER,
            "org.apache.cxf.systest.ws.rm.sec.UTPasswordCallback");
        requestContext.put(SecurityConstants.ENCRYPT_PROPERTIES, "bob.properties");
        requestContext.put(SecurityConstants.ENCRYPT_USERNAME, "bob");
        requestContext.put(SecurityConstants.SIGNATURE_PROPERTIES, "alice.properties");
        requestContext.put(SecurityConstants.SIGNATURE_USERNAME, "alice");
        RMManager manager = bus.getExtension(RMManager.class);
        boolean empty = manager.getRetransmissionQueue().isEmpty();
        assertTrue("RetransmissionQueue is not empty", empty);
        GreetMe param = new GreetMe();
        param.setRequestType("testContextProperty");
        Object[] answer = client.invoke(boi, new Object[]{param}, invocationContext);
        Assert.assertEquals("TESTCONTEXTPROPERTY", answer[0].toString());
        Thread.sleep(5000);
        empty = manager.getRetransmissionQueue().isEmpty();
        assertTrue("RetransmissionQueue not empty", empty);
    }
}
 
Example 16
Source File: AegisClientServerTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamicClient() throws Exception {
    DynamicClientFactory dcf = DynamicClientFactory.newInstance();
    Client client = dcf.createClient("http://localhost:" + PORT + "/jaxwsAndAegisSports?wsdl&dynamic");

    Object r = client.invoke("getAttributeBean")[0];
    Method getAddrPlainString = r.getClass().getMethod("getAttrPlainString");
    String s = (String)getAddrPlainString.invoke(r);

    assertEquals("attrPlain", s);
}
 
Example 17
Source File: WebservicesClient.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public Object[] jaxws(String wsdl, String method, Object... objects) {
	Object[] result = null;
	try {
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient(wsdl);
		result = client.invoke(method, objects);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return result;
}
 
Example 18
Source File: WebservicesClient.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public Object[] jaxws(String wsdl, String method, Object... objects) {
	Object[] result = null;
	try {
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient(wsdl);
		result = client.invoke(method, objects);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return result;
}
 
Example 19
Source File: WebservicesClient.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
public Object[] jaxws(String wsdl, String method, Object... objects) {
	Object[] result = null;
	try {
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		Client client = dcf.createClient(wsdl);
		result = client.invoke(method, objects);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return result;
}
 
Example 20
Source File: Proxy.java    From cxf with Apache License 2.0 4 votes vote down vote up
Object invoke(OperationInfo oi, ProtocolVariation protocol,
              Object[] params, Map<String, Object> context, 
              Exchange exchange,
              Level exceptionLevel) throws RMException {

    if (LOG.isLoggable(Level.INFO)) {
        LOG.log(Level.INFO, "Sending out-of-band RM protocol message {0}.",
                oi == null ? null : oi.getName());
    }

    RMManager manager = reliableEndpoint.getManager();
    Bus bus = manager.getBus();
    Endpoint endpoint = reliableEndpoint.getEndpoint(protocol);
    BindingInfo bi = reliableEndpoint.getBindingInfo(protocol);
    Conduit c = reliableEndpoint.getConduit();
    Client client = null;
    if (params.length > 0 && params[0] instanceof DestinationSequence) {
        EndpointReferenceType acksTo = ((DestinationSequence)params[0]).getAcksTo();
        String acksAddress = acksTo.getAddress().getValue();
        AttributedURIType attrURIType = new AttributedURIType();
        attrURIType.setValue(acksAddress);
        EndpointReferenceType acks = new EndpointReferenceType();
        acks.setAddress(attrURIType);
        client = createClient(bus, endpoint, protocol, c, acks);
        params = new Object[] {};
    } else {
        EndpointReferenceType replyTo = reliableEndpoint.getReplyTo();
        client = createClient(bus, endpoint, protocol, c, replyTo);
    }

    BindingOperationInfo boi = bi.getOperation(oi);
    try {
        if (context != null) {
            client.getRequestContext().putAll(context);
        }
        Object[] result = client.invoke(boi, params, context, exchange);
        if (result != null && result.length > 0) {
            return result[0];
        }

    } catch (Exception ex) {
        org.apache.cxf.common.i18n.Message msg =
            new org.apache.cxf.common.i18n.Message("SEND_PROTOCOL_MSG_FAILED_EXC", LOG,
                                                   oi == null ? null : oi.getName());
        LOG.log(exceptionLevel, msg.toString(), ex);
        throw new RMException(msg, ex);
    }
    return null;
}