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

The following examples show how to use org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#setProperties() . 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: JAXRSSamlAuthorizationTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private WebClient createWebClient(String address, Map<String, Object> extraProperties) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSSamlAuthorizationTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);

    Map<String, Object> properties = new HashMap<>();
    properties.put(SecurityConstants.SAML_CALLBACK_HANDLER,
                   "org.apache.cxf.systest.jaxrs.security.saml.SamlCallbackHandler");
    if (extraProperties != null) {
        properties.putAll(extraProperties);
    }
    bean.setProperties(properties);

    bean.getOutInterceptors().add(new SamlEnvelopedOutInterceptor());

    return bean.createWebClient();
}
 
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: 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 4
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 5
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 6
Source File: JAXRSOAuth2Test.java    From cxf with Apache License 2.0 5 votes vote down vote up
private WebClient createWebClientWithProps(String address) {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);

    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSOAuth2Test.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");

    SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler(true);
    samlCallbackHandler.setIssuer("alice");
    String audienceURI = "https://localhost:" + port + "/oauth2-auth/token";
    samlCallbackHandler.setAudience(audienceURI);
    properties.put(SecurityConstants.SAML_CALLBACK_HANDLER, samlCallbackHandler);

    properties.put(SecurityConstants.SIGNATURE_USERNAME, "alice");
    properties.put(SecurityConstants.SIGNATURE_PROPERTIES, CRYPTO_RESOURCE_PROPERTIES);
    bean.setProperties(properties);

    bean.getOutInterceptors().add(new Saml2BearerAuthOutInterceptor());

    WebClient wc = bean.createWebClient();
    wc.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON);
    return wc;
}
 
Example 7
Source File: JAXRSSoapBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoBook357WebClient() throws Exception {

    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    Map<String, Object> properties = new HashMap<>();
    properties.put("org.apache.cxf.http.throw_io_exceptions", Boolean.TRUE);
    bean.setProperties(properties);
    bean.setAddress("http://localhost:" + PORT + "/test/services/rest/bookstore/356");
    WebClient wc = bean.createWebClient();
    Response response = wc.get();
    assertEquals(404, response.getStatus());
    String msg = IOUtils.readStringFromStream((InputStream)response.getEntity());
    assertEquals("No Book with id 356 is available", msg);

}
 
Example 8
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 9
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());
}
 
Example 10
Source File: JAXRSXmlSecTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncryptionNoSignature() 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);

    XmlEncOutInterceptor encInterceptor = new XmlEncOutInterceptor();
    encInterceptor.setKeyIdentifierType(RSSecurityUtils.X509_CERT);
    encInterceptor.setSymmetricEncAlgorithm(XMLCipher.AES_128);
    bean.getOutInterceptors().add(encInterceptor);
    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 Signature");
    } catch (WebApplicationException ex) {
        // expected
    }
}
 
Example 11
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
    }
}
 
Example 12
Source File: JAXRSMultipartTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testXopWebClient() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/xop";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    bean.setProperties(Collections.singletonMap(org.apache.cxf.message.Message.MTOM_ENABLED,
                                                (Object)"true"));
    WebClient client = bean.createWebClient();
    WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor());
    WebClient.getConfig(client).getOutInterceptors().add(new LoggingOutInterceptor());
    WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart",
        "true");
    client.type("multipart/related").accept("multipart/related");

    XopType xop = new XopType();
    xop.setName("xopName");
    InputStream is =
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/book.xsd");
    byte[] data = IOUtils.readBytesFromStream(is);
    xop.setAttachinfo(new DataHandler(new ByteArrayDataSource(data, "application/octet-stream")));
    xop.setAttachInfoRef(new DataHandler(new ByteArrayDataSource(data, "application/octet-stream")));

    String bookXsd = IOUtils.readStringFromStream(getClass().getResourceAsStream(
        "/org/apache/cxf/systest/jaxrs/resources/book.xsd"));
    xop.setAttachinfo2(bookXsd.getBytes());

    xop.setImage(getImage("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));

    XopType xop2 = client.post(xop, XopType.class);

    String bookXsdOriginal = IOUtils.readStringFromStream(getClass().getResourceAsStream(
            "/org/apache/cxf/systest/jaxrs/resources/book.xsd"));
    String bookXsd2 = IOUtils.readStringFromStream(xop2.getAttachinfo().getInputStream());
    assertEquals(bookXsdOriginal, bookXsd2);
    String bookXsdRef = IOUtils.readStringFromStream(xop2.getAttachInfoRef().getInputStream());
    assertEquals(bookXsdOriginal, bookXsdRef);

    String ctString =
        client.getResponse().getMetadata().getFirst("Content-Type").toString();
    MediaType mt = MediaType.valueOf(ctString);
    Map<String, String> params = mt.getParameters();
    assertEquals(4, params.size());
    assertNotNull(params.get("boundary"));
    assertNotNull(params.get("type"));
    assertNotNull(params.get("start"));
    assertNotNull(params.get("start-info"));
}