Charon

In addition to standard HTTP requests, Charon can use OAuth to retrieve a secure token from an external authentication service. This token is used to authenticate subsequent requests on the remote service.

Get Charon

To get Charon, use the following dependency configuration:

<dependency>
   <groupId>com.hybris</groupId>
   <artifactId>charon</artifactId>
   <version>1.2.0-M0</version>
</dependency>

Remote Service Definition

The External Service pubsub service:

@Http(value = "pubsub", url = "https://${environment}/hybris/pubsub/v1")
@OAuth
@Control(timeout = "${timeout:4000}")
public interface PubSubClient
{
 
   @POST
   @Path("/topics/${client}/order-created/read")
   @Produces("application/json")
   @OAuth(scope = "hybris.pubsub.topic=hybris.order.order-created")
   Observable<RawResponse<ReadResponse>> readOrderCreated(ReadRequestParameters options);
 
   @POST
   @Path("/topics/${client}/order-created/commit")
   @Produces("application/json")
   @Control(retries = "3", retriesInterval = "2000")
   @OAuth(scope = "hybris.pubsub.topic=hybris.order.order-created")
   Observable<Void> commitOrderCreated(CommitRequestParameters options);
}

The External Product Service:

@Http(url = "https://api.external.io/hybris/product/b1")
@OAuth
public interface ProductClient
{
   @POST
   @Produces("application/json")
   @Path("/${tenant}/products")
   Observable<Void> createByMap(Map<String, Object> productMap);
    
   @POST
   @Produces("application/json")
   @Path("/${tenant}/products")
   Observable<Void> create(Product product);
    
   @GET
   @Produces("application/json")
   @Path("/${tenant}/products/?q=sku:{sku}")
   Observable<List<Product>> findBySku(@PathParam("sku") String sku);
}
  • @Http is used in conjunction with @EnableHttpClients for automatic client discovery.

  • "pubsub" in @Http is the clientId and it is used as configuration key's prefix.

  • @OAuth means all the requests made within that client are subject of oauth authentication.

  • ${tenant} is a config placeholder. Configuration provider is inquired for that specific key (tenant).

Charon is responsible to proxy those interfaces and making HTTP calls accordingly to the JAX-RS annotations and the dynamic configuration provided.

To properly set up Charon you need to:
  • construct your interface(s) for define the REST client interface

  • instantiate the client within the Charon builder or spring context

  • inject the proper configuration management

Creating a Client

  1. Get Charon api:

    ProductClient client = Charon.from(ProductClient.class).build()
  2. Configure Sprig Java:

    @Configuration
    @EnableHttpClients(classes = {ProductClient.class})
  3. Configure Spring XML:

    <bean id="productClient" class="com.hybris.charon.HttpClientFactoryBean">
      <property name="type" value="com.hybris.client.ProductClient"/>
    </bean>

Using OAuth

When interfaces are annotated with @OAuth an authorization request is first performed against a remote authentication server. Upon successful authentication the returned TOKEN is then cached for its entire time to live, and used in each subsequent transaction against the remote service, therefore a client credentials authorization grant is performed.

@OAuth parameters may also be provided as hard-coded values or placeholders, the same way as @Http annotations. @OAuth configuration follows the same hierarchy of defaults. For more information, see the Configuring Charon section of this document.

Below is an example of configuration properties provided in the annotation. The properties could be hardcoded or make use of placeholders.

  • In the case the token is used against a service, and rejected (by the oauth2 resource server) within NotAuthorizedException (401 Not Authorized), Charon will perform the oauth2/token request again.

  • All client-based exceptions (not the >500 error codes) are remapped in OAuthException. The original exception is mapped in OAuthException.getCause().

@OAuth(clientId = "...", clientSecret = "...", scope = "...", url = "...") //all annotation properties could make use of ${} placeholders

OAuth is also able to make use of the serviceId provided in the @Http annotation. In other words, if no properties are provided for @OAuth, then the serviceId provided in the @Http client interface annotation are used.

//client interface
@Http("myClient")
@OAuth

//if properties are not provided in the annotation,  they will be taken from the given configuration.

The above example would therefore look for properties provided as follows:

myClient.oauth.url=https://accounts.google.com/o/oauth2/auth
myClient.oauth.clientId=...
myClient.oauth.clientSecret=...
myClient.oauth.scope=...

OAuth annotation can be specified also at method level, and it overrides the values present at class level:

@Http("order")
@OAuth // all oauth inputs are specified in the given property resolver
@Control(timeout = "${timeout:4000}")
public interface OrderClient
{
   @GET
   @Path("/${tenant}/salesorders/{orderId}")
   Observable<Order> getOrder(@PathParam("orderId") String id);
 
   @POST
   @Path("/${tenant}/salesorders/{orderId}/transitions")
   @OAuth(scope = "hybris.order_update")
   Observable<Void> changeStatus(@PathParam("orderId") String id, OrderStatus newStatus);
}

Connection Pooling

Charon reuses TCP connections for lowering latency and improving speed. The pool size for a given host:port in unlimited and new connection is created on concurrent requests, if not available in the pool. For more control on concurrent requests, see Secure HTTP Transactions.

Usage of JAX-RS Annotations

HTTP Method

Charon supports the @GET, @POST, @DELETE, and @PUT methods. The default method is @GET.

The following sections show how to provide query strings for GET requests, and form parameters for POST. In addition, you can also provide custom headers and path parameters, and define the request content type.

The @Produces annotation defines the request's content type. In the case of application/json only the first argument without annotation (@FormParam, @QueryParam, @HeaderParam) is to be serialized. If more than one argument without annotation is present, an exception is thrown.

@Produces("application/json")
    void create(Wishlist wishlist);

To include a GET query string in the URI, provide the name-value pairs in the getAll() method as follows. This adds the query parameters as a form-url-encoded series of <query-param-name>=<argument-value>.

@GET
   @Path("/order")
   Observable<Order> getAll(@QueryParam("pageNumber") int pageNumber);

To submit form values for the POST method, provide the field name-value pairs in the postForm() method as follows:

@POST
   void postForm(@FormParam("fp1") String param1, @FormParam("fp2") String param2);

When @FormParam arguments are present, the application/x-www-form-urlencoded content type is sent to the server within the url-encoded parameters.

Path parameters may be injected into the URI using the getOrder() method as follows:

@Path("/order/{id}")
   Observable<Order> getOrder(@PathParam("id") String id);

You can also provide headers, using the following method:

@Header( name="X-appId", val="static-value-${customKeyValue}")
Observable<Product> retrieveProduct()

Custom headers can also be provided using the getWithHeaders() method:

String getWithHeaders(@HeaderParam("header1") String header1, @HeaderParam("header2") String header2);

For performing a basic authentication per request:

Observable<Result> getResult(@HeaderParam("Authorization") String basic);
//basic="Basic QWxhZGRpbjpPcGVuU2VzYW1l"

For performing a basic authentication by configuration:

@Header( name="Authorization", val="Basic ${basicAuth}")
Observable<Result> getResult();
//basic="QWxhZGRpbjpPcGVuU2VzYW1l"

Request body encoding

The encoding of the request body is specified by the @Produces annotation. Only two encoders are currently provided:
  • plain/text

    Simply invoke .toString() on the request object

  • application/json

    Serialize the given object with Jackson

  • multipart/form-data uploads files and mixed parts

@POST
	@Produces("application/json") //the POJO `post` argument is serialized in json
	Observable<Void> createPost(Post post);

The decoding of the response is driven by the Content-Type header sent by the server. However, some arguments are self-explanatory regarding their encoding.

Type Based Encoding utilizes the following:
  • java.net.URL: the remote content is fetched and redirected to the target endpoint, as long as it is <content-type> and <content-length>

  • java.nio.file.Path: the content-type is deducted by Files.probContentType and it is forward along with the binary data

  • java.io.File: as <Path>

  • java.io.InputStream: the stream is redirected to the target endpoint, though the <content-type> is by default application/octet-stream and the request will be chunked

  • java.lang.String: the string is sent as is with <content-type: text/plain>

  • byte[]: the byte array is sent as is with content-type: application/octet-stream

//content-type is determined by the file itself
@POST
Observable<Response> sendFile(Path path)

//send a text file
sendFile(Path.get("/tmp/test.txt"))

//will send
content-type: text/plain
content-length: ..

//content-type could be overrided by @Produces annotation
@POST
@Produces("application/json")
Observable<Response> sendFile(Path path)

//will send
content-type: application/json
...

For adding or replacing the default encoders, you may use the builder:

Typicode service = Charon.from(Typicode.class)
         .putEncoder(MediaType.APPLICATION_XML, (EncoderRequest req)-> ....) // XML encoder
         .putDecoder(MediaType.APPLICATION_XML, (Observable<byte[]) bytes, Type type)->...) // XML decoder
         .build();

where EncodeRequest contains arguments and parameters, and EncodeResult contains the headers and the content.

The encoder is a Function<EncodeRequest, Observable<EncodeResult>.

The decoder is a BiFunction<Observable<byte[]>, Type, Observable<Object>>.

BiFunction<Object[], Parameter[], byte[]> firstArgumentToStringEncoder = (args, parameters)-> args[0].toString().getBytes();

Annotation-Based Configuration

@Encoder(contentType = "application/xml", encoder = XMLEncoder.class)
@Decoder(contentType = "application/xml", decoder = XMLDecoder.class)
public interface Test{
   @POST
   @Path("/")
   @Produces("application/xml")
   PojoResponse post(PojoRequest val);
}

The decoder type is a BiFunction<ByteBuf, Type, Object>.

BiFunction<ByteBuf, Type, Object> toStringDecoder = (buffer, type)-> buffer.toString(defaultCharset());

The decoder function takes the ByteBuf and the Type of the returned object as input, then returns the actual Type instance.

Upload Files and Multipart Requests

MediaClient

@Http(url = "https://api.external.io/hybris/media/b2")
@OAuth(scope = "hybris.media_manage")
interface MediaClient {
   @POST
   @Path("/${tenant}/${client}/media")
   @Produces("multipart/form-data")
   Observable<MediaCreateResponse> upload(@Disposition URL from);
 
   @POST
   @Path("/${tenant}/${client}/media")
   @Produces("multipart/form-data")
   Observable<MediaCreateResponse> upload(@Disposition URL from, @Disposition(contentType = "application/json", name = "metadata") Metadata metadata);
}

File upload is performed through multipart/form-data requests and with Charon it is possible to specify the details for every part defined as Content-Disposition in HTTP.

@Disposition(contentType = "application/json", name = "metadata", fileName = "report.json")
The @Disposition annotation defines the following properties:
  • <ContentType>: the HTTP header included in the request and by which the parameter is serialized. This value must be specified, there is no default value.

  • <Name>: the name field for that Content-Disposition. Default: <file>.

  • <FileName>: the filename field for that Content-Disposition. Default: <fileName>.

In the case of URL type parameter:
  • <ContentType> is given by remote endpoint Content-Type, and it overrides the @Disposition <contentType> value.

  • <FileName> is given by the <file> url part, if it is present.

In case of <Path/File> type parameter:
  • <ContentType> is given by the type of file

  • <FileName>: the file/s name

In case of <String> type parameter:
  • <ContentType>: text/plain

In case of <byte[]> type parameter:
  • <ContentType>: application/octet-stream

Upload without MultiPart

@POST
@Path("s3-url")
Observable<S3Response> upload(URL fromEndpoint);

Similarly to the multi-part request, Charon uploads the content from fromEndpoint to the specified target as a single-part request. The <content-type> and <content-length> will be the one returned by the typed parameter (see the Type-Based Encoding paragraph)

Error Handling

The exceptions thrown by Charon are the standard exceptions defined in com.hybris.charon.exp package. These are listed in the following table.

Http Code

Exception

304

NotModifiedException

300–399

RedirectException

400

BadRequestException

401

NotAuthorizedException

403

ForbiddenException

404

NotFoundException

405

NotAllowedException

406

NotAcceptableException

409

ConflictException

500

InternalServerErrorException

503

ServiceUnavailableException

505

NotSupportedException

50*

ServerErrorException

client errors in oauth2/token

OAuthException

*

java.lang.RuntimeException

Logging

The Platform makes use of the Loopback logger for logging purposes. For dumping all HTTP activities during development phases, the logger category has to be set to debug level.

<logger name="io.netty.handler.logging.LoggingHandler" level="debug" />

This outputs hex content similar to the following:

+-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 50 4f 53 54 20 68 74 74 70 3a 2f 2f 6c 6f 63 61 |POST http://loca|
|00000010| 6c 68 6f 73 74 3a 38 30 38 30 2f 6f 61 75 74 68 |lhost:8080/oauth|
|00000020| 2f 74 6f 6b 65 6e 20 48 54 54 50 2f 31 2e 31 0d |/token HTTP/1.1.|
|00000030| 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 |.Content-Type: a|
|00000040| 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 77 77 77 |pplication/x-www|
|00000050| 2d 66 6f 72 6d 2d 75 72 6c 65 6e 63 6f 64 65 64 |-form-urlencoded|
|00000060| 0d 0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 |..Content-Length|
|00000070| 3a 20 38 37 0d 0a 41 63 63 65 70 74 3a 20 61 70 |: 87..Accept: ap|
|00000080| 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 2c 74 |plication/json,t|
|00000090| 65 78 74 2f 70 6c 61 69 6e 0d 0a 48 6f 73 74 3a |ext/plain..Host:|
|000000a0| 20 6c 6f 63 61 6c 68 6f 73 74 3a 38 30 38 30 0d | localhost:8080.|
|000000b0| 0a 55 73 65 72 2d 41 67 65 6e 74 3a 20 52 78 4e |.User-Agent: RxN|
|000000c0| 65 74 74 79 20 43 6c 69 65 6e 74 0d 0a 0d 0a    |etty Client.... |
+--------+-------------------------------------------------+----------------+

SAP Commerce Data Hub Integration

For integrating Charon with the SAP Commerce Data Hub in a servlet engine, follow Spring XML Integration section of the Charon Spring Integration page.

You should keep all the configuration in the local.properties and let Charon to grab the configuration from there using the HybrisCommonsResolver. An Example could be:
  • Pubsub client

    @OAuth
    public interface PubSubClient
    {
    
       @POST
       @Path("/topics/${client}/{topic}/read")
       @Produces("application/json")
       @Control(timeout = "${timeout:2000}")
       Observable<ReadResponse> read(@PathParam("topic") String topic, ReadRequestParameters options);
    
    }
  • local.properties

    external.pubsub.url=https://api.external.io/hybris/pubsub/b2
    external.pubsub.client=hybris.order
    external.pubsub.oauth.clientId=....
    external.pubsub.oauth.clientSecret=....
    external.pubsub.oauth.scope=....
  • ext-datahub-spring.xml

    <bean id="pubsubClient" class="com.hybris.charon.HttpClientFactoryBean">
     <property name="type" value="com.hybris.client.PubSubClient"/>
     <property name="clientId" value="pubsub"/>
     <property name="propertyResolver">
      <bean class="com.hybris.charon.conf.HybrisCommonsResolver">
    	<construct-arg value="external"/>
      </bean>
     </property>
    </bean>

Proxy Support

It is possible to perform request through an HTTP proxy following the system properties provided by Oracle:
  • The following proxy settings are used by the HTTP protocol handler:

    • http.proxyHost (default: <none>)

      The hostname, or address, of the proxy server.

    • http.proxyPort (default: 80)

      The port number of the proxy server.

    • http.nonProxyHosts (default: localhost|127.*|[::1])

      Indicates the hosts that have to be accessed without going through the proxy. Typically this defines internal hosts. The value of this property is a list of hosts, separated by the | character. In addition, the wildcard character * can be used for pattern matching. For example -Dhttp.nonProxyHosts=”*.foo.com|localhost indicates that every host in the foo.com domain and the localhost should be accessed directly even if a proxy server is specified. The default value excludes all common variations of the loopback address.

  • The following proxy settings are used by the HTTPS protocol handler:

    • https.proxyHost(default: <none>)

      The hostname, or address, of the proxy serve.

  • https.proxyPort (default: 443)

    The port number of the proxy server.

    The HTTPS protocol handler uses the same nonProxyHosts property as the HTTP protocol.

Request Throttling

Requests performed by Charon are natively asynchronous, that means if Charon is invoked in a hypothetical infinite loop, the client will finally run into a too many open files exception or the target server will reject the large amount of concurrent requests. For that case, it is possible to limit concurrent requests with the @Control annotation:
@Control(maxConcurrentRequests = "50") //50 is intended for in a single method
interface ProductServiceClient{
    
	@Control(maxConcurrentRequests = "20") //overrides the interface-level attribute
	Observable<ProductCreationResponse> createProduct(Product product)
}
By default it throws an com.hybris.charon.exp.ExhaustedPoolException that could be caught in the <.onError() Observable> method. For specifying a different error handling there are the following options:
//exceptionInline throws the exception in the method
@Control(maxConcurrentRequests = "50", exhaustedPoolStrategy = ExhaustedPoolStrategy.exceptionInline)
Observable<ProductCreationResponse> createProduct(Product product) throws ExhaustedPoolException;

//disabled will disable request throttling for that specific method
@Control (exhaustedPoolStrategy = ExhaustedPoolStrategy.disabled)
Observable<Void> unlimited(...);

//wait for available connections when pool is fully in action
@Control(exhaustedPoolStrategy = ExhaustedPoolStrategy.blocking, blockWait="2000")
Observable<ProductCreationResponse> createProduct(Product product) throws ExhaustedPoolException;
When the <strategy> is set to <blocking>, the invoking thread will be suspended until an available connection will be returned to the pool or the blockWait will elapse after a configurable number of milliseconds (1000ms by default). When the blockWait expires, an ExhaustedPoolException is thrown.

Getting Headers from Response

Declare com.hybris.charon.RawResponse as return type:
@POST
@Path("/topics/${client}/{topic}/read")
@Produces("application/json")
Observable<RawResponse<ReadResponse>> read(@PathParam("topic") String topic, ReadRequestParameters options);
In RawResponse the content is accessible through: Observable<T> content();.
All major headers are accessible to their specific methods:
Optional<Integer> contentLength();

Optional<String> contentType();

List<Link> links();

Optional<Long> age();

List<HttpMethod> allows();

List<CacheControl> cacheControls();

List<String> contentEncodings();

List<Locale> contentLanguages();

Optional<Date> date();

Optional<String> eTag();

Optional<Date> expires();

Optional<Date> lastModified();

Optional<URL> location();

Optional<String> server();

List<NewCookie> getSetCookies();

Response.Status status();
While help methods can enable developer to access custom headers:
Optional<Long> headerLong(String header);

Optional<Integer> headerInt(String header);

Optional<Boolean> headerBoolean(String header);

Optional<Date> headerDate(String header);

Multimap<String, String> headers();

Testing

It is possible to run integration tests which perform real HTTP request to a test web server:
com.hybris.charon.TestUtils.runTest(TestUtils.ServerHandler requestHandler, TestUtils.Task task) throws Throwable;
For example:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {FeatureIT.Config.class})
public class FeatureIT
{
	@Inject
	TestLocal client1;

	@Test
	public void simpleGet() throws Throwable
	{
	   TestUtils.runTest((req, resp, content) -> {
			//server side
			//incoming request from the client
	      resp.setStatus(HttpResponseStatus.OK);
		  TestUtils.writeString(resp, "ciao");
	      return resp.close();
	   }, () -> {
			//client side
			//execute client calls
	      assertThat(client1.getSimpleResponse()).isEqualTo("ciao");
	   });
	}

	@Http
	interface TestLocal
	{
	   @GET
	   String getSimpleResponse();
	}

	@Configuration
	@EnableHttpClients(classes = {TestLocal.class})
	@PropertySource("classpath:/test.properties")
	static class Config
	{
	   @Bean
	   SpringEnvironmentResolver propertyResolver()
	   {
	      return new SpringEnvironmentResolver("external");
	   }
	}
}
To use it you must include the following artifacts:
<dependency>
   <groupId>com.hybris</groupId>
   <artifactId>charon</artifactId>
   <version>1.0.0-SNAPSHOT</version>
   <scope>test</scope>
   <type>test-jar</type>
</dependency>
<dependency>
   <groupId>io.reactivex</groupId>
   <artifactId>rxnetty</artifactId>
   <version>0.4.15</version>
   <scope>test</scope>
</dependency>

Configuring Charon

Charon can be used out of the box with no environment, or with Spring. Configuration is primarily annotation-based, however with Spring you may also use XML.

Annotation-Based Configuration

The URL and clientId values can be declared in the interface using the @Http annotation.

@Http(value = "pubsub", url = "https://api.external.io/hybris/pubsub/b2")
interface PubSubClient {
...
}

The @Http annotation may be static as per the above example, or you can define a unique client ID for the entire remote service. This is a key the system uses for finding all associated properties. For example:

@Http('pubsub')

Given this clientId, the system looks for the following properties:

pubsub.url = ...
pubsub.oauth.url = ...

You may use standard Spring-style placeholders of the form ${VALUE} for some or all parts of the property.

//placeholders are similar to Spring. Use default value after ':'
@Http(value= "pubsub", url = "https://${environment}/hybris/pubsub/b2")
And your property file looks like:
#pubsub.environment=stage.external.io
environment=api.external.io

Dynamic Configuration with PropertyResolver

  • Using a Given Map

    You may provide the property values as a map. In this example the Guava's ImmutableMap.of method provides a simple way to build a map with a single key-value pair:

    ...
    PubSubClient client = Charon.from(PubSubClient.class).config(ImmutableMap.of("environment", "api.external.io")).build();
  • Using a Properties Object

    The properties object is a representation of a properties file, usually this is a filepath, for example load(String FILEPATH).

    Properties props =...
    Typicode client = Charon.from(Typicode.class).config((Map)props)).build();
    <bean id="client" class="com.hybris.charon.HttpClientFactoryBean"
      p:clientId="pubsub"
      p:config-ref="map"/>
     
    <util:map id="map">
      <entry key="environment" value="api.external.io"/>
    </util:map>
  • Using a Spring Property Resolver

    In Spring, you can use the SpringEnvironmentResolver to define different properties per object or path.

    @Inject
    SpringEnvironmentResolver resolver;
    
    Typicode client = Charon.from(Typicode.class).config(resolver).build();
    @Configuration
    @PropertySource("classpath:*config.properties")
    @EnableHttpClients(classes = {PubSubClient.class})
    public class SpringConfig {  
        @Bean  
        public SpringEnvironmentResolver resolver(){
           return new SpringEnvironmentResolver("prefix");  
        }
    }
  • Using the SAP Commerce ECP Configuration Service

    A platform extension is available for handling through the ECP configuration service. The property resolver implementation is named platformPropertyResolver and it is configured as below.

    <bean id="platformPropertyResolver" class="de.hybris.platform.yaas.config.PlatformPropertyResolver">
       <constructor-arg value="external" /> <!-- example configuration prefix -->
       <constructor-arg ref="configurationService" />
    </bean>
  • Using a Custom Property Resolver

    Alternatively, you may create your own custom property resolver.

    PropertyResolver resolver = new PropertyResolver(){
      boolean contains(String key){...}
      String lookup(String key){...};
    }
    PubSubClient client = Charon.from(PubSubClient.class).config(resolver).build();
  • Using SAP Commerce Data Hub configuration

    On datahub extension, you may use the local.properties defined in configuration, in order to do this, you may simply add the HybrisCommonsResolver:

    <bean class="com.hybris.charon.conf.HybrisCommonsResolver"/>

Default Values and the Configuration Property Hierarchy

Configuration properties are hierarchical and are first searched by the annotations. For example:

@Http( url = "http://www.example.com")

The configuration hierarchy follows this order:

1. annotation - hard-coded or dynamic with placeholders

2. provided configuration

3. system properties

The properties file may contain generic property values as well as those identified by clientId. For example:

myClient.url = ...

In this case if no matching clientId is provided, the generic property values for url and port are used. If generic property values are not provided then the system default is used. Examples of these three types of property definition are provided below.

@Http("myClient")
...
 
#properties example
myClient.url=http://jsonplaceholder.typicode.com
 
#if myClient.url is not present it will lookup for url property
url=http://jsonplaceholder.typicode.com