org.codegist.crest
Class CRestBuilder

java.lang.Object
  extended by org.codegist.crest.CRestBuilder

public class CRestBuilder
extends Object

The default build :

 CRest crest = new CRestBuilder().build();
 

will create CRest with the following features :

- Annotation driven configuration handled by AnnotationDrivenInterfaceConfigFactory, lookup for annotation in package org.codegist.crest.annotate.

- Raw response return, meaning the given interface method return type must be either java.io.String, java.io.InputStream or java.io.Reader.

- HTTP calls handled by DefaultRestService

- Uses JDK dynamics proxies to instanciates given interfaces

This default configuration has the benefit to not require any third party dependencies, but is not the recommanded one.

For best performances, it is recommended to use the CGLib proxy factory, CglibProxyFactory (requires cglib available in the classpath) and the apache http client backed rest service HttpClientRestService, see useHttpClientRestService().

Author:
Laurent Gilles (laurent.gilles@codegist.org)
See Also:
AnnotationDrivenInterfaceConfigFactory, PropertiesDrivenInterfaceConfigFactory, DefaultRestService, HttpClientRestService, CglibProxyFactory, JdkProxyFactory, DefaultCRest

Constructor Summary
CRestBuilder()
           
 
Method Summary
 CRestBuilder addProperties(Map<String,Object> customProperties)
          Adds all custom properties every services build with the resulting CRest instance will be passed.
 CRest build()
           
 CRestBuilder expectsJson()
          Resulting CRest instance will create interface instances that will auto marshall the response from JSON to user object model.
 CRestBuilder expectsXml(Class<?> factory)
          Resulting CRest instance will create interface instances that will auto marshall the response from XML to user object model.
 CRestBuilder expectsXml(String modelPackageName)
          Resulting CRest instance will create interface instances that will auto marshall the response from XML to user object model.
 CRestBuilder overrideDefaultConfigWith(Document document)
          Resulting CRest instance will overrides any configuration resulting from its current InterfaceConfigFactory with the given xml configuration.
 CRestBuilder overrideDefaultConfigWith(InterfaceConfigFactory overridesFactory)
          Resulting CRest instance will overrides any configuration resulting from its internal InterfaceConfigFactory with the configuration issued by the given overridesFactory.
 CRestBuilder overrideDefaultConfigWith(Map<String,String> props)
          Resulting CRest instance will overrides any configuration resulting from its current InterfaceConfigFactory with the given properties.
 CRestBuilder returnRawResults()
          Resulting CRest instance will create interface instances that will return raw response.
 CRestBuilder setBooleanSerializer(String trueSerialized, String falseSerialized)
          Sets how boolean should be serialized.
 CRestBuilder setDateSerializerFormat(String format)
          Sets date serializer format to the given format.
 CRestBuilder setListSerializerSeparator(String sep)
          Sets the list separator for the list serializer
 CRestBuilder setProperties(Map<String,Object> customProperties)
          Sets a custom properties every services build with the resulting CRest instance will be passed.
 CRestBuilder setProperty(String name, Object value)
          Sets a custom property every services build with the resulting CRest instance will be passed.
 CRestBuilder setRestService(RestService restService)
          Overrides the rest service every services build with the resulting CRest instance will use.
 CRestBuilder setSerializer(Type type, Serializer serializer)
          Sets a custom serializer for the given type the resulting CRest instance will use to serialize method arguments.
 CRestBuilder useCglibProxies()
          Resulting CRest instance will use cglib proxies to build interface instances.
 CRestBuilder useHttpClientRestService()
          Resulting CRest instance's RestService will be a single threaded instance of HttpClientRestService.
 CRestBuilder useHttpClientRestService(int maxConnections)
          Resulting CRest instance's RestService will be a multi-threaded instance of HttpClientRestService.
 CRestBuilder useHttpClientRestService(int maxConnections, int maxConnectionsPerRoute)
          Resulting CRest instance's RestService will be a multi-threaded instance of HttpClientRestService.
 CRestBuilder useJdkProxies()
          Resulting CRest instance will use native jdk proxies to build interface instances.
 CRestBuilder usePreauthentifiedOAuth(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret)
          Authentification parameters are added to the request headers.
 CRestBuilder usePreauthentifiedOAuth(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret, boolean authParamsInHeaders)
          Resulting CRest instance will authentify every requests using OAuth (http://oauth.net/) authentification mechanism, using a pre-authentified access token and consumer information.
 CRestBuilder withAnnotatedConfig()
          Resulting CRest instance will handle annotated configurated interfaces.
 CRestBuilder withPropertiesConfig(Map<String,String> props)
          Resulting CRest instance will handle properties based configuration.
 CRestBuilder withXmlConfig(Document document)
          Resulting CRest instance will handle xml based configuration.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CRestBuilder

public CRestBuilder()
Method Detail

build

public CRest build()

useHttpClientRestService

public CRestBuilder useHttpClientRestService()
Resulting CRest instance's RestService will be a single threaded instance of HttpClientRestService.

Returns:
current builder
See Also:
HttpClientRestService

useHttpClientRestService

public CRestBuilder useHttpClientRestService(int maxConnections)
Resulting CRest instance's RestService will be a multi-threaded instance of HttpClientRestService.

Parameters:
maxConnections - max concurrent connections (includes max connection per route as well)
Returns:
current builder
See Also:
HttpClientRestService

useHttpClientRestService

public CRestBuilder useHttpClientRestService(int maxConnections,
                                             int maxConnectionsPerRoute)
Resulting CRest instance's RestService will be a multi-threaded instance of HttpClientRestService.

Parameters:
maxConnections - max concurrent connections
maxConnectionsPerRoute - max connection per route
Returns:
current builder
See Also:
HttpClientRestService

setRestService

public CRestBuilder setRestService(RestService restService)
Overrides the rest service every services build with the resulting CRest instance will use.

Parameters:
restService - rest service instance
Returns:
current builder

setProperty

public CRestBuilder setProperty(String name,
                                Object value)
Sets a custom property every services build with the resulting CRest instance will be passed.

Parameters:
name - property key
value - property value
Returns:
current builder
See Also:
InterfaceContext.getProperties()

setSerializer

public CRestBuilder setSerializer(Type type,
                                  Serializer serializer)
Sets a custom serializer for the given type the resulting CRest instance will use to serialize method arguments.

The given type reflects the given Interface type, polymorphism is not considered.

Parameters:
type - Type to seralize
serializer - Serializer
Returns:
current builder
See Also:
InterfaceContext.getProperties()

addProperties

public CRestBuilder addProperties(Map<String,Object> customProperties)
Adds all custom properties every services build with the resulting CRest instance will be passed.

Parameters:
customProperties - properties map
Returns:
current builder
See Also:
InterfaceContext.getProperties()

setProperties

public CRestBuilder setProperties(Map<String,Object> customProperties)
Sets a custom properties every services build with the resulting CRest instance will be passed.

Parameters:
customProperties - properties map
Returns:
current builder
See Also:
InterfaceContext.getProperties()

withAnnotatedConfig

public CRestBuilder withAnnotatedConfig()
Resulting CRest instance will handle annotated configurated interfaces.

Returns:
current builder
See Also:
AnnotationDrivenInterfaceConfigFactory

withPropertiesConfig

public CRestBuilder withPropertiesConfig(Map<String,String> props)
Resulting CRest instance will handle properties based configuration.

Given properties must be able to configure any possible interface given to the resulting CRest instance.

Parameters:
props - configuration properties
Returns:
current builder

withXmlConfig

public CRestBuilder withXmlConfig(Document document)
Resulting CRest instance will handle xml based configuration.

Given xml must be able to configure any possible interface given to the resulting CRest instance.

Parameters:
document - xml configuration document
Returns:
current builder

overrideDefaultConfigWith

public CRestBuilder overrideDefaultConfigWith(Map<String,String> props)
Resulting CRest instance will overrides any configuration resulting from its current InterfaceConfigFactory with the given properties.

Properties must be formatted as documentated in PropertiesDrivenInterfaceConfigFactory

Can be used for instance to override the server end-point for differents devs environment.

Parameters:
props - properties
Returns:
current builder
See Also:
PropertiesDrivenInterfaceConfigFactory

overrideDefaultConfigWith

public CRestBuilder overrideDefaultConfigWith(Document document)
Resulting CRest instance will overrides any configuration resulting from its current InterfaceConfigFactory with the given xml configuration.

Document must be formatted as documentated in XmlDrivenInterfaceConfigFactory

Can be used for instance to override the server end-point for differents devs environment.

Parameters:
document - xml configuration
Returns:
current builder
See Also:
XmlDrivenInterfaceConfigFactory

overrideDefaultConfigWith

public CRestBuilder overrideDefaultConfigWith(InterfaceConfigFactory overridesFactory)
Resulting CRest instance will overrides any configuration resulting from its internal InterfaceConfigFactory with the configuration issued by the given overridesFactory.

This factory is meant to returns template configs, thus can return configuration with null values that will be interpreted as fallbacking to the current InterfaceConfigFactory.

Parameters:
overridesFactory - config overrider factory
Returns:
current builder

expectsJson

public CRestBuilder expectsJson()
Resulting CRest instance will create interface instances that will auto marshall the response from JSON to user object model.

Interfaces given to the CRest instance can return any object type as long as the marshaller can unmarshall them. (requires jackson available in the classpath)

Returns:
current builder
See Also:
JacksonMarshaller

returnRawResults

public CRestBuilder returnRawResults()
Resulting CRest instance will create interface instances that will return raw response.

Given interface methods return types must be either java.lang.String, java.io.Reader or java.io.InputStream

Returns:
current builder

expectsXml

public CRestBuilder expectsXml(Class<?> factory)
Resulting CRest instance will create interface instances that will auto marshall the response from XML to user object model.

Interface given to the CRest instance can return any object type as long as the marshaller can unmarshall them.

Parameters:
factory - The JAXb user object model factory class
Returns:
current builder
See Also:
JaxbMarshaller

expectsXml

public CRestBuilder expectsXml(String modelPackageName)
Resulting CRest instance will create interface instances that will auto marshall the response from XML to user object model.

Interface given to the CRest instance can return any object type as long as the marshaller can unmarshall them.

Parameters:
modelPackageName - The package name where the user object model is located
Returns:
current builder
See Also:
JaxbMarshaller

useJdkProxies

public CRestBuilder useJdkProxies()
Resulting CRest instance will use native jdk proxies to build interface instances.

Returns:
current builder
See Also:
JdkProxyFactory

useCglibProxies

public CRestBuilder useCglibProxies()
Resulting CRest instance will use cglib proxies to build interface instances. (requires cglib available in the classpath)

Returns:
current builder
See Also:
CglibProxyFactory

usePreauthentifiedOAuth

public CRestBuilder usePreauthentifiedOAuth(String consumerKey,
                                            String consumerSecret,
                                            String accessToken,
                                            String accessTokenSecret)

Authentification parameters are added to the request headers.

See usePreauthentifiedOAuth(String, String, String, String, boolean)

Parameters:
consumerKey - Consumer key
consumerSecret - Consumer secret
accessToken - Preauthentified access token
accessTokenSecret - Preauthentified access token secret
Returns:
current builder
See Also:
usePreauthentifiedOAuth(String, String, String, String, boolean)

usePreauthentifiedOAuth

public CRestBuilder usePreauthentifiedOAuth(String consumerKey,
                                            String consumerSecret,
                                            String accessToken,
                                            String accessTokenSecret,
                                            boolean authParamsInHeaders)
Resulting CRest instance will authentify every requests using OAuth (http://oauth.net/) authentification mechanism, using a pre-authentified access token and consumer information.

Parameters:
consumerKey - Consumer key
consumerSecret - Consumer secret
accessToken - Preauthentified access token
accessTokenSecret - Preauthentified access token secret
authParamsInHeaders - If true, adds the authentification information into the request headers, otherwise in the query string
Returns:
current builder

setDateSerializerFormat

public CRestBuilder setDateSerializerFormat(String format)
Sets date serializer format to the given format.

Shortcut to builder.setProperty(CRestProperty.SERIALIZER_DATE_FORMAT, format)

Parameters:
format - Date format to use
Returns:
current builder
See Also:
CRestProperty.SERIALIZER_DATE_FORMAT

setBooleanSerializer

public CRestBuilder setBooleanSerializer(String trueSerialized,
                                         String falseSerialized)
Sets how boolean should be serialized.

Shortcut to:

builder.setProperty(CRestProperty.SERIALIZER_BOOLEAN_TRUE, trueSerialized)

builder.setProperty(CRestProperty.SERIALIZER_BOOLEAN_FALSE, falseSerialized)

Parameters:
trueSerialized - String representing serialized form of TRUE
falseSerialized - String representing serialized form of FALSE
Returns:
current builder
See Also:
CRestProperty.SERIALIZER_BOOLEAN_TRUE, CRestProperty.SERIALIZER_BOOLEAN_FALSE

setListSerializerSeparator

public CRestBuilder setListSerializerSeparator(String sep)
Sets the list separator for the list serializer

Shortcut to builder.setProperty(CRestProperty.SERIALIZER_LIST_SEPARATOR, sep)

Parameters:
sep - Separator string
Returns:
current builder
See Also:
CRestProperty.SERIALIZER_LIST_SEPARATOR


Copyright © 2011. All Rights Reserved.