Class: modelit.web.server.HttpExchange

DESCRIPTION:

Create a Matlab wrapper for the com.sun.net.httpserver.HttpExchange java object. This object is used when a webservice is called to retrieve request info, such as the data and headers and to prepare the message to be returned. The data sent by the client are stored as redundant data in this constructor.

Properties

No properties

Public Method: HttpExchange

DESCRIPTION:

Create a Matlab wrapper for the com.sun.net.httpserver.HttpExchange java object. This object is used when a webservice is called to retrieve request info, such as the data and headers and to prepare the message to be returned. The data sent by the client are stored as redundant data in this constructor.

CALL:

obj = HttpExchange(httpExchange) obj = HttpExchange(charString)

INPUT:

httpExchange sun.net.httpserver.HttpExchangeImpl

OUTPUT:

obj modelit.webserver.HttpExchange
Documentation for modelit.web.server.HttpExchange/HttpExchange
   doc modelit.web.server.HttpExchange

Public Method: http_callback

DESCRIPTION:

Default callback for webservice.

CALL:

http_callback(obj,port, callbackFcn) http_callback(obj,port, requestName, callbackFcnList)

INPUT:

obj modelit.web.server.HttpExchange
port int
callbackFcn: <@(event, port) => [msg,response,content_type]>
    function with signature:
        [msg,response,content_type]=fp_callback(event,port)
requestName string
Refers to field in http-get string that corresponds
to "callbackFcnList.name". By convention either:
  empty
    ==> skip search
  char
    ==> retrieve parameter and check callbackFcnList.name for
    match
function pointer ==> check callbackFcnList.name for match with
    requestName(obj)
callbackFcnList struct[]
struct array with fields
+----name
+----action

OUTPUT:

      No output.
 
  EXAMPLE (1):
     actions        = struct('name','traveltime','action',@do_tileViewer_spt);
     actions(end+1) = struct('name','speed',     'action',@do_tileViewer_ndw);
     server = modelit.web.server.Server(hostname, port, @(e)http_callback(e, port,'maptype',actions));
     In this example the "maptype" parameter in queries will be
     matched with the "name" parameters in the struct array
     "actions".
 
     Note that a better way to divert calls to
     different callback is to use the "context" property, as in
     the next example.
 
  EXAMPLE (2):
     server = modelit.web.server.Server(hostname, port)
     addContext(obj, '/spt', @(e)http_callback(e, port,@do_tileViewer_spt) )
     addContext(obj, '/spt', @(e)http_callback(e, port,@do_tileViewer_ndw) )

Public Method: send

DESCRIPTION:

Send the response and statuscode to the requester.

CALL:

send(obj, statuscode, response)

INPUT:

obj modelit.webserver.HttpExchange
statuscode number
(optional, default: 200) http statuscode,
e.g. 200 ok, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
response modelit.webserver.HttpExchange
The response data
(optional, default: '')

OUTPUT:

      No output.

Public Method: getQueryValue

DESCRIPTION:

Returns the value for a given query parameter.

CALL:

value = getQueryValue(obj, name, default) value = getQueryValue(obj, name, default,query)

INPUT:

obj modelit.webserver.HttpExchange
name string
name of attribute to retrieve
default any
Default value for this parameter, also used to convert
the value to the required format. The query values are always stored as strings.
query struct array
Preprocessed value obtained with "query=getQuery(obj)"

OUTPUT:

value any
The value for the given query name, converted to the
same format as the default value.

EXAMPLE:


            

Public Method: getQuery

DESCRIPTION:

Returns the query parameters of the http request. (The parameters after the ? in the URL).

CALL:

value = getQuery(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value struct[]
One struct per parameter, value pair
fields: -name, -value

Public Method: httpget2struct

DESCRIPTION:

Convert httpget string to structure with input arguments. Take care of name completion; Supply default arguments; Cast parameters to correct type.

INPUT:

obj modelit.web.server.HttpExchange
defopt any
Structure with default parameters.
deftop.NAME=VALUE implies that VALUE is default parameter value
for NAME.
varargin any

OUTPUT:

opt struct
structure with options.
errorMsg string
informative textual feedback in case of syntax error
qstr string
text of queary after urldecode

NOTES any
- Current utility reads "simple" hhtp get strings, meaning that
name/value pairs are read from input string. Values must be scalar
and of type CHAR, LOGICAL, NUMERIC. LOGICAL values must be supplied
as "0" or "1"
- An equivalent function httpget2struct exists as well.
This can be used while debugging callback functions from
example query strings.

Public Method: getQueryString

DESCRIPTION:

Returns the query parameters of the http request as a string.

CALL:

value = getQueryString(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value string
With name value pairs

Public Method: char

DESCRIPTION:

Equivalent to getQueryString but returns requestBody if requestQuery is empty.

CALL:

value = char(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value string
With the query string or the requestBody if the
requestQuery is empty.

Public Method: addResponseHeader

DESCRIPTION:

Add a header with a value to the response.

CALL:

obj = HttpExchange(httpExchange)

INPUT:

obj modelit.webserver.HttpExchange
varargin containers.Map|cellstr
with param, value pairs.

OUTPUT:

      No output.
 

EXAMPLE:

    addResponseHeader(obj, {'contenttype', 'text/html'})
 

Public Method: getPath

DESCRIPTION:

Returns the decoded path component of this URI.

CALL:

value = getPath(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value string
The path of the the URI.

Public Method: getResponseHeaders

DESCRIPTION:

Returns the response headers.

CALL:

value = getResponseHeaders(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value containers.Map<string, string
Map with the header
name, value pairs. Multiple values for a single name or
concatenate in a ,-separated list, e.g. "gzip, deflate, br"

Public Method: getRequestBody

DESCRIPTION:

Return the data sent with the http request.

CALL:

value = getRequestBody(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value int8[]
The data as a bytearray (int8) as it was sent
with the http request.

Public Method: getRequestHeaders

DESCRIPTION:

Get the request headers.

CALL:

value = getRequestHeaders(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value containers.Map<string, string
Map with the header
name, value pairs. Multiple values for a single name or
concatenate in a ,-separated list, e.g. "gzip, deflate, br"

Public Method: getRequestMethod

DESCRIPTION:

Returns the http request method.

CALL:

value = getRequestMethod(obj)

INPUT:

obj any
<modelit.webserver.HttpExchange>

OUTPUT:

value string
The http request method. e.g. Post or Get.
See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

Public Method: getLocalAddress

DESCRIPTION:

Returns the local address in the form hostname:port.

CALL:

value = getRemoteAddress(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value string
The local address: hostname:port

Public Method: getRemoteAddress

DESCRIPTION:

Returns the remote address in the form hostname:port.

CALL:

value = getRemoteAddress(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value string
The remote address: hostname:port

Public Method: getProtocol

DESCRIPTION:

Returns the protocol in the form protocol/majorVersion.minorVersion.

CALL:

value = getProtocol(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value string
protocol/majorVersion.minorVersion. e.g. HTTP/1.1

Public Method: getPrincipal

DESCRIPTION:

Returns the HttpPrincipal or '' if no authentication is used. If an authenticator is set on the HttpContext that owns this exchange, then this method will return the HttpPrincipal that represents the authenticated user for this HttpExchange.

CALL:

value = getPrincipal(obj)

INPUT:

obj modelit.webserver.HttpExchange

OUTPUT:

value string
the name of the HttpPrincipal or '' if no
authentication was used.

Private Method: get_action_index

DESCRIPTION:

INPUT:

event any
http event object
requestName any
1 of the following
empty string
    this will result in f_action=1 and errormsg=''
string
    this will read property <string> from event as request_type
    and then find f_action by matching request_type with
    callbackFcnList.name
function pointer
    this will result in request_type=requestName(event);
    and then find f_action by matching request_type with
    callbackFcnList.name
callbackFcnList any
if requestName is nonempty. callbackFcnList.name is matched with
requesttype.

OUTPUT:

errormsg any
Debug message (will be displayed on console)
f_action any
index in struct array "callbackFcnList"