REST service example

In this example, the REST service returns an HTTP error code 404 to the browser until the query string contains the string ByPass.

When a Genero web client application is started, the parameter ACCESS is also set and can be retrieved in the Genero program with fgl_getenv("ACCESS").

IMPORT COM
IMPORT XML
IMPORT FGL WSHelper

PRIVATE CONSTANT C_BASEURL = "/ws/r/qa-test/MyService/"
PRIVATE CONSTANT C_X_FOURJS_ENVIRONMENT_ = "X-FourJs-Environment-"
PRIVATE CONSTANT C_GENERO_INTERNAL_DELEGATE = "_GENERO_INTERNAL_DELEGATE_"

MAIN
  DEFINE  req       com.HttpServiceRequest
  DEFINE  methd     STRING
  DEFINE  url       STRING
  DEFINE  path      STRING
  DEFINE  ind       INTEGER 
  DEFINE  operation STRING 
   
  CALL com.WebServiceEngine.Start()

  WHILE TRUE
    TRY
      LET req = com.WebServiceEngine.GetHttpServiceRequest(-1)
      IF req IS NULL THEN
        EXIT WHILE
      ELSE
        LET url = req.getUrl()
        LET methd = req.getMethod()
        LET path = req.getUrlPath()
        DISPLAY "Incoming request: ",methd," path=",path
        LET ind = path.getIndexOf(C_BASEURL,1)
        IF ind<1 THEN
          CALL req.sendResponse(400,"Invalid request")
        ELSE
          # Dispatch request based on operation name (ex : /ws/r/qa-test/MyService/operation1)
          LET operation = path.subString(ind+C_BASEURL.getLength(),path.getLength())
          CALL DispatchService(req,operation)
        END IF 
        DISPLAY "Response sent"
      END IF
    CATCH
      EXIT WHILE 
    END TRY
  END WHILE 
END MAIN

FUNCTION DispatchService(req,operation)
  DEFINE  req         com.HttpServiceRequest
  DEFINE  operation   STRING
  DEFINE  ind         INTEGER
  LET ind = operation.getIndexOf("/",1)
  IF ind>0 THEN
    CALL req.sendResponse(400,"invalid operation")
  ELSE
    CASE operation
      WHEN "Delegate" # Handle a dispatcher delegate service
        CALL DelegateWA(req)
      OTHERWISE
        CALL req.sendResponse(400,"unknown service '"||operation||"'.");
    END CASE 
  END IF
END FUNCTION
        
#
# Delegate WA service
#  If browser URL doesn't contain 'ByPass' in query string then return a 404 error,
#  otherwise start GWC application
#        
FUNCTION DelegateWA(req)
  DEFINE  req               com.HttpServiceRequest
  DEFINE  q                 WSHelper.WSQueryType
  DEFINE  url               STRING
  DEFINE  ind               INTEGER
  DEFINE  ByPass            BOOLEAN
  
  # Decode Delegate query
  CALL req.getURLQuery(q)
  IF q.getLength()==0 THEN
    CALL req.sendResponse(400,"no query string")
    RETURN
  ELSE  
    IF q[1].name=="url" THEN
      LET url = q[1].value
      CALL q.deleteElement(1)
      # Valid delegate request with original URL as key parameter
    ELSE
      CALL req.sendResponse(400,"no valid delegate request")
      RETURN
    END IF
  END IF
  # Process Delegate request
  LET ByPass = FALSE
  # Check if user-agent query string has a ByPass string ?
  FOR ind=1 TO q.getLength()
    IF q[ind].name="ByPass" THEN
      LET ByPass = TRUE
    END IF
  END FOR
  IF NOT ByPass THEN
    # return error
    CALL req.sendResponse(404,"ByPass is missing")
  ELSE
    # Set parameter for the web client application via environment variable: ACCESS=OK
    CALL req.setResponseHeader(C_X_FOURJS_ENVIRONMENT_||"ACCESS","OK")
    # Start application with HTTP code 307
    CALL req.sendResponse(307,C_GENERO_INTERNAL_DELEGATE)
  END IF
END FUNCTION