From b81f231c707ba86e6958a0bd66b80df97e01f5cc Mon Sep 17 00:00:00 2001 From: hns Date: Fri, 13 Jan 2006 16:50:41 +0000 Subject: [PATCH] * Encapsulate fields in ResponseTrans and make them private. * Store response debug buffer in session over redirects (in addition to message). --- src/helma/framework/ResponseBean.java | 26 +- src/helma/framework/ResponseTrans.java | 251 +++++++++++++----- .../framework/core/RequestEvaluator.java | 16 +- src/helma/framework/core/Session.java | 57 +++- src/helma/framework/core/Skin.java | 4 +- src/helma/servlet/AbstractServletClient.java | 10 +- 6 files changed, 256 insertions(+), 108 deletions(-) diff --git a/src/helma/framework/ResponseBean.java b/src/helma/framework/ResponseBean.java index 4a7d6109..6e80b068 100644 --- a/src/helma/framework/ResponseBean.java +++ b/src/helma/framework/ResponseBean.java @@ -228,7 +228,7 @@ public class ResponseBean implements Serializable { * @return true if the response may be cached by the HTTP client, false otherwise */ public boolean getCache() { - return res.cache; + return res.isCacheable(); } /** @@ -237,7 +237,7 @@ public class ResponseBean implements Serializable { * @param cache true if the response may be cached by the HTTP client, false otherwise */ public void setCache(boolean cache) { - res.cache = cache; + res.setCacheable(cache); } /** @@ -246,7 +246,7 @@ public class ResponseBean implements Serializable { * @return The charset name */ public String getCharset() { - return res.charset; + return res.getCharset(); } /** @@ -255,7 +255,7 @@ public class ResponseBean implements Serializable { * @param charset The charset name */ public void setCharset(String charset) { - res.charset = charset; + res.setCharset(charset); } /** @@ -264,7 +264,7 @@ public class ResponseBean implements Serializable { * @return the content type */ public String getContentType() { - return res.contentType; + return res.getContentType(); } /** @@ -273,7 +273,7 @@ public class ResponseBean implements Serializable { * @param contentType The charset name */ public void setContentType(String contentType) { - res.contentType = contentType; + res.setContentType(contentType); } /** @@ -309,7 +309,7 @@ public class ResponseBean implements Serializable { * @return the error message */ public String getError() { - return res.error; + return res.getError(); } /** @@ -318,7 +318,7 @@ public class ResponseBean implements Serializable { * @return the message */ public String getMessage() { - return res.message; + return res.getMessage(); } /** @@ -327,7 +327,7 @@ public class ResponseBean implements Serializable { * @param message the message property */ public void setMessage(String message) { - res.message = message; + res.setMessage(message); } /** @@ -336,7 +336,7 @@ public class ResponseBean implements Serializable { * @return the HTTP authentication realm */ public String getRealm() { - return res.realm; + return res.getRealm(); } /** @@ -345,7 +345,7 @@ public class ResponseBean implements Serializable { * @param realm the HTTP authentication realm */ public void setRealm(String realm) { - res.realm = realm; + res.setRealm(realm); } /** @@ -372,7 +372,7 @@ public class ResponseBean implements Serializable { * @return the HTTP status code */ public int getStatus() { - return res.status; + return res.getStatus(); } /** @@ -381,7 +381,7 @@ public class ResponseBean implements Serializable { * @param status the HTTP status code */ public void setStatus(int status) { - res.status = status; + res.setStatus(status); } /** diff --git a/src/helma/framework/ResponseTrans.java b/src/helma/framework/ResponseTrans.java index fc87b6a6..18c745d1 100644 --- a/src/helma/framework/ResponseTrans.java +++ b/src/helma/framework/ResponseTrans.java @@ -38,30 +38,20 @@ public final class ResponseTrans extends Writer implements Serializable { static final String newLine = System.getProperty("line.separator"); - /** - * Set the MIME content type of the response. - */ - public String contentType = "text/html"; + // MIME content type of the response. + private String contentType = "text/html"; - /** - * Set the charset (encoding) to use for the response. - */ - public String charset; + // Charset (encoding) to use for the response. + private String charset; - /** - * used to allow or disable client side caching - */ - public boolean cache = true; + // Used to allow or disable client side caching + private boolean cacheable = true; - /** - * Value for HTTP response code, defaults to 200 (OK). - */ - public int status = 200; + // HTTP response code, defaults to 200 (OK). + private int status = 200; - /** - * Used for HTTP authentication - */ - public String realm; + // HTTP authentication realm + private String realm; // the actual response private byte[] response = null; @@ -102,15 +92,11 @@ public final class ResponseTrans extends Writer implements Serializable { // buffer for debug messages - will be automatically appended to response private transient StringBuffer debugBuffer; - /** - * string fields that hold a user message - */ - public transient String message; + // field for generic message to be displayed + private transient String message; - /** - * string fields that hold an error message - */ - public transient String error; + // field for error message + private transient String error; // the res.data map of form and cookie data private transient Map values = new SystemMap(); @@ -191,7 +177,7 @@ public final class ResponseTrans extends Writer implements Serializable { buffers = null; response = null; - cache = true; + cacheable = true; redir = forward = message = error = null; etag = realm = charset = null; contentType = "text/html"; @@ -629,22 +615,24 @@ public final class ResponseTrans extends Writer implements Serializable { wait(10000L); } } catch (InterruptedException ix) { + // Ignore } } /** + * Get the body content for this response as byte array, encoded using the + * response's charset. * - * - * @return ... + * @return the response body */ public byte[] getContent() { return (response == null) ? new byte[0] : response; } /** + * Get the number of bytes of the response body. * - * - * @return ... + * @return the length of the response body */ public int getContentLength() { if (response != null) { @@ -655,9 +643,9 @@ public final class ResponseTrans extends Writer implements Serializable { } /** + * Get the response's MIME content type * - * - * @return ... + * @return the MIME type for this response */ public String getContentType() { if (charset != null) { @@ -667,10 +655,20 @@ public final class ResponseTrans extends Writer implements Serializable { return contentType; } + /** + * Set the response's MIME content type * + * @param contentType MIME type for this response + */ + public void setContentType(String contentType) { + this.contentType = contentType; + } + + /** + * Set the Last-Modified header for this response * - * @param modified ... + * @param modified the Last-Modified header in milliseconds */ public void setLastModified(long modified) { if ((modified > -1) && (reqtrans != null) && @@ -683,18 +681,18 @@ public final class ResponseTrans extends Writer implements Serializable { } /** + * Get the value of the Last-Modified header for this response. * - * - * @return ... + * @return the Last-Modified header in milliseconds */ public long getLastModified() { return lastModified; } /** + * Set the ETag header value for this response. * - * - * @param value ... + * @param value the ETag header value */ public void setETag(String value) { etag = (value == null) ? null : ("\"" + value + "\""); @@ -706,27 +704,27 @@ public final class ResponseTrans extends Writer implements Serializable { } /** + * Get the ETag header value for this response. * - * - * @return ... + * @return the ETag header value */ public String getETag() { return etag; } /** + * Check if this response should generate a Not-Modified response. * - * - * @return ... + * @return true if the the response wasn't modified since the client last saw it. */ public boolean getNotModified() { return notModified; } /** + * Add a dependency to this response. * - * - * @param what ... + * @param what an item this response's output depends on. */ public void dependsOn(Object what) { if (digest == null) { @@ -755,7 +753,7 @@ public final class ResponseTrans extends Writer implements Serializable { } /** - * + * Digest all dependencies to a checksum to see if the response has changed. */ public void digestDependencies() { if (digest == null) { @@ -770,9 +768,10 @@ public final class ResponseTrans extends Writer implements Serializable { } /** + * Set the path in which to look for skins. This may contain file locations and + * HopObjects. * - * - * @param arr ... + * @param arr the skin path */ public void setSkinpath(Object[] arr) { this.skinpath = arr; @@ -780,9 +779,10 @@ public final class ResponseTrans extends Writer implements Serializable { } /** + * Get the path in which to look for skins. This may contain file locations and + * HopObjects. * - * - * @return ... + * @return the skin path */ public Object[] getSkinpath() { if (skinpath == null) { @@ -793,11 +793,10 @@ public final class ResponseTrans extends Writer implements Serializable { } /** + * Look up a cached skin. * - * - * @param id ... - * - * @return ... + * @param id the skin key + * @return the skin, or null if no skin is cached for the given key */ public Skin getCachedSkin(Object id) { if (skincache == null) { @@ -808,10 +807,10 @@ public final class ResponseTrans extends Writer implements Serializable { } /** + * Cache a skin for the length of this response. * - * - * @param id ... - * @param skin ... + * @param id the skin key + * @param skin the skin to cache */ public void cacheSkin(Object id, Skin skin) { if (skincache == null) { @@ -822,13 +821,13 @@ public final class ResponseTrans extends Writer implements Serializable { } /** + * Set a cookie. * - * - * @param key ... - * @param value ... - * @param days ... - * @param path ... - * @param domain ... + * @param key the cookie key + * @param value the cookie value + * @param days the cookie's lifespan in days + * @param path the URL path to apply the cookie to + * @param domain the domain to apply the cookie to */ public void setCookie(String key, String value, int days, String path, String domain) { CookieTrans c = null; @@ -852,7 +851,7 @@ public final class ResponseTrans extends Writer implements Serializable { } /** - * + * Reset all previously set cookies. */ public void resetCookies() { if (cookies != null) { @@ -861,9 +860,9 @@ public final class ResponseTrans extends Writer implements Serializable { } /** + * Get the number of cookies set in this response. * - * - * @return ... + * @return the number of cookies */ public int countCookies() { if (cookies != null) { @@ -874,9 +873,9 @@ public final class ResponseTrans extends Writer implements Serializable { } /** + * Get the cookies set in this response. * - * - * @return ... + * @return the cookies */ public CookieTrans[] getCookies() { if (cookies == null) { @@ -884,9 +883,119 @@ public final class ResponseTrans extends Writer implements Serializable { } CookieTrans[] c = new CookieTrans[cookies.size()]; - cookies.values().toArray(c); - return c; } + + /** + * Get the message to display to the user, if any. + * @return the message + */ + public String getMessage() { + return message; + } + + /** + * Set a message to display to the user. + * @param message the message + */ + public void setMessage(String message) { + this.message = message; + } + + /** + * Get the error message to display to the user, if any. + * @return the error message + */ + public String getError() { + return error; + } + + /** + * Set a message to display to the user. + * @param error the error message + */ + public void setError(String error) { + this.error = error; + } + + /** + * Get debug messages to append to the response, if any. + * @return the response's debug buffer + */ + public StringBuffer getDebugBuffer() { + return debugBuffer; + } + + /** + * Set debug messages to append to the response. + * @param debugBuffer the response's debug buffer + */ + public void setDebugBuffer(StringBuffer debugBuffer) { + this.debugBuffer = debugBuffer; + } + + /** + * Get the charset/encoding for this response + * @return the charset name + */ + public String getCharset() { + return charset; + } + + /** + * Set the charset/encoding for this response + * @param charset the charset name + */ + public void setCharset(String charset) { + this.charset = charset; + } + + /** + * Returns true if this response may be cached by the client + * @return true if the response may be cached + */ + public boolean isCacheable() { + return cacheable; + } + + /** + * Set the cacheability of this response + * @param cache true if the response may be cached + */ + public void setCacheable(boolean cache) { + this.cacheable = cache; + } + + /** + * Get the HTTP response status code + * @return the HTTP response code + */ + public int getStatus() { + return status; + } + + /** + * Set the HTTP response status code + * @param status the HTTP response code + */ + public void setStatus(int status) { + this.status = status; + } + + /** + * Get the HTTP authentication realm + * @return the name of the authentication realm + */ + public String getRealm() { + return realm; + } + + /** + * Set the HTTP authentication realm + * @param realm the name of the authentication realm + */ + public void setRealm(String realm) { + this.realm = realm; + } } \ No newline at end of file diff --git a/src/helma/framework/core/RequestEvaluator.java b/src/helma/framework/core/RequestEvaluator.java index e5696329..cd1ef1ea 100644 --- a/src/helma/framework/core/RequestEvaluator.java +++ b/src/helma/framework/core/RequestEvaluator.java @@ -88,7 +88,6 @@ public final class RequestEvaluator implements Runnable { if (scriptingEngine == null) { String engineClassName = app.getProperty("scriptingEngine", "helma.scripting.rhino.RhinoEngine"); - try { Class clazz = app.getClassLoader().loadClass(engineClassName); @@ -183,17 +182,14 @@ public final class RequestEvaluator implements Runnable { String action = null; if (error != null) { - res.error = error; + res.setError(error); } switch (reqtype) { case HTTP: - if (session.message != null) { - // bring over the message from a redirect - res.message = session.message; - session.message = null; - } + // bring over the message from a redirect + session.recoverResponseMessages(res); // catch redirect in path resolution or script execution try { @@ -292,7 +288,7 @@ public final class RequestEvaluator implements Runnable { // The path could not be resolved. Check if there is a "not found" action // specified in the property file. - res.status = 404; + res.setStatus(404); String notFoundAction = app.props.getProperty("notfound", "notfound"); @@ -381,9 +377,7 @@ public final class RequestEvaluator implements Runnable { } } catch (RedirectException redirect) { // if there is a message set, save it on the user object for the next request - if (res.message != null) { - session.message = res.message; - } + session.storeResponseMessages(res); } // check if we're still the one and only or if the waiting thread has given up on us already diff --git a/src/helma/framework/core/Session.java b/src/helma/framework/core/Session.java index 480e05f7..dae5b429 100644 --- a/src/helma/framework/core/Session.java +++ b/src/helma/framework/core/Session.java @@ -18,6 +18,8 @@ package helma.framework.core; import helma.objectmodel.*; import helma.objectmodel.db.*; +import helma.framework.ResponseTrans; + import java.io.*; import java.util.*; @@ -47,6 +49,7 @@ public class Session implements Serializable { // used to remember messages to the user between requests, mainly between redirects. protected String message; + protected StringBuffer debugBuffer; /** * Creates a new Session object. @@ -105,11 +108,7 @@ public class Session implements Serializable { * @return ... */ public boolean isLoggedIn() { - if ((userHandle != null) && (uid != null)) { - return true; - } else { - return false; - } + return (userHandle != null) && (uid != null); } /** @@ -256,6 +255,30 @@ public class Session implements Serializable { } + /** + * Set the user and debug messages over from a previous response. + * This is used for redirects, where messages can't be displayed immediately. + * @param res the response to set the messages on + */ + public synchronized void recoverResponseMessages(ResponseTrans res) { + if (message != null || debugBuffer != null) { + res.setMessage(message); + res.setDebugBuffer(debugBuffer); + message = null; + debugBuffer = null; + } + } + + /** + * Remember the response's user and debug messages for a later response. + * This is used for redirects, where messages can't be displayed immediately. + * @param res the response to retrieve the messages from + */ + public synchronized void storeResponseMessages(ResponseTrans res) { + message = res.getMessage(); + debugBuffer = res.getDebugBuffer(); + } + /** * Return the message that is to be displayed upon the next * request within this session. @@ -272,9 +295,31 @@ public class Session implements Serializable { * the current request can't be used to display a user visible * message. * - * @param msg + * @param msg the message */ public void setMessage(String msg) { message = msg; } + + /** + * Return the debug buffer that is to be displayed upon the next + * request within this session. + * + * @return the debug buffer, or null if none was set. + */ + public StringBuffer getDebugBuffer() { + return debugBuffer; + } + + /** + * Set the debug buffer to be displayed to this session's user. This + * can be used to save the debug buffer over to the next request when + * the current request can't be used to display a user visible + * message. + * + * @param buffer the buffer + */ + public void setDebugBuffer(StringBuffer buffer) { + debugBuffer = buffer; + } } diff --git a/src/helma/framework/core/Skin.java b/src/helma/framework/core/Skin.java index e3fd1ba9..280ee0b5 100644 --- a/src/helma/framework/core/Skin.java +++ b/src/helma/framework/core/Skin.java @@ -594,9 +594,9 @@ public final class Skin { Object value = null; if ("message".equals(name)) { - value = reval.getResponse().message; + value = reval.getResponse().getMessage(); } else if ("error".equals(name)) { - value = reval.getResponse().error; + value = reval.getResponse().getError(); } if (value == null) { diff --git a/src/helma/servlet/AbstractServletClient.java b/src/helma/servlet/AbstractServletClient.java index 05ba2a39..c2ac652e 100644 --- a/src/helma/servlet/AbstractServletClient.java +++ b/src/helma/servlet/AbstractServletClient.java @@ -366,7 +366,7 @@ public abstract class AbstractServletClient extends HttpServlet { } else if (hopres.getNotModified()) { res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); } else { - if (!hopres.cache || !caching) { + if (!hopres.isCacheable() || !caching) { // Disable caching of response. // for HTTP 1.0 res.setDateHeader("Expires", System.currentTimeMillis() - 10000); @@ -377,12 +377,12 @@ public abstract class AbstractServletClient extends HttpServlet { "no-cache, no-store, must-revalidate, max-age=0"); } - if (hopres.realm != null) { - res.setHeader("WWW-Authenticate", "Basic realm=\"" + hopres.realm + "\""); + if (hopres.getRealm() != null) { + res.setHeader("WWW-Authenticate", "Basic realm=\"" + hopres.getRealm() + "\""); } - if (hopres.status > 0) { - res.setStatus(hopres.status); + if (hopres.getStatus() > 0) { + res.setStatus(hopres.getStatus()); } // set last-modified header to now