From d37d3914c9cab8696c0f93de61dcd630542efc73 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 19 Jun 2015 21:30:02 -0700 Subject: [PATCH] GUAC-1172: Implement JSONreader for convenience. --- .../src/main/webapp/modules/JSONReader.js | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 guacamole-common-js/src/main/webapp/modules/JSONReader.js diff --git a/guacamole-common-js/src/main/webapp/modules/JSONReader.js b/guacamole-common-js/src/main/webapp/modules/JSONReader.js new file mode 100644 index 000000000..14e2e16da --- /dev/null +++ b/guacamole-common-js/src/main/webapp/modules/JSONReader.js @@ -0,0 +1,117 @@ +/* + * Copyright (C) 2015 Glyptodon LLC + * + * 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. + */ + +var Guacamole = Guacamole || {}; + +/** + * A reader which automatically handles the given input stream, assembling all + * received blobs into a JavaScript object by appending them to each other, in + * order, and decoding the result as JSON. Note that this object will overwrite + * any installed event handlers on the given Guacamole.InputStream. + * + * @constructor + * @param {Guacamole.InputStream} stream + * The stream that JSON will be read from. + */ +Guacamole.JSONReader = function(stream) { + + /** + * Reference to this Guacamole.JSONReader. + * + * @private + * @type Guacamole.JSONReader + */ + var guacReader = this; + + /** + * Wrapped Guacamole.StringReader. + * + * @private + * @type Guacamole.StringReader + */ + var stringReader = new Guacamole.StringReader(stream); + + /** + * All JSON read thus far. + * + * @private + * @type String + */ + var json = ''; + + /** + * Returns the current length of this Guacamole.JSONReader, in characters. + * + * @return {Number} + * The current length of this Guacamole.JSONReader. + */ + this.getLength = function() { + return json.length; + }; + + /** + * Returns the contents of this Guacamole.JSONReader as a JavaScript + * object. + * + * @return {Object} + * The contents of this Guacamole.JSONReader, as parsed from the JSON + * contents of the input stream. + */ + this.getJSON = function() { + return JSON.parse(json); + }; + + // Append all received text + stringReader.ontext = function ontext(text) { + + // Append received text + json += text; + + // Call handler, if present + if (guacReader.onprogress) + guacReader.onprogress(text.length); + + }; + + // Simply call onend when end received + stringReader.onend = function() { + if (guacReader.onend) + guacReader.onend(); + }; + + /** + * Fired once for every blob of data received. + * + * @event + * @param {Number} length + * The number of characters received. + */ + this.onprogress = null; + + /** + * Fired once this stream is finished and no further data will be written. + * + * @event + */ + this.onend = null; + +};