From the user agent to the REST service

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.

REST service 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
...

Passing parameters to the REST service

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.

REST sample:
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
...
The sample is based on this configuration:
<EXECUTION>
  <PATH>$(res.path)</PATH>
  <MODULE>myApp.42r</MODULE>
  <DELEGATE service="MyGroup/MyDelegateService">
    <AnyParameter>MyFirstParameter</AnyParameter>
    <Other>MySecondParameter</Other>
  </DELEGATE>
</EXECUTION>

Passing the user agent URL to the REST service

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
The resulting URL passed to the delegation service will be:
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
...
If the service must also handle non-delegated requests, use the getURL() method to retrieve the REST operation to perform. If the REST URL contains the "/Delegate" string, it is a delelation request and you need to extract the original URL after the ? character. You can then check the original URL the user agent wants to access, and extract potential parameters in its query string:
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
...