Fixing invoker service return nothing on httpClient requests.

merge-requests/7/head
Rasika Perera 8 years ago
parent 98ab29fe94
commit 3e2731a772

@ -72,7 +72,7 @@ var invokers = function () {
var xmlHttpRequest = new XMLHttpRequest(); var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open(httpMethod, endpoint); xmlHttpRequest.open(httpMethod, endpoint);
for(var i in headers){ for (var i in headers) {
xmlHttpRequest.setRequestHeader(headers[i].name, headers[i].value); 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"]);
@ -117,9 +117,10 @@ 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, headers) { privateMethods["initiateXMLHTTPRequest"] =
return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, 0, headers); function (httpMethod, requestPayload, endpoint, responseCallback, headers) {
}; return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, 0, headers);
};
/** /**
* This method invokes return initiateXMLHttpRequest for get calls. * This method invokes return initiateXMLHttpRequest for get calls.
@ -128,7 +129,8 @@ var invokers = function () {
*/ */
publicXMLHTTPInvokers["get"] = function (endpoint, responseCallback, headers) { publicXMLHTTPInvokers["get"] = function (endpoint, responseCallback, headers) {
var requestPayload = null; var requestPayload = null;
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_GET"], requestPayload, endpoint, responseCallback, headers); return privateMethods.initiateXMLHTTPRequest(constants["HTTP_GET"], requestPayload, endpoint, responseCallback,
headers);
}; };
/** /**
@ -138,7 +140,8 @@ var invokers = function () {
* @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, headers) { publicXMLHTTPInvokers["post"] = function (endpoint, requestPayload, responseCallback, headers) {
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_POST"], requestPayload, endpoint, responseCallback, headers); return privateMethods.initiateXMLHTTPRequest(constants["HTTP_POST"], requestPayload, endpoint, responseCallback,
headers);
}; };
/** /**
@ -148,7 +151,8 @@ var invokers = function () {
* @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, headers) { publicXMLHTTPInvokers["put"] = function (endpoint, requestPayload, responseCallback, headers) {
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_PUT"], requestPayload, endpoint, responseCallback, headers); return privateMethods.initiateXMLHTTPRequest(constants["HTTP_PUT"], requestPayload, endpoint, responseCallback,
headers);
}; };
/** /**
@ -158,7 +162,8 @@ var invokers = function () {
*/ */
publicXMLHTTPInvokers["delete"] = function (endpoint, responseCallback, headers) { publicXMLHTTPInvokers["delete"] = function (endpoint, responseCallback, headers) {
var requestPayload = null; var requestPayload = null;
return privateMethods.initiateXMLHTTPRequest(constants["HTTP_DELETE"], requestPayload, endpoint, responseCallback, headers); return privateMethods.initiateXMLHTTPRequest(constants["HTTP_DELETE"], requestPayload, endpoint,
responseCallback, headers);
}; };
/** /**
@ -245,100 +250,105 @@ var invokers = function () {
* @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. * @param headers a list of name value pairs for additional http headers.
*/ */
privateMethods["initiateHTTPClientRequest"] = function (method, url, successCallback, errorCallback, payload, headers) { privateMethods["initiateHTTPClientRequest"] =
//noinspection JSUnresolvedVariable function (method, url, successCallback, errorCallback, payload, headers) {
var HttpClient = Packages.org.apache.commons.httpclient.HttpClient; //noinspection JSUnresolvedVariable
var httpMethodObject; var HttpClient = Packages.org.apache.commons.httpclient.HttpClient;
switch (method) { var httpMethodObject;
case constants["HTTP_GET"]: switch (method) {
//noinspection JSUnresolvedVariable case constants["HTTP_GET"]:
var GetMethod = Packages.org.apache.commons.httpclient.methods.GetMethod; //noinspection JSUnresolvedVariable
httpMethodObject = new GetMethod(url); var GetMethod = Packages.org.apache.commons.httpclient.methods.GetMethod;
break; httpMethodObject = new GetMethod(url);
case constants["HTTP_POST"]: break;
//noinspection JSUnresolvedVariable case constants["HTTP_POST"]:
var PostMethod = Packages.org.apache.commons.httpclient.methods.PostMethod; //noinspection JSUnresolvedVariable
httpMethodObject = new PostMethod(url); var PostMethod = Packages.org.apache.commons.httpclient.methods.PostMethod;
break; httpMethodObject = new PostMethod(url);
case constants["HTTP_PUT"]: break;
//noinspection JSUnresolvedVariable case constants["HTTP_PUT"]:
var PutMethod = Packages.org.apache.commons.httpclient.methods.PutMethod; //noinspection JSUnresolvedVariable
httpMethodObject = new PutMethod(url); var PutMethod = Packages.org.apache.commons.httpclient.methods.PutMethod;
break; httpMethodObject = new PutMethod(url);
case constants["HTTP_DELETE"]: break;
//noinspection JSUnresolvedVariable case constants["HTTP_DELETE"]:
var DeleteMethod = Packages.org.apache.commons.httpclient.methods.DeleteMethod; //noinspection JSUnresolvedVariable
httpMethodObject = new DeleteMethod(url); var DeleteMethod = Packages.org.apache.commons.httpclient.methods.DeleteMethod;
break; httpMethodObject = new DeleteMethod(url);
default: break;
//noinspection JSUnresolvedFunction default:
throw new IllegalArgumentException("Invalid HTTP request method: " + method); //noinspection JSUnresolvedFunction
} throw new IllegalArgumentException("Invalid HTTP request method: " + method);
}
//noinspection JSUnresolvedVariable
var Header = Packages.org.apache.commons.httpclient.Header;
for(var i in headers){
var header = new Header();
header.setName(headers[i].name);
header.setValue(headers[i].value);
httpMethodObject.addRequestHeader(header);
}
var header = new Header();
header.setName(constants["CONTENT_TYPE_IDENTIFIER"]);
header.setValue(constants["APPLICATION_JSON"]);
//noinspection JSUnresolvedFunction
httpMethodObject.addRequestHeader(header);
header = new Header();
header.setName(constants["ACCEPT_IDENTIFIER"]);
header.setValue(constants["APPLICATION_JSON"]);
//noinspection JSUnresolvedFunction
httpMethodObject.addRequestHeader(header);
if (devicemgtProps["isOAuthEnabled"]) { //noinspection JSUnresolvedVariable
var accessToken = privateMethods.getAccessToken(); var Header = Packages.org.apache.commons.httpclient.Header;
if (accessToken) { for (var i in headers) {
header = new Header(); var header = new Header();
header.setName(constants["AUTHORIZATION_HEADER"]); header.setName(headers[i].name);
header.setValue(constants["BEARER_PREFIX"] + accessToken); header.setValue(headers[i].value);
//noinspection JSUnresolvedFunction
httpMethodObject.addRequestHeader(header); httpMethodObject.addRequestHeader(header);
} else {
response.sendRedirect(devicemgtProps["appContext"] + "login");
} }
}
//noinspection JSUnresolvedFunction var header = new Header();
if (payload != null) { header.setName(constants["CONTENT_TYPE_IDENTIFIER"]);
var StringRequestEntity = Packages.org.apache.commons.httpclient.methods.StringRequestEntity; header.setValue(constants["APPLICATION_JSON"]);
var stringRequestEntity = new StringRequestEntity(stringify(payload));
//noinspection JSUnresolvedFunction
httpMethodObject.setRequestEntity(stringRequestEntity);
}
var client = new HttpClient();
try {
//noinspection JSUnresolvedFunction //noinspection JSUnresolvedFunction
client.executeMethod(httpMethodObject); httpMethodObject.addRequestHeader(header);
header = new Header();
header.setName(constants["ACCEPT_IDENTIFIER"]);
header.setValue(constants["APPLICATION_JSON"]);
//noinspection JSUnresolvedFunction //noinspection JSUnresolvedFunction
var status = httpMethodObject.getStatusCode(); httpMethodObject.addRequestHeader(header);
if (status == 200) {
var responseContentDispositionHeader = httpMethodObject.getResponseHeader(constants["CONTENT_DISPOSITION_IDENTIFIER"]); if (devicemgtProps["isOAuthEnabled"]) {
if (responseContentDispositionHeader) { var accessToken = privateMethods.getAccessToken();
return successCallback(httpMethodObject.getResponseBodyAsStream(), httpMethodObject.getResponseHeaders()); if (accessToken) {
header = new Header();
header.setName(constants["AUTHORIZATION_HEADER"]);
header.setValue(constants["BEARER_PREFIX"] + accessToken);
//noinspection JSUnresolvedFunction
httpMethodObject.addRequestHeader(header);
} else { } else {
return successCallback(httpMethodObject.getResponseBody()); response.sendRedirect(devicemgtProps["appContext"] + "login");
} }
} else {
return errorCallback(httpMethodObject.getResponseBody());
} }
} catch (e) {
return errorCallback(response);
} finally {
//noinspection JSUnresolvedFunction //noinspection JSUnresolvedFunction
if (method != constants["HTTP_GET"]) { if (payload != null) {
method.releaseConnection(); var StringRequestEntity = Packages.org.apache.commons.httpclient.methods.StringRequestEntity;
var stringRequestEntity = new StringRequestEntity(stringify(payload));
//noinspection JSUnresolvedFunction
httpMethodObject.setRequestEntity(stringRequestEntity);
} }
} var client = new HttpClient();
}; try {
//noinspection JSUnresolvedFunction
client.executeMethod(httpMethodObject);
//noinspection JSUnresolvedFunction
var status = httpMethodObject.getStatusCode();
if (status == 200) {
var responseContentDispositionHeader = httpMethodObject.getResponseHeader(
constants["CONTENT_DISPOSITION_IDENTIFIER"]);
if (responseContentDispositionHeader) {
return successCallback(httpMethodObject.getResponseBodyAsStream(),
httpMethodObject.getResponseHeaders());
} else {
return successCallback(httpMethodObject.getResponseBodyAsString(),
httpMethodObject.getResponseHeaders());
}
} else {
return errorCallback(httpMethodObject.getResponseBodyAsString(),
httpMethodObject.getResponseHeaders());
}
} catch (e) {
return errorCallback(response);
} finally {
//noinspection JSUnresolvedFunction
if (method != constants["HTTP_GET"]) {
method.releaseConnection();
}
}
};
/** /**
* This method invokes return initiateHTTPClientRequest for get calls. * This method invokes return initiateHTTPClientRequest for get calls.
@ -349,8 +359,8 @@ var invokers = function () {
*/ */
publicHTTPClientInvokers["get"] = function (url, successCallback, errorCallback, headers) { publicHTTPClientInvokers["get"] = function (url, successCallback, errorCallback, headers) {
var requestPayload = null; var requestPayload = null;
return privateMethods. return privateMethods.initiateHTTPClientRequest(constants["HTTP_GET"], url, successCallback, errorCallback,
initiateHTTPClientRequest(constants["HTTP_GET"], url, successCallback, errorCallback, requestPayload, headers); requestPayload, headers);
}; };
/** /**
@ -362,8 +372,8 @@ var invokers = function () {
* @param headers a list of name value pairs for additional http headers. * @param headers a list of name value pairs for additional http headers.
*/ */
publicHTTPClientInvokers["post"] = function (url, payload, successCallback, errorCallback, headers) { publicHTTPClientInvokers["post"] = function (url, payload, successCallback, errorCallback, headers) {
return privateMethods. return privateMethods.initiateHTTPClientRequest(constants["HTTP_POST"], url, successCallback, errorCallback,
initiateHTTPClientRequest(constants["HTTP_POST"], url, successCallback, errorCallback, payload, headers); payload, headers);
}; };
/** /**
@ -375,8 +385,8 @@ var invokers = function () {
* @param headers a list of name value pairs for additional http headers. * @param headers a list of name value pairs for additional http headers.
*/ */
publicHTTPClientInvokers["put"] = function (url, payload, successCallback, errorCallback, headers) { publicHTTPClientInvokers["put"] = function (url, payload, successCallback, errorCallback, headers) {
return privateMethods. return privateMethods.initiateHTTPClientRequest(constants["HTTP_PUT"], url, successCallback, errorCallback,
initiateHTTPClientRequest(constants["HTTP_PUT"], url, successCallback, errorCallback, payload, headers); payload, headers);
}; };
/** /**
@ -388,8 +398,8 @@ var invokers = function () {
*/ */
publicHTTPClientInvokers["delete"] = function (url, successCallback, errorCallback, headers) { publicHTTPClientInvokers["delete"] = function (url, successCallback, errorCallback, headers) {
var requestPayload = null; var requestPayload = null;
return privateMethods. return privateMethods.initiateHTTPClientRequest(constants["HTTP_DELETE"], url, successCallback, errorCallback,
initiateHTTPClientRequest(constants["HTTP_DELETE"], url, successCallback, errorCallback, requestPayload, headers); requestPayload, headers);
}; };
var publicMethods = {}; var publicMethods = {};

Loading…
Cancel
Save