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

The following examples show how to use org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#setServiceClass() . 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: 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 2
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 3
Source File: JaxRsProxyClientConfiguration.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected void setJaxrsResources(JAXRSClientFactoryBean factory) {
    Class<?> serviceClass = getServiceClass();
    if (serviceClass != null) {
        factory.setServiceClass(serviceClass);
    } else if (!StringUtils.isEmpty(scanPackages)) {
        try {
            final Map< Class< ? extends Annotation >, Collection< Class< ? > > > classes =
                ClasspathScanner.findClasses(scanPackages, Path.class, Provider.class);
            factory.setServiceClass(
                JAXRSClientFactoryBeanDefinitionParser.getServiceClass(classes.get(Path.class)));
            factory.setProviders(
                JAXRSClientFactoryBeanDefinitionParser.getProviders(context, classes.get(Provider.class)));
        } catch (Exception ex) {
            throw new ServiceConstructionException(ex);
        }
    }
}
 
Example 4
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 5
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testJweJwkAesWrap() throws Exception {
    String address = "https://localhost:" + PORT + "/jwejwkaeswrap";
    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",
                                 "org/apache/cxf/systest/jaxrs/security/secret.jwk.properties");
    bean.getProperties(true).put("jose.debug", true);
    BookStore bs = bean.create(BookStore.class);
    String text = bs.echoText("book");
    assertEquals("book", text);
}
 
Example 6
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private BookStore createJweBookStore(String address,
                                  List<?> mbProviders) 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<>();
    JweWriterInterceptor jweWriter = new JweWriterInterceptor();
    jweWriter.setUseJweOutputStream(true);
    providers.add(jweWriter);
    providers.add(new JweClientResponseFilter());
    if (mbProviders != null) {
        providers.addAll(mbProviders);
    }
    bean.setProviders(providers);
    bean.getProperties(true).put("rs.security.encryption.out.properties",
                                 "org/apache/cxf/systest/jaxrs/security/bob.jwk.properties");
    bean.getProperties(true).put("rs.security.encryption.in.properties",
                                 "org/apache/cxf/systest/jaxrs/security/alice.jwk.properties");
    return bean.create(BookStore.class);
}
 
Example 7
Source File: JaxRsWebClientConfiguration.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Override
protected void setJaxrsResources(JAXRSClientFactoryBean factory) {
    factory.setServiceClass(WebClient.class);

    if (!StringUtils.isEmpty(scanPackages)) {
        try {
            final Map< Class< ? extends Annotation >, Collection< Class< ? > > > classes =
                ClasspathScanner.findClasses(scanPackages, Provider.class);

            factory.setProviders(
                JAXRSClientFactoryBeanDefinitionParser.getProviders(context, classes.get(Provider.class)));
        } catch (Exception ex) {
            throw new ServiceConstructionException(ex);
        }
    }
}
 
Example 8
Source File: JAXRSJweJsonTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private BookStore createBookStore(String address, String propLoc) 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);
    List<Object> providers = new LinkedList<>();
    JweJsonWriterInterceptor writer = new JweJsonWriterInterceptor();
    providers.add(writer);
    providers.add(new JweJsonClientResponseFilter());
    bean.setProviders(providers);
    bean.getProperties(true).put(JoseConstants.RSSEC_ENCRYPTION_PROPS,
                                 propLoc);
    return bean.create(BookStore.class);
}
 
Example 9
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 10
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 11
Source File: CXFITest.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) {
  System.setProperty("sa.integration.cxf.interceptors.client.in", ClientSpanTagInterceptor.class.getName());
  System.setProperty("sa.integration.cxf.interceptors.server.out", ServerSpanTagInterceptor.class.getName());
  System.setProperty("sa.integration.cxf.interceptors.classpath", "taget/test-classes");

  final String msg = "hello";

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

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

  echo.echo(msg);

  // CXF Tracing span has no "component" tag, cannot use TestUtil.checkSpan()
  checkSpans(2);
  checkTag();

  server.destroy();
  serverFactory.getBus().shutdown(true);
}
 
Example 12
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testBookWithSpaceProxyPathUrlEncodedSemicolonOnly() throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setServiceClass(BookStore.class);
    bean.setAddress("http://localhost:" + PORT);
    bean.getProperties(true).put("url.encode.client.parameters", "true");
    bean.getProperties(true).put("url.encode.client.parameters.list", ";");
    BookStore store = bean.create(BookStore.class);
    Book book = store.getBookWithSemicolon("123;:", "custom;:header");
    assertEquals(123L, book.getId());
    assertEquals("CXF in Action%3B:", book.getName());
}
 
Example 13
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 14
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testJweRsaJwsRsaEncryptThenSign() throws Exception {
    String address = "https://localhost:" + PORT + "/jwejwsrsaencrsign";

    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 EncrSignJweWriterInterceptor();
    jweWriter.setUseJweOutputStream(true);
    providers.add(jweWriter);
    JwsWriterInterceptor jwsWriter = new EncrSignJwsWriterInterceptor();
    jwsWriter.setUseJwsOutputStream(true);
    providers.add(jwsWriter);
    bean.setProviders(providers);
    bean.getProperties(true).put("rs.security.encryption.out.properties", SERVER_JWEJWS_PROPERTIES);
    bean.getProperties(true).put("rs.security.signature.out.properties", CLIENT_JWEJWS_PROPERTIES);
    PrivateKeyPasswordProvider provider = new PrivateKeyPasswordProviderImpl();
    bean.getProperties(true).put("rs.security.signature.key.password.provider", provider);
    BookStore bs = bean.create(BookStore.class);
    String text = bs.echoText("book");
    assertEquals("book", text);
}
 
Example 15
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testJweRsaJwsRsaCert() throws Exception {
    String address = "https://localhost:" + PORT + "/jwejwsrsacert";

    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());
    JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor();
    jwsWriter.setUseJwsOutputStream(true);
    providers.add(jwsWriter);
    providers.add(new JwsClientResponseFilter());

    bean.setProviders(providers);
    bean.getProperties(true).put("rs.security.keystore.file",
                                 "org/apache/cxf/systest/jaxrs/security/certs/jwkPublicSet.txt");
    bean.getProperties(true).put("rs.security.signature.out.properties", CLIENT_JWEJWS_PROPERTIES);
    bean.getProperties(true).put("rs.security.encryption.in.properties", CLIENT_JWEJWS_PROPERTIES);
    PrivateKeyPasswordProvider provider = new PrivateKeyPasswordProviderImpl();
    bean.getProperties(true).put("rs.security.signature.key.password.provider", provider);
    bean.getProperties(true).put("rs.security.decryption.key.password.provider", provider);
    BookStore bs = bean.create(BookStore.class);

    WebClient.getConfig(bs).getRequestContext().put("rs.security.keystore.alias.jwe.out", "AliceCert");
    WebClient.getConfig(bs).getRequestContext().put("rs.security.keystore.alias.jws.in", "AliceCert");
    String text = bs.echoText("book");
    assertEquals("book", text);
}
 
Example 16
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doTestJwsJwkRSA(String address,
                             boolean includePublicKey,
                             boolean includeKeyId) 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.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/alice.jwk.properties");
    bean.getProperties(true).put("rs.security.signature.in.properties",
        "org/apache/cxf/systest/jaxrs/security/bob.jwk.properties");
    if (includePublicKey) {
        bean.getProperties(true).put("rs.security.signature.include.public.key", true);
    }
    if (includeKeyId) {
        bean.getProperties(true).put("rs.security.signature.include.key.id", true);
    }
    BookStore bs = bean.create(BookStore.class);
    String text = bs.echoText("book");
    assertEquals("book", text);
}
 
Example 17
Source File: JAXRSJweJwsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private BookStore createJweJwsBookStore(String address,
                             JwsSignatureProvider jwsSigProvider,
                             List<?> mbProviders) 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<>();
    JweWriterInterceptor jweWriter = new JweWriterInterceptor();
    jweWriter.setUseJweOutputStream(true);
    providers.add(jweWriter);
    providers.add(new JweClientResponseFilter());
    JwsWriterInterceptor jwsWriter = new JwsWriterInterceptor();
    if (jwsSigProvider != null) {
        jwsWriter.setSignatureProvider(jwsSigProvider);
    }
    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.encryption.out.properties", SERVER_JWEJWS_PROPERTIES);
    bean.getProperties(true).put("rs.security.signature.out.properties", CLIENT_JWEJWS_PROPERTIES);
    bean.getProperties(true).put("rs.security.encryption.in.properties", CLIENT_JWEJWS_PROPERTIES);
    bean.getProperties(true).put("rs.security.signature.in.properties", SERVER_JWEJWS_PROPERTIES);
    PrivateKeyPasswordProvider provider = new PrivateKeyPasswordProviderImpl();
    bean.getProperties(true).put("rs.security.signature.key.password.provider", provider);
    bean.getProperties(true).put("rs.security.decryption.key.password.provider", provider);
    return bean.create(BookStore.class);
}
 
Example 18
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 19
Source File: AbstractFailoverTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected BookStore getBookStore(String address,
                                 FailoverFeature feature) throws Exception {
    JAXRSClientFactoryBean bean = createBean(address, feature);
    bean.setServiceClass(BookStore.class);
    return bean.create(BookStore.class);
}
 
Example 20
Source File: LoadDistributorTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected BookStore getBookStore(String address,
                                 FailoverFeature feature) throws Exception {
    JAXRSClientFactoryBean bean = createBean(address, feature);
    bean.setServiceClass(BookStore.class);
    return bean.create(BookStore.class);
}