1 // it is depending on Datajs.js and httpsproxy.js
2 /*
3 * Sybase datajs integration with PhoneGap HTTPS proxy
4 *
5 * datajs-https-proxy.js
6 * This file will not be regenerated, so it is possible to modify it, but it
7 * is not recommended.
8 *
9 * Copyright (c) 2013 Sybase Inc. All rights reserved.
10 */
11
12
13 /**
14 * The namespace for HTTP(S) proxy
15 * @namespace
16 */
17 HttpsConnection = (typeof HttpsConnection === "undefined" || !HttpsConnection) ? {} : HttpsConnection; // 'namespace'
18
19 (function(HttpsConnection, window, undefined) {
20
21 /**
22 * Generate an OData HttpClient object over https proxy of native platform.
23 *
24 * This object will re-direct all odata request to the http proxy because even with HTTP connection, there are
25 * are some known issue by default setting since the application in device is cross server accessing the odata service.
26 * See: http://datajs.codeplex.com/discussions/396112 for details of the issue.
27 *
28 * Call this method normally on HTML page load event to replace the default odata HTTP client.
29 * @memberOf HttpsConnection
30 * @public
31 * @example
32 * // Call datajs api without certificate, users could call just as normal by passing
33 * // URL as first argument
34 * var length = 0;
35 * var updateUri = server + "/example.svc/Categories(1)";
36 *
37 * OData.read(server + "/example.svc/Categories",
38 * function (data, response) {
39 * alert("length " + data.results.length);
40 * length = data.results.length;
41 * if ( length > 0 )
42 * {
43 * var updateRequest = {
44 * requestUri: updateUri,
45 * method: "PUT",
46 * data:
47 * {
48 * Picture: new Date().getTime(),
49 * Description: "Update Record",
50 * CategoryName: "Updated Category",
51 * CategoryID: 1
52 * }
53 * };
54 *
55 * OData.request(updateRequest,
56 * function (data, response) {
57 * alert("Response " + JSON.stringify(response));
58 * },
59 * function (err) {
60 * alert("Error occurred " + err.message);
61 * }
62 * );
63 * };
64 * },
65 * function (err) {
66 * alert("Error occurred " + err.message);
67 * });
68 *
69 * // However, to specify certificate source in the method call, users need to pass in
70 * // the request object instead of URL,
71 * // and add the field "certificateSource" to the request object.
72 * var length = 0;
73 * var updateUri = server + "/example.svc/Categories(1)";
74 *
75 * OData.read({ requestUri: server + "/example.svc/Categories", certificateSource : cert},
76 * function (data, response) {
77 * alert("length " + data.results.length);
78 * length = data.results.length;
79 * if ( length > 0 )
80 * {
81 * var updateRequest = {
82 * requestUri: updateUri,
83 * certificateSource : cert,
84 * method: "PUT",
85 * data:
86 * {
87 * Picture: new Date().getTime(),
88 * Description: "Update Record",
89 * CategoryName: "Updated Category",
90 * CategoryID: 1
91 * }
92 * };
93 *
94 * OData.request(updateRequest,
95 * function (data, response) {
96 * alert("Response " + JSON.stringify(response));
97 * },
98 * function (err) {
99 * alert("Error occurred " + err.message);
100 * }
101 * );
102 * };
103 * },
104 * function (err) {
105 * alert("Error occurred " + err.message);
106 * });
107 */
108 HttpsConnection.generateODataHttpClient = function () {
109 if ( HttpsConnection.sendRequest && !OData.defaultHttpClient.HttpsConnectionWrapper) {
110 OData.defaultHttpClient = {
111 HttpsConnectionWrapper: true,
112 request: function (request, success, error) {
113 var url, requestHeaders, requestBody, statusCode, statusText, responseHeaders;
114 var responseBody, requestTimeout, requestUserName, requestPassword, requestCertificate;
115 var client, result;
116
117 url = request.requestUri;
118 requestHeaders = request.headers;
119 requestBody = request.body;
120
121 var successCB = function( data ) {
122 var response = {
123 requestUri: url,
124 statusCode: data.status,
125 statusText: data.status,
126 headers: data.headers,
127 body: (data.responseText ? data.responseText : data.responseBase64)
128 };
129
130 if (response.statusCode >= 200 && response.statusCode <= 299) {
131 if ( success ) {
132 success(response);
133 }
134 } else {
135 if ( error ) {
136 error({ message: "HTTP request failed", request: request, response: response });
137 }
138 }
139 };
140
141 var errorCB = function( data ) {
142 if ( error ) {
143 error({message: data});
144 }
145 };
146
147 if ( request.timeoutMS ) {
148 requestTimeout = request.timeoutMS / 1000;
149 }
150
151 if ( request.certificateSource ) {
152 requestCertificate = request.certificateSource;
153 }
154
155 if ( request.user ) {
156 requestUserName = request.user;
157 }
158
159 if ( request.password ) {
160 requestPassword = request.password;
161 }
162
163 client = HttpsConnection.sendRequest(request.method || "GET", url, requestHeaders, requestBody, successCB, errorCB, requestUserName, requestPassword, requestTimeout, requestCertificate );
164
165 result = {};
166 result.abort = function () {
167 client.abort();
168
169 if ( error ) {
170 error({ message: "Request aborted" });
171 }
172 };
173 return result;
174 }
175 };
176 }
177 };
178
179 })(HttpsConnection, window);
180