Adding custom headers support to invoker util

revert-70aa11f8
Rasika Perera 8 years ago
parent e71d3947cb
commit 1d3f5f39a7

@ -66,11 +66,15 @@ var invokers = function () {
* @param endpoint Backend REST API url. * @param endpoint Backend REST API url.
* @param responseCallback a function to be called with response retrieved. * @param responseCallback a function to be called with response retrieved.
* @param count a counter which hold the number of recursive execution * @param count a counter which hold the number of recursive execution
* @param headers a list of name value pairs for additional http headers
*/ */
privateMethods["execute"] = function (httpMethod, requestPayload, endpoint, responseCallback, count) { privateMethods["execute"] = function (httpMethod, requestPayload, endpoint, responseCallback, count, headers) {
var xmlHttpRequest = new XMLHttpRequest(); var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open(httpMethod, endpoint); xmlHttpRequest.open(httpMethod, endpoint);
for(var i in headers){
xmlHttpRequest.setRequestHeader(headers[i].name, headers[i].value);
}
xmlHttpRequest.setRequestHeader(constants["CONTENT_TYPE_IDENTIFIER"], constants["APPLICATION_JSON"]); xmlHttpRequest.setRequestHeader(constants["CONTENT_TYPE_IDENTIFIER"], constants["APPLICATION_JSON"]);
xmlHttpRequest.setRequestHeader(constants["ACCEPT_IDENTIFIER"], constants["APPLICATION_JSON"]); xmlHttpRequest.setRequestHeader(constants["ACCEPT_IDENTIFIER"], constants["APPLICATION_JSON"]);
@ -82,7 +86,7 @@ var invokers = function () {
}); });
} else { } else {
xmlHttpRequest.setRequestHeader(constants["AUTHORIZATION_HEADER"], xmlHttpRequest.setRequestHeader(constants["AUTHORIZATION_HEADER"],
constants["BEARER_PREFIX"] + accessToken); constants["BEARER_PREFIX"] + accessToken);
} }
} }
@ -98,9 +102,9 @@ var invokers = function () {
log.debug("Response payload if any : " + xmlHttpRequest.responseText); log.debug("Response payload if any : " + xmlHttpRequest.responseText);
if (xmlHttpRequest.status == 401 && (xmlHttpRequest.responseText == TOKEN_EXPIRED || if (xmlHttpRequest.status == 401 && (xmlHttpRequest.responseText == TOKEN_EXPIRED ||
xmlHttpRequest.responseText == TOKEN_INVALID ) && count < 5) { xmlHttpRequest.responseText == TOKEN_INVALID ) && count < 5) {
tokenUtil.refreshTokenPair(); tokenUtil.refreshTokenPair();
return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, ++count); return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, ++count, headers);
} else { } else {
return responseCallback(xmlHttpRequest); return responseCallback(xmlHttpRequest);
} }
@ -113,8 +117,8 @@ var invokers = function () {
* @param endpoint Backend REST API url. * @param endpoint Backend REST API url.
* @param responseCallback a function to be called with response retrieved. * @param responseCallback a function to be called with response retrieved.
*/ */
privateMethods["initiateXMLHTTPRequest"] = function (httpMethod, requestPayload, endpoint, responseCallback) { privateMethods["initiateXMLHTTPRequest"] = function (httpMethod, requestPayload, endpoint, responseCallback, headers) {
return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, 0); return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, 0, headers);
}; };
/** /**
@ -122,9 +126,9 @@ var invokers = function () {
* @param endpoint Backend REST API url. * @param endpoint Backend REST API url.
* @param responseCallback a function to be called with response retrieved. * @param responseCallback a function to be called with response retrieved.
*/ */
publicXMLHTTPInvokers["get"] = function (endpoint, responseCallback) { publicXMLHTTPInvokers["get"] = function (endpoint, responseCallback, headers) {
var requestPayload = null; var requestPayload = null;
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_GET"], requestPayload, endpoint, responseCallback); return privateMethods.initiateXMLHTTPRequest(constants["HTTP_GET"], requestPayload, endpoint, responseCallback, headers);
}; };
/** /**
@ -133,8 +137,8 @@ var invokers = function () {
* @param requestPayload payload/data if exists which is needed to be send. * @param requestPayload payload/data if exists which is needed to be send.
* @param responseCallback a function to be called with response retrieved. * @param responseCallback a function to be called with response retrieved.
*/ */
publicXMLHTTPInvokers["post"] = function (endpoint, requestPayload, responseCallback) { publicXMLHTTPInvokers["post"] = function (endpoint, requestPayload, responseCallback, headers) {
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_POST"], requestPayload, endpoint, responseCallback); return privateMethods.initiateXMLHTTPRequest(constants["HTTP_POST"], requestPayload, endpoint, responseCallback, headers);
}; };
/** /**
@ -143,8 +147,8 @@ var invokers = function () {
* @param requestPayload payload/data if exists which is needed to be send. * @param requestPayload payload/data if exists which is needed to be send.
* @param responseCallback a function to be called with response retrieved. * @param responseCallback a function to be called with response retrieved.
*/ */
publicXMLHTTPInvokers["put"] = function (endpoint, requestPayload, responseCallback) { publicXMLHTTPInvokers["put"] = function (endpoint, requestPayload, responseCallback, headers) {
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_PUT"], requestPayload, endpoint, responseCallback); return privateMethods.initiateXMLHTTPRequest(constants["HTTP_PUT"], requestPayload, endpoint, responseCallback, headers);
}; };
/** /**
@ -152,9 +156,9 @@ var invokers = function () {
* @param endpoint Backend REST API url. * @param endpoint Backend REST API url.
* @param responseCallback a function to be called with response retrieved. * @param responseCallback a function to be called with response retrieved.
*/ */
publicXMLHTTPInvokers["delete"] = function (endpoint, responseCallback) { publicXMLHTTPInvokers["delete"] = function (endpoint, responseCallback, headers) {
var requestPayload = null; var requestPayload = null;
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_DELETE"], requestPayload, endpoint, responseCallback); return privateMethods.initiateXMLHTTPRequest(constants["HTTP_DELETE"], requestPayload, endpoint, responseCallback, headers);
}; };
/** /**
@ -239,8 +243,9 @@ var invokers = function () {
* @param payload payload/data which need to be send. * @param payload payload/data which need to be send.
* @param successCallback a function to be called if the respond if successful. * @param successCallback a function to be called if the respond if successful.
* @param errorCallback a function to be called if en error is reserved. * @param errorCallback a function to be called if en error is reserved.
* @param headers a list of name value pairs for additional http headers.
*/ */
privateMethods["initiateHTTPClientRequest"] = function (method, url, successCallback, errorCallback, payload) { privateMethods["initiateHTTPClientRequest"] = function (method, url, successCallback, errorCallback, payload, headers) {
//noinspection JSUnresolvedVariable //noinspection JSUnresolvedVariable
var HttpClient = Packages.org.apache.commons.httpclient.HttpClient; var HttpClient = Packages.org.apache.commons.httpclient.HttpClient;
var httpMethodObject; var httpMethodObject;
@ -269,6 +274,14 @@ var invokers = function () {
//noinspection JSUnresolvedFunction //noinspection JSUnresolvedFunction
throw new IllegalArgumentException("Invalid HTTP request method: " + method); throw new IllegalArgumentException("Invalid HTTP request method: " + method);
} }
for(var i in headers){
var header = new Header();
header.setName(headers[i].name);
header.setValue(headers[i].value);
httpMethodObject.addRequestHeader(header);
}
//noinspection JSUnresolvedVariable //noinspection JSUnresolvedVariable
var Header = Packages.org.apache.commons.httpclient.Header; var Header = Packages.org.apache.commons.httpclient.Header;
var header = new Header(); var header = new Header();
@ -295,15 +308,19 @@ var invokers = function () {
} }
} }
//noinspection JSUnresolvedFunction //noinspection JSUnresolvedFunction
var stringRequestEntity = new StringRequestEntity(stringify(payload)); if (payload != null) {
//noinspection JSUnresolvedFunction var StringRequestEntity = Packages.org.apache.commons.httpclient.methods.StringRequestEntity;
httpMethodObject.setRequestEntity(stringRequestEntity); var stringRequestEntity = new StringRequestEntity(stringify(payload));
//noinspection JSUnresolvedFunction
httpMethodObject.setRequestEntity(stringRequestEntity);
}
var client = new HttpClient(); var client = new HttpClient();
try { try {
//noinspection JSUnresolvedFunction //noinspection JSUnresolvedFunction
client.executeMethod(httpMethodObject); client.executeMethod(httpMethodObject);
//noinspection JSUnresolvedFunction //noinspection JSUnresolvedFunction
var status = httpMethodObject.getStatusCode(); var status = httpMethodObject.getStatusCode();
new Log().error(status);
if (status == 200) { if (status == 200) {
//noinspection JSUnresolvedFunction //noinspection JSUnresolvedFunction
return successCallback(httpMethodObject.getResponseBody()); return successCallback(httpMethodObject.getResponseBody());
@ -315,7 +332,9 @@ var invokers = function () {
return errorCallback(response); return errorCallback(response);
} finally { } finally {
//noinspection JSUnresolvedFunction //noinspection JSUnresolvedFunction
method.releaseConnection(); if (method != constants["HTTP_GET"]) {
method.releaseConnection();
}
} }
}; };
@ -324,11 +343,12 @@ var invokers = function () {
* @param url target url. * @param url target url.
* @param successCallback a function to be called if the respond if successful. * @param successCallback a function to be called if the respond if successful.
* @param errorCallback a function to be called if en error is reserved. * @param errorCallback a function to be called if en error is reserved.
* @param headers a list of name value pairs for additional http headers.
*/ */
publicHTTPClientInvokers["get"] = function (url, successCallback, errorCallback) { publicHTTPClientInvokers["get"] = function (url, successCallback, errorCallback, headers) {
var requestPayload = null; var requestPayload = null;
return privateMethods. return privateMethods.
initiateHTTPClientRequest(constants["HTTP_GET"], url, successCallback, errorCallback, requestPayload); initiateHTTPClientRequest(constants["HTTP_GET"], url, successCallback, errorCallback, requestPayload, headers);
}; };
/** /**
@ -337,10 +357,11 @@ var invokers = function () {
* @param payload payload/data which need to be send. * @param payload payload/data which need to be send.
* @param successCallback a function to be called if the respond if successful. * @param successCallback a function to be called if the respond if successful.
* @param errorCallback a function to be called if en error is reserved. * @param errorCallback a function to be called if en error is reserved.
* @param headers a list of name value pairs for additional http headers.
*/ */
publicHTTPClientInvokers["post"] = function (url, payload, successCallback, errorCallback) { publicHTTPClientInvokers["post"] = function (url, payload, successCallback, errorCallback, headers) {
return privateMethods. return privateMethods.
initiateHTTPClientRequest(constants["HTTP_POST"], url, successCallback, errorCallback, payload); initiateHTTPClientRequest(constants["HTTP_POST"], url, successCallback, errorCallback, payload, headers);
}; };
/** /**
@ -349,10 +370,11 @@ var invokers = function () {
* @param payload payload/data which need to be send. * @param payload payload/data which need to be send.
* @param successCallback a function to be called if the respond if successful. * @param successCallback a function to be called if the respond if successful.
* @param errorCallback a function to be called if en error is reserved. * @param errorCallback a function to be called if en error is reserved.
* @param headers a list of name value pairs for additional http headers.
*/ */
publicHTTPClientInvokers["put"] = function (url, payload, successCallback, errorCallback) { publicHTTPClientInvokers["put"] = function (url, payload, successCallback, errorCallback, headers) {
return privateMethods. return privateMethods.
initiateHTTPClientRequest(constants["HTTP_PUT"], url, successCallback, errorCallback, payload); initiateHTTPClientRequest(constants["HTTP_PUT"], url, successCallback, errorCallback, payload, headers);
}; };
/** /**
@ -360,11 +382,12 @@ var invokers = function () {
* @param url target url. * @param url target url.
* @param successCallback a function to be called if the respond if successful. * @param successCallback a function to be called if the respond if successful.
* @param errorCallback a function to be called if en error is reserved. * @param errorCallback a function to be called if en error is reserved.
* @param headers a list of name value pairs for additional http headers.
*/ */
publicHTTPClientInvokers["delete"] = function (url, successCallback, errorCallback) { publicHTTPClientInvokers["delete"] = function (url, successCallback, errorCallback, headers) {
var requestPayload = null; var requestPayload = null;
return privateMethods. return privateMethods.
initiateHTTPClientRequest(constants["HTTP_DELETE"], url, successCallback, errorCallback, requestPayload); initiateHTTPClientRequest(constants["HTTP_DELETE"], url, successCallback, errorCallback, requestPayload, headers);
}; };
var publicMethods = {}; var publicMethods = {};

Loading…
Cancel
Save