api-rules

Rules

There might be a situation when you need to modify your request or response before sending it to backend system or back to the client. For example, you want to allow both XML and JSON as valid payload for your APIs but your backend system is written in such a way that it only accepts XML. In that case, before sending the request to backend system, you need to convert the request payload from JSON to XML and vice versa. There can be another case that if some parameter is not set, you will return the response from there itself instead of forwarding it to backend system as you are sure that server would return an error in this case. All this can be achieved by setting Pre/Post rules on REST APIs in App42 API Gateway.

App42 API Gateway provides two types of rules:

Pre Rules are those rules that will be executed after request is received from client but is not forwarded to the backend system. Pre rules allow you to modify the request.

Post Rules are those rules that will be executed after response is received from backend system but is not sent back to the client. Post rules allow you to modify the response.

Both Pre and Post rules can be executed in both situations— if condition is defined or if there is no condition. For example, a user created rule called DeviceRule will only be executed on the condition that the value of one header param says deviceType as Android. If that condition is defined, request will be intercepted and will be checked for the header deviceType and if its value is Android, then DeviceRule will be executed otherwise not.

Note: You can apply as many Pre/Post rules on an API as you want.

Why Pre/Post Rules are required?

Below are some of the cases when you would want to apply Pre/Post Rules:

  • 1.You want to add some data in the request/response before forwarding it to backend system or sending it back to client
  • 2.You want to delete unnecessary data from header or payload which is not required at backend
  • 3.You want to trigger some event before forwarding the request or getting the response back
  • 4.You want to change the payload type to make it understand for the backend system. For example JSON to XML or XML to JSON

Rule Creation

To Create Rules in App42 API Gateway you need to first upload Rules Jar(containing your Pre and Post Rules) from Management Console. Once you have uploaded your Rules Jar, you can create your Pre and Post Rules.

Steps to Upload Rules Jar:

  • 1. Go to Rules -> Jars -> Upload Jar
  • 2. Choose the Jar file and click on Upload

Steps to Manage/Create Rules:

  • 1.Go to Rules -> Manage -> Click on Create Rule
  • 2.Provide Rule name
  • 3.Select the methods (GET, PUT, POST, DELETE) on which this rule will be applicable
  • 4.Provide condition on which you want to apply this Rule (optional). In this you can make your condition based on Header and Query Params (Key Name & Key Value). Clicking on Add Condition will attach it to the rule
  • 5.Select Jar file for the Rule which you must have uploaded
  • 6.Select Rule type as Pre or Post
  • 7.Select the Executor class for the Rule which is present in your Jar file
  • 8.Click on Create and your rule will be created
  • 9.Similarly you can create Post Rules also

Writing Rules

Writing Pre-Process Rule

Pre-Rules can be used to apply pre-processing on your API. Pre-Rules can be written in JAVA language by implementing com.shephertz.app42.paas.customcode.PreProcessRuleExecutor interface as shown in below code snippet. You have to override the method preProcessExecute (HttpRequestObject request, HttpResponseObject response) to do the pre-processing. This class will be called before calling your API.

package com.app42.gateway.sample;
import com.shephertz.app42.paas.customcode.HttpRequestObject;
import com.shephertz.app42.paas.customcode.HttpResponseObject;
import com.shephertz.app42.paas.customcode.PreProcessRuleExecutor;
public class MyPreRule implements PreProcessRuleExecutor{
    @Override
    public void preProcessExecute(HttpRequestObject request, HttpResponseObject response) {
        System.out.println(" ########## Inside Pre Process Rule ######## " + request.getApiKey());
        /*********************
         * 
         * Write your Pre Processing Logic
         * 
         **********************/
        //setting response by calling response.setStatusCode, will return response from here 
        //without further processing on your main API and next Pre Process rules.
    }
}

If pre-processing class sets the response code on response object by calling response.setStatusCode method, response will be returned to client and your main API logic will not be called. If you just want to make some changes in request body/header, you just do it without setting response code. If you want to send the response in case of any required condition, you have to set the response code in that case.

Writing Post-Process Rule

Post Rules can be used to apply post processing on your API. Post Rules can be written in JAVA language by implementing com.shephertz.app42.paas.customcode.PostProcessRuleExecutor interface as shown in below snippet. You have to override the method postProcessExecute (HttpRequestObject arg0, HttpResponseObject arg1) to do the post processing. This class will be called after calling your API. This class will have response received by your API and will to the post process before sending it to client.

package com.app42.gateway.sample;
import com.shephertz.app42.paas.customcode.HttpRequestObject;
import com.shephertz.app42.paas.customcode.HttpResponseObject;
import com.shephertz.app42.paas.customcode.PostProcessRuleExecutor;
public class MyPostRule1 implements PostProcessRuleExecutor{
    @Override
    public void postProcessExecute(HttpRequestObject request, HttpResponseObject response) {
        System.out.println(" ########## Inside Pre Process Rule ######## " + request.getApiKey());
        /*********************
         * 
         * Write your Post Processing Logic
         * 
         **********************/
    }
}

API Mapping

To map Rules with your APIs from GatewayHQ, please follow these steps:

  • 1.Go to Rules -> API Mapping
  • 2.Select Project
  • 3.Click on Attach Rules and Select the API Operations on which you want to attach
  • 4.Select Pre Rules and Post Rules which you want to attach to the selected API operation
  • 5.You can select multiple API operations and attach/detach rules on all of them at once

Note – You can attach multiple Pre and Post Rules on any API Operation.