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