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

The following examples show how to use org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#setFeatures() . 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: JAXRSClientServerValidationSpringTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testHelloRestValidationFailsIfNameIsNullClient() throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress("http://localhost:" + PORT + "/bwrest");
    bean.setServiceClass(BookWorld.class);
    bean.setFeatures(Arrays.asList(new JAXRSClientBeanValidationFeature()));
    BookWorld service = bean.create(BookWorld.class);
    BookWithValidation bw = service.echoBook(new BookWithValidation("RS", "123"));
    assertEquals("123", bw.getId());

    try {
        service.echoBook(new BookWithValidation(null, "123"));
        fail("Validation failure expected");
    } catch (ConstraintViolationException ex) {
        // complete
    }

}
 
Example 2
Source File: JAXRSClientMetricsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void usingClientProxyStopIsCalledWhenServerReturnsNotFound() throws Exception {
    final JAXRSClientFactoryBean factory = new JAXRSClientFactoryBean();
    factory.setResourceClass(Library.class);
    factory.setAddress("http://localhost:" + wireMockRule.port() + "/");
    factory.setFeatures(Arrays.asList(new MetricsFeature(provider)));
    factory.setProvider(JacksonJsonProvider.class);
    
    stubFor(get(urlEqualTo("/books/10"))
        .willReturn(aResponse()
            .withStatus(404)));

    try {
        final Library client = factory.create(Library.class);
        expectedException.expect(NotFoundException.class);
        client.getBook(10);
    } finally {
        Mockito.verify(resourceContext, times(1)).start(any(Exchange.class));
        Mockito.verify(resourceContext, 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(operationContext);
    }
}
 
Example 3
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 4
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 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: JAXRSSoapBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddFeatureToClient() throws Exception {
    String baseAddress = "http://localhost:" + PORT + "/test/services/rest";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(baseAddress);
    bean.setResourceClass(BookStoreJaxrsJaxws.class);
    TestFeature testFeature = new TestFeature();
    List<AbstractFeature> features = new ArrayList<>();
    features.add(testFeature);
    bean.setFeatures(features);
    BookStoreJaxrsJaxws proxy = (BookStoreJaxrsJaxws)bean.create();
    Book b = proxy.getBook(Long.valueOf("123"));
    assertTrue("Out Interceptor not invoked", testFeature.handleMessageOnOutInterceptorCalled());
    assertTrue("In Interceptor not invoked", testFeature.handleMessageOnInInterceptorCalled());
    assertEquals(123, b.getId());
    assertEquals("CXF in Action", b.getName());
}
 
Example 7
Source File: JAXRSSoapBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testClientFaultOutInterceptor() throws Exception {
    //testing faults created by client out interceptor chain handled correctly
    String baseAddress = "http://localhost:" + PORT + "/test/services/rest";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(baseAddress);
    bean.setResourceClass(BookStoreJaxrsJaxws.class);
    final boolean addBadOutInterceptor = true;
    TestFeature testFeature = new TestFeature(addBadOutInterceptor);
    List<AbstractFeature> features = new ArrayList<>();
    features.add(testFeature);
    bean.setFeatures(features);
    BookStoreJaxrsJaxws proxy = (BookStoreJaxrsJaxws)bean.create();
    try {
        //321 is special case - causes error code of 525
        proxy.getBook(Long.valueOf("123"));
        fail("Method should have thrown an exception");
    } catch (Exception e) {
        assertTrue("Out Interceptor not invoked", testFeature.handleMessageOnOutInterceptorCalled());
        assertFalse("In Interceptor not invoked", testFeature.handleMessageOnInInterceptorCalled());
        assertTrue("Wrong exception caught",
                   "fault from bad interceptor".equals(e.getCause().getMessage()));
        assertTrue("Client In Fault In Interceptor was invoked",
                testFeature.faultInInterceptorCalled());
    }
}
 
Example 8
Source File: AmbariClientBuilder.java    From components with Apache License 2.0 5 votes vote down vote up
/**
 * Build a client proxy, for a specific proxy type.
 * 
 * @param proxyType proxy type class
 * @return client proxy stub
 */
protected <T> T build(Class<T> proxyType) {
    String address = generateAddress();
    T rootResource;
    // Synchronized on the class to correlate with the scope of clientStaticResources
    // We want to ensure that the shared bean isn't set concurrently in multiple callers
    synchronized (AmbariClientBuilder.class) {
        JAXRSClientFactoryBean bean = cleanFactory(clientStaticResources.getUnchecked(proxyType));
        bean.setAddress(address);
        if (username != null) {
            bean.setUsername(username);
            bean.setPassword(password);
        }

        if (enableLogging) {
            bean.setFeatures(Arrays.<AbstractFeature> asList(new LoggingFeature()));
        }
        rootResource = bean.create(proxyType);
    }

    boolean isTlsEnabled = address.startsWith("https://");
    ClientConfiguration config = WebClient.getConfig(rootResource);
    HTTPConduit conduit = (HTTPConduit) config.getConduit();
    if (isTlsEnabled) {
        TLSClientParameters tlsParams = new TLSClientParameters();
        if (!validateCerts) {
            tlsParams.setTrustManagers(new TrustManager[] { new AcceptAllTrustManager() });
        } else if (trustManagers != null) {
            tlsParams.setTrustManagers(trustManagers);
        }
        tlsParams.setDisableCNCheck(!validateCn);
        conduit.setTlsClientParameters(tlsParams);
    }

    HTTPClientPolicy policy = conduit.getClient();
    policy.setConnectionTimeout(connectionTimeoutUnits.toMillis(connectionTimeout));
    policy.setReceiveTimeout(receiveTimeoutUnits.toMillis(receiveTimeout));
    return rootResource;
}
 
Example 9
Source File: SelfKeymasterClientContext.java    From syncope with Apache License 2.0 5 votes vote down vote up
@ConditionalOnExpression("#{'${keymaster.address}' matches '^http.+'}")
@Bean
@ConditionalOnMissingBean(name = "selfKeymasterRESTClientFactoryBean")
public JAXRSClientFactoryBean selfKeymasterRESTClientFactoryBean() {
    JAXRSClientFactoryBean restClientFactoryBean = new JAXRSClientFactoryBean();
    restClientFactoryBean.setAddress(address);
    restClientFactoryBean.setUsername(username);
    restClientFactoryBean.setPassword(password);
    restClientFactoryBean.setThreadSafe(true);
    restClientFactoryBean.setInheritHeaders(true);
    restClientFactoryBean.setFeatures(List.of(new LoggingFeature()));
    restClientFactoryBean.setProviders(
        List.of(new JacksonJsonProvider(), new SelfKeymasterClientExceptionMapper()));
    return restClientFactoryBean;
}
 
Example 10
Source File: AbstractFailoverTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private static JAXRSClientFactoryBean createBean(String address,
                                            FailoverFeature feature) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    bean.setFeatures(Arrays.asList(feature));
    return bean;
}
 
Example 11
Source File: LoadDistributorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected JAXRSClientFactoryBean createBean(String address,
                                            FailoverFeature feature) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    List<AbstractFeature> features = new ArrayList<>();
    features.add(feature);
    bean.setFeatures(features);

    return bean;
}
 
Example 12
Source File: JAXRSSoapBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void serverFaultInInterceptorTest(String param) {
    String baseAddress = "http://localhost:" + PORT + "/test/services/rest";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(baseAddress);
    bean.setResourceClass(BookStoreJaxrsJaxws.class);
    TestFeature testFeature = new TestFeature();
    List<AbstractFeature> features = new ArrayList<>();
    features.add(testFeature);
    bean.setFeatures(features);
    BookStoreJaxrsJaxws proxy = (BookStoreJaxrsJaxws)bean.create();
    WebClient.getConfig(proxy).getRequestContext().put("org.apache.cxf.transport.no_io_exceptions", false);
    try {
        //321 is special case - causes error code of 525
        proxy.getBook(Long.valueOf(param));
        fail("Method should have thrown an exception");
    } catch (Exception e) {
        assertTrue("Out Interceptor not invoked", testFeature.handleMessageOnOutInterceptorCalled());
        if ("322".equals(param)) {
            //In interceptors not called when checked exception thrown from server
            assertTrue("In Interceptor not invoked", testFeature.handleMessageOnInInterceptorCalled());
        } else {
            assertFalse("In Interceptor not invoked", testFeature.handleMessageOnInInterceptorCalled());
        }
        assertTrue("Client In Fault In Interceptor not invoked",
                testFeature.faultInInterceptorCalled());
    }
}
 
Example 13
Source File: RESTLoggingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private WebClient createClient(String serviceURI, LoggingFeature loggingFeature) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(serviceURI);
    bean.setFeatures(Collections.singletonList(loggingFeature));
    bean.setTransportId(LocalTransportFactory.TRANSPORT_ID);
    return bean.createWebClient().path("test1");
}
 
Example 14
Source File: AmbariClientBuilder.java    From components with Apache License 2.0 4 votes vote down vote up
private static JAXRSClientFactoryBean cleanFactory(JAXRSClientFactoryBean bean) {
    bean.setUsername(null);
    bean.setPassword(null);
    bean.setFeatures(Arrays.<AbstractFeature> asList());
    return bean;
}