org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean Java Examples

The following examples show how to use org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean. 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: 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 #2
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 #3
Source File: WebClientSubstitution.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@Substitute
static JAXRSClientFactoryBean getBean(String baseAddress, String configLocation) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    // configLocation is always null and no need to create SpringBusFactory.
    CXFBusFactory bf = new CXFBusFactory();

    // It can not load the extensions from the bus-extensions.txt dynamically.
    // So have to set all of necessary ones here.
    List<Extension> extensions = new ArrayList<>();
    Extension http = new Extension();
    http.setClassname(HTTPTransportFactory.class.getName());
    http.setDeferred(true);
    extensions.add(http);
    ExtensionRegistry.addExtensions(extensions);

    Bus bus = bf.createBus();
    bus.setExtension(new PhaseManagerImpl(), PhaseManager.class);
    bus.setExtension(new ClientLifeCycleManagerImpl(), ClientLifeCycleManager.class);
    bus.setExtension(new ConduitInitiatorManagerImpl(bus), ConduitInitiatorManager.class);

    bean.setBus(bus);
    bean.setAddress(baseAddress);
    return bean;
}
 
Example #4
Source File: SyncopeClient.java    From syncope with Apache License 2.0 6 votes vote down vote up
public SyncopeClient(
        final MediaType mediaType,
        final JAXRSClientFactoryBean restClientFactory,
        final RestClientExceptionMapper exceptionMapper,
        final AuthenticationHandler handler,
        final boolean useCompression,
        final TLSClientParameters tlsClientParameters) {

    this.mediaType = mediaType;
    this.restClientFactory = restClientFactory;
    if (this.restClientFactory.getHeaders() == null) {
        this.restClientFactory.setHeaders(new HashMap<>());
    }
    this.exceptionMapper = exceptionMapper;
    this.tlsClientParameters = tlsClientParameters;
    init(handler);
    this.useCompression = useCompression;
}
 
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: 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 #8
Source File: JAXRSClientServerNonSpringBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBook123UserModelAuthorize() throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress("http://localhost:" + PORT + "/usermodel/bookstore/books");
    bean.setUsername("Barry");
    bean.setPassword("password");
    bean.setModelRef("classpath:org/apache/cxf/systest/jaxrs/resources/resources.xml");
    WebClient proxy = bean.createWebClient();
    proxy.path("{id}/authorize", 123);

    Book book = proxy.get(Book.class);
    assertEquals(123L, book.getId());



}
 
Example #9
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 #10
Source File: JAXRSSamlTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidSAMLTokenAsHeader() throws Exception {
    String address = "https://localhost:" + PORT + "/samlheader/bookstore/books/123";

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

    WebClient wc = bean.createWebClient();
    wc.header("Authorization", "SAML invalid_grant");
    Response r = wc.get();
    assertEquals(401, r.getStatus());
}
 
Example #11
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 #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
Source File: ClientImpl.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void initTargetClientIfNeeded(Map<String, Object> configProps) {
    URI uri = uriBuilder.build();
    if (targetClient == null) {
        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
        bean.setAddress(uri.toString());
        bean.setProperties(configProps);
        Boolean threadSafe = getBooleanValue(configProps.get(THREAD_SAFE_CLIENT_PROP));
        if (threadSafe == null) {
            threadSafe = DEFAULT_THREAD_SAFETY_CLIENT_STATUS;
        }
        bean.setThreadSafe(threadSafe);
        if (threadSafe) {
            Integer cleanupPeriod = getIntValue(configProps.get(THREAD_SAFE_CLIENT_STATE_CLEANUP_PROP));
            if (cleanupPeriod == null) {
                cleanupPeriod = THREAD_SAFE_CLIENT_STATE_CLEANUP_PERIOD;
            }
            if (cleanupPeriod != null) {
                bean.setSecondsToKeepState(cleanupPeriod);
            }
        }
        targetClient = bean.createWebClient();
        ClientImpl.this.baseClients.add(targetClient);
    } else if (!targetClient.getCurrentURI().equals(uri)) {
        targetClient.to(uri.toString(), false);
    }
}
 
Example #20
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 #21
Source File: JAXRSHttpsBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void doTestGetBook123ProxyFromSpring(String cfgFile) throws Exception {

        ClassPathXmlApplicationContext ctx =
            new ClassPathXmlApplicationContext(new String[] {cfgFile});
        Object bean = ctx.getBean("bookService.proxyFactory");
        assertNotNull(bean);
        JAXRSClientFactoryBean cfb = (JAXRSClientFactoryBean) bean;
        Bus bus = cfb.getBus();
        ClientLifeCycleManager manager = bus.getExtension(ClientLifeCycleManager.class);
        TestClientLifeCycleListener listener = new TestClientLifeCycleListener();
        manager.registerListener(listener);
        BookStore bs = cfb.create(BookStore.class);
        assertNotNull(listener.getEp());
        assertEquals("{http://service.rs}BookService",
                     listener.getEp().getEndpointInfo().getName().toString());
        assertEquals("https://localhost:" + PORT, WebClient.client(bs).getBaseURI().toString());
        Book b = bs.getSecureBook("123");
        assertEquals(b.getId(), 123);
        b = bs.getSecureBook("123");
        assertEquals(b.getId(), 123);
        ctx.close();
    }
 
Example #22
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 #23
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 #24
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 #25
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 #26
Source File: JAXRSHttpsBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomVerbProxyFromSpringWildcard() throws Exception {
    ClassPathXmlApplicationContext ctx =
        new ClassPathXmlApplicationContext(new String[] {CLIENT_CONFIG_FILE3});
    Object bean = ctx.getBean("bookService.proxyFactory");
    assertNotNull(bean);
    JAXRSClientFactoryBean cfb = (JAXRSClientFactoryBean) bean;

    BookStore bs = cfb.create(BookStore.class);
    WebClient.getConfig(bs).getRequestContext().put("use.httpurlconnection.method.reflection", true);
    // CXF RS Client code will set this property to true if the http verb is unknown
    // and this property is not already set. The async conduit is loaded in the tests module
    // but we do want to test HTTPUrlConnection reflection hence we set this property to false
    WebClient.getConfig(bs).getRequestContext().put("use.async.http.conduit", false);

    Book book = bs.retrieveBook(new Book("Retrieve", 123L));
    assertEquals("Retrieve", book.getName());

    ctx.close();
}
 
Example #27
Source File: JAXRSHttpsBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBook123ProxyFromSpringWildcard() throws Exception {
    ClassPathXmlApplicationContext ctx =
        new ClassPathXmlApplicationContext(new String[] {CLIENT_CONFIG_FILE4});
    Object bean = ctx.getBean("bookService.proxyFactory");
    assertNotNull(bean);
    JAXRSClientFactoryBean cfb = (JAXRSClientFactoryBean) bean;

    BookStore bs = cfb.create(BookStore.class);
    assertEquals("https://localhost:" + PORT, WebClient.client(bs).getBaseURI().toString());

    WebClient wc = WebClient.fromClient(WebClient.client(bs));
    assertEquals("https://localhost:" + PORT, WebClient.client(bs).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 #28
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 #29
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 #30
Source File: JAXRSJwsMultipartTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private JAXRSClientFactoryBean createJAXRSClientFactoryBean(String address,
                                                            boolean bufferPayload,
                                                            boolean useJwsJsonSignatureFormat) throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSJwsMultipartTest.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<>();
    JwsMultipartClientRequestFilter outFilter = new JwsMultipartClientRequestFilter();
    outFilter.setUseJwsJsonSignatureFormat(useJwsJsonSignatureFormat);
    providers.add(outFilter);
    JwsMultipartClientResponseFilter inFilter = new JwsMultipartClientResponseFilter();
    inFilter.setBufferPayload(bufferPayload);
    providers.add(inFilter);
    providers.add(new JwsDetachedSignatureProvider());
    bean.setProviders(providers);
    return bean;
}