Java Code Examples for org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#createWebClient()

The following examples show how to use org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#createWebClient() . 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: ClientImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void initTargetClientIfNeeded(Map<String, Object> configProps) {
    URI uri = uriBuilder.build();
    if (targetClient == null) {
        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
        bean.setAddress(uri.toString());
        bean.setProperties(configProps);
        Boolean threadSafe = getBooleanValue(configProps.get(THREAD_SAFE_CLIENT_PROP));
        if (threadSafe == null) {
            threadSafe = DEFAULT_THREAD_SAFETY_CLIENT_STATUS;
        }
        bean.setThreadSafe(threadSafe);
        if (threadSafe) {
            Integer cleanupPeriod = getIntValue(configProps.get(THREAD_SAFE_CLIENT_STATE_CLEANUP_PROP));
            if (cleanupPeriod == null) {
                cleanupPeriod = THREAD_SAFE_CLIENT_STATE_CLEANUP_PERIOD;
            }
            if (cleanupPeriod != null) {
                bean.setSecondsToKeepState(cleanupPeriod);
            }
        }
        targetClient = bean.createWebClient();
        ClientImpl.this.baseClients.add(targetClient);
    } else if (!targetClient.getCurrentURI().equals(uri)) {
        targetClient.to(uri.toString(), false);
    }
}
 
Example 2
Source File: JAXRSClientServerNonSpringBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBook123UserModelAuthorize() throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress("http://localhost:" + PORT + "/usermodel/bookstore/books");
    bean.setUsername("Barry");
    bean.setPassword("password");
    bean.setModelRef("classpath:org/apache/cxf/systest/jaxrs/resources/resources.xml");
    WebClient proxy = bean.createWebClient();
    proxy.path("{id}/authorize", 123);

    Book book = proxy.get(Book.class);
    assertEquals(123L, book.getId());



}
 
Example 3
Source File: JAXRSSoapBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testPostGetBookFastinfoset() throws Exception {

    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress("http://localhost:" + PORT + "/test/services/rest3/bookstore/fastinfoset");
    bean.getOutInterceptors().add(new FIStaxOutInterceptor());
    bean.getInInterceptors().add(new FIStaxInInterceptor());
    JAXBElementProvider<?> p = new JAXBElementProvider<>();
    p.setConsumeMediaTypes(Collections.singletonList("application/fastinfoset"));
    p.setProduceMediaTypes(Collections.singletonList("application/fastinfoset"));
    bean.setProvider(p);

    Map<String, Object> props = new HashMap<>();
    props.put(FIStaxOutInterceptor.FI_ENABLED, Boolean.TRUE);
    bean.setProperties(props);

    WebClient client = bean.createWebClient();
    Book b = new Book("CXF", 1L);
    Book b2 = client.type("application/fastinfoset").accept("application/fastinfoset")
        .post(b, Book.class);
    assertEquals(b2.getName(), b.getName());
    assertEquals(b2.getId(), b.getId());
}
 
Example 4
Source File: JAXRSSoapBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBookFastinfoset() throws Exception {

    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress("http://localhost:" + PORT + "/test/services/rest3/bookstore/fastinfoset2");
    bean.getInInterceptors().add(new FIStaxInInterceptor());
    JAXBElementProvider<?> p = new JAXBElementProvider<>();
    p.setConsumeMediaTypes(Collections.singletonList("application/fastinfoset"));
    bean.setProvider(p);

    Map<String, Object> props = new HashMap<>();
    props.put(FIStaxInInterceptor.FI_GET_SUPPORTED, Boolean.TRUE);
    bean.setProperties(props);

    WebClient client = bean.createWebClient();
    Book b = client.accept("application/fastinfoset").get(Book.class);
    assertEquals("CXF2", b.getName());
    assertEquals(2L, b.getId());
}
 
Example 5
Source File: FailoverWebClientTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryFailoverAlternateAddresses() throws Exception {
    String address = "http://localhost:" + AbstractFailoverTest.NON_PORT + "/bookstore/unavailable";

    final FailoverFeature feature = new FailoverFeature();
    RetryStrategy strategy = new RetryStrategy();
    strategy.setAlternateAddresses(Arrays.asList("http://localhost:" + PORT1 + "/bookstore/unavailable"));
    strategy.setMaxNumberOfRetries(5);
    strategy.setDelayBetweenRetries(500);
    feature.setStrategy(strategy);

    final JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    bean.setFeatures(Arrays.asList(feature));
    bean.setServiceClass(FailoverBookStore.class);
    WebClient webClient = bean.createWebClient();
    
    final Book b = webClient.get(Book.class);
    assertEquals(124L, b.getId());
    assertEquals("root", b.getName());
    assertEquals("http://localhost:" + PORT1 + "/bookstore/unavailable",
        webClient.getBaseURI().toString());
}
 
Example 6
Source File: FailoverWebClientTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCircuitBreakerRetryFailover() throws Exception {
    String address = "http://localhost:" + PORT1 + "/bookstore/unavailable";

    final CircuitBreakerFailoverFeature feature = new CircuitBreakerFailoverFeature();
    feature.setThreshold(5);
    RetryStrategy strategy = new RetryStrategy();
    strategy.setMaxNumberOfRetries(5);
    strategy.setDelayBetweenRetries(1000);
    feature.setStrategy(strategy);

    final JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    bean.setFeatures(Arrays.asList(feature));
    bean.setServiceClass(FailoverBookStore.class);
    WebClient webClient = bean.createWebClient();
    
    final Book b = webClient.get(Book.class);
    assertEquals(124L, b.getId());
    assertEquals("root", b.getName());
    assertEquals(address, webClient.getBaseURI().toString());
}
 
Example 7
Source File: FailoverWebClientTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testRetryFailover() throws Exception {
    String address = "http://localhost:" + PORT1 + "/bookstore/unavailable";

    final FailoverFeature feature = new FailoverFeature();
    RetryStrategy strategy = new RetryStrategy();
    strategy.setMaxNumberOfRetries(5);
    feature.setStrategy(strategy);

    final JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    bean.setFeatures(Arrays.asList(feature));
    bean.setServiceClass(FailoverBookStore.class);
    WebClient webClient = bean.createWebClient();
    
    final Book b = webClient.get(Book.class);
    assertEquals(124L, b.getId());
    assertEquals("root", b.getName());
    assertEquals(address, webClient.getBaseURI().toString());
}
 
Example 8
Source File: JAXRSSamlAuthorizationTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private WebClient createWebClient(String address, Map<String, Object> extraProperties) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSSamlAuthorizationTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    Map<String, Object> properties = new HashMap<>();
    properties.put(SecurityConstants.SAML_CALLBACK_HANDLER,
                   "org.apache.cxf.systest.jaxrs.security.saml.SamlCallbackHandler");
    if (extraProperties != null) {
        properties.putAll(extraProperties);
    }
    bean.setProperties(properties);

    bean.getOutInterceptors().add(new SamlEnvelopedOutInterceptor());

    return bean.createWebClient();
}
 
Example 9
Source File: JAXRSSamlTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private WebClient createWebClientForExistingToken(String address,
                                  Interceptor<Message> outInterceptor,
                                  Object provider) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSSamlTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    bean.getOutInterceptors().add(outInterceptor);
    bean.getOutInterceptors().add(new SamlRetrievalInterceptor());
    if (provider != null) {
        bean.setProvider(provider);
    }
    return bean.createWebClient();
}
 
Example 10
Source File: JAXRSSamlTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSAMLTokenAsHeader() throws Exception {
    String address = "https://localhost:" + PORT + "/samlheader/bookstore/books/123";

    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSSamlTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    WebClient wc = bean.createWebClient();
    wc.header("Authorization", "SAML invalid_grant");
    Response r = wc.get();
    assertEquals(401, r.getStatus());
}
 
Example 11
Source File: JAXRSOAuth2TlsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private WebClient createOAuth2WebClient(String address) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSOAuth2TlsTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    WebClient wc = bean.createWebClient();
    wc.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON);
    return wc;
}
 
Example 12
Source File: JAXRSOAuth2TlsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private WebClient createDynRegWebClient(String address) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    bean.setProvider(new JsonMapObjectProvider());

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSOAuth2TlsTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    WebClient wc = bean.createWebClient();
    wc.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
    return wc;
}
 
Example 13
Source File: JAXRSOAuth2TlsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private WebClient createRsWebClient(String address, ClientAccessToken at, String clientContext) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSOAuth2TlsTest.class.getResource(clientContext);
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    WebClient wc = bean.createWebClient();
    wc.accept(MediaType.APPLICATION_XML);
    wc.authorization(at);
    return wc;
}
 
Example 14
Source File: JAXRSOAuth2Test.java    From cxf with Apache License 2.0 5 votes vote down vote up
private WebClient createWebClientWithProps(String address) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSOAuth2Test.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    Map<String, Object> properties = new HashMap<>();
    properties.put(SecurityConstants.CALLBACK_HANDLER,
                   "org.apache.cxf.systest.jaxrs.security.saml.KeystorePasswordCallback");

    SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler(true);
    samlCallbackHandler.setIssuer("alice");
    String audienceURI = "https://localhost:" + port + "/oauth2-auth/token";
    samlCallbackHandler.setAudience(audienceURI);
    properties.put(SecurityConstants.SAML_CALLBACK_HANDLER, samlCallbackHandler);

    properties.put(SecurityConstants.SIGNATURE_USERNAME, "alice");
    properties.put(SecurityConstants.SIGNATURE_PROPERTIES, CRYPTO_RESOURCE_PROPERTIES);
    bean.setProperties(properties);

    bean.getOutInterceptors().add(new Saml2BearerAuthOutInterceptor());

    WebClient wc = bean.createWebClient();
    wc.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON);
    return wc;
}
 
Example 15
Source File: JAXRSOAuth2Test.java    From cxf with Apache License 2.0 5 votes vote down vote up
private WebClient createWebClient(String address) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSOAuth2Test.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    WebClient wc = bean.createWebClient();
    wc.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON);
    return wc;
}
 
Example 16
Source File: JAXRSSoapBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoBook357WebClient() throws Exception {

    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    Map<String, Object> properties = new HashMap<>();
    properties.put("org.apache.cxf.http.throw_io_exceptions", Boolean.TRUE);
    bean.setProperties(properties);
    bean.setAddress("http://localhost:" + PORT + "/test/services/rest/bookstore/356");
    WebClient wc = bean.createWebClient();
    Response response = wc.get();
    assertEquals(404, response.getStatus());
    String msg = IOUtils.readStringFromStream((InputStream)response.getEntity());
    assertEquals("No Book with id 356 is available", msg);

}
 
Example 17
Source File: JAXRSSoapBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testOtherInterceptorDrainingStream() throws Exception {

    String baseAddress = "http://localhost:" + PORT + "/test/services/rest";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(baseAddress);
    bean.getInInterceptors().add(new TestStreamDrainInterptor());
    WebClient client = bean.createWebClient();
    client.path("/bookstore/123").accept(MediaType.APPLICATION_XML_TYPE);
    Book b = client.get(Book.class);
    assertEquals(123, b.getId());
    assertEquals("CXF in Action", b.getName());
}
 
Example 18
Source File: JAXRSMultipartTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testXopWebClient() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/xop";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    bean.setProperties(Collections.singletonMap(org.apache.cxf.message.Message.MTOM_ENABLED,
                                                (Object)"true"));
    WebClient client = bean.createWebClient();
    WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor());
    WebClient.getConfig(client).getOutInterceptors().add(new LoggingOutInterceptor());
    WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart",
        "true");
    client.type("multipart/related").accept("multipart/related");

    XopType xop = new XopType();
    xop.setName("xopName");
    InputStream is =
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/book.xsd");
    byte[] data = IOUtils.readBytesFromStream(is);
    xop.setAttachinfo(new DataHandler(new ByteArrayDataSource(data, "application/octet-stream")));
    xop.setAttachInfoRef(new DataHandler(new ByteArrayDataSource(data, "application/octet-stream")));

    String bookXsd = IOUtils.readStringFromStream(getClass().getResourceAsStream(
        "/org/apache/cxf/systest/jaxrs/resources/book.xsd"));
    xop.setAttachinfo2(bookXsd.getBytes());

    xop.setImage(getImage("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));

    XopType xop2 = client.post(xop, XopType.class);

    String bookXsdOriginal = IOUtils.readStringFromStream(getClass().getResourceAsStream(
            "/org/apache/cxf/systest/jaxrs/resources/book.xsd"));
    String bookXsd2 = IOUtils.readStringFromStream(xop2.getAttachinfo().getInputStream());
    assertEquals(bookXsdOriginal, bookXsd2);
    String bookXsdRef = IOUtils.readStringFromStream(xop2.getAttachInfoRef().getInputStream());
    assertEquals(bookXsdOriginal, bookXsdRef);

    String ctString =
        client.getResponse().getMetadata().getFirst("Content-Type").toString();
    MediaType mt = MediaType.valueOf(ctString);
    Map<String, String> params = mt.getParameters();
    assertEquals(4, params.size());
    assertNotNull(params.get("boundary"));
    assertNotNull(params.get("type"));
    assertNotNull(params.get("start"));
    assertNotNull(params.get("start-info"));
}
 
Example 19
Source File: JAXRSXmlSecTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testPostBookWithEnvelopedSigKeyName() throws Exception {
    // This test only applies to StAX - see CXF-7084
    if (!test.streaming || !STAX_PORT.equals(test.port)) {
        return;
    }
    String address = "https://localhost:" + test.port + "/xmlsigkeyname/bookstore/books";

    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSXmlSecTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    Map<String, Object> properties = new HashMap<>();
    properties.put(SecurityConstants.CALLBACK_HANDLER,
                   "org.apache.cxf.systest.jaxrs.security.saml.KeystorePasswordCallback");
    properties.put(SecurityConstants.SIGNATURE_USERNAME, "alice");
    properties.put(SecurityConstants.SIGNATURE_PROPERTIES,
                   "org/apache/cxf/systest/jaxrs/security/alice.properties");
    bean.setProperties(properties);
    XmlSecOutInterceptor sigOutInterceptor = new XmlSecOutInterceptor();
    sigOutInterceptor.setSignRequest(true);
    sigOutInterceptor.setKeyInfoMustBeAvailable(true);

    SignatureProperties sigProps = new SignatureProperties();
    sigProps.setSignatureKeyName("alice-kn");
    sigProps.setSignatureKeyIdType("KeyName");
    sigOutInterceptor.setSignatureProperties(sigProps);

    bean.getOutInterceptors().add(sigOutInterceptor);

    XmlSecInInterceptor sigInInterceptor = new XmlSecInInterceptor();
    sigInInterceptor.setRequireSignature(true);
    bean.setProvider(sigInInterceptor);

    WebClient wc = bean.createWebClient();
    WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L);
    Book book = wc.type("application/xml").post(new Book("CXF", 126L), Book.class);
    assertEquals(126L, book.getId());
}
 
Example 20
Source File: AbstractFailoverTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected WebClient getWebClient(String address,
                                 FailoverFeature feature) throws Exception {
    JAXRSClientFactoryBean bean = createBean(address, feature);
    return bean.createWebClient();
}