1 jls.loader.provide('jls.net.http.HttpURLConnection');
  2 
  3 jls.loader.require('jls.net.Socket');
  4 jls.loader.require('jls.net.InetSocketAddress');
  5 jls.loader.require('jls.net.http.HttpHeader');
  6 jls.loader.require('jls.net.http.HttpRequestHeader');
  7 jls.loader.require('jls.net.http.HttpResponseHeader');
  8 jls.loader.require('jls.net.SelectionHandler');
  9 jls.loader.require('jls.net.SelectionHandlerSequence');
 10 
 11 /**
 12  * @class This class represents an URL connection over HTTP.
 13  */
 14 jls.net.http.HttpURLConnection = jls.lang.Class.create( /** @lends jls.net.http.HttpURLConnection.prototype */
 15 {
 16     initialize : function(url) {
 17         this._requestHeader = new jls.net.http.HttpRequestHeader();
 18         this._requestBody = null;
 19         this._responseHeader = new jls.net.http.HttpResponseHeader();
 20         this._responseBody = null;
 21         this.setUrl(url);
 22     },
 23     setUrl : function(url) {
 24 		var port = url.getPort();
 25 		if (port == -1) {
 26 			port = url.getDefaultPort();
 27 		}
 28 		this._socket = new jls.net.Socket();
 29         this._address = new jls.net.InetSocketAddress(url.getHost(), port);
 30         this._requestHeader.clear();
 31         this._responseHeader.clear();
 32         this._requestHeader.setUri(url.getFile());
 33         this._requestHeader.setField(jls.net.http.HttpHeader.HEADER_HOST, url.getHost());
 34     },
 35     /**
 36      * Connects.
 37      *
 38      */
 39     connect : function() {
 40 		this._socket.connect(this._address);
 41     },
 42     /**
 43      * Sends.
 44      *
 45      */
 46     send : function() {
 47         jls.logger.info('send() request header ->' + this._requestHeader.toString() + '<-');
 48         var sequenceHandler = new jls.net.SelectionHandlerSequence(this._requestHeader);
 49         if (this._requestBody != null) {
 50             sequenceHandler.addSelectionHandler(this._requestBody);
 51         }
 52         if (sequenceHandler.onWrite(this._socket) != jls.net.SelectionHandler.STATUS_DONE) {
 53             throw new jls.lang.Exception('Cannot send request');
 54         }
 55         if (this._responseHeader.onRead(this._socket) != jls.net.SelectionHandler.STATUS_DONE) {
 56             throw new jls.lang.Exception('Cannot receive response header');
 57         }
 58         jls.logger.info('response header ->' + this._responseHeader.toString() + '<-');
 59         this.onResponseHeader();
 60         if (this._responseBody == null) {
 61             // TODO Create a nop selection handler with content length
 62             return;
 63         }
 64         if (this._responseBody.onRead(this._socket) != jls.net.SelectionHandler.STATUS_DONE) {
 65             throw new jls.lang.Exception('Cannot receive response body');
 66         }
 67         this.onResponseBody();
 68     },
 69     /**
 70      * This function is called when the response header is available, can be overrided.
 71      *
 72      */
 73     onResponseHeader : function() {
 74     },
 75     /**
 76      * This function is called when the response body is available, can be overrided.
 77      *
 78      */
 79     onResponseBody : function() {
 80     },
 81     /**
 82      * Connects this socket to the server.
 83      * 
 84      * @returns {Number} The response header.
 85      */
 86     getContentLength : function() {
 87         return this._responseHeader.getField(jls.net.http.HttpHeader.HEADER_CONTENT_LENGTH);
 88     },
 89     /**
 90      * Returns the response header.
 91      * 
 92      * @returns {jls.net.http.HttpResponseHeader} The response header.
 93      */
 94     getResponseHeader : function() {
 95         return this._responseHeader;
 96     },
 97     /**
 98      * Returns the response body selection handler.
 99      * 
100      * @returns {jls.net.SelectionHandler} The response body selection handler.
101      */
102     getResponseBodySelectionHandler : function() {
103         return this._responseBody;
104     },
105     /**
106      * Sets the response body selection handler.
107      *
108      * @param {jls.net.SelectionHandler} sh The response body selection handler.
109      */
110     setResponseBodySelectionHandler : function(sh) {
111         this._responseBody = sh;
112     },
113     /**
114      * Sets the requests method.
115      *
116      * @param {String} value The requests method.
117      */
118     setRequestMethod : function(value) {
119 		this._requestHeader.setMethod(value);
120     },
121     /**
122      * Sets a requests property.
123      *
124      * @param {String} key The property key.
125      * @param {String} value The property value.
126      */
127     setRequestProperty : function(key, value) {
128 		this._requestHeader.setField(key, value);
129     },
130     /**
131      * Disconnects.
132      *
133      */
134     disconnect : function() {
135 		this._socket.close();
136     }
137 });
138 
139 /*Object.extend(jls.net.http.HttpURLConnection,
140 {
141     HTTP_PORT : 80,
142     HTTPS_PORT : 443
143 });
144 */