| Configuring the Genero Application Server / How to implement delegation | |
Each request of the form /ua/r, /ja/r and /ws/r coming from the user agent are delegated to the Genero REST service via its entry point.
When a /ua/r, /ja/r or /ws/r request is delegated to the REST service, the dispatcher appends the string /Delegate to the service URL in order to distinguish a dispatcher delegation from any other standard REST request. In other words, if an application has delegation, the REST service is called with a /Delegate appended in the URL.
IMPORT com
DEFINE req com.HTTPServiceRequest
...
LET req = com.WebServiceEngine.GetHttpServiceRequest(-1)
LET url = req.getUrl()
IF url.getIndexOf("/ws/r/RestGroup/RestService/Delegate",1)>1 THEN
CALL HandleDelegation(req)
ELSE
CALL HandleStandardService(req)
END IF
...
If parameters are defined in the DELEGATE configuration, they will be transmitted to the Genero REST service at each /wa/r, /ja/r and /ws/r request as HTTP headers.
There is one HTTP header per parameter set in the configuration, and it is of the form X-FourJs-Environment-Parameter-XXX where XXX is the parameter name and the parameter value is the HTTP header value.
IMPORT com
DEFINE req com.HTTPServiceRequest
...
LET param1 = req.getRequestHeader("X-FourJs-Environment-Parameter-AnyParameter")
DISPLAY param1 # Displays MyFirstParameter
LET param2 = req.getRequestHeader("X-FourJs-Environment-Parameter-Other")
DISPLAY param2 # Displays MySecondParameter
...
<EXECUTION>
<PATH>$(res.path)</PATH>
<MODULE>myApp.42r</MODULE>
<DELEGATE service="MyGroup/MyDelegateService">
<AnyParameter>MyFirstParameter</AnyParameter>
<Other>MySecondParameter</Other>
</DELEGATE>
</EXECUTION>
When a /wa/r, /ja/r or /ws/r request is delegated to a REST service, the original URL is transmitted to the service in the URL query string of the request.
For example, if the user types the following original URL in a browser:
http://host:port/ua/r/MyGrp/MyApp?P1=1&P2=2
http://localhost:port/ws/r/MyGroup/MyDelegateService/Delegate?http://host:port/ua/r/MyGrp/MyApp?P1=1&P2=2
To process the request in the delegation service, if the service handles only delegation, you can directly extract the original URL with the readFormEncodedRequest() method:
IMPORT com ... DEFINE req com.HTTPServiceRequest DEFINE original STRING DEFINE url STRING DEFINE query STRING ... LET original = req.readFormEncodedRequest(false) CALL SplitUrl(original) RETURNING url, query DISPLAY url # http://host:port/ua/r/MyGrp/MyApp DISPLAY query # P=1&P=2 ...
IMPORT com
...
DEFINE req com.HTTPServiceRequest
DEFINE url STRING
DEFINE rest_url STRING
DEFINE original STRING
DEFINE orig_url STRING
DEFINE query STRING
...
LET url = req.getURL()
CALL SplitUrl(url) RETURNING rest_url, original
CASE
WHEN rest_url.getIndexOf("/Delegate",1)
CALL SplitUrl(original) RETURNING orig_url, query
DISPLAY orig_url # http://host:port/ua/r/MyGrp/MyApp
DISPLAY query # P=1&P=2
WHEN rest_url.getIndexOf("/GetCurstomerInfo",1)
# Handle regular REST request
...
OTHERWISE
CALL req.sendTextResponse(400, NULL, "Invalid REST request")
END CASE
...