1 jls.loader.provide('jls.net.URL'); 2 3 /** 4 * @namespace Provides the classes for implementing networking applications. 5 * @name jls.net 6 */ 7 8 jls.net.URL = jls.lang.Class.create( /** @lends jls.net.URL.prototype */ 9 { 10 /** 11 * Creates an URL. 12 * 13 * @param {String} [protocol] The protocol or the URL as a string. 14 * @param {String} [host] The host name. 15 * @param {Number} [port] The port number. 16 * @param {String} [file] The file part of the URL. 17 * @constructs 18 * @class This class represents an Uniform Resource Locator. 19 * scheme://username:password@domain:port/path?query_string#anchor 20 */ 21 initialize : function(protocol, host, port, file) { 22 this._protocol = null; 23 this._host = null; 24 this._port = -1; 25 this._path = null; 26 this._query = null; 27 this._ref = null; 28 if (typeof protocol == 'string') { 29 if (typeof host == 'undefined') { 30 this._fromExternalForm(protocol); 31 } else { 32 this._protocol = protocol; 33 this._host = host; 34 this._port = port; 35 this.setFile(file); 36 } 37 } else if (typeof protocol == 'object') { 38 this._protocol = protocol.protocol; 39 this._host = protocol.host; 40 this._port = protocol.port; 41 this.setFile(protocol.file); 42 } else { 43 throw new jls.lang.Exception('Invalid arguments'); 44 } 45 }, 46 /** 47 * Returns the protocol. 48 * 49 * @returns {String} The protocol. 50 */ 51 getProtocol : function() { 52 return this._protocol; 53 }, 54 /** 55 * Returns the host. 56 * 57 * @returns {String} The host. 58 */ 59 getHost : function() { 60 return this._host; 61 }, 62 /** 63 * Returns the port. 64 * 65 * @returns {Number} The port. 66 */ 67 getPort : function() { 68 return this._port; 69 }, 70 /** 71 * Returns the default port associated protocol. 72 * 73 * @returns {String} The default port associated protocol. 74 */ 75 getDefaultPort : function() { 76 if (this._protocol == null) { 77 return -1; 78 } 79 var cname = this._protocol.toUpperCase() + '_PORT'; 80 if (cname in jls.net.URL) { 81 return jls.net.URL[cname]; 82 } 83 return -1; 84 }, 85 /** 86 * Returns the file. 87 * 88 * @returns {String} The file. 89 */ 90 getFile : function() { 91 // URL: <protocol>://<host>:<port>/<file> 92 // file: <path>(#<ref>|?<query>) 93 var file = this._path; 94 if (this._query) { 95 file += '?' + this._query; 96 } else if (this._ref) { 97 file += '#' + this._ref; 98 } 99 return file; 100 }, 101 setFile : function(file) { 102 var i = file.indexOf('?'); 103 if (i > 0) { 104 this._path = file.substring(0, i); 105 this._query = file.substr(i + 1); 106 this._ref = null; 107 } else { 108 i = file.indexOf('#'); 109 if (i > 0) { 110 this._path = file.substring(0, i); 111 this._ref = file.substr(i + 1); 112 } else { 113 this._path = file; 114 this._ref = null; 115 } 116 this._query = null; 117 } 118 return this; 119 }, 120 /** 121 * Returns the path. 122 * 123 * @returns {String} The path. 124 */ 125 getPath : function() { 126 return this._path; 127 }, 128 /** 129 * Returns the query. 130 * 131 * @returns {String} The query. 132 */ 133 getQuery : function() { 134 return this._query; 135 }, 136 /** 137 * Returns the anchor. 138 * 139 * @returns {String} The anchor. 140 */ 141 getRef : function() { 142 return this._ref; 143 }, 144 /** 145 * Opens a connection. 146 * 147 * @returns {Object} The connection. 148 */ 149 openConnection : function() { 150 // Look for a URLConnection class for the protocol 151 var protocol = this._protocol.toLowerCase(); 152 if (! (protocol in jls.net.URL._urlConnectionClassMap)) { 153 throw new jls.lang.Exception('No available URLConnection class for the protocol "' + protocol + '"'); 154 } 155 var classname = jls.net.URL._urlConnectionClassMap[protocol]; 156 var clazz = jls.loader.require(classname); 157 return new clazz(this); 158 }, 159 _fromExternalForm : function(s) { 160 var i = s.indexOf('://'); 161 if (i == -1) { 162 throw new jls.lang.Exception('Invalid URL'); 163 } 164 this._protocol = s.substring(0, i); 165 i += 3; 166 var j = s.indexOf('/', i); 167 var file; 168 if ((j > 0) && (j < s.length)) { 169 file = s.substr(j); 170 } else { 171 file = '/'; 172 } 173 var k = s.indexOf(':', i); 174 if ((k != -1) && (k < j)) { 175 this._host = s.substring(i, k); 176 this._port = parseInt(s.substring(k + 1, j)); 177 } else { 178 this._host = s.substring(i, j); 179 this._port = -1; 180 } 181 this.setFile(file); 182 return this; 183 }, 184 /** 185 * Returns the external form of the URL. 186 * 187 * @returns {String} The URL as a string. 188 */ 189 toExternalForm : function() { 190 var s = this._protocol; 191 s += '://' + this._host; 192 if (this._port >= 0) { 193 s += ':' + this._port; 194 } 195 s += this.getFile(); 196 return s; 197 }, 198 /** 199 * Returns the string value of the URL that is its external form. 200 * 201 * @returns {String} The URL as a string. 202 */ 203 toString : function() { 204 return this.toExternalForm(); 205 } 206 }); 207 208 Object.extend(jls.net.URL, /** @lends jls.net.URL */ 209 { 210 _urlConnectionClassMap : { 211 'http' : 'jls.net.http.HttpURLConnection' 212 }, 213 HTTP_PORT : 80, 214 HTTPS_PORT : 443 215 }); 216