Getting and using REST client instances

Getting and using client instances

restfn templating functions

The restfn templating functions allow you to obtain and use REST clients. This works only for clients that have been configured and declared with RESTEasy client.

[#assign jokesService = restfn.getService("icndbClient", "info.magnolia.documentation.modules.restclientexamples.client.IcndbService")] (1)
[#assign response = jokesService.joke("random", "Tiger", "Lilly") /] (2)
<i>${response.get("value").get("joke").getTextValue()!"Nix found"}</i>
  1. Line 1 accesses an instance of the client interface. In this example it is of type IcndbService.

    • The first parameter is the name of the client configuration node (icndbClient)

    • The second parameter is the fully qualified class name of the declared service interface (info.magnolia.documentation.modules.restclientexamples.client.IcndbService).

  1. Line 2 calls the method #joke which returns a JsonNode object. NOTE: This is a simplified example. See jackson javadoc how to further process the JsonNode or assign a raw response to a JavaScript variable to process JSON in a JavaScript context.

    var jokeJson = '${jokesService.joke("random", "Tiger", "Lilly")!}';

During installation of the magnolia-resteasy-client module, RestEasyClientModuleVersionHandler adds restfn to renderer configurations. Make sure that the renderType of your template definition points to a renderer that is configured to use restfn. See Configure the functions in a renderer on how to add templating functions to a renderer.

Java using client registry

To explain how to obtain and use REST client, we look at a model class which uses the same client as in the above example:

  • Add RestClientRegistry as parameter into the constructor and assign it as final instance variable (See lines 5, 7, 9.)

  • Obtain RestEasyClient object from the RestClientRegistry. Note that #getRestClient requires the configured name of the rest client definition. (See line 17.)

  • Create an instance of the declared interface IcndbService. (See line 18.)

  • Now call the declared method(s) on the service (line 23).

Component example

You have the option to configure components (filters) to be executed on every request. One use case could be authentication.

Client Request Filter

Here is a sample filter that would handle authorization for each request.

package org.example.filters;

import info.magnolia.context.Context;
import info.magnolia.context.MgnlContext;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 * Interceptor for rest easy to handle authentication with an access token.
public class AuthenticationInterceptor implements ClientRequestFilter {

    private static final Logger log = LoggerFactory.getLogger(AuthenticationInterceptor.class);

    public void filter(ClientRequestContext requestContext) throws IOException {
        if (MgnlContext.hasInstance() && MgnlContext.hasAttribute("ACCESS_TOKEN", Context.APPLICATION_SCOPE)) {
                "OAuth " + MgnlContext.getAttribute("ACCESS_TOKEN", Context.APPLICATION_SCOPE));

            log.trace("Authorization : {}",
                (String) MgnlContext.getAttribute("ACCESS_TOKEN", Context.APPLICATION_SCOPE));
dev days event sign up
dev days event sign up