Class next.wo.Response

CLASS DESCRIPTION

Extends:
next.util.NextObject

A Response object represents an HTTP response that an application returns to a Web server to complete a cycle of the request-response loop. The composition of a response occurs during the third and final phase of this loop, a phase marked by the propagation of the appendToResponse message through the objects of the application. The Application object first sends this message, passing in a newly-created Response object as an argument. Element objects, which represent the dynamic and static HTML elements on a page, respond to the message by appending their HTML representation to the content of the Response object. Application, Session, and Component objects can also respond to the message by adding information to the Response object.

A Response has two major parts: HTML content and HTTP information. The content is what is displayed in a Web browser; it can include escaped HTML, which is HTML code shown "as is," uninterpreted. The other information encapsulated by a Response object is in the handling the response. This HTTP data includes headers, status codes, and version string. See the HTTP specification or HTTP documentation for descriptions of these items.

As you might expect, the methods of the Response class can be divided into two groups, those that add to a response's HTML content and those that read and set HTTP information. The former group consists of methods that escape HTML (appendContentHTMLAttributeValue and appendContentHTMLString) and those that don't. For images and other binary data, you can use the appendContentData. You can obtain and set the entire content of the response with content and setContent. The following example shows a sequence of "appendContent" messages that compose an HTTP "POST" message:

    aResponse.appendContentString("<form method=\"POST\" action=\"");
    aResponse.appendContentHTMLAttributeValue(aContext.url());
    aResponse.appendContentCharacter('"');
    aResponse.appendContentString(">");

Most of the remaining Response methods set and read the response's HTTP headers, the HTTP status code, and the HTTP version.

Content Encodings

You can set the string encoding used for the response content with setContentEncoding and you find out what the current encoding is with contentEncoding. An integer represents the type of encoding. The following table lists these integer values along with their OPENSTEP string-constant names.

int
Value
OPENSTEP NameNotes
1NSASCIIStringEncoding0 through 127
2NSNEXTSTEPStringEncoding
3NSJapaneseEUCStringEncoding
4NSUTF8StringEncoding
5NSISOLatin1StringEncodingdefault
6NSSymbolStringEncoding
7NSNonLossyASCIIStringEncoding7-bit verbose ASCII to represent all unichars
8NSShiftJISStringEncoding
9NSISOLatin2StringEncoding
10NSUnicodeStringEncoding
11NSWindowsCP1251StringEncodingCyrillic; same as AdobeStandardCyrillic
12NSWindowsCP1252StringEncodingWindows Latin1
13NSWindowsCP1253StringEncodingWindows Greek
14NSWindowsCP1254StringEncodingWindows Turkish
15NSWindowsCP1250StringEncodingWindows Latin2
21NSISO2022JPStringEncoding ISO 2022 Japanese encoding for electronic mail




CONSTRUCTORS

Response

public Response()

Returns an initialized Response instance. HTTP status is set to 200 (OK), client caching is enabled, and the default string encoding is made ISO Latin 1.


METHODS

appendContentCharacter

public void appendContentCharacter(byte aChar)

Appends a single ASCII character (aChar) to the HTTP response.

Example:

    // ...
    if (aFlag)
        aResponse.appendContentCharacter('Y');
    else
        aResponse.appendContentCharacter('N');

appendContentData

public void appendContentData(next.util.ImmutableBytes dataObject)

Appends a data-encapsulating object (dataObject) to the HTTP response.


appendContentHTMLAttributeValue

public void appendContentHTMLAttributeValue(java.lang.String aValue)

Appends an HTML attribute value to the HTTP content after transforming the string aValue into an ImmutableBytes object using the receiver's content encoding. Special HTML characters ("<", ">", "&", and double quote) are escaped so that the browser does not interpret them. In other words, the message

    aResponse.appendContentHTMLAttributeValue("<B>"); 

would transform the argument to "&lt;B&gt;".

See Also: setContentEncoding


appendContentHTMLString

public void appendContentHTMLString(java.lang.String aString)

Appends an HTML string to the HTTP response after transforming the string aString into an ImmutableBytes object using the receiver's content encoding. Special HTML characters ("<", ">", "&", and double quote) are escaped so that the browser does not interpret them. For example, "<P>" becomes "&ltP&gt".

See Also: setContentEncoding


appendContentString

public void appendContentString(java.lang.String aString)

Appends a string to the content of the HTTP response. The string is transformed into an ImmutableBytes object using the receiver's content encoding. The special HTML characters "<", ">", "&", and double-quote are not escaped so the browser can interpret them as HTML.


content

public next.util.ImmutableBytes content()

Returns the HTML content of the receiver as an ImmutableBytes data object. An exception is raised if you attempt to get the content when all elements of the page have not had their chance to append HTML to the response. Thus you should invoke this method only in the application object's appendToResponse method, and then only after super's appendToResponse has been invoked. (For scripted applications, the appendToResponse is implemented in Application.wos).

See Also: setContent, setContentEncoding


contentEncoding

public int contentEncoding()

Returns an integer representing the encoding used for the response content. See Content Encodings in the class description for a mapped list of supported encodings and their Objective-C names. Usually, you will want the response encoding to be the same as that used by the submitting form on the client browser. In this case it is preferable to use Request's formValueEncoding:

    int theEncoding = aContext.request().formValueEncoding();

The default string encoding is ISO Latin1.

See Also: setContent, setContentEncoding


headerForKey

public java.lang.String headerForKey(java.lang.String aKey)

Returns the HTTP header information identified by aKey. If there are multiple headers associated with the one key, only the first one is returned. Returns null if there are no headers for the key.

See Also: setHeader


headerKeys

public next.util.ImmutableVector headerKeys()

Returns an array of string keys associated with the receiver's HTTP headers. Returns null if there are no headers. You could easily test to see if a header is included by doing something similar to this:

    ImmutableVector hKeys =  aResponse.headerKeys();
    if (hKeys.contains("expires")) {
        // do something
    }

See Also: setHeaders


headersForKey

public next.util.ImmutableVector headersForKey(java.lang.String aKey)

Returns all HTTP headers identified by aKey.

See Also: setHeaders


httpVersion

public java.lang.String httpVersion()

Returns the version of HTTP used for the response (for example, "HTTP/1.0").

See Also: setHTTPVersion


setContent

public void setContent(next.util.ImmutableBytes someData)

Sets the HTML content of the HTTP response to someData.

See Also: content


setContentEncoding

public void setContentEncoding(int anInt)

Sets the encoding used for the content of the HTTP response. See Content Encodings in the class description for a mapped list of supported encodings and their Objective-C names. The default string encoding is ISO Latin1.

See Also: contentEncoding


setHTTPVersion

public void setHTTPVersion(java.lang.String aString)

Sets the version of HTTP used for the response (for example, "HTTP/1.0").

See Also: httpVersion


setHeader

public void setHeader(java.lang.String aHeader, java.lang.String aKey)

Sets the HTTP header aHeader in the receiver and associates, for retrieval, the HTTP key aKey with the header. This method is commonly used to set the type of content in a response, for example:

    aResponse.setHeader("text/html", "content-type");

See Also: headerForKey


setHeaders

public void setHeaders(next.util.ImmutableVector headerList, java.lang.String aKey)

Sets the list of HTTP headers headerList in the receiver and associates, for retrieval, the HTTP key aKey with the list of header elements.

See Also: headerKeys, headersForKey


setStatus

public void setStatus(int anInt)

Sets the HTTP status to anInt. Consult the HTTP specification or HTTP documentation for the significance of status integers.

See Also: status


setUserInfo

public void setUserInfo(next.util.ImmutableHashtable anImmutableHashtable)

Sets a dictionary (of type ImmutableHashtable) in the Response object that, as a convenience, can contain any kind of information related to the current response. Objects further down the appendToResponse message "chain" can retrieve this information using userInfo.


status

public int status()

Returns an integer code representing the HTTP status. Consult the HTTP specification or HTTP documentation for the significance of these status codes. By default, the status is 200 ("OK" status).

See Also: setStatus


userInfo

public next.util.ImmutableHashtable userInfo()

Returns a dictionary (of type ImmutableHashtable) that, as a convenience, can contain any kind of information related to the current response. An object further "upstream" in the appendToResponse message "chain" can set this dictionary in the Response object as a way to pass information to other objects.

See Also: setUserInfo