See all tutorials

Tutorials » How to fetch content to create other content

Webpagebytes CMS allows to create very easy static and dynamic content. This content can be present in multiple sources like web pages, emails, images, pdf documents and so on. This tutorial will show how to fetch content in a server side controller, content that can be used further to generate other content.
At the first sight this feature might look odd but consider the following cases:

  • When a user registration form is submitted, the server sends a welcome email to the user
  • When an account is created, the server sends an SMS activation code
  • User can request his account statement in a pdf document sent to his email address
All these cases have something in common, the content requested is not returned as part of the request operation, but it is provided via another channel like email or SMS. Another common thing is that the requested content is dynamic and personalized for each user.

A controller is a server side plugin (in Webpagebytes CMS it implements WPBRequestHandler interface) that can be executed on an HTTP request. The controller code can fetch content like the email subject and email body as strings and send an email with these values.

To fetch content in a controller take the following tasks:

  1. Create or identify the content that will be fetched
  2. Create or identify the controller that will fetch the content
The tutorial below will consider the case of sending an activation code to the user.

Create or identify the content that will be fetched

Step 1

This step will create the content for the activation code, it will be a simple site page with the code below
<#assign x=wpbAppModel["code"] >
Your activation code is ${x?string.computer}
Note: since x is a number, Freemarker ${x} will display the number like '43,678', to remove the comma character use ${x?string.computer} and it will display '43678'.

Create or identify the controller that will fetch the content

Step 2

This step will create a controller that should correspond to a POST on /registration, and which will fetch the activation text and use it. The controller code is below
package com.example;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.webpagebytes.cms.WPBContentProvider;
import com.webpagebytes.cms.WPBForward;
import com.webpagebytes.cms.WPBModel;
import com.webpagebytes.cms.WPBRequestHandler;
import com.webpagebytes.cms.exception.WPBException;

public class RegistrationController implements WPBRequestHandler {

    WPBContentProvider contentProvider;
    @Override
    public void initialize(WPBContentProvider contentProvider) 
    {
        this.contentProvider = contentProvider;        
    }

    @Override
    public void handleRequest(HttpServletRequest request,
                              HttpServletResponse response, 
                              WPBModel model, 
                              WPBForward forward) throws WPBException 
    {
        
        String mobileNumber = request.getParameter("mobileNumber");
        
        // validate the mobile number ...
        // create the user account ...      
        
        //generate a random number between 10000 and 99999
        Random r = new Random();
        int randomNumber = r.nextInt(89999) + 10000;
        
        //put the random number into the application specific model under key 'code'
        model.getCmsApplicationModel().put("code", randomNumber);
        
        // take the activation-text page guid from a url customization parameter named "activationTextGuid"
        String activationTextGuid = model.getCmsModel().get(WPBModel.URI_PARAMETERS_KEY).get("activationTextGuid");
        
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
        contentProvider.writePageContent(activationTextGuid, model, bos);
        String alertText = bos.toString();
        
        // call the relevant api to sent alertText to mobileNumber
    }
}

The code that fetch the activation text content is in bold, it will produce a text like: Your activation code is 53858
The code explanation is the following:

  • The randomNumber value is added to the application specific model under 'code' key, the same key used in the site page at Step 1
  • The activation-text site page guid is obtained from a customization parameter named 'activationTextGuid'
  • contentProvider reference to WPBContentProvider interface is used to fetch the activation-text site page content

Fork me on GitHub