diff options
| author | Benjamin Culkin <scorpress@gmail.com> | 2024-05-19 17:56:33 -0400 |
|---|---|---|
| committer | Benjamin Culkin <scorpress@gmail.com> | 2024-05-19 17:56:33 -0400 |
| commit | aedc34d55462a75e329bbf342251ff6504cd117e (patch) | |
| tree | bcc8f1f2352582717b484df302aeea6696b8f000 /projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOResponse.java | |
Initial import from SVN
Diffstat (limited to 'projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOResponse.java')
| -rw-r--r-- | projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOResponse.java | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOResponse.java b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOResponse.java new file mode 100644 index 0000000..d9fbade --- /dev/null +++ b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOResponse.java @@ -0,0 +1,184 @@ +/* +Wotonomy: OpenStep design patterns for pure Java applications. +Copyright (C) 2000 Blacksmith, Inc. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, see http://www.gnu.org +*/ + +package net.wotonomy.web; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + +import net.wotonomy.foundation.NSArray; + +/** +* A pure java implementation of WOResponse. +* +* @author michael@mpowers.net +* @author $Author: cgruber $ +* @version $Revision: 905 $ +*/ +public class WOResponse extends WOMessage + implements WOActionResults +{ + protected static String defaultEncoding; + private static SimpleDateFormat htmlDateFormat; + static + { + defaultEncoding = "ISO8859_1"; + htmlDateFormat = new SimpleDateFormat( + "EEE, dd MMM yyyy HH:mm:ss z" ); + htmlDateFormat.setTimeZone( + TimeZone.getTimeZone( "GMT" ) ); + } + + private int status; + + /** + * Parameterless constructor which should not be called. + */ + public WOResponse () + { + status = -1; // -1 indicates not yet set + } + + /** + * Sets the status code of the response. + * You should use the constants defined in HttpServletResponse. + */ + public void setStatus (int code) + { + status = code; + } + + /** + * Gets the current status code for the response. + */ + public int status () + { + return status; + } + + /** + * Sets a header in the response to disable client caching. + * (Whether this works depends on the client implementation.) + */ + public void disableClientCaching () + { + String dateString = htmlDateFormat.format( new Date() ); + setHeader( dateString, "Date" ); + setHeader( dateString, "Expires" ); + setHeader( "no-cache", "Pragma" ); + setHeaders( new NSArray( new Object[] { + "private", "no-cache", "max-age = 0" } ), "Cache-Control" ); + //System.out.println( "disableClientCaching: " + dateString ); + } + + /** + * Returns this object. (Implements the WOActionResults interface.) + */ + public WOResponse generateResponse() + { + return this; + } + + /** + * Generates the response using the specified servlet response, + * but does not flush the buffer. The caller is responsible + * for sending the response to the client. <br><br> + * Note that this method is currently responsible for setting + * the content type to "text/html". As far as I can tell, WORequests + * have no way to set the content type and therefore must always + * be of type "text/html". + */ + void generateServletResponse + (javax.servlet.http.HttpServletResponse response) + { + if ( WOApplication.application().isPageRefreshOnBacktrackEnabled() ) + { + disableClientCaching(); + } + + String key; + java.util.Enumeration e, f; + + // set content type: might be overwritten by headers below + response.setContentType( "text/html" ); + + // set status + if ( status != -1 ) + { + response.setStatus( status ); + } + + // set headers + f = _headers.allKeys().objectEnumerator(); + while ( f.hasMoreElements() ) + { + key = f.nextElement().toString(); + e = ((NSArray)_headers.objectForKey( key )).objectEnumerator(); + if ( e.hasMoreElements() ) + { + // overwrite existing header + response.setHeader( key, e.nextElement().toString() ); + } + while ( e.hasMoreElements() ) + { + response.addHeader( key, e.nextElement().toString() ); + } + } + + // set cookies + e = _cookies.allValues().objectEnumerator(); + while ( e.hasMoreElements() ) + { + response.addCookie( (WOCookie) e.nextElement() ); + } + + try + { + // write content + response.getOutputStream().write(_contentData.bytes() ); + if ( status > 299 ) + { + response.sendError( status ); + } + } + catch ( Exception exc ) + { + throw new RuntimeException( + "Error writing response: " + exc ); + } + } + + + /** + * Returns the current default encoding seting. + */ + public static String defaultEncoding () + { + return defaultEncoding; + } + + /** + * Sets the default encoding setting. + */ + public static void setDefaultEncoding (String encoding) + { + defaultEncoding = encoding; + } + +} |
