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

The following examples show how to use org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#create() . 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: JAXRSHttpsBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore("Works in the studio only if local jaxrs.xsd is updated to have jaxrs:client")
public void testGetBook123WebClientFromSpringWildcardOldJaxrsClient() throws Exception {
    ClassPathXmlApplicationContext ctx =
        new ClassPathXmlApplicationContext(new String[] {CLIENT_CONFIG_FILE_OLD});
    Object bean = ctx.getBean("bookService.proxyFactory");
    assertNotNull(bean);
    JAXRSClientFactoryBean cfb = (JAXRSClientFactoryBean) bean;

    WebClient wc = (WebClient)cfb.create();
    assertEquals("https://localhost:" + PORT, wc.getBaseURI().toString());

    wc.accept("application/xml");
    wc.path("bookstore/securebooks/123");
    TheBook b = wc.get(TheBook.class);

    assertEquals(b.getId(), 123);
    b = wc.get(TheBook.class);
    assertEquals(b.getId(), 123);
    ctx.close();
}
 
Example 3
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 4
Source File: JAXRSJwsJsonTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private BookStore createBookStore(String address,
                                  Map<String, Object> mapProperties,
                                  List<?> extraProviders,
                                  boolean encodePayload) throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSJwsJsonTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    bean.setServiceClass(BookStore.class);
    bean.setAddress(address);
    List<Object> providers = new LinkedList<>();
    JwsJsonWriterInterceptor writer = new JwsJsonWriterInterceptor();
    writer.setUseJwsJsonOutputStream(true);
    writer.setEncodePayload(encodePayload);
    providers.add(writer);
    providers.add(new JwsJsonClientResponseFilter());
    if (extraProviders != null) {
        providers.addAll(extraProviders);
    }
    bean.setProviders(providers);
    bean.getProperties(true).putAll(mapProperties);
    return bean.create(BookStore.class);
}
 
Example 5
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 6
Source File: JAXRSXmlSecTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testPostBookWithNoSig() throws Exception {
    if (test.streaming) {
        // Only testing the endpoints, not the clients here
        return;
    }
    String address = "https://localhost:" + test.port + "/xmlsig";

    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);

    bean.setServiceClass(BookStore.class);

    BookStore store = bean.create(BookStore.class);
    try {
        store.addBook(new Book("CXF", 126L));
        fail("Failure expected on no Signature");
    } catch (WebApplicationException ex) {
        // expected
    }
}
 
Example 7
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 8
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testJwsJwkEC() throws Exception {
    String address = "https://localhost:" + PORT + "/jwsjwkec";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSJweJwsTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    bean.setServiceClass(BookStore.class);
    bean.setAddress(address);
    List<Object> providers = new LinkedList<>();
    JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor();
    jwsWriter.setUseJwsOutputStream(true);
    providers.add(jwsWriter);
    providers.add(new JwsClientResponseFilter());
    bean.setProviders(providers);
    bean.getProperties(true).put("rs.security.signature.out.properties",
        "org/apache/cxf/systest/jaxrs/security/jws.ec.private.properties");
    bean.getProperties(true).put("rs.security.signature.in.properties",
        "org/apache/cxf/systest/jaxrs/security/jws.ec.public.properties");
    BookStore bs = bean.create(BookStore.class);
    String text = bs.echoText("book");
    assertEquals("book", text);
}
 
Example 9
Source File: JAXRSHttpsBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBook123WebClientFromSpringWildcard() throws Exception {
    ClassPathXmlApplicationContext ctx =
        new ClassPathXmlApplicationContext(new String[] {CLIENT_CONFIG_FILE5});
    Object bean = ctx.getBean("bookService.proxyFactory");
    assertNotNull(bean);
    JAXRSClientFactoryBean cfb = (JAXRSClientFactoryBean) bean;

    WebClient wc = (WebClient)cfb.create();
    assertEquals("https://localhost:" + PORT, wc.getBaseURI().toString());

    wc.accept("application/xml");
    wc.path("bookstore/securebooks/123");
    TheBook b = wc.get(TheBook.class);

    assertEquals(b.getId(), 123);
    b = wc.get(TheBook.class);
    assertEquals(b.getId(), 123);
    ctx.close();
}
 
Example 10
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private BookStore createJwsBookStore(String address,
                                     List<?> mbProviders,
                                     boolean encodePayload,
                                     boolean protectHttpHeaders) throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSJweJwsTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    bean.setServiceClass(BookStore.class);
    bean.setAddress(address);
    List<Object> providers = new LinkedList<>();
    JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor();
    jwsWriter.setProtectHttpHeaders(protectHttpHeaders);
    jwsWriter.setEncodePayload(encodePayload);
    jwsWriter.setUseJwsOutputStream(true);
    providers.add(jwsWriter);
    providers.add(new JwsClientResponseFilter());
    if (mbProviders != null) {
        providers.addAll(mbProviders);
    }
    bean.setProviders(providers);
    bean.getProperties(true).put("rs.security.signature.properties",
        "org/apache/cxf/systest/jaxrs/security/secret.jwk.properties");
    return bean.create(BookStore.class);
}
 
Example 11
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void doTestJweJwkAesCbcHMac(String propFile) throws Exception {
    String address = "https://localhost:" + PORT + "/jwejwkaescbchmac";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSJweJwsTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    bean.setServiceClass(BookStore.class);
    bean.setAddress(address);
    List<Object> providers = new LinkedList<>();
    JweWriterInterceptor jweWriter = new JweWriterInterceptor();
    jweWriter.setUseJweOutputStream(true);
    providers.add(jweWriter);
    providers.add(new JweClientResponseFilter());
    bean.setProviders(providers);
    bean.getProperties(true).put("rs.security.encryption.properties", propFile);
    PrivateKeyPasswordProvider provider =
        new PrivateKeyPasswordProviderImpl("Thus from my lips, by yours, my sin is purged.");
    bean.getProperties(true).put("rs.security.key.password.provider", provider);
    BookStore bs = bean.create(BookStore.class);
    String text = bs.echoText("book");
    assertEquals("book", text);
}
 
Example 12
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testJweAesCbcHmac() throws Exception {
    String address = "https://localhost:" + PORT + "/jweaescbchmac";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSJweJwsTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    bean.setServiceClass(BookStore.class);
    bean.setAddress(address);
    List<Object> providers = new LinkedList<>();
    // writer
    JweWriterInterceptor jweWriter = new JweWriterInterceptor();
    jweWriter.setUseJweOutputStream(true);

    final String cekEncryptionKey = "GawgguFyGrWKav7AX4VKUg";
    AesWrapKeyEncryptionAlgorithm keyEncryption =
        new AesWrapKeyEncryptionAlgorithm(cekEncryptionKey, KeyAlgorithm.A128KW);
    jweWriter.setEncryptionProvider(new AesCbcHmacJweEncryption(ContentAlgorithm.A128CBC_HS256,
                                                                keyEncryption));

    // reader
    JweClientResponseFilter jweReader = new JweClientResponseFilter();
    jweReader.setDecryptionProvider(new AesCbcHmacJweDecryption(
                                new AesWrapKeyDecryptionAlgorithm(cekEncryptionKey)));

    providers.add(jweWriter);
    providers.add(jweReader);
    bean.setProviders(providers);

    BookStore bs = bean.create(BookStore.class);
    String text = bs.echoText("book");
    assertEquals("book", text);
}
 
Example 13
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 14
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 15
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testJweAesGcmDirect() throws Exception {
    String address = "https://localhost:" + PORT + "/jweaesgcmdirect";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSJweJwsTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    bean.setServiceClass(BookStore.class);
    bean.setAddress(address);
    List<Object> providers = new LinkedList<>();
    // writer
    JweWriterInterceptor jweWriter = new JweWriterInterceptor();
    jweWriter.setUseJweOutputStream(true);
    // reader
    JweClientResponseFilter jweReader = new JweClientResponseFilter();

    providers.add(jweWriter);
    providers.add(jweReader);
    bean.setProviders(providers);

    bean.getProperties(true).put("rs.security.encryption.properties",
                                 "org/apache/cxf/systest/jaxrs/security/jwe.direct.properties");

    BookStore bs = bean.create(BookStore.class);
    String text = bs.echoText("book");
    assertEquals("book", text);
}
 
Example 16
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testBookWithSpaceProxyPathUrlEncoded() throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setServiceClass(BookStore.class);
    bean.setAddress("http://localhost:" + PORT);
    bean.setProperties(Collections.<String, Object>singletonMap("url.encode.client.parameters", Boolean.TRUE));
    BookStore store = bean.create(BookStore.class);
    Book book = store.getBookWithSemicolon("123;:", "custom;:header");
    assertEquals(123L, book.getId());
    assertEquals("CXF in Action%3B%3A", book.getName());
}
 
Example 17
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());
}
 
Example 18
Source File: CxfTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Test
public void testRs(final MockTracer tracer) {
  System.setProperty(Configuration.INTERCEPTORS_CLIENT_IN, ClientSpanTagInterceptor.class.getName());
  System.setProperty(Configuration.INTERCEPTORS_SERVER_OUT, ServerSpanTagInterceptor.class.getName());
  final String msg = "hello";

  // prepare server
  final JAXRSServerFactoryBean serverFactory = new JAXRSServerFactoryBean();
  serverFactory.setAddress(BASE_URI);
  serverFactory.setServiceBean(new EchoImpl());
  final Server server = serverFactory.create();

  // prepare client
  final JAXRSClientFactoryBean clientFactory = new JAXRSClientFactoryBean();
  clientFactory.setServiceClass(Echo.class);
  clientFactory.setAddress(BASE_URI);
  final Echo echo = clientFactory.create(Echo.class);

  final String response = echo.echo(msg);

  assertEquals(msg, response);
  assertEquals(2, tracer.finishedSpans().size());

  final WebClient client = WebClient.create(BASE_URI);
  final String result = client.post(msg, String.class);

  assertEquals(msg, result);
  assertEquals(4, tracer.finishedSpans().size());

  final List<MockSpan> spans = tracer.finishedSpans();
  for (final MockSpan span : spans) {
    assertEquals(AbstractSpanTagInterceptor.SPAN_TAG_VALUE, span.tags().get(AbstractSpanTagInterceptor.SPAN_TAG_KEY));
  }

  server.destroy();
  serverFactory.getBus().shutdown(true);
}
 
Example 19
Source File: JAXRSJwsMultipartTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private BookStore createJwsBookStoreRSA(String address) throws Exception {
    JAXRSClientFactoryBean bean = createJAXRSClientFactoryBean(address, false, false);
    bean.getProperties(true).put("rs.security.signature.properties",
        "org/apache/cxf/systest/jaxrs/security/alice.jwk.properties");
    return bean.create(BookStore.class);
}
 
Example 20
Source File: JAXRSXmlSecTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testSignatureNoEncryption() throws Exception {
    if (test.streaming) {
        // Only testing the endpoints, not the clients here
        return;
    }
    String address = "https://localhost:" + test.port + "/xmlsec-validate";

    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.ENCRYPT_USERNAME, "bob");
    properties.put(SecurityConstants.ENCRYPT_PROPERTIES,
                   "org/apache/cxf/systest/jaxrs/security/bob.properties");
    properties.put(SecurityConstants.SIGNATURE_PROPERTIES,
                   "org/apache/cxf/systest/jaxrs/security/alice.properties");
    bean.setProperties(properties);

    XmlSigOutInterceptor sigInterceptor = new XmlSigOutInterceptor();
    bean.getOutInterceptors().add(sigInterceptor);
    bean.getInInterceptors().add(new XmlEncInInterceptor());
    bean.getInInterceptors().add(new XmlSigInInterceptor());

    bean.setServiceClass(BookStore.class);

    BookStore store = bean.create(BookStore.class);
    try {
        store.addBook(new Book("CXF", 126L));
        fail("Failure expected on no Encryption");
    } catch (WebApplicationException ex) {
        // expected
    }
}