1 /*
2 * Sybase Hybrid App version 2.3
3 *
4 * ExternalResource.js
5 *
6 * This file will not be regenerated, so it is possible to modify it, but it
7 * is not recommended.
8 *
9 * Copyright (c) 2012 Sybase Inc. All rights reserved.
10 *
11 */
12 /**
13 * The namespace for the Hybrid Web Container javascript
14 * @namespace
15 */
16 hwc = (typeof hwc === "undefined" || !hwc) ? {} : hwc; // SUP 'namespace'
17
18 (function() {
19
20 /**
21 * Makes an external cross domain request.
22 *
23 * @public
24 * @memberOf hwc
25 * @param {String} url The url to make request to
26 * @param {anonymous.options} options a set of key/value pairs that configure the underlying request.
27 *
28 * @example
29 *
30 * var options = {
31 * method: "GET",
32 * data: "data",
33 * async: true,
34 * headers: {
35 * "Content-Type": "text/plain;charset=UTF-8"
36 * },
37 * complete: function(response) {
38 * // invoked when the request completes (asynchronous mode)
39 * if (response.status === 200)
40 * alert("Update successful");
41 * else
42 * alert("Update Failed");
43 * }
44 * };
45 *
46 * getExternalResource(url, options);
47 *
48 */
49 hwc.getExternalResource = function(url, options) {
50 var key, _options, params=[], queryString, request, callbackSet, jsonOptions, jsonText, xmlhttp;
51
52 hwc.traceEnteringMethod("hwc.getExternalResource");
53 try {
54 // Default options
55 _options = {
56 method: "GET",
57 async: true
58 //headers: {},
59 //data: '',
60 //complete: function() {}
61 };
62
63 // Fill in options
64 options = options || {};
65
66 for (key in options) {
67 _options[key] = options[key];
68 }
69
70 options = _options;
71 options.method = options.method.toUpperCase();
72
73 if (typeof (options.data) === 'string') {
74 params.push(options.data);
75 }
76 else if (Object.prototype.toString.call(options.data) === '[object Array]') {
77 params = options.data;
78 }
79 else {
80 for (key in options.data) {
81 params.push(encodeURIComponent(key) + "=" + encodeURIComponent(options.data[key]));
82 }
83 }
84
85 // Format query string and post data
86 queryString = params.join("&");
87
88 if (queryString) {
89 if (options.method === "GET") {
90 url = url + (url.indexOf("?") === -1 ? '?' : '&') + queryString;
91 options.data = "";
92 }
93 else {
94 options.data = queryString;
95 }
96 }
97
98 // Make request
99 if (hwc.isBlackBerry()) {
100 request = hwc.getXMLHTTPRequest();
101 request.open(options.method, url, options.async);
102
103 if (options.headers) {
104 for (key in options.headers) {
105 request.setRequestHeader(key, options.headers[key]);
106 }
107 }
108
109 request.onreadystatechange = function() {
110 if (request.readyState === 4) {
111 handleResponse(options, request);
112 }
113 };
114
115 request.send(options.data);
116 }
117 else if (hwc.isAndroid()){
118 if (options.async) {
119 // Setup callbacks
120 callbackSet = new hwc.CallbackSet();
121 options.callback = callbackSet.registerCallback("callback", function(response) { handleResponse(options, response); });
122 }
123
124 // Create a json string for options
125 jsonOptions = JSON.stringify(options);
126
127 jsonText = _HWC.makeExternalRequest(url, jsonOptions) + "";
128
129 if (!options.async && jsonText) {
130 handleResponse(options, JSON.parse(jsonText));
131 }
132 }
133 else if (hwc.isWindowsMobile() || hwc.isWindows()) {
134 // Create a json string for options
135 jsonOptions = JSON.stringify(options);
136
137 try {
138 //make xmlhttp request to load the rmi response from server
139 xmlhttp = hwc.getXMLHTTPRequest();
140
141 //container always sends the request as synced, javascript sends the request based on
142 //caller's choice
143 xmlhttp.open("POST", "/sup.amp?querytype=externalresource&" + hwc.versionURLParam, options.async);
144
145 xmlhttp.onreadystatechange = function() {
146 if (xmlhttp.readyState === 4) {
147 // Success
148 if (xmlhttp.status === 200) {
149 handleResponse(options, JSON.parse(xmlhttp.responseText));
150 }
151 }
152 };
153
154 xmlhttp.send("url=" + encodeURIComponent(url) + "&options=" + encodeURIComponent(jsonOptions));
155 }
156 catch (ex) {
157 alert(ex);
158 }
159 }
160 else if (hwc.isIOS()) {
161 // Create a json string for options
162 jsonOptions = JSON.stringify(options);
163
164 try {
165 //make xmlhttp request to load the rmi response from server
166 xmlhttp = hwc.getXMLHTTPRequest();
167
168 //container always sends the request as synced, javascript sends the request based on
169 //caller's choice
170 xmlhttp.open("GET", "http://localhost/sup.amp?querytype=externalresource&" + hwc.versionURLParam + "&url=" + encodeURIComponent(url) + "&options=" + encodeURIComponent(jsonOptions), options.async);
171 xmlhttp.onreadystatechange = function() {
172 if (xmlhttp.readyState === 4) {
173 // Success
174 handleResponse(options, JSON.parse(xmlhttp.responseText));
175 }
176 };
177
178 xmlhttp.send("");
179
180 }
181 catch (err) {
182 alert(err);
183 }
184 }
185 } finally {
186 hwc.traceLeavingMethod("hwc.getExternalResource");
187 }
188 };
189
190 /**
191 * Internal method to wrap response in a fake xhr
192 * @private
193 * @param options The options provided for the request
194 * @param response The response provided by the container
195 */
196 function handleResponse(options, response) {
197 hwc.traceEnteringMethod("handleResponse");
198 try {
199 var fakeXHR = {
200 "status": response.status,
201 "statusText": response.statusText,
202 "responseText": response.responseText,
203 "getResponseHeader": function(key) {
204 var headerValue, header;
205
206 hwc.traceEnteringMethod("fakeXHR.getResponseHeader");
207 try {
208 if (response.getResponseHeader) {
209 headerValue = response.getResponseHeader(key);
210 }
211 else if (response.headers)
212 {
213 for ( header in response.headers) {
214 if ( key.toLowerCase() === header.toLowerCase() )
215 {
216 headerValue = response.headers[header];
217 break;
218 }
219 }
220 }
221
222 return headerValue === undefined ? null : headerValue;
223 } finally {
224 hwc.traceLeavingMethod("fakeXHR.getResponseHeader");
225 }
226 },
227 "getAllResponseHeaders": function() {
228 var allHeaders, key;
229 hwc.traceEnteringMethod("fakeXHR.getAllResponseHeaders");
230 try {
231 if (response.getAllResponseHeaders) {
232 return response.getAllResponseHeaders();
233 }
234 if (response.headers) {
235 for (key in response.headers) {
236 if (allHeaders) {
237 allHeaders += "\r\n";
238 }
239
240 allHeaders += (key + ":" + response.headers[key]);
241 }
242 return allHeaders;
243 }
244
245 return null;
246 } finally {
247 hwc.traceLeavingMethod("fakeXHR.getAllResponseHeaders");
248 }
249 }
250 };
251
252 if (options.complete) {
253 options.complete(fakeXHR);
254 }
255 } finally {
256 hwc.traceLeavingMethod("handleResponse");
257 }
258 }
259 } ());
260
261 /**
262 * Used to group anonymous objects and callback functions used as method parameters only for purposes of API docs generation only.
263 * Methods and fields in this namespace cannot be instantiated.
264 * <br/>
265 * <b>Used for API docs generation only.</b>
266 * @namespace
267 */
268 anonymous = (typeof anonymous === "undefined" || !anonymous) ? {} : anonymous; // SUP 'namespace'
269
270 /**
271 * Options object used with the {@link getExternalResource} function.
272 *
273 * Supported options are:
274 * <ul>
275 * <li> method: one of GET, PUT, DELETE, HEAD, OPTIONS, or POST. The default is GET.</li>
276 * <li> HTTP and HTTPS urls are supported. </li>
277 * <li> async: request should be sent asynchronously. The default is true. </li>
278 * <li> headers: request headers to be sent with request. </li>
279 * <li> data: data to be sent. If this is an array, it is converted to a query string. For a GET request, this is added to the end of the URL. </li>
280 * <li> {@link anonymous.complete} is a callback function that will be invoked with the resultXHR when this method completes </li>
281 * </ul>
282 * @name anonymous.options
283 */
284
285 /**
286 * Callback function used in the {@link Options} object.
287 *
288 * @name anonymous.complete
289 * @param {object} resultXHR the response object.
290 * <br/>
291 * The fields/methods available on resultXHR are
292 * <ol>
293 * <li> status</li>
294 * <li> statusText</li>
295 * <li> responseText</li>
296 * <li> getReponseHeader(key)</li>
297 * <li> getAllResponesHeaders()</li>
298 * </ol>
299 * These fields and methods are not supported for resultXHR:
300 * <ul>
301 * <li> open() </li>
302 * </ul>
303 * @function
304 */
305