1 jls.loader.provide('jls.net.http.HttpExchange');
  2 
  3 jls.loader.require('jls.net.http.HttpResponseHeader');
  4 jls.loader.require('jls.net.SelectionHandlerSequence');
  5 
  6 /**
  7  * @class This class encapsulates a HTTP request received and a response to be generated in one exchange.
  8  * It provides methods for examining the request from the client, and for building and sending the response.
  9  */
 10 jls.net.http.HttpExchange = jls.lang.Class.create(/** @lends jls.net.http.HttpExchange.prototype */
 11 {
 12     initialize : function(context, header) {
 13         this._context = context;
 14         this._header = header; // jls.net.http.HttpRequestHeader
 15 		var version = header.getVersion();
 16 		//version = jls.net.http.HttpHeader.VERSION_PREFIX + jls.net.http.HttpHeader.VERSION_1_0;
 17         this._responseHeader = new jls.net.http.HttpResponseHeader(version);
 18         this._requestHandler = null;
 19     },
 20     /**
 21      * Gets the request URI.
 22      * 
 23      * @returns {String} the request URI.
 24      */
 25     getRequestURI : function() {
 26         return this._header.getUri();
 27     },
 28     /**
 29      * Gets the request method.
 30      * 
 31      * @returns {String} the request method.
 32      */
 33     getRequestMethod : function() {
 34         return this._header.getMethod();
 35     },
 36     /**
 37      * Gets the context.
 38      * 
 39      * @returns {jls.net.http.HttpContext} the context.
 40      */
 41     getHttpContext : function() {
 42         return this._context;
 43     },
 44     close : function() {
 45     },
 46     getRemoteAddress : function() {},
 47     /**
 48      * Gets the response code.
 49      * 
 50      * @returns {Number} the response code.
 51      */
 52     getResponseCode : function() {
 53         this._responseHeader.getStatusCode();
 54     },
 55     getLocalAddress : function() {},
 56     getProtocol : function() {
 57         return this._header.getVersion();
 58     },
 59     /**
 60      * Gets the request header.
 61      * 
 62      * @returns {jls.net.http.HttpRequestHeader} the request header.
 63      */
 64     getRequestHeaders : function() {
 65         return this._header;
 66     },
 67     /**
 68      * Gets the response header.
 69      * 
 70      * @returns {jls.net.http.HttpResponseHeader} the response header.
 71      */
 72     getResponseHeaders : function() {
 73         return this._responseHeader;
 74     },
 75     /*
 76      * If responseLength > 0, specifies a fixed response body length that exact number of bytes must be written
 77 	 * to the stream acquired from getResponseBody(), or else if equal to 0, then chunked encoding is used, 
 78 	 * and an arbitrary number of bytes may be written.
 79 	 * if <= -1, then no response body length is specified and no response body may be written.
 80      */
 81     sendResponseHeaders : function(rCode, responseLength) {
 82         this._responseHeader.setStatusCode(rCode);
 83 		if (typeof responseLength == 'undefined') {
 84 			throw new jls.lang.Exception('Invalid response length');
 85 		} else if (typeof responseLength == 'string') {
 86 			var sh = new jls.net.BufferSelectionHandler(jls.lang.ByteBuffer.fromString(responseLength));
 87 			jls.logger.info('response body: ->' + responseLength + '<-');
 88 			this.setResponseBodySelectionHandler(sh);
 89 			responseLength = sh.hasLength() ? sh.length() : 0;
 90             this._responseHeader.setField(jls.net.http.HttpHeader.HEADER_CONTENT_TYPE, 'text/html; charset=UTF-8');
 91 		} else if (responseLength instanceof jls.net.SelectionHandler) {
 92 			this.setResponseBodySelectionHandler(responseLength);
 93 			responseLength = responseLength.hasLength() ? responseLength.length() : 0;
 94 		}
 95         this._responseHeader.setField(jls.net.http.HttpHeader.HEADER_CONTENT_LENGTH, responseLength);
 96 		jls.logger.info('response header: ->' + this._responseHeader.toString() + '<-');
 97     },
 98     /**
 99      * Sets the request body selection handler.
100      * 
101      * @param {jls.net.SelectionHandler} sh The selection handler to use for the request body.
102      */
103     setRequestBodySelectionHandler : function(sh) {
104         this._requestHandler = sh;
105     },
106     /**
107      * Sets the response body selection handler.
108      * 
109      * @param {jls.net.SelectionHandler} sh The selection handler to use for the response body.
110      */
111     setResponseBodySelectionHandler : function(sh) {
112         this._responseHandler = new jls.net.SelectionHandlerSequence(this._responseHeader, sh);
113         //this._responseHandler.addSelectionHandler(sh);
114     },
115     onRead : function(channel) {
116         if (this._requestHandler == null) {
117             // No request body to read ...
118             return jls.net.SelectionHandler.STATUS_DONE;
119         }
120     },
121     onWrite : function(channel) {
122         return this._responseHandler.onWrite(channel);
123     }
124 });
125 
126 /*Object.extend(jls.net.http.HttpExchange,
127 {
128 });*/
129 
130