Usage

Previous

In the previous steps, you created a simple REST client and made it dynamically configurable.

Within an HST component, the REST client can be retrieved through the ComponentManager.

For example, you would use the following code to retrieve the GoGreenClient:

final GoGreenClient client = HstServices.getComponentManager().getComponent(GoGreenClient.class);

Below is a simple HST component which uses the GoGreenClient to retrieve the top products from the GoGreen RESTful service and makes them available to the rendering engine:

package org.example.components;

import java.util.List;

import org.example.jaxb.Products;
import org.example.jaxb.Products.Product;
import org.example.rest.client.GoGreenClient;
import org.hippoecm.hst.component.support.bean.BaseHstComponent;
import org.hippoecm.hst.core.component.HstComponentException;
import org.hippoecm.hst.core.component.HstRequest;
import org.hippoecm.hst.core.component.HstResponse;
import org.hippoecm.hst.site.HstServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TopProducts extends BaseHstComponent {

    public static final Logger log = LoggerFactory.getLogger(TopProducts.class);

    @Override
    public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException {
        final GoGreenClient client = HstServices.getComponentManager().getComponent(GoGreenClient.class);
        final Products topProducts = client.getTopTenProducts();
        final List<Product> products = topProducts.getProduct();
        request.setAttribute("products", products);
    }
}

A Freemarker template can be used to render the products:

<#if products??>
  <ul>
    <#list products as product>
      <li>${product.localizedName}</li>
    </#list>
  </ul>
</#if>

Previous

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?