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, 'UTF-8'));
 87 			jls.logger.info('response body: ->' + responseLength + '<-');
 88 			this.setResponseBodySelectionHandler(sh);
 89 			responseLength = sh.hasLength() ? sh.length() : 0;
 90             if (! this._responseHeader.hasField(jls.net.http.HttpHeader.HEADER_CONTENT_TYPE)) {
 91                 this._responseHeader.setField(jls.net.http.HttpHeader.HEADER_CONTENT_TYPE, 'text/html; charset=UTF-8');
 92             }
 93 		} else if (responseLength instanceof jls.net.SelectionHandler) {
 94 			this.setResponseBodySelectionHandler(responseLength);
 95 			responseLength = responseLength.hasLength() ? responseLength.length() : 0;
 96 		}
 97         this._responseHeader.setField(jls.net.http.HttpHeader.HEADER_CONTENT_LENGTH, responseLength);
 98 		jls.logger.info('response header: ->' + this._responseHeader.toString() + '<-');
 99     },
100     /**
101      * Sets the request body selection handler.
102      * 
103      * @param {jls.net.SelectionHandler} sh The selection handler to use for the request body.
104      */
105     setRequestBodySelectionHandler : function(sh) {
106         this._requestHandler = sh;
107     },
108     /**
109      * Sets the response body selection handler.
110      * 
111      * @param {jls.net.SelectionHandler} sh The selection handler to use for the response body.
112      */
113     setResponseBodySelectionHandler : function(sh) {
114         this._responseHandler = new jls.net.SelectionHandlerSequence(this._responseHeader, sh);
115         //this._responseHandler.addSelectionHandler(sh);
116     },
117     onRead : function(channel) {
118         if (this._requestHandler == null) {
119             // No request body to read ...
120             return jls.net.SelectionHandler.STATUS_DONE;
121         }
122         return this._requestHandler.onRead(channel);
123     },
124     onWrite : function(channel) {
125         return this._responseHandler.onWrite(channel);
126     }
127 });
128 
129 /*Object.extend(jls.net.http.HttpExchange,
130 {
131 });*/
132 
133