1 var REST = function() 2 { 3 var REST = {}; 4 logRequest(); 5 6 /** 7 * Adds an error with the given key to the error object in myData. 8 * @param theKey 9 * @param theErrorMessage 10 */ 11 REST.submitError = function (theKey, theErrorMessage) 12 { 13 if (myData["error"] == null) 14 { 15 myData["error"] = {}; 16 } 17 myData["error"][theKey] = theErrorMessage; 18 }; 19 20 /** 21 * if the input is an array it is returned, else the input is wrapped in an array 22 * @param theInput 23 * @returns {*} 24 */ 25 REST.toArray = function (theInput) 26 { 27 var anArray; 28 if (theInput instanceof Array) 29 { 30 anArray = theInput; 31 } else 32 { 33 anArray = [theInput]; 34 } 35 return anArray; 36 }; 37 38 /** 39 * gets the parameter from context. If theIsMandatory is true an error is submitted 40 * @param theParameter 41 * @param theIsMandatory 42 * @returns {*} 43 */ 44 REST.getParameter = function (theParameter, theIsMandatory) 45 { 46 var aParameter = context.getParameter(theParameter); 47 if (theIsMandatory == true && aParameter == null) 48 { 49 REST.submitError("format", "Invalid input format. Missing \"" + theParameter + "\" parameter"); 50 return REST.formatResponse(); 51 } 52 if (aParameter == null) 53 { 54 aParameter = ""; 55 } 56 if (theParameter == "savedSelection" && aParameter != "") 57 { 58 aParameter = savedSelectionManager.getSelection(aParameter).getAssetIds(); 59 } 60 return aParameter; 61 }; 62 63 /** 64 * makes an array of elements in the each array in the2dArray at the given index 65 * @param the2dArray 66 * @param theIndex 67 * @returns {Array} 68 */ 69 REST.getAllAtIndex = function (the2dArray, theIndex) 70 { 71 var aArrayOfValues = []; 72 var aLength = the2dArray[0].length; 73 for (var i = 0;i < the2dArray.length;i++) 74 { 75 if(the2dArray[i].length != aLength) 76 { 77 throw "LengthNotEq " + aLength + " " + the2dArray[i].length; 78 } 79 aArrayOfValues[aArrayOfValues.length] = the2dArray[i][theIndex]; 80 } 81 return aArrayOfValues; 82 }; 83 84 /** 85 * Gets theParameters from context and formats them correctly for REST.iterateThroughParameters 86 * @param theParameters ex 'src' ['src'] ['src','dest'] 87 * @returns [[valueOfSrc], [valueOfDest]] 88 */ 89 REST.getParametersToIterate = function(theParameters) 90 { 91 theParameters = REST.toArray(theParameters); 92 var aParameterValues = []; 93 if(theParameters.indexOf("src") >= 0 && theParameters.indexOf("savedSelection") >= 0) 94 { 95 var aSS = REST.toArray(REST.getParameter("savedSelection", false)); 96 if (aSS != "") 97 { 98 aParameterValues[aParameterValues.length] = aSS; 99 } else 100 { 101 aParameterValues[aParameterValues.length] = REST.toArray(REST.getParameter("src", true)); 102 } 103 var anIndex = theParameters.indexOf("savedSelection"); 104 theParameters.splice(anIndex, 1); 105 anIndex = theParameters.indexOf("src"); 106 theParameters.splice(anIndex, 1); 107 } 108 for (var i = 0;i < theParameters.length; i++) 109 { 110 aParameterValues[aParameterValues.length] = REST.toArray(REST.getParameter(theParameters[i], true)); 111 } 112 return aParameterValues; 113 }; 114 115 /** 116 * Calls the callback with the values of the parameters 117 * @param theParameters 118 * @param theCallback 119 */ 120 REST.iterateThroughParameters = function (theParameters, theCallback) 121 { 122 try 123 { 124 for (var i = 0; i < theParameters[0].length; i++) 125 { 126 var anArgs = REST.getAllAtIndex(theParameters,i); 127 try 128 { 129 theCallback.apply(theCallback,anArgs); 130 }catch (anErr) 131 { 132 REST.submitError(anArgs[0], theCallback.name + " failed to execute: " + anErr + ". Check Workflow logs for more info"); 133 logger.error("ERROR running a REST workflow: " + anErr + "\n" + anErr.stack); 134 } 135 } 136 } catch (anE) 137 { 138 REST.submitError("format","not all input lengths are equal " + anE); 139 } 140 }; 141 142 /** 143 * makes the JSON look good 144 * @returns {*} 145 */ 146 REST.formatResponse = function() 147 { 148 return JSON.stringify(myData, null, 2); 149 }; 150 151 /** 152 * Returns the apikey request parameter and value in string form 153 * @returns {string} 154 */ 155 REST.getAuthParam = function() 156 { 157 return "apikey=" + apiKeyManager.getOrCreateApiKey("", context.getUser().username); 158 }; 159 160 /** 161 * Adds the acl root path to thePath if the user is in a subroot acl 162 * @param thePath 163 * @returns {*} 164 */ 165 REST.getFullPath = function(thePath) 166 { 167 var aPath = getACLRoot() + thePath + "/"; 168 //normalize any paths like Assets//test/ to Assets/test 169 aPath = aPath.split("//").join("/"); 170 return aPath; 171 }; 172 173 /** 174 * TODO 175 * @param theFullPath 176 * @returns {string|void|*|XML} 177 */ 178 REST.getACLRelativePath = function(theFullPath) 179 { 180 return theFullPath.replace(getACLRoot(), ""); 181 }; 182 183 function getACLRoot() 184 { 185 var aUsersRootPath = context.getACL().getRootPath(); 186 if (aUsersRootPath == null && aUsersRootPath == "") 187 { 188 var aUsersAcls = context.getUser().getPrimaryGroup().getACLs(); 189 if (aUsersAcls.length <= 0) 190 { 191 aUsersRootPath = null; 192 } else 193 { 194 aUsersRootPath = aUsersAcls[0].getRootPath(); 195 } 196 } 197 if (aUsersRootPath == null) 198 { 199 throw "Couldn't find ACL for current user"; 200 } 201 return aUsersRootPath; 202 } 203 204 /** 205 * Executes a search with the parameters the pageSize, pageNumber, sortDirection, and sortField 206 * @param theSearch 207 * @param theCallback the search callback 208 */ 209 REST.executeWithPaging = function(theSearch, theCallback) 210 { 211 var aPageSize = REST.getParameter("pageSize", false); 212 var aPageNumber = REST.getParameter("pageNumber", false); 213 var aSortDirection = REST.getParameter("sortDirection", false); 214 var aSortField = REST.getParameter("sortField",false); 215 if (!!aSortDirection && !!aSortField) 216 { 217 theSearch.setSortDirection(aSortDirection); 218 theSearch.setSortField(aSortField); 219 } 220 aPageSize = !aPageSize ? 100 : aPageSize; 221 aPageNumber = !aPageNumber ? 0 : aPageNumber; 222 theSearch.execute(theCallback, aPageNumber, aPageSize); 223 }; 224 225 /** 226 * Logs the request made to the REST workflow 227 */ 228 function logRequest() 229 { 230 var aToIgnore = ["request.headers"]; 231 var aToLog = {}; 232 var aParameters = context.getParameterNames(); 233 for (var i in aParameters) 234 { 235 var aParamName = aParameters[i]; 236 var aParamVal = context.getParameter(aParamName); 237 if (aToIgnore.indexOf(aParameters[i]) >= 0) 238 { 239 continue; 240 } 241 if (aParamName.indexOf("MBUploadedFile") >= 0) 242 { 243 //looks like we found our selves an uploaded file 244 //add it back as its original name 245 var anOriginal = aParamName.split("MBUploadedFile").join(""); 246 var aFileObject = context.getParameter(anOriginal); 247 aToLog[anOriginal] = { 248 "isFile" : true, 249 "name" : aFileObject.name, 250 "length" : aFileObject.length 251 } 252 } else 253 { 254 aToLog[aParamName] = aParamVal; 255 } 256 } 257 logger.config("REST API request parameters:\n " + JSON.stringify(aToLog, null, 2)); 258 } 259 260 return REST; 261 };