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

The following examples show how to use org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean#setHeaders() . 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: 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 3
Source File: IdentityManagementEndpointUtil.java    From carbon-identity-framework with Apache License 2.0 5 votes vote down vote up
static JAXRSClientFactoryBean getBean(String baseAddress, String configLocation, Map<String, String> headers) {

        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
        if (configLocation != null) {
            SpringBusFactory bf = new SpringBusFactory();
            Bus bus = bf.createBus(configLocation);
            bean.setBus(bus);
        }
        bean.setAddress(baseAddress);
        if (headers != null && !headers.isEmpty()) {
            bean.setHeaders(headers);
        }
        return bean;
    }
 
Example 4
Source File: AbstractJaxRsClientConfiguration.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected Client createClient() {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setBus(bus);
    bean.setAddress(address);
    bean.setThreadSafe(threadSafe);
    setJaxrsResources(bean);

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

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