* Encapsulate fields in ResponseTrans and make them private.

* Store response debug buffer in session over redirects (in addition to message).
This commit is contained in:
hns 2006-01-13 16:50:41 +00:00
parent a3fa56750c
commit b81f231c70
6 changed files with 256 additions and 108 deletions

View file

@ -228,7 +228,7 @@ public class ResponseBean implements Serializable {
* @return true if the response may be cached by the HTTP client, false otherwise * @return true if the response may be cached by the HTTP client, false otherwise
*/ */
public boolean getCache() { 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 * @param cache true if the response may be cached by the HTTP client, false otherwise
*/ */
public void setCache(boolean cache) { public void setCache(boolean cache) {
res.cache = cache; res.setCacheable(cache);
} }
/** /**
@ -246,7 +246,7 @@ public class ResponseBean implements Serializable {
* @return The charset name * @return The charset name
*/ */
public String getCharset() { public String getCharset() {
return res.charset; return res.getCharset();
} }
/** /**
@ -255,7 +255,7 @@ public class ResponseBean implements Serializable {
* @param charset The charset name * @param charset The charset name
*/ */
public void setCharset(String charset) { public void setCharset(String charset) {
res.charset = charset; res.setCharset(charset);
} }
/** /**
@ -264,7 +264,7 @@ public class ResponseBean implements Serializable {
* @return the content type * @return the content type
*/ */
public String getContentType() { public String getContentType() {
return res.contentType; return res.getContentType();
} }
/** /**
@ -273,7 +273,7 @@ public class ResponseBean implements Serializable {
* @param contentType The charset name * @param contentType The charset name
*/ */
public void setContentType(String contentType) { public void setContentType(String contentType) {
res.contentType = contentType; res.setContentType(contentType);
} }
/** /**
@ -309,7 +309,7 @@ public class ResponseBean implements Serializable {
* @return the error message * @return the error message
*/ */
public String getError() { public String getError() {
return res.error; return res.getError();
} }
/** /**
@ -318,7 +318,7 @@ public class ResponseBean implements Serializable {
* @return the message * @return the message
*/ */
public String getMessage() { public String getMessage() {
return res.message; return res.getMessage();
} }
/** /**
@ -327,7 +327,7 @@ public class ResponseBean implements Serializable {
* @param message the message property * @param message the message property
*/ */
public void setMessage(String message) { 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 * @return the HTTP authentication realm
*/ */
public String getRealm() { public String getRealm() {
return res.realm; return res.getRealm();
} }
/** /**
@ -345,7 +345,7 @@ public class ResponseBean implements Serializable {
* @param realm the HTTP authentication realm * @param realm the HTTP authentication realm
*/ */
public void setRealm(String 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 * @return the HTTP status code
*/ */
public int getStatus() { public int getStatus() {
return res.status; return res.getStatus();
} }
/** /**
@ -381,7 +381,7 @@ public class ResponseBean implements Serializable {
* @param status the HTTP status code * @param status the HTTP status code
*/ */
public void setStatus(int status) { public void setStatus(int status) {
res.status = status; res.setStatus(status);
} }
/** /**

View file

@ -38,30 +38,20 @@ public final class ResponseTrans extends Writer implements Serializable {
static final String newLine = System.getProperty("line.separator"); static final String newLine = System.getProperty("line.separator");
/** // MIME content type of the response.
* Set the MIME content type of the response. private String contentType = "text/html";
*/
public String contentType = "text/html";
/** // Charset (encoding) to use for the response.
* Set the charset (encoding) to use for the response. private String charset;
*/
public String charset;
/** // Used to allow or disable client side caching
* used to allow or disable client side caching private boolean cacheable = true;
*/
public boolean cache = true;
/** // HTTP response code, defaults to 200 (OK).
* Value for HTTP response code, defaults to 200 (OK). private int status = 200;
*/
public int status = 200;
/** // HTTP authentication realm
* Used for HTTP authentication private String realm;
*/
public String realm;
// the actual response // the actual response
private byte[] response = null; 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 // buffer for debug messages - will be automatically appended to response
private transient StringBuffer debugBuffer; private transient StringBuffer debugBuffer;
/** // field for generic message to be displayed
* string fields that hold a user message private transient String message;
*/
public transient String message;
/** // field for error message
* string fields that hold an error message private transient String error;
*/
public transient String error;
// the res.data map of form and cookie data // the res.data map of form and cookie data
private transient Map values = new SystemMap(); private transient Map values = new SystemMap();
@ -191,7 +177,7 @@ public final class ResponseTrans extends Writer implements Serializable {
buffers = null; buffers = null;
response = null; response = null;
cache = true; cacheable = true;
redir = forward = message = error = null; redir = forward = message = error = null;
etag = realm = charset = null; etag = realm = charset = null;
contentType = "text/html"; contentType = "text/html";
@ -629,22 +615,24 @@ public final class ResponseTrans extends Writer implements Serializable {
wait(10000L); wait(10000L);
} }
} catch (InterruptedException ix) { } catch (InterruptedException ix) {
// Ignore
} }
} }
/** /**
* Get the body content for this response as byte array, encoded using the
* response's charset.
* *
* * @return the response body
* @return ...
*/ */
public byte[] getContent() { public byte[] getContent() {
return (response == null) ? new byte[0] : response; return (response == null) ? new byte[0] : response;
} }
/** /**
* Get the number of bytes of the response body.
* *
* * @return the length of the response body
* @return ...
*/ */
public int getContentLength() { public int getContentLength() {
if (response != null) { if (response != null) {
@ -655,9 +643,9 @@ public final class ResponseTrans extends Writer implements Serializable {
} }
/** /**
* Get the response's MIME content type
* *
* * @return the MIME type for this response
* @return ...
*/ */
public String getContentType() { public String getContentType() {
if (charset != null) { if (charset != null) {
@ -667,10 +655,20 @@ public final class ResponseTrans extends Writer implements Serializable {
return contentType; 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) { public void setLastModified(long modified) {
if ((modified > -1) && (reqtrans != null) && 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 the Last-Modified header in milliseconds
* @return ...
*/ */
public long getLastModified() { public long getLastModified() {
return lastModified; return lastModified;
} }
/** /**
* Set the ETag header value for this response.
* *
* * @param value the ETag header value
* @param value ...
*/ */
public void setETag(String value) { public void setETag(String value) {
etag = (value == null) ? null : ("\"" + 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 the ETag header value
* @return ...
*/ */
public String getETag() { public String getETag() {
return etag; return etag;
} }
/** /**
* Check if this response should generate a Not-Modified response.
* *
* * @return true if the the response wasn't modified since the client last saw it.
* @return ...
*/ */
public boolean getNotModified() { public boolean getNotModified() {
return notModified; return notModified;
} }
/** /**
* Add a dependency to this response.
* *
* * @param what an item this response's output depends on.
* @param what ...
*/ */
public void dependsOn(Object what) { public void dependsOn(Object what) {
if (digest == null) { 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() { public void digestDependencies() {
if (digest == null) { 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 the skin path
* @param arr ...
*/ */
public void setSkinpath(Object[] arr) { public void setSkinpath(Object[] arr) {
this.skinpath = 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 the skin path
* @return ...
*/ */
public Object[] getSkinpath() { public Object[] getSkinpath() {
if (skinpath == null) { if (skinpath == null) {
@ -793,11 +793,10 @@ public final class ResponseTrans extends Writer implements Serializable {
} }
/** /**
* Look up a cached skin.
* *
* * @param id the skin key
* @param id ... * @return the skin, or null if no skin is cached for the given key
*
* @return ...
*/ */
public Skin getCachedSkin(Object id) { public Skin getCachedSkin(Object id) {
if (skincache == null) { 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 the skin key
* @param id ... * @param skin the skin to cache
* @param skin ...
*/ */
public void cacheSkin(Object id, Skin skin) { public void cacheSkin(Object id, Skin skin) {
if (skincache == null) { if (skincache == null) {
@ -822,13 +821,13 @@ public final class ResponseTrans extends Writer implements Serializable {
} }
/** /**
* Set a cookie.
* *
* * @param key the cookie key
* @param key ... * @param value the cookie value
* @param value ... * @param days the cookie's lifespan in days
* @param days ... * @param path the URL path to apply the cookie to
* @param path ... * @param domain the domain to apply the cookie to
* @param domain ...
*/ */
public void setCookie(String key, String value, int days, String path, String domain) { public void setCookie(String key, String value, int days, String path, String domain) {
CookieTrans c = null; CookieTrans c = null;
@ -852,7 +851,7 @@ public final class ResponseTrans extends Writer implements Serializable {
} }
/** /**
* * Reset all previously set cookies.
*/ */
public void resetCookies() { public void resetCookies() {
if (cookies != null) { 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 the number of cookies
* @return ...
*/ */
public int countCookies() { public int countCookies() {
if (cookies != null) { if (cookies != null) {
@ -874,9 +873,9 @@ public final class ResponseTrans extends Writer implements Serializable {
} }
/** /**
* Get the cookies set in this response.
* *
* * @return the cookies
* @return ...
*/ */
public CookieTrans[] getCookies() { public CookieTrans[] getCookies() {
if (cookies == null) { if (cookies == null) {
@ -884,9 +883,119 @@ public final class ResponseTrans extends Writer implements Serializable {
} }
CookieTrans[] c = new CookieTrans[cookies.size()]; CookieTrans[] c = new CookieTrans[cookies.size()];
cookies.values().toArray(c); cookies.values().toArray(c);
return 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;
}
} }

View file

@ -88,7 +88,6 @@ public final class RequestEvaluator implements Runnable {
if (scriptingEngine == null) { if (scriptingEngine == null) {
String engineClassName = app.getProperty("scriptingEngine", String engineClassName = app.getProperty("scriptingEngine",
"helma.scripting.rhino.RhinoEngine"); "helma.scripting.rhino.RhinoEngine");
try { try {
Class clazz = app.getClassLoader().loadClass(engineClassName); Class clazz = app.getClassLoader().loadClass(engineClassName);
@ -183,17 +182,14 @@ public final class RequestEvaluator implements Runnable {
String action = null; String action = null;
if (error != null) { if (error != null) {
res.error = error; res.setError(error);
} }
switch (reqtype) { switch (reqtype) {
case HTTP: case HTTP:
if (session.message != null) {
// bring over the message from a redirect // bring over the message from a redirect
res.message = session.message; session.recoverResponseMessages(res);
session.message = null;
}
// catch redirect in path resolution or script execution // catch redirect in path resolution or script execution
try { 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 // The path could not be resolved. Check if there is a "not found" action
// specified in the property file. // specified in the property file.
res.status = 404; res.setStatus(404);
String notFoundAction = app.props.getProperty("notfound", String notFoundAction = app.props.getProperty("notfound",
"notfound"); "notfound");
@ -381,9 +377,7 @@ public final class RequestEvaluator implements Runnable {
} }
} catch (RedirectException redirect) { } catch (RedirectException redirect) {
// if there is a message set, save it on the user object for the next request // if there is a message set, save it on the user object for the next request
if (res.message != null) { session.storeResponseMessages(res);
session.message = res.message;
}
} }
// check if we're still the one and only or if the waiting thread has given up on us already // check if we're still the one and only or if the waiting thread has given up on us already

View file

@ -18,6 +18,8 @@ package helma.framework.core;
import helma.objectmodel.*; import helma.objectmodel.*;
import helma.objectmodel.db.*; import helma.objectmodel.db.*;
import helma.framework.ResponseTrans;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
@ -47,6 +49,7 @@ public class Session implements Serializable {
// used to remember messages to the user between requests, mainly between redirects. // used to remember messages to the user between requests, mainly between redirects.
protected String message; protected String message;
protected StringBuffer debugBuffer;
/** /**
* Creates a new Session object. * Creates a new Session object.
@ -105,11 +108,7 @@ public class Session implements Serializable {
* @return ... * @return ...
*/ */
public boolean isLoggedIn() { public boolean isLoggedIn() {
if ((userHandle != null) && (uid != null)) { return (userHandle != null) && (uid != null);
return true;
} else {
return false;
}
} }
/** /**
@ -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 * Return the message that is to be displayed upon the next
* request within this session. * 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 * the current request can't be used to display a user visible
* message. * message.
* *
* @param msg * @param msg the message
*/ */
public void setMessage(String msg) { public void setMessage(String msg) {
message = 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;
}
} }

View file

@ -594,9 +594,9 @@ public final class Skin {
Object value = null; Object value = null;
if ("message".equals(name)) { if ("message".equals(name)) {
value = reval.getResponse().message; value = reval.getResponse().getMessage();
} else if ("error".equals(name)) { } else if ("error".equals(name)) {
value = reval.getResponse().error; value = reval.getResponse().getError();
} }
if (value == null) { if (value == null) {

View file

@ -366,7 +366,7 @@ public abstract class AbstractServletClient extends HttpServlet {
} else if (hopres.getNotModified()) { } else if (hopres.getNotModified()) {
res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
} else { } else {
if (!hopres.cache || !caching) { if (!hopres.isCacheable() || !caching) {
// Disable caching of response. // Disable caching of response.
// for HTTP 1.0 // for HTTP 1.0
res.setDateHeader("Expires", System.currentTimeMillis() - 10000); 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"); "no-cache, no-store, must-revalidate, max-age=0");
} }
if (hopres.realm != null) { if (hopres.getRealm() != null) {
res.setHeader("WWW-Authenticate", "Basic realm=\"" + hopres.realm + "\""); res.setHeader("WWW-Authenticate", "Basic realm=\"" + hopres.getRealm() + "\"");
} }
if (hopres.status > 0) { if (hopres.getStatus() > 0) {
res.setStatus(hopres.status); res.setStatus(hopres.getStatus());
} }
// set last-modified header to now // set last-modified header to now