All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jslsolucoes.jax.rs.client.se.api.impl.DefaultJaxRsApiClientBuilder Maven / Gradle / Ivy

There is a newer version: 1.0.32
Show newest version
package com.jslsolucoes.jax.rs.client.se.api.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.JerseyClientBuilder;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClient;
import com.jslsolucoes.jax.rs.client.se.api.JaxRsApiClientBuilder;
import com.jslsolucoes.jax.rs.provider.se.JaxRsObjectMapper;
import com.jslsolucoes.jax.rs.provider.se.JsonContentTypeResponseFilter;

public class DefaultJaxRsApiClientBuilder implements JaxRsApiClientBuilder {

    private Integer connectTimeout = 10000;
    private Integer readTimeout = 20000;
    private Boolean debug = Boolean.FALSE;
    private Level level = Level.INFO;
    private List> providerClasses = new ArrayList<>();
    private List providerObjects = new ArrayList<>();

    @Override
    public JaxRsApiClientBuilder debug(Boolean debug) {
	this.debug = debug;
	return this;
    }

    @Override
    public JaxRsApiClientBuilder level(Level level) {
	this.level = level;
	return this;
    }
    
    @Override
    public JaxRsApiClientBuilder register(Class clazz) {
	this.providerClasses.add(clazz);
	return this;
    }
    
    @Override
    public JaxRsApiClientBuilder register(Object object) {
	this.providerObjects.add(object);
	return this;
    }

    @Override
    public JaxRsApiClientBuilder connectTimeout(Integer connectTimeout) {
	this.connectTimeout = connectTimeout;
	return this;
    }

    @Override
    public JaxRsApiClientBuilder readTimeout(Integer readTimeout) {
	this.readTimeout = readTimeout;
	return this;
    }

    @Override
    public JaxRsApiClient build() {
	return new DefaultJaxRsApiClient(JerseyClientBuilder.createClient(jerseyClientConfig()));
    }

    private ClientConfig jerseyClientConfig() {
	ClientConfig clientConfig = new ClientConfig().property(ClientProperties.CONNECT_TIMEOUT, connectTimeout)
		.property(ClientProperties.READ_TIMEOUT, readTimeout).register(new JsonContentTypeResponseFilter())
		.register(new JaxRsObjectMapper())
		.register(MultiPartFeature.class);
	if (debug) {
	    clientConfig = clientConfig
		    .register(new LoggingFeature(java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME),
			    level, LoggingFeature.Verbosity.PAYLOAD_ANY, 10000));
	}
	for(Class provideClass : providerClasses) {
	    clientConfig = clientConfig.register(provideClass);
	}
	for(Object provideObject : providerObjects) {
	    clientConfig = clientConfig.register(provideObject);
	}
	return clientConfig;
    }
}