Source: main/webapp/modules/Status.js

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var Guacamole = Guacamole || {};
  20. /**
  21. * A Guacamole status. Each Guacamole status consists of a status code, defined
  22. * by the protocol, and an optional human-readable message, usually only
  23. * included for debugging convenience.
  24. *
  25. * @constructor
  26. * @param {!number} code
  27. * The Guacamole status code, as defined by Guacamole.Status.Code.
  28. *
  29. * @param {string} [message]
  30. * An optional human-readable message.
  31. */
  32. Guacamole.Status = function(code, message) {
  33. /**
  34. * Reference to this Guacamole.Status.
  35. *
  36. * @private
  37. * @type {!Guacamole.Status}
  38. */
  39. var guac_status = this;
  40. /**
  41. * The Guacamole status code.
  42. *
  43. * @see Guacamole.Status.Code
  44. * @type {!number}
  45. */
  46. this.code = code;
  47. /**
  48. * An arbitrary human-readable message associated with this status, if any.
  49. * The human-readable message is not required, and is generally provided
  50. * for debugging purposes only. For user feedback, it is better to translate
  51. * the Guacamole status code into a message.
  52. *
  53. * @type {string}
  54. */
  55. this.message = message;
  56. /**
  57. * Returns whether this status represents an error.
  58. *
  59. * @returns {!boolean}
  60. * true if this status represents an error, false otherwise.
  61. */
  62. this.isError = function() {
  63. return guac_status.code < 0 || guac_status.code > 0x00FF;
  64. };
  65. };
  66. /**
  67. * Enumeration of all Guacamole status codes.
  68. */
  69. Guacamole.Status.Code = {
  70. /**
  71. * The operation succeeded.
  72. *
  73. * @type {!number}
  74. */
  75. "SUCCESS": 0x0000,
  76. /**
  77. * The requested operation is unsupported.
  78. *
  79. * @type {!number}
  80. */
  81. "UNSUPPORTED": 0x0100,
  82. /**
  83. * The operation could not be performed due to an internal failure.
  84. *
  85. * @type {!number}
  86. */
  87. "SERVER_ERROR": 0x0200,
  88. /**
  89. * The operation could not be performed as the server is busy.
  90. *
  91. * @type {!number}
  92. */
  93. "SERVER_BUSY": 0x0201,
  94. /**
  95. * The operation could not be performed because the upstream server is not
  96. * responding.
  97. *
  98. * @type {!number}
  99. */
  100. "UPSTREAM_TIMEOUT": 0x0202,
  101. /**
  102. * The operation was unsuccessful due to an error or otherwise unexpected
  103. * condition of the upstream server.
  104. *
  105. * @type {!number}
  106. */
  107. "UPSTREAM_ERROR": 0x0203,
  108. /**
  109. * The operation could not be performed as the requested resource does not
  110. * exist.
  111. *
  112. * @type {!number}
  113. */
  114. "RESOURCE_NOT_FOUND": 0x0204,
  115. /**
  116. * The operation could not be performed as the requested resource is
  117. * already in use.
  118. *
  119. * @type {!number}
  120. */
  121. "RESOURCE_CONFLICT": 0x0205,
  122. /**
  123. * The operation could not be performed as the requested resource is now
  124. * closed.
  125. *
  126. * @type {!number}
  127. */
  128. "RESOURCE_CLOSED": 0x0206,
  129. /**
  130. * The operation could not be performed because the upstream server does
  131. * not appear to exist.
  132. *
  133. * @type {!number}
  134. */
  135. "UPSTREAM_NOT_FOUND": 0x0207,
  136. /**
  137. * The operation could not be performed because the upstream server is not
  138. * available to service the request.
  139. *
  140. * @type {!number}
  141. */
  142. "UPSTREAM_UNAVAILABLE": 0x0208,
  143. /**
  144. * The session within the upstream server has ended because it conflicted
  145. * with another session.
  146. *
  147. * @type {!number}
  148. */
  149. "SESSION_CONFLICT": 0x0209,
  150. /**
  151. * The session within the upstream server has ended because it appeared to
  152. * be inactive.
  153. *
  154. * @type {!number}
  155. */
  156. "SESSION_TIMEOUT": 0x020A,
  157. /**
  158. * The session within the upstream server has been forcibly terminated.
  159. *
  160. * @type {!number}
  161. */
  162. "SESSION_CLOSED": 0x020B,
  163. /**
  164. * The operation could not be performed because bad parameters were given.
  165. *
  166. * @type {!number}
  167. */
  168. "CLIENT_BAD_REQUEST": 0x0300,
  169. /**
  170. * Permission was denied to perform the operation, as the user is not yet
  171. * authorized (not yet logged in, for example).
  172. *
  173. * @type {!number}
  174. */
  175. "CLIENT_UNAUTHORIZED": 0x0301,
  176. /**
  177. * Permission was denied to perform the operation, and this permission will
  178. * not be granted even if the user is authorized.
  179. *
  180. * @type {!number}
  181. */
  182. "CLIENT_FORBIDDEN": 0x0303,
  183. /**
  184. * The client took too long to respond.
  185. *
  186. * @type {!number}
  187. */
  188. "CLIENT_TIMEOUT": 0x0308,
  189. /**
  190. * The client sent too much data.
  191. *
  192. * @type {!number}
  193. */
  194. "CLIENT_OVERRUN": 0x030D,
  195. /**
  196. * The client sent data of an unsupported or unexpected type.
  197. *
  198. * @type {!number}
  199. */
  200. "CLIENT_BAD_TYPE": 0x030F,
  201. /**
  202. * The operation failed because the current client is already using too
  203. * many resources.
  204. *
  205. * @type {!number}
  206. */
  207. "CLIENT_TOO_MANY": 0x031D
  208. };
  209. /**
  210. * Returns the Guacamole protocol status code which most closely
  211. * represents the given HTTP status code.
  212. *
  213. * @param {!number} status
  214. * The HTTP status code to translate into a Guacamole protocol status
  215. * code.
  216. *
  217. * @returns {!number}
  218. * The Guacamole protocol status code which most closely represents the
  219. * given HTTP status code.
  220. */
  221. Guacamole.Status.Code.fromHTTPCode = function fromHTTPCode(status) {
  222. // Translate status codes with known equivalents
  223. switch (status) {
  224. // HTTP 400 - Bad request
  225. case 400:
  226. return Guacamole.Status.Code.CLIENT_BAD_REQUEST;
  227. // HTTP 403 - Forbidden
  228. case 403:
  229. return Guacamole.Status.Code.CLIENT_FORBIDDEN;
  230. // HTTP 404 - Resource not found
  231. case 404:
  232. return Guacamole.Status.Code.RESOURCE_NOT_FOUND;
  233. // HTTP 429 - Too many requests
  234. case 429:
  235. return Guacamole.Status.Code.CLIENT_TOO_MANY;
  236. // HTTP 503 - Server unavailable
  237. case 503:
  238. return Guacamole.Status.Code.SERVER_BUSY;
  239. }
  240. // Default all other codes to generic internal error
  241. return Guacamole.Status.Code.SERVER_ERROR;
  242. };
  243. /**
  244. * Returns the Guacamole protocol status code which most closely
  245. * represents the given WebSocket status code.
  246. *
  247. * @param {!number} code
  248. * The WebSocket status code to translate into a Guacamole protocol
  249. * status code.
  250. *
  251. * @returns {!number}
  252. * The Guacamole protocol status code which most closely represents the
  253. * given WebSocket status code.
  254. */
  255. Guacamole.Status.Code.fromWebSocketCode = function fromWebSocketCode(code) {
  256. // Translate status codes with known equivalents
  257. switch (code) {
  258. // Successful disconnect (no error)
  259. case 1000: // Normal Closure
  260. return Guacamole.Status.Code.SUCCESS;
  261. // Codes which indicate the server is not reachable
  262. case 1006: // Abnormal Closure (also signalled by JavaScript when the connection cannot be opened in the first place)
  263. case 1015: // TLS Handshake
  264. return Guacamole.Status.Code.UPSTREAM_NOT_FOUND;
  265. // Codes which indicate the server is reachable but busy/unavailable
  266. case 1001: // Going Away
  267. case 1012: // Service Restart
  268. case 1013: // Try Again Later
  269. case 1014: // Bad Gateway
  270. return Guacamole.Status.Code.UPSTREAM_UNAVAILABLE;
  271. }
  272. // Default all other codes to generic internal error
  273. return Guacamole.Status.Code.SERVER_ERROR;
  274. };