public
Created

  • Download Gist
http://localhost:8080/cdi-demo/rest-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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
// start RESTEasy client API
// namespace
var REST = {
apiURL : null,
loglevel : 0
};
 
// constructor
REST.Request = function (){
REST.log("Creating new Request");
}
 
REST.Request.prototype = {
uri : null,
method : "GET",
username : null,
password : null,
acceptHeader : "*/*",
contentTypeHeader : null,
async : true,
queryParameters : [],
matrixParameters : [],
cookies : [],
headers : [],
entity : null,
execute : function(callback){
var request = new XMLHttpRequest();
var url = this.uri;
var restRequest = this;
for(var i=0;i<this.matrixParameters.length;i++){
url += ";" + REST.encodePathParamName(this.matrixParameters[i][0]);
url += "=" + REST.encodePathParamValue(this.matrixParameters[i][1]);
}
for(var i=0;i<this.queryParameters.length;i++){
if(i == 0)
url += "?";
else
url += "&";
url += REST.encodeQueryParamNameOrValue(this.queryParameters[i][0]);
url += "=" + REST.encodeQueryParamNameOrValue(this.queryParameters[i][1]);
}
for(var i=0;i<this.cookies.length;i++){
document.cookie = escape(this.cookies[i][0])
+ "=" + escape(this.cookies[i][1]);
}
request.open(this.method, url, this.async, this.username, this.password);
var acceptSet = false;
var contentTypeSet = false;
for(var i=0;i<this.headers.length;i++){
if(this.headers[i][0].toLowerCase() == 'accept')
acceptSet = this.headers[i][1];
if(this.headers[i][0].toLowerCase() == 'content-type')
contentTypeSet = this.headers[i][1];
request.setRequestHeader(REST.encodeHeaderName(this.headers[i][0]),
REST.encodeHeaderValue(this.headers[i][1]));
}
if(!acceptSet)
request.setRequestHeader('Accept', this.acceptHeader);
if(this.entity && !contentTypeSet && this.contentTypeHeader){
contentTypeSet = this.contentTypeHeader;
request.setRequestHeader('Content-Type', this.contentTypeHeader);
}
// we use this flag to work around buggy browsers
var gotReadyStateChangeEvent = false;
if(callback){
request.onreadystatechange = function() {
gotReadyStateChangeEvent = true;
REST.log("Got readystatechange");
REST._complete(this, callback);
};
}
var data = this.entity;
if(this.entity){
if(this.entity instanceof Element){
if(!contentTypeSet || REST._isXMLMIME(contentTypeSet))
data = REST.serialiseXML(this.entity);
}else if(this.entity instanceof Document){
if(!contentTypeSet || REST._isXMLMIME(contentTypeSet))
data = this.entity;
}else if(this.entity instanceof Object){
if(!contentTypeSet || contentTypeSet == "application/json")
data = JSON.stringify(this.entity);
}
}
REST.log("Content-Type set to "+contentTypeSet);
REST.log("Entity set to "+data);
request.send(data);
// now if the browser did not follow the specs and did not fire the events while synchronous,
// handle it manually
if(!this.async && !gotReadyStateChangeEvent && callback){
REST.log("Working around browser readystatechange bug");
REST._complete(request, callback);
}
},
setAccepts : function(acceptHeader){
REST.log("setAccepts("+acceptHeader+")");
this.acceptHeader = acceptHeader;
},
setCredentials : function(username, password){
this.password = password;
this.username = username;
},
setEntity : function(entity){
REST.log("setEntity("+entity+")");
this.entity = entity;
},
setContentType : function(contentType){
REST.log("setContentType("+contentType+")");
this.contentTypeHeader = contentType;
},
setURI : function(uri){
REST.log("setURI("+uri+")");
this.uri = uri;
},
setMethod : function(method){
REST.log("setMethod("+method+")");
this.method = method;
},
setAsync : function(async){
REST.log("setAsync("+async+")");
this.async = async;
},
addCookie : function(name, value){
this.cookies.push([name, value]);
},
addQueryParameter : function(name, value){
this.queryParameters.push([name, value]);
},
addMatrixParameter : function(name, value){
this.matrixParameters.push([name, value]);
},
addHeader : function(name, value){
this.headers.push([name, value]);
}
}
 
REST.log = function(string){
if(REST.loglevel > 0)
print(string);
}
 
REST._complete = function(request, callback){
REST.log("Request ready state: "+request.readyState);
if(request.readyState == 4) {
var entity;
REST.log("Request status: "+request.status);
REST.log("Request response: "+request.responseText);
if(request.status >= 200 && request.status < 300){
var contentType = request.getResponseHeader("Content-Type");
if(contentType != null){
if(REST._isXMLMIME(contentType))
entity = request.responseXML;
else if(contentType == "application/json")
entity = JSON.parse(request.responseText);
else
entity = request.responseText;
}else
entity = request.responseText;
}
REST.log("Calling callback with: "+entity);
callback(request.status, request, entity);
}
}
 
REST._isXMLMIME = function(contentType){
return contentType == "text/xml"
|| contentType == "application/xml"
|| (contentType.indexOf("application/") == 0
&& contentType.lastIndexOf("+xml") == (contentType.length - 4));
}
 
//see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
REST.encodeHeaderName = function (val){
// token+ from http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2
// FIXME: implement me
return val;
}
 
//see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
REST.encodeHeaderValue = function (val){
// *TEXT or combinations of token, separators, and quoted-string from http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2
// FIXME: implement me
return val;
}
 
// see http://www.ietf.org/rfc/rfc3986.txt
REST.encodeQueryParamNameOrValue = function (val){
// FIXME: implement me
return val;
}
 
//see http://www.ietf.org/rfc/rfc3986.txt
REST.encodePathSegment = function (val){
// FIXME: implement me
return val;
}
 
//see http://www.ietf.org/rfc/rfc3986.txt
REST.encodePathParamName = function (val){
// FIXME: implement me
return val;
}
 
//see http://www.ietf.org/rfc/rfc3986.txt
REST.encodePathParamValue = function (val){
// FIXME: implement me
return val;
}
 
REST.serialiseXML = function(node){
if (typeof XMLSerializer != "undefined")
return (new XMLSerializer()).serializeToString(node) ;
else if (node.xml) return node.xml;
else throw "XML.serialize is not supported or can't serialize " + node;
}
// start JAX-RS API
REST.apiURL = 'http://localhost:8080/cdi-demo';

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.