Writing Rules - shephertz/App42_APIGateway_Docs GitHub Wiki
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. Download API Sample from here which has sample Pre and Post Process Class.
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. Download API Sample from here which has sample Pre and Post Process Class.
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
*
**********************/
}
}
You can create as many pre/post process rules and set the priority/order to call them one after another. This option can be set while deploying the rules. See here for details about how you can deploy these rules.