diff options
Diffstat (limited to 'projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOMessage.java')
| -rw-r--r-- | projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOMessage.java | 354 |
1 files changed, 148 insertions, 206 deletions
diff --git a/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOMessage.java b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOMessage.java index be76be1..f2310c2 100644 --- a/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOMessage.java +++ b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOMessage.java @@ -24,12 +24,12 @@ import net.wotonomy.foundation.NSMutableData; import net.wotonomy.foundation.NSMutableDictionary; /** -* A pure java implementation of WOResponse. -* -* @author ezamudio@nasoft.com -* @author $Author: cgruber $ -* @version $Revision: 905 $ -*/ + * A pure java implementation of WOResponse. + * + * @author ezamudio@nasoft.com + * @author $Author: cgruber $ + * @version $Revision: 905 $ + */ public class WOMessage { protected String _contentEncoding = "ISO8859_1"; @@ -43,291 +43,233 @@ public class WOMessage { } /** - * Sets the content encoding for the response. - */ - public void setContentEncoding (String encoding) - { + * Sets the content encoding for the response. + */ + public void setContentEncoding(String encoding) { _contentEncoding = encoding; } /** - * Gets the current content encoding for the response. - */ - public String contentEncoding () { + * Gets the current content encoding for the response. + */ + public String contentEncoding() { return _contentEncoding; - } + } /** - * Sets the specified array of values as headers under - * the specified key. - */ - public void setHeaders (NSArray headerArray, String aKey) { - _headers.setObjectForKey( headerArray, aKey ); + * Sets the specified array of values as headers under the specified key. + */ + public void setHeaders(NSArray headerArray, String aKey) { + _headers.setObjectForKey(headerArray, aKey); } /** - * Sets the specified header value for the specified key. - */ - public void setHeader (String aValue, String aKey) { - _headers.setObjectForKey( new NSArray( aValue ), aKey ); + * Sets the specified header value for the specified key. + */ + public void setHeader(String aValue, String aKey) { + _headers.setObjectForKey(new NSArray(aValue), aKey); } /** - * Returns an array of all the header keys that have been - * set in the response. - */ - public NSArray headerKeys () { + * Returns an array of all the header keys that have been set in the response. + */ + public NSArray headerKeys() { return _headers.allKeys(); } /** - * Returns an array of all the header values for the specified key, - * or null if the key does not exist. - */ - public NSArray headersForKey (String aKey) { - return (NSArray)_headers.objectForKey( aKey ); + * Returns an array of all the header values for the specified key, or null if + * the key does not exist. + */ + public NSArray headersForKey(String aKey) { + return (NSArray) _headers.objectForKey(aKey); } /** - * Returns one header value for the specified key. - * Provided as a convenience, since most header keys - * will have a single value. - */ - public String headerForKey (String aKey) { - NSArray values = (NSArray)_headers.objectForKey( aKey ); - if ( values != null && values.count() > 0 ) { - return values.objectAtIndex( 0 ).toString(); + * Returns one header value for the specified key. Provided as a convenience, + * since most header keys will have a single value. + */ + public String headerForKey(String aKey) { + NSArray values = (NSArray) _headers.objectForKey(aKey); + if (values != null && values.count() > 0) { + return values.objectAtIndex(0).toString(); } return null; } /** - * Sets the content of the response to the bytes represented - * by the specified data object. - */ + * Sets the content of the response to the bytes represented by the specified + * data object. + */ public void setContent(NSData aData) { - _contentData.setData( aData ); + _contentData.setData(aData); setHeader(Integer.toString(aData.length()), "content-length"); } /** - * Retrieves the current content of the response. - */ + * Retrieves the current content of the response. + */ public NSData content() { return _contentData; } /** - * Sets the current user info dictionary. These values - * are for application-specific uses and are available to - * other actions and components in the request-response cycle. - */ - public void setUserInfo (NSDictionary aDict) { - _userInfo = new NSMutableDictionary( aDict ); + * Sets the current user info dictionary. These values are for + * application-specific uses and are available to other actions and components + * in the request-response cycle. + */ + public void setUserInfo(NSDictionary aDict) { + _userInfo = new NSMutableDictionary(aDict); } /** - * Gets the current user info dictionary. These values - * are for application-specific uses are are available to - * other actions and components in the request-response cycle. - */ - public NSDictionary userInfo () { + * Gets the current user info dictionary. These values are for + * application-specific uses are are available to other actions and components + * in the request-response cycle. + */ + public NSDictionary userInfo() { return new NSDictionary(_userInfo); } /** - * Appends the bytes in the specified data object to the response. - */ - public void appendContentData (NSData aData) - { - _contentData.appendData( aData ); + * Appends the bytes in the specified data object to the response. + */ + public void appendContentData(NSData aData) { + _contentData.appendData(aData); setHeader(Integer.toString(_contentData.length()), "content-length"); } /** - * Appends the specified byte to the response. - */ - public void appendContentCharacter (char character) { - _contentData.appendByte((byte)character); + * Appends the specified byte to the response. + */ + public void appendContentCharacter(char character) { + _contentData.appendByte((byte) character); setHeader(Integer.toString(_contentData.length()), "content-length"); } /** - * Appends the specified string to the response. - * Any special characters will not be escaped. - * The string will be encoded in the current content encoding. - */ - public void appendContentString (String aString) - { - _contentData.appendData( new NSData( aString.getBytes() ) ); + * Appends the specified string to the response. Any special characters will not + * be escaped. The string will be encoded in the current content encoding. + */ + public void appendContentString(String aString) { + _contentData.appendData(new NSData(aString.getBytes())); setHeader(Integer.toString(_contentData.length()), "content-length"); } /** - * Appends the specified string containing HTML to the response. - * Any special characters will be escaped appropriately. - * The string will be encoded in the current content encoding. - */ - public void appendContentHTMLString (String aString) - { - _contentData.appendData( - new NSData( stringByEscapingHTMLString( - aString ).getBytes() ) ); + * Appends the specified string containing HTML to the response. Any special + * characters will be escaped appropriately. The string will be encoded in the + * current content encoding. + */ + public void appendContentHTMLString(String aString) { + _contentData.appendData(new NSData(stringByEscapingHTMLString(aString).getBytes())); setHeader(Integer.toString(_contentData.length()), "content-length"); } /** - * Appends the specified string containing HTML to the response. - * Any special characters will be escaped appropriately. - * This method escapes tabs and new-line characters as well. - * The string will be encoded in the current content encoding. - */ - public void appendContentHTMLAttributeValue (String aString) - { - _contentData.appendData ( - new NSData( stringByEscapingHTMLAttributeValue( - aString ).getBytes() ) ); + * Appends the specified string containing HTML to the response. Any special + * characters will be escaped appropriately. This method escapes tabs and + * new-line characters as well. The string will be encoded in the current + * content encoding. + */ + public void appendContentHTMLAttributeValue(String aString) { + _contentData.appendData(new NSData(stringByEscapingHTMLAttributeValue(aString).getBytes())); setHeader(Integer.toString(_contentData.length()), "content-length"); } /** - * Adds the specified cookie to the response. - */ - public void addCookie (WOCookie aCookie) - { - _cookies.setObjectForKey( aCookie, aCookie.name() ); + * Adds the specified cookie to the response. + */ + public void addCookie(WOCookie aCookie) { + _cookies.setObjectForKey(aCookie, aCookie.name()); } /** - * Removes the specified cookie from the response. - */ - public void removeCookie (WOCookie aCookie) - { - _cookies.removeObjectForKey( aCookie.name() ); + * Removes the specified cookie from the response. + */ + public void removeCookie(WOCookie aCookie) { + _cookies.removeObjectForKey(aCookie.name()); } /** - * Returns an array of cookies currently being sent with the response. - * Contains whatever cookies have previously been set in this response. - */ - public NSArray cookies () - { - return _cookies.allValues(); + * Returns an array of cookies currently being sent with the response. Contains + * whatever cookies have previously been set in this response. + */ + public NSArray cookies() { + return _cookies.allValues(); } /** - * Sets the HTTP version header in the response. - */ - public void setHTTPVersion (String aString) - { - setHeader( aString, "Protocol"); + * Sets the HTTP version header in the response. + */ + public void setHTTPVersion(String aString) { + setHeader(aString, "Protocol"); } /** - * Gets the current HTTP version header for the response. - * Because servlet responses do not allow read access - * to headers, this method returns null if setHTTPVersion - * has not been called. - */ - public String httpVersion () - { - return headerForKey( "Protocol" ); + * Gets the current HTTP version header for the response. Because servlet + * responses do not allow read access to headers, this method returns null if + * setHTTPVersion has not been called. + */ + public String httpVersion() { + return headerForKey("Protocol"); } /** - * Returns a sting containing the contents of the specified - * string after escaping all special HTML characters. - */ - public static String stringByEscapingHTMLString - (String aString) - { + * Returns a sting containing the contents of the specified string after + * escaping all special HTML characters. + */ + public static String stringByEscapingHTMLString(String aString) { int len = aString.length(); StringBuffer result = new StringBuffer(); - char[] buf = new char[ len ]; - aString.getChars( 0, len, buf, 0 ); - for ( int i = 0; i < len; i++ ) - { - if ( buf[i] == '&' ) - { - result.append( "&" ); - } - else - if ( buf[i] == '\\' ) - { - result.append( """ ); - } - else - if ( buf[i] == '<' ) - { - result.append( "<" ); + char[] buf = new char[len]; + aString.getChars(0, len, buf, 0); + for (int i = 0; i < len; i++) { + if (buf[i] == '&') { + result.append("&"); + } else if (buf[i] == '\\') { + result.append("""); + } else if (buf[i] == '<') { + result.append("<"); + } else if (buf[i] == '>') { + result.append(">"); + } else { + result.append(buf[i]); } - else - if ( buf[i] == '>' ) - { - result.append( ">" ); - } - else - { - result.append( buf[i] ); - } - } - return result.toString(); + } + return result.toString(); } - + /** - * Returns a sting containing the contents of the specified - * string after escaping all special HTML characters. - * This method escapes tabs and new-line characters as well. - */ - public static String stringByEscapingHTMLAttributeValue - (String aString) - { + * Returns a sting containing the contents of the specified string after + * escaping all special HTML characters. This method escapes tabs and new-line + * characters as well. + */ + public static String stringByEscapingHTMLAttributeValue(String aString) { int len = aString.length(); StringBuffer result = new StringBuffer(); - char[] buf = new char[ len ]; - aString.getChars( 0, len, buf, 0 ); - for ( int i = 0; i < len; i++ ) - { - if ( buf[i] == '&' ) - { - result.append( "&" ); + char[] buf = new char[len]; + aString.getChars(0, len, buf, 0); + for (int i = 0; i < len; i++) { + if (buf[i] == '&') { + result.append("&"); + } else if (buf[i] == '\\') { + result.append("""); + } else if (buf[i] == '<') { + result.append("<"); + } else if (buf[i] == '>') { + result.append(">"); + } else if (buf[i] == '\t') { + result.append("	"); + } else if (buf[i] == '\n') { + result.append(" "); + } else if (buf[i] == '\r') { + result.append(" "); + } else { + result.append(buf[i]); } - else - if ( buf[i] == '\\' ) - { - result.append( """ ); - } - else - if ( buf[i] == '<' ) - { - result.append( "<" ); - } - else - if ( buf[i] == '>' ) - { - result.append( ">" ); - } - else - if ( buf[i] == '\t' ) - { - result.append( "	" ); - } - else - if ( buf[i] == '\n' ) - { - result.append( " " ); - } - else - if ( buf[i] == '\r' ) - { - result.append( " " ); - } - else - { - result.append( buf[i] ); - } - } - return result.toString(); + } + return result.toString(); } } |
