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

The following examples show how to use org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#setProvider() . 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: 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 2
Source File: JAXRSJweJsonTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private BookStore createBookStoreTwoRecipients(String address) throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSJweJsonTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    bean.setServiceClass(BookStore.class);
    bean.setAddress(address);
    bean.setProvider(new JweJsonWriterInterceptor());

    List<String> properties = new ArrayList<>();
    properties.add("org/apache/cxf/systest/jaxrs/security/jwejson1.properties");
    properties.add("org/apache/cxf/systest/jaxrs/security/jwejson2.properties");
    bean.getProperties(true).put(JoseConstants.RSSEC_ENCRYPTION_PROPS,
                             properties);
    return bean.create(BookStore.class);
}
 
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 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 6
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 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: JAXRSSamlTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private WebClient createWebClient(String address,
                                  Interceptor<Message> outInterceptor,
                                  Object provider,
                                  CallbackHandler samlCallbackHandler) {
    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);

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

    bean.getOutInterceptors().add(outInterceptor);
    if (provider != null) {
        bean.setProvider(provider);
    }
    return bean.createWebClient();
}
 
Example 9
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 10
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 11
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 12
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 13
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 14
Source File: AbstractJaxRsClientConfiguration.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Client createClient() {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setBus(bus);
    bean.setAddress(address);
    bean.setThreadSafe(threadSafe);
    setJaxrsResources(bean);

    for (String beanName : context.getBeanDefinitionNames()) {
        if (context.findAnnotationOnBean(beanName, Provider.class) != null) {
            bean.setProvider(context.getBean(beanName));
        } else if (context.findAnnotationOnBean(beanName, org.apache.cxf.annotations.Provider.class) != null) {
            addCxfProvider(bean, context.getBean(beanName));
        }
    }

    Map<String, String> extraHeaders = new HashMap<>();
    if (!StringUtils.isEmpty(accept)) {
        extraHeaders.put("Accept", accept);
    }
    if (!StringUtils.isEmpty(contentType)) {
        extraHeaders.put("Content-Type", contentType);
    }
    if (!extraHeaders.isEmpty()) {
        bean.setHeaders(extraHeaders);
    }
    return bean.create();
}
 
Example 15
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());
}