% /* -*- java -*- */ %>
<%;
/*
Copyright (c) 2005 Tony Garnock-Jones
Copyright (c) 2005 LShift Ltd.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
///////////////////////////////////////////////////////////////////////////
// AJAJ - Like AJAX, but using JSON instead of XML.
//
// Instead of using a clumsy alien data language, XML, to communicate
// with the client, we use a data language closer to the built-in
// constructs of JavaScript: JSON. (See http://www.json.org/.)
///////////////////////////////////////////////////////////////////////////
// This AJAJ namespace contains functions for building AJAJ services.
// The main function is AJAJ.respond; AJAJ.die and
// AJAJ.exceptionToJson are utilities.
//
// Exceptions are sent back to the client using the constructor
// mechanism (constructor "@exception").
//
var AJAJ = {
die: function (message, detail) {
throw {name: "AJAJServerException",
message: message,
detail: detail};
},
// AJAJ.respond takes as its only argument a handler function. The
// handler function is expected to take a object and return an
// object that can be serialized using JSON.stringify.
//
// AJAJ.respond takes the raw binary data that was POSTed to the
// ASP service we are currently running, deserializes it using
// JSON.parse, and gives it to the handler. The resulting object
// is serialized using JSON.stringify and sent back to the client.
//
// Note the use of a global variable, requestBinaryData! It is
// filled in by the VBScript code above. The reason we need
// VBScript code is that the Microsoft documentation for the
// Request.BinaryRead method is both misleading and wrong. Here's
// a quote from
// http://www.sloppycode.net/Reference/Asp/Ref-32.html :
//
// "Request.BinaryRead() returns (so IIS 5.0 documentation
// says), a VB SafeArray of unsigned bytes. This is NOT SO. In
// JScript, // it isn't anything recognisable at all, and it is
// definitely not // a VB SafeArray as the "toArray()" SafeArray
// method barfs // spectacularly. Stranger still, even though
// VBScript says it's // an array, it can't actually be accessed
// as an array because // it's actually an unsigned 8-bit
// character (Multibyte) STRING!
//
// "Given we know this, VBScript does have the ability to
// manipulate Multibyte strings whereas JScript only allows
// Unicode. So, providing that you are fairly structured and
// write a function to return the next "record" from the raw
// data in VBScript, the rest can be done in JScript."
//
respond: function (handler) {
try {
Response.Write(JSON.stringify(handler(JSON.parse(requestBinaryData))));
} catch (e) {
Response.Write(JSON.stringify(AJAJ.exceptionToJson(e)));
}
},
exceptionToJson: function (e) {
var result;
if ((typeof e == typeof {}) && ('name' in e) && (e.name == "AJAJServerException")) {
result = e;
} else if ((typeof e == typeof {}) && ('message' in e)) {
result = { message: e.message, name: e.name };
} else {
result = { message: e.toString(), name: null };
}
return {toJsonString: function () {
return "@exception " + JSON.stringify(result);
}};
}
};
%>