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         var sequenceHandler = new jls.net.SelectionHandlerSequence(this._requestHeader);
 48         if (this._requestBody != null) {
 49         	var length = this._requestBody.length();
 50             jls.logger.info('send() body ' + length);
 51             this._requestHeader.setContentLength(length);
 52             sequenceHandler.addSelectionHandler(this._requestBody);
 53         }
 54         jls.logger.info('send() request header ->' + this._requestHeader.toString() + '<-');
 55         if (sequenceHandler.onWrite(this._socket) != jls.net.SelectionHandler.STATUS_DONE) {
 56             throw new jls.lang.Exception('Cannot send request');
 57         }
 58         if (this._responseHeader.onRead(this._socket) != jls.net.SelectionHandler.STATUS_DONE) {
 59             throw new jls.lang.Exception('Cannot receive response header');
 60         }
 61         jls.logger.info('response header ->' + this._responseHeader.toString() + '<-');
 62         this.onResponseHeader();
 63         if (this._responseBody == null) {
 64             // TODO Create a nop selection handler with content length
 65             return;
 66         }
 67         if (this._responseBody.onRead(this._socket) != jls.net.SelectionHandler.STATUS_DONE) {
 68             throw new jls.lang.Exception('Cannot receive response body');
 69         }
 70         this.onResponseBody();
 71     },
 72     /**
 73      * This function is called when the response header is available, can be overrided.
 74      *
 75      */
 76     onResponseHeader : function() {
 77     },
 78     /**
 79      * This function is called when the response body is available, can be overrided.
 80      *
 81      */
 82     onResponseBody : function() {
 83     },
 84     /**
 85      * Connects this socket to the server.
 86      * 
 87      * @returns {Number} The response header.
 88      */
 89     getContentLength : function() {
 90         return this._responseHeader.getContentLength();
 91     },
 92     /**
 93      * Returns the response header.
 94      * 
 95      * @returns {jls.net.http.HttpResponseHeader} The response header.
 96      */
 97     getResponseHeader : function() {
 98         return this._responseHeader;
 99     },
100     /**
101      * Returns the response body selection handler.
102      * 
103      * @returns {jls.net.SelectionHandler} The response body selection handler.
104      */
105     getResponseBodySelectionHandler : function() {
106         return this._responseBody;
107     },
108     /**
109      * Sets the response body selection handler.
110      *
111      * @param {jls.net.SelectionHandler} sh The response body selection handler.
112      */
113     setResponseBodySelectionHandler : function(sh) {
114         this._responseBody = sh;
115     },
116     /**
117      * Sets the request body selection handler.
118      *
119      * @param {jls.net.SelectionHandler} sh The request body selection handler.
120      */
121     setRequestBodySelectionHandler : function(sh) {
122         this._requestBody = sh;
123     },
124     /**
125      * Sets the requests method.
126      *
127      * @param {String} value The requests method.
128      */
129     setRequestMethod : function(value) {
130 		this._requestHeader.setMethod(value);
131     },
132     /**
133      * Sets a requests property.
134      *
135      * @param {String} key The property key.
136      * @param {String} value The property value.
137      */
138     setRequestProperty : function(key, value) {
139 		this._requestHeader.setField(key, value);
140     },
141     /**
142      * Disconnects.
143      *
144      */
145     disconnect : function() {
146 		this._socket.close();
147     }
148 });
149 
150 /*Object.extend(jls.net.http.HttpURLConnection,
151 {
152     HTTP_PORT : 80,
153     HTTPS_PORT : 443
154 });
155 */