From the user agent to the REST service

Each request of the form /ua/r and /ws/r coming from the user agent is delegated to the Genero REST service via its entry point.

REST service delegation entry point

When a /ua/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 is configured for 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 path = req.getUrlPath()
IF path.getIndexOf("/ws/r/RestGroup/RestService/Delegate",1)>1 THEN
  CALL HandleDelegation(req)
ELSE
  CALL HandleStandardService(req)
END IF
...

Pass parameters to the REST service

If parameters are defined in the DELEGATE configuration, they are transmitted to the Genero REST service at each /ua/r or /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
...
Important: Some Web servers convert all HTTP header names to lowercase. For example, a parameter called AnyParameter in the configuration, in the header may become:
x-fourjs-environment-parameter-anyparameter

Therefore, it is not recommended to rely on case in the naming of parameters as there is no guarantee that it will be preserved. When working with HTTP headers in your Genero program code, make sure you allow for this by converting them to either upper or lower case.

The sample is based on the example application configuration in Configure delegation for application or service.

Pass the user agent URL to the REST service

When a request is delegated to a REST service, the original URL is transmitted to the service in the URL query string of the request with url as the key and the original URL as its value. It is properly encoded so that the req.getURLQuery() function can decode the URL and all query parameters.

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?url=http%3A%2F%2Fhost%3Aport%2Fua%2Fr%2FMyGrp%2FMyApp&P1=1&P2=2

Process the delegation request in the delegation service

To process the request in the delegation service, if the service handles only delegation, you can directly extract the original URL with the getURLQuery() or readFormEncodedRequest() method:

IMPORT com
IMPORT FGL WSHelper
...
DEFINE req       com.HTTPServiceRequest
DEFINE original  STRING
DEFINE url       STRING
DEFINE query     WSHelper.WSQueryType
DEFINE ind       INTEGER
...
CALL req.getURLQuery(query)
IF query.getLength()==0 THEN
  CALL req.sendTextResponse(400,NULL,"Not a valid delegate request")
ELSE
  IF query[1].name == "url" THEN
    # retrieve original URL
    LET url = query[1].value
    # remove original URL from query array to keep only query of original URL
    CALL query.deleteElement(1)
  ELSE
    CALL req.sendTextResponse(400,NULL,"No url parameter found")
  END IF
END IF
DISPLAY url      # http://host:port/ua/r/MyGrp/MyApp
# Handle additional query parameters from the original URL
FOR ind = 1 TO query.getLength()
  DISPLAY "query"||ind
  DISPLAY "  name is ",query[ind].name
  DISPLAY "  value is ",query[ind].value
END FOR
...

Process non-delegated and delegated requests

If the service must also handle non-delegated requests, use the getURLPath() method to retrieve the REST operation to perform them.

If the REST path contains the "/Delegate" string, it is a delegation request and you need to extract the original URL as a query value of the query key named url, as described in Process the delegation request in the delegation service.
IMPORT com
IMPORT FGL WSHelper
...
DEFINE req com.HTTPServiceRequest
DEFINE path STRING
...
LET path = req.getURLPath()
CASE
WHEN path.getIndexOf("/Delegate",1) #Perform delegate operation as above example
...
WHEN path.getIndexOf("/GetCurstomerInfo",1) # Handle regular REST request
...
CALL req.sendTextResponse(200,NULL, "Done...")
OTHERWISE
CALL req.sendTextResponse(400, NULL, "Invalid REST request")
END CASE
...