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

The following examples show how to use org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#setResourceClass() . 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: RestClientBuilder.java    From microshed-testing with Apache License 2.0 6 votes vote down vote up
public <T> T build(Class<T> clazz) {
    // Apply default values if unspecified
    if (appContextRoot == null)
        appContextRoot = ApplicationEnvironment.Resolver.load().getApplicationURL();
    if (jaxrsPath == null)
        jaxrsPath = locateApplicationPath(clazz);
    if (providers == null)
        providers = Collections.singletonList(JsonBProvider.class);

    JAXRSClientFactoryBean bean = new org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean();
    String basePath = join(appContextRoot, jaxrsPath);
    LOG.info("Building rest client for " + clazz + " with base path: " + basePath + " and providers: " + providers);
    bean.setResourceClass(clazz);
    bean.setProviders(providers);
    bean.setAddress(basePath);
    bean.setHeaders(headers);
    return bean.create(clazz);
}
 
Example 2
Source File: JAXRSUtilities.java    From microprofile-sandbox with Apache License 2.0 6 votes vote down vote up
public static <T> T createRestClient(Class<T> clazz, String appContextRoot, String applicationPath, String jwt) {
    Objects.requireNonNull(appContextRoot, "Supplied 'appContextRoot' must not be null");
    Objects.requireNonNull(applicationPath, "Supplied 'applicationPath' must not be null");
    String basePath = join(appContextRoot, applicationPath);
    // TODO: Allow the provider list to be customized
    List<Class<?>> providers = Collections.singletonList(JsonBProvider.class);
    LOGGER.info("Building rest client for " + clazz + " with base path: " + basePath + " and providers: " + providers);
    JAXRSClientFactoryBean bean = new org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean();
    bean.setProviders(providers);
    bean.setAddress(basePath);
    if(jwt != null && jwt.length()>0) {
     Map headers = new HashMap();
     headers.put("Authorization", "Bearer " + jwt);
     bean.setHeaders(headers);
    }
    bean.setResourceClass(clazz);
    return bean.create(clazz);        
    //return JAXRSClientFactory.create(basePath, clazz, providers);
}
 
Example 3
Source File: JAXRSServerMetricsTest.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:" + PORT + "/");
    factory.setProvider(JacksonJsonProvider.class);
    
    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 4
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 5
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 6
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 7
Source File: AmbariClientBuilder.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public JAXRSClientFactoryBean load(Class<?> proxyType) throws Exception {
    JAXRSClientFactoryBean clientFactoryBean = new JAXRSClientFactoryBean();
    clientFactoryBean.setResourceClass(proxyType);
    clientFactoryBean.setProvider(new TextJacksonJsonProvider(new ApiObjectMapper()));
    return clientFactoryBean;
}
 
Example 8
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 9
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetStringArray() throws Exception {
    String address = "http://localhost:" + PORT;
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setProvider(new BookStore.StringArrayBodyReaderWriter());
    bean.setAddress(address);
    bean.setResourceClass(BookStore.class);
    BookStore store = bean.create(BookStore.class);
    String[] str = store.getBookStringArray();
    assertEquals("Good book", str[0]);
}
 
Example 10
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetPrimitiveIntArray() throws Exception {
    String address = "http://localhost:" + PORT;
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setProvider(new BookStore.PrimitiveIntArrayReaderWriter());
    bean.setAddress(address);
    bean.setResourceClass(BookStore.class);
    BookStore store = bean.create(BookStore.class);
    int[] arr = store.getBookIndexAsIntArray();
    assertEquals(3, arr.length);
    assertEquals(1, arr[0]);
    assertEquals(2, arr[1]);
    assertEquals(3, arr[2]);
}
 
Example 11
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetPrimitiveDoubleArray() throws Exception {
    String address = "http://localhost:" + PORT;
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setProvider(new BookStore.PrimitiveDoubleArrayReaderWriter());
    bean.setAddress(address);
    bean.setResourceClass(BookStore.class);
    BookStore store = bean.create(BookStore.class);
    double[] arr = store.getBookIndexAsDoubleArray();
    assertEquals(3, arr.length);
    assertEquals(1, arr[0], 0.0);
    assertEquals(2, arr[1], 0.0);
    assertEquals(3, arr[2], 0.0);
}
 
Example 12
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetStringList() throws Exception {
    String address = "http://localhost:" + PORT;
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setProvider(new BookStore.StringListBodyReaderWriter());
    bean.setAddress(address);
    bean.setResourceClass(BookStore.class);
    BookStore store = bean.create(BookStore.class);
    List<String> str = store.getBookListArray();
    assertEquals("Good book", str.get(0));
}
 
Example 13
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyPostProxy2() throws Exception {
    String address = "http://localhost:" + PORT;
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    bean.setResourceClass(BookStore.class);
    BookStore store = bean.create(BookStore.class);
    store.emptypostNoPath();
    assertEquals(204, WebClient.client(store).getResponse().getStatus());
}