Client Configuration - BloomReach Experience - Open Source CMS

This article covers a Hippo CMS version 10. There's an updated version available that covers our most recent release.

15-02-2016

Client Configuration

In the previous step you developed a simple REST client to be used in your Hippo project. Typically the connection parameters will be different per environment, i.e. between test, staging and production environments you might want to use different values for baseAddress or connectionTimeout. This page describes how to use a dynamic resouce bundle to make these parameters configurable within the CMS UI, and how to dynamically re-initialize the client when the parameters change by using the Hippo Event Bus.

Add Resouce Bundle Dependency

Add the following dependency to site/pom.xml:

    <dependency>
      <groupId>org.onehippo.cms7.hst.components</groupId>
      <artifactId>hst-resourcebundle</artifactId>
    </dependency>

Create a Client Configuration Resource Bundle

Log in to the CMS as admin and create a new document REST Client Configuration of type Resource Bundle in the folder administration/configuration.

In the ID field enter: rest-client-configuration.

Enter the following key, value pairs:

  • client.baseaddress: https://www.demo.onehippo.com/
  • client.conduit.connection.timeout: 2000
  • client.conduit.receive.timeout: 2500
  • client.path: restapi/topproducts

Publish the resource bundle.

Create a Workflow Event Listener

In your project's site module create a Java class  org.example.rest.client.ClientComponentConfigurationManager and let it implement  org.hippoecm.hst.core.container.ComponentManagerAware.

In short this class will do the following:

  • Register itself with the HippoEventBus service.
  • Subscribe to  HippoWorkflowEvents.
  • Check each workflow event if it concerns publication of the REST Client Configuration resource bundle.
  • Re-initialize the GoGreenClient if the resource bundle was published.

site/src/main/java/org/example/rest/client/ClientComponentConfigurationManager.java:

package org.example.rest.client;

import java.util.ResourceBundle;

import org.hippoecm.hst.core.container.ComponentManager;
import org.hippoecm.hst.core.container.ComponentManagerAware;
import org.hippoecm.hst.resourcebundle.ResourceBundleFamily;
import org.hippoecm.hst.resourcebundle.internal.HippoRepositoryResourceBundleFamilyFactory;
import org.onehippo.cms7.services.HippoServiceRegistry;
import org.onehippo.cms7.services.eventbus.HippoEventBus;
import org.onehippo.cms7.services.eventbus.Subscribe;
import org.onehippo.repository.events.HippoWorkflowEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClientComponentConfigurationManager implements ComponentManagerAware {

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

    private ComponentManager componentManager;
    private boolean initialized;

    @Override
    public void setComponentManager(ComponentManager componentManager) {
        this.componentManager = componentManager;
    }

    public void init() {
        HippoServiceRegistry.registerService(this, HippoEventBus.class);
    }

    public void destroy() {
        HippoServiceRegistry.unregisterService(this, HippoEventBus.class);
    }

    @Subscribe
    public void handleEvent(final HippoWorkflowEvent event) {
        if (event.success() && "default:handle:publish".equals(event.interaction())) {
            if ((event.get("documentPath").toString()
                    .startsWith("/content/documents/administration/configuration/rest-client-configuration"))) {
                initialize();
            }
        }
    }

    public void initialize() {

        //retrieving resource bundle factory component
        HippoRepositoryResourceBundleFamilyFactory familyFactory = componentManager
                .getComponent("org.hippoecm.hst.resourcebundle.internal.ResourceBundleFamilyFactory");

        //retrieve the gogreen client
        GoGreenClient client = componentManager.getComponent(GoGreenClient.class.getName(), GoGreenClient.class
                .getPackage().getName());

        //retrieving resourcebundle and values
        final ResourceBundleFamily bundleFamily = familyFactory.createBundleFamily("rest-client-configuration");
        final ResourceBundle rsvpBundle = bundleFamily.getDefaultBundle();

        final String baseAddress = rsvpBundle.getString("client.baseaddress");
        final String path = rsvpBundle.getString("client.path");

        final long receiveTimeout = Long.parseLong(rsvpBundle.getString("client.conduit.receive.timeout"));
        final long connectionTimeout = Long.parseLong(rsvpBundle.getString("client.conduit.connection.timeout"));

        client.setBaseAddress(baseAddress);
        client.setPath(path);

        client.setReceiveTimeout(receiveTimeout);
        client.setConnectionTimeout(connectionTimeout);
        client.init();

    }
}

Add the following Spring bean configuration file to your project's site module:

site/src/main/resources/META-INF/hst-assembly/overrides/client-component-configuration-listener.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean class="org.example.rest.client.ClientComponentConfigurationManager"
        init-method="init"
        destroy-method="destroy"/>

</beans>

To test the workflow event listener, add the following to conf/log4j-dev.xml:
  <logger name="org.example.rest.client">
    <level value="info"/>
  </logger>

Then edit and publish the REST Client Configuration resource bundle in the CMS.

If everything works as expected a message similar to the one below will be logged:

08.12.2014 16:13:45 INFO  pool-1-thread-1 [GoGreenClient.init:32] (Re-)Initializing GoGreenClient

Next Step

Usage

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?