Merge pull request #11 from openwhisk/mgmt-update

Mgmt update
diff --git a/README.md b/README.md
index 5a8d298..208a825 100644
--- a/README.md
+++ b/README.md
@@ -76,10 +76,10 @@
   "subscription": "false"
 ```
 This will set a rateLimit ratio of 10 calls per 60 second, at an API level.  
-This rateLimit is shared across all users (subescription:false).
+This rateLimit is shared across all users (subescription:false).  
 
 #####reqMapping:
-Supported actions: `remove`, `insert`, `transform`.  
+Supported actions: `remove`, `default`, `insert`, `transform`.  
 Supported locations: `body`, `path`, `header`, `query`.  
 
 _remove:_
@@ -92,7 +92,23 @@
    }
 }
 ```
-This will remove the `password` field from the body of the incoming request, so it is not sent to the backendURL
+This will remove the `password` field from the body of the incoming request, so it's not passed to the backendURL  
+
+_default:_  
+Only `body`, `header`, `query` parameters can have default values.  
+```
+{
+   "action":"default",
+   "from":{
+      "value":"BASIC XXX"
+   },
+   "to":{
+      "name":"Authorization",
+      "location":"header"
+   }
+}
+```
+This will assign the value of `BASIC XXX` to a `header` called `Authorization` but only if the value is not already set.  
 
 _insert:_
 ```
@@ -124,35 +140,30 @@
 }
 ```
 This will transform all incoming `query` parameters into `body` parameters in the outgoing request to the backendURL.  
-Where `*` is a wild card, or you can use the variable name.
+Where `*` is a wild card, or you can use the variable name.  
+
+_Path Parameter Mappings:_  
+To map a path parameter from the incoming Url to a path parameter on the backend Url, you will need to wrap brackets `{}` around the path parameter on the incoming Url as well as the backend Url, for example:  
+`IP:Port/resources/tenant_id/serverless/{myAction}/restified`
 ```
-policies":[
-     {
-        "type":"rateLimit",
-        "value":[
-            "interval":60,
-            "rate":100,
-            "scope":"api"
-            "subscription": "true"
-        ]
-     },
-        "type":"reqMapping",
-        "value":[
-        {
-           "action":"transform",
-           "from":{
-              "name":"<user>",
-              "location":"query"
-           },
-           "to":{
-              "name":"<id>",
-              "location":"body"
-           }
-        }]
-     }]
+"backendURL":"https://openwhisk.stage1.ng.bluemix.net/api/v1/namespaces/APIC-Whisk_test/actions/{ACTION}?blocking=true&result=true",
+"policies":
+  [{
+    "type": "reqMapping",
+    "value": [{
+        "action": "transform",
+        "from": {
+          "name": "myAction",
+          "location": "path"
+        },
+        "to": {
+          "name": "ACTION",
+          "location": "path"
+        }
+      }]
+  }]
 ```
-Each user (subscription:true) will have a rateLimit ratio of 100 calls per 60 seconds at the API level.  
-This will also assign the vaule from the `query` parameter named `user` to a body parameter named `id`.  
+If a path is then invoked on `/serverless/Hello World/restified`, then the value from `{myAction}`, which is `Hello World`, will be assigned to the variable `ACTION` on the backend path.
 
 ####Security
 Supported types: `apiKey`.  
diff --git a/api-gateway-config/scripts/lua/lib/filemgmt.lua b/api-gateway-config/scripts/lua/lib/filemgmt.lua
index 9a50e3f..ca1d793 100644
--- a/api-gateway-config/scripts/lua/lib/filemgmt.lua
+++ b/api-gateway-config/scripts/lua/lib/filemgmt.lua
@@ -18,9 +18,9 @@
 --   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 --   DEALINGS IN THE SOFTWARE.
 
---- @module
---
--- @author Alex Song (songs)
+--- @module filemgmt
+-- Creates the Nginx Conf files
+-- @author Alex Song (songs), David Green (greend)
 
 local utils = require "lib/utils"
 local cjson = require "cjson"
diff --git a/api-gateway-config/scripts/lua/lib/utils.lua b/api-gateway-config/scripts/lua/lib/utils.lua
index 003b74d..d1c1cf1 100644
--- a/api-gateway-config/scripts/lua/lib/utils.lua
+++ b/api-gateway-config/scripts/lua/lib/utils.lua
@@ -18,11 +18,10 @@
 --   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 --   DEALINGS IN THE SOFTWARE.
 
---- @module
---
--- @author Alex Song (songs), Cody Walker (cmwalker)
+--- @module utils
+-- Holds the common supporting functions in one file to be referenced else where
+-- @author Alex Song (songs), Cody Walker (cmwalker), David Green (greend)
 
-local cjson = require "cjson"
 local _Utils = {}
 
 --- Concatenate a list of strings into a single string. This is more efficient than concatenating
diff --git a/api-gateway-config/scripts/lua/policies/mapping.lua b/api-gateway-config/scripts/lua/policies/mapping.lua
index a970bf4..6748fe3 100644
--- a/api-gateway-config/scripts/lua/policies/mapping.lua
+++ b/api-gateway-config/scripts/lua/policies/mapping.lua
@@ -20,6 +20,7 @@
 
 --- @module mapping
 -- Process mapping object, turning implementation details into request transformations
+-- @author Cody Walker (cmwalker), Alex Song (songs), David Green (greend)
 
 local logger = require "lib/logger"
 local utils = require "lib/utils"
@@ -43,6 +44,8 @@
       removeParam(v)
     elseif v.action == "transform" then
       transformParam(v)
+    elseif v.action == "default" then
+      checkDefault(v)
     else
       logger.err(utils.concatStrings({'Map action not recognized. Skipping... ', v.action}))
     end
@@ -62,7 +65,6 @@
   for k, v in pairs (incomingQuery) do
     query[k] = v
   end
-
 end
 
 --- Insert parameter value to header, body, or query params into request
@@ -116,6 +118,18 @@
   end
 end
 
+--- Checks if the header has been set, and sets the header to a value if found to be null.
+-- @param m Header name and value to be set, if header is null.
+function checkDefault(m)
+  if m.to.location == "header" and headers[m.to.name] == nil then
+    insertHeader(m.to.name, m.from.value)
+  elseif m.to.location == "query" and query[m.to.name] == nil then
+    insertQuery(m.to.name, m.from.value)
+  elseif m.to.location == "body" and body[m.to.name] == nil then
+    insertBody(m.to.name, m.from.value)
+  end
+end
+
 --- Function to handle wildcarding in the transform process.
 -- If the value in the from object is '*', this function will pull all values from the incoming request
 -- and move them to the location provided in the to object
@@ -181,6 +195,7 @@
 
 function insertHeader(k, v)
   ngx.req.set_header(k, v)
+  headers[k] = v
 end
 
 function insertQuery(k, v)
@@ -211,7 +226,7 @@
 
 function parseUrl(url)
   local map = {}
-  for k,v in url:gmatch('([^&=?]+)=([^&=?]+)' ) do
+  for k,v in url:gmatch('([^&=?]+)=([^&=?]+)') do
     map[ k ] = decodeQuery(v)
   end
   return map
diff --git a/api-gateway-config/scripts/lua/routing.lua b/api-gateway-config/scripts/lua/routing.lua
index 0ed8f65..d1b45ec 100644
--- a/api-gateway-config/scripts/lua/routing.lua
+++ b/api-gateway-config/scripts/lua/routing.lua
@@ -114,7 +114,7 @@
 function getUriPath(backendPath)
   local uriPath
   local i, j = ngx.var.uri:find(ngx.var.gatewayPath)
-  local incomingPath = ngx.var.uri:sub(j + 1)
+  local incomingPath = ((j and ngx.var.uri:sub(j + 1)) or nil)
   -- Check for backendUrl path
   if backendPath == nil or backendPath== '' or backendPath== '/' then
     uriPath = (incomingPath and incomingPath ~= '') and incomingPath or '/'