forked from ariatemplates/ariatemplates
/
RequestHandler.js
143 lines (130 loc) · 6.2 KB
/
RequestHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2012 Amadeus s.a.s.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Base class for request handler, that handles HTTP errors
* @class aria.modules.test.RequestHandler
*/
Aria.classDefinition({
$classpath : "aria.modules.requestHandler.RequestHandler",
$implements : ["aria.modules.requestHandler.IRequestHandler"],
$dependencies : ["aria.modules.requestHandler.environment.RequestHandler"],
$statics : {
HTTP_ERRORS_GENERAL : "An uncatalogued HTTP error was generated",
HTTP_ERRORS_400 : "400 Bad Request: The request cannot be fulfilled due to bad syntax.",
HTTP_ERRORS_401 : "401 Unauthorized: Similar to 403 Forbidden, but specifically for use when authentication is possible but has failed or not yet been provided.",
HTTP_ERRORS_403 : "403 Forbidden: The request was a legal request, but the server is refusing to respond to it.",
HTTP_ERRORS_404 : "404 Not Found: The requested resource could not be found but may be available again in the future. Subsequent requests by the client are permissible.",
HTTP_ERRORS_500 : "500 Internal Server Error: A generic error message, given when no more specific message is suitable."
},
$constructor : function () {
/**
* JSON serialiazer used to stringify the request jason data before sending the request
* @protected
* @type aria.modules.requestHandler.environment.RequestHandler.RequestJsonSerializerCfg
*/
this._requestJsonSerializer = aria.modules.requestHandler.environment.RequestHandler.getRequestJsonSerializerCfg();
// Listen for environment change event
aria.core.AppEnvironment.$on({
changingEnvironment : {
fn : this.__environmentUpdated,
scope : this
},
environmentChanged : {
fn : this.__environmentUpdated,
scope : this
}
});
},
$destructor : function () {
this._requestJsonSerializer = null;
},
$prototype : {
/**
* Handles the response from the server, and call the associated callback
* @param {aria.modules.RequestBeans.SuccessResponse} successResponse
* @param {aria.modules.RequestBeans.Request} request
* @param {aria.core.JsObject.Callback} callback to call with the response
*/
processSuccess : function (successResponse, request, callback) {
this.$callback(callback, successResponse);
},
/**
* Handles the response from the server, and call the associated callback
* @param {aria.modules.RequestBeans.FailureResponse} failureResponse
* @param {aria.modules.RequestBeans.Request} request
* @param {aria.core.JsObject.Callback} callback to call when the failure is processed
*/
processFailure : function (failureResponse, request, callback) {
var status = failureResponse.status;
// resource forwarded to the callback
var res = {
response : null,
error : true
};
// HTTP error
var messageId = "HTTP_ERRORS_" + status;
var message = this[messageId];
if (!message) {
message = this.HTTP_ERRORS_GENERAL;
}
res.errorData = {
"messageBean" : {
"code" : status,
"localizedMessage" : message,// currently sourced from res, to be migrated to the db as a
// part of another sprint task.
"type" : "HTTPERROR"
}
};
this.$callback(callback, res);
},
/**
* Prepares the request body before the request is sent out
* @param {Object} jsonData The json data that will be sent with this request
* @param {aria.modules.RequestBeans.RequestObject} requestObject The request object being used for this request
* @return {String} The string which should be used as the body of the POST request
*/
prepareRequestBody : function (jsonData, requestObject) {
// we encode POST parameters
return this.serializeRequestData(jsonData, requestObject);
},
/**
* Serializes the data by using the serializer specified in the request or the one specified in the application
* environment
* @param {Object} jsonData
* @param {aria.modules.RequestBeans.RequestObject} requestObject
* @return {String} Stringified representation of the input data
*/
serializeRequestData : function (jsonData, requestObject) {
var reqSerializer = requestObject ? requestObject.requestJsonSerializer : null;
var options = reqSerializer ? reqSerializer.options : null;
var instance = reqSerializer ? reqSerializer.instance : null;
if (instance) {
return aria.utils.Json.convertToJsonString(jsonData, aria.utils.Json.copy(options, true), instance);
} else {
options = options || this._requestJsonSerializer.options;
instance = this._requestJsonSerializer.instance;
return aria.utils.Json.convertToJsonString(jsonData, aria.utils.Json.copy(options, true), instance);
}
},
/**
* Listener for the event environmentChanged raised by any environment container
* (aria.core.environment.Environment)
* @private
*/
__environmentUpdated : function () {
this._requestJsonSerializer = aria.modules.requestHandler.environment.RequestHandler.getRequestJsonSerializerCfg();
}
}
});