Request context example for access policy

You can specify an access policy that makes access decisions based on context that is obtained from the request.

Requests can contain headers, cookies, and parameters. The following example uses the request context to make an access decision.


//Retrieve request context
var requestJSON = (function() {
       var request = context.getRequest();
       var requestReturn = {};
	
       var headersJSON = (function() {
              var headersReturn = {};
              var headerNames = request.getHeaderNames();

              for (var it = headerNames.iterator(); it.hasNext();) {
                     var headerName = it.next();
                     var headerValue = request.getHeader(headerName);

                     headersReturn["" + headerName] = "" + headerValue;
              }

              return headersReturn;
       })();
	
       var cookiesJSON = (function() {
             var cookiesReturn = {};
             var cookies = request.getCookies();

             for (var it = cookies.iterator(); it.hasNext();) {
                     var cookie = it.next();
                     var cookieComment = cookie.getComment();
                     var cookieDomain = cookie.getDomain();
                     var cookieHttpOnly = cookie.isHttpOnly();
                     var cookieMaxAge = cookie.getMaxAge();
                     var cookieName = cookie.getName();
                     var cookiePath = cookie.getPath();
                     var cookieSecure = cookie.isSecure();
                     var cookieValue = cookie.getValue();
                     var cookieVersion = cookie.getVersion();

                     cookiesReturn["" + cookieName] = {
                            comment: "" + cookieComment,
                            domain: "" + cookieDomain,
                            httpOnly: cookieHttpOnly,
                            maxAge: cookieMaxAge,
                            path: "" + cookiePath,
                            secure: cookieSecure,
                            value: "" + cookieValue,
                            version: cookieVersion
                     };
              }

              return cookiesReturn;
       })();

       var parametersJSON = (function() {
              var parametersReturn = {};
              var parameterNames = request.getParameterNames();

              for (var it = parameterNames.iterator(); it.hasNext();) {
                     var parameterName = it.next();
                     var parameterValue = request.getParameter(parameterName);

                     parametersReturn["" + parameterName] = "" + parameterValue;
              }

              return parametersReturn;
       })();

       requestReturn["headers"] = headersJSON;
       requestReturn["parameters"] = parametersJSON;

       return requestReturn;
})();