$res

Global object for managing the servlet response.

All content produced via $res.print or Myna.print is appended to an internal response buffer.  This content is nomally sent to the browser at the end of the request.  By using the content maniplulation functions in this object (getContent, clear and print) you can alter content before it reaches the browser.

Note that flush sends content to the browser immediately and resets the buffer.

Summary
$resGlobal object for managing the servlet response.
Functions
clearClears response buffer and returns the previous contents.
getContentReturns current response buffer contents.
flushSends the current response buffer contents to the browser.
requestBasicAuthreturns a “Basic” http auth request
setContentTypesets the content MIME type of the response.
setStatusCodereturns an HTTP status code and optionally a message for this response
setExitCodeSets an exit code (commandline mode only), and optionally sends a message to standard err
metaRedirectRedirect this page using META HTTP-EQUIV=”refresh” tag.
redirectRedirect this page using HTTP 302 temporary redirect.
redirectLoginRedirect this page to the central login page
redirectWithTokenredirect to a URL, including an auth_token for the current user.
serveFileEfficiently serves a file on the filesystem.
setHeadersets a header to return to the browser
printBinarysends binary data to the browser
printAppends content to the response buffer.

Functions

clear

clear:function ()

Clears response buffer and returns the previous contents.

Returns

Response content before it wwas cleared

Detail

Response content created by $res.print is buffered internally.  This function clears that buffer and returns it’s previous contents.

See

$res.print, $res.getContent

getContent

getContent:function ()

Returns current response buffer contents.

Returns

Current response buffer contents

Detail

Response content created by $res.print is buffered internally.  This function returns the current contents of that buffer.

See

$res.print, $res.clear

flush

flush:function (paddingSize)

Sends the current response buffer contents to the browser.

Paramters

paddingSizeOptional default 250 Number of space (“ “) charcters to append to encourage the browser to display the content immediately.

Returns

Current response buffer contents.

Detail

Response content created by $res.print is buffered internally.  This function sends the current contents of that buffer to the browser, and then clears the buffer and returns its former contents.  No changes to the HTTP response can be sent after a call to flushContent, such as cookies.

See

$res.print, $res.clear

requestBasicAuth

requestBasicAuth:function(realm,
message)

returns a “Basic” http auth request

Parameters

realmOptional, default ‘Myna Application Server’ an organization or application name to indicate to the user what credentials to enter
messageOptional, default null A message to include withthe request.  This may not be shown by the browser, or may only be shown after three failures

Detail

sets the WWW-Authenticate header and returns a 401 HTTP status. any content sent after this function call will likely not be displayed by a browser

setContentType

setContentType:function(type)

sets the content MIME type of the response.

Parameters

typedefault ‘text/html’ MIME content type string Returns: Current response buffer contents.

Detail

Response content created by $res.print is buffered internally.  This function returns the current contents of that buffer.

setStatusCode

setStatusCode:function(code,
msg)

returns an HTTP status code and optionally a message for this response

Parameters

codeNumeric HTTP status code such as 403,401,500 etc
msgOptional default null A string message to return to the browser

Example

$res.metaRedirect(“some_other_page.ejs”);

setExitCode

setExitCode:function(code,
msg)

Sets an exit code (commandline mode only), and optionally sends a message to standard err

Parameters

codeNumeric exit code.  0 is considered “OK”, -1 will prevent exiting
msgOptional default null A string message print on standard error output

Example

$res.setExitCode(“1”,”Incorrect number of parameters”)

metaRedirect

metaRedirect:function(url)

Redirect this page using META HTTP-EQUIV=”refresh” tag.

Note

This method preserves any headers, such as cookies, sent to the browser during this request.  Page processing continues after calling this function.

Parameters

urlabsolurte url to redirect to

Example

$res.metaRedirect(“http://example.com/page.html”);

redirect

redirect:function(url)

Redirect this page using HTTP 302 temporary redirect.

Note

This method does NOT preserve any headers, such as cookies, sent to the browser during this request.  Page processing is aborted when calling this function

Security Note

This will only send text before the first hard return to prevent header injection/split responses

Parameters

urlabsolute url (including server) to redirect to.  See $server.resolveUrl for converting relative URLs to absolute URLs

Examples

$res.redirect("http://example.com/page.html");
$res.redirect($server.resolveUrl("fusebox.sjs?fuseaction=main"));

redirectLogin

redirectLogin:function(options)

Redirect this page to the central login page

Parameters

optionsoptions object, see below

Options

callbackUrlURL relative to the current directory that the user should return to after login.
titleOptional, default “Login” Window title for login page
providersOptional, default Myna.Permissions.getAuthTypes + “openid” An array or comma separated list of auth_types to include in the login window.
loginPageOptional, default $server.rootUrl+”myna/auth/auth.sjs?fuseaction=login” If you want to provide a customized login page, indicate the URL here.  Be sure that your page posts to auth.sjs in the same way that dsp_login.ejs does.
messageOptional, default “” A short one-line message to display above the login area

Detail

Myna provides a centralized authentication application that can authenticate a users against any defined auth type.  Calling this function will send an HTTP redirect to the browser to send the user to Myna’s authentication page.  Once the user is authenticated he/she will be redirected back to callbackUrl and an authentication cookie for your application will be generated.  You can examine the logged in user by calling $cookie.getAuthUser.  Authorization can then be performed via Myna.Permissions.User.hasRight

Note

This function calls Myna.abort, so no further processing will be done after a call to this function

redirectWithToken

redirectWithToken:function(url)

redirect to a URL, including an auth_token for the current user.

Parameters

urlurl to redirect to

serveFile

serveFile:function(file)

Efficiently serves a file on the filesystem.

Parameters

filea Myna.File, java.io.File, or MynaPath to a file to serve to the client

Detail

This function replaces the normal output of this response with the contents of file.  Executing this function after a call to $res.flush will likely fail or corrupt the file.  This function is appropriate for for large file downloads as it supports resuming.

Note

When this function returns, the file may not have been downloaded yet.  This is because some browsers/downloaders have advanced multi-threaded download support, so you should plan for this request to be called several times in parallel.  Also if the client pauses and resumes the download you may get multiple calls.

setHeader

setHeader:function(name,
value)

sets a header to return to the browser

Parameters

nameHeader name
value(String or Date) Header value.  If a date, it will be formated to the appropriate format for HTTP headers

Example

$res.setHeader(“Content-disposition”, ‘attachment; filename=”result.xls”’);

Security Note

This will only set text before the first hard return to prevent header injection/split responses

printBinary

printBinary:function(data,
contentType,
filename)

sends binary data to the browser

Parameters

dataBinary data from Myna.File.readBinary or from binary database query (byte [])
contentTypeOptional, default application/octet-stream MIME type of the binary data.  If “” or null, the default will be used
filenameOptional, default null if defined, a “Content-disposition” response header is set to present the standard “Save or Open?” dialog to the client.  Use this if offering a file for download, but not if you expect the content to be rendered inline

Detail

This function replaces the normal output of this response with the contents of data.  Executing this function after a call to $res.flush will likely fail or corrupt the file.

Example

var bytes = new Myna.File("path_to_doc.pdf").readBinary();
$res.printBinary(bytes,"application/pdf","result.pdf");

See Also:

print

print:function (text)

Appends content to the response buffer.

Parameters

texttext to append to response buffer

Returns

void

Detail

Response content created by $res.print is buffered internally.  At the end of the request this content is flushed to the browser

See

$res.clear, $res.getContent

clear:function ()
Clears response buffer and returns the previous contents.
getContent:function ()
Returns current response buffer contents.
flush:function (paddingSize)
Sends the current response buffer contents to the browser.
requestBasicAuth:function(realm,
message)
returns a “Basic” http auth request
setContentType:function(type)
sets the content MIME type of the response.
setStatusCode:function(code,
msg)
returns an HTTP status code and optionally a message for this response
setExitCode:function(code,
msg)
Sets an exit code (commandline mode only), and optionally sends a message to standard err
metaRedirect:function(url)
Redirect this page using META HTTP-EQUIV=”refresh” tag.
redirect:function(url)
Redirect this page using HTTP 302 temporary redirect.
redirectLogin:function(options)
Redirect this page to the central login page
redirectWithToken:function(url)
redirect to a URL, including an auth_token for the current user.
serveFile:function(file)
Efficiently serves a file on the filesystem.
setHeader:function(name,
value)
sets a header to return to the browser
printBinary:function(data,
contentType,
filename)
sends binary data to the browser
print:function (text)
Appends content to the response buffer.
Myna.print=function Myna_print(string)
This is an alias for $res.print
takes a url path relative to the request directory and returns an absolute URL, including the server part.
getAuthTypes:function()
returns an array of valid auth type names for this system
getAuthUser:function()
Return the result of Myna.Permissions.getUserById for the user_id in “myna_auth_cookie”, or null if there is no cookie
Myna.Permissions.User.prototype.hasRight = function(appname,
rightName)
returns true if this user has been assigned the supplied appname and right name
Myna.abort=function Myna_abort(label,
value)
Halts JavaScript execution, optionally showing a message or dumping an object
Myna.File.prototype.readBinary = function()
returns the contents of this file as a java byte[] array