CCM NG/ccm-core: Dispatcher used none-locale save DateFormat

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4382 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2016-10-17 06:57:02 +00:00
parent 44ddf8a8f4
commit 42e18ee8cb
1 changed files with 321 additions and 255 deletions

View File

@ -54,8 +54,8 @@ import org.apache.log4j.Logger;
import java.net.URLEncoder; import java.net.URLEncoder;
/** /**
* Class static helper methods for request dispatching. * Class static helper methods for request dispatching. Contains various
* Contains various generally useful procedural abstractions. * generally useful procedural abstractions.
* *
* @author Bill Schneider * @author Bill Schneider
* @since 4.5 * @since 4.5
@ -63,10 +63,12 @@ import java.net.URLEncoder;
*/ */
public final class DispatcherHelper implements DispatcherConstants { public final class DispatcherHelper implements DispatcherConstants {
/** Internal logger instance to faciliate debugging. Enable logging output /**
* by editing /WEB-INF/conf/log4j.properties int hte runtime environment * Internal logger instance to faciliate debugging. Enable logging output by
* and set com.arsdigita.dispatcher.DispatcherHelper=DEBUG * editing /WEB-INF/conf/log4j.properties int hte runtime environment and
* by uncommenting or adding the line. */ * set com.arsdigita.dispatcher.DispatcherHelper=DEBUG by uncommenting or
* adding the line.
*/
private static final Logger s_log = Logger.getLogger(DispatcherHelper.class); private static final Logger s_log = Logger.getLogger(DispatcherHelper.class);
private static String s_webappCtx; private static String s_webappCtx;
private static String s_staticURL; private static String s_staticURL;
@ -80,7 +82,8 @@ public final class DispatcherHelper implements DispatcherConstants {
return; return;
} }
rfc1123_formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z"); rfc1123_formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",
Locale.ROOT);
rfc1123_formatter.setTimeZone(TimeZone.getTimeZone("GMT")); rfc1123_formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
s_staticURL = DispatcherConfig.getConfig().getStaticUrlPrefix(); s_staticURL = DispatcherConfig.getConfig().getStaticUrlPrefix();
@ -90,17 +93,20 @@ public final class DispatcherHelper implements DispatcherConstants {
initialized = true; initialized = true;
} }
/** The current HttpServletRequest. */ /**
* The current HttpServletRequest.
*/
private static final ThreadLocal s_request = new ThreadLocal(); private static final ThreadLocal s_request = new ThreadLocal();
/** null constructor, private so no one can instantiate! */ /**
* null constructor, private so no one can instantiate!
*/
private DispatcherHelper() { private DispatcherHelper() {
} }
/** /**
* Return default cache expiry. * Return default cache expiry. Default is specified in the configuration
* Default is specified in the configuration file (registry) if not * file (registry) if not otherweise set.
* otherweise set.
* *
* @return default cache expiry * @return default cache expiry
*/ */
@ -125,12 +131,12 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Returns the URL path (relative to the webapp root) for the * Returns the URL path (relative to the webapp root) for the current
* current (calling) resource. This works around the quirk that, * (calling) resource. This works around the quirk that, if servlet A
* if servlet A includes servlet B, calling getRequestURI() in B * includes servlet B, calling getRequestURI() in B returns "A" and not "B".
* returns "A" and not "B".
* *
* @param req * @param req
*
* @return the URL path (relative to the webapp root) for the currently * @return the URL path (relative to the webapp root) for the currently
* executing resource. * executing resource.
*/ */
@ -156,7 +162,9 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* Gets the application context from the request attributes. * Gets the application context from the request attributes.
*
* @param req * @param req
*
* @return the application context from the request attributes. * @return the application context from the request attributes.
*/ */
public static RequestContext getRequestContext(HttpServletRequest req) { public static RequestContext getRequestContext(HttpServletRequest req) {
@ -177,14 +185,16 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Sets the current request context as a request attribute for * Sets the current request context as a request attribute for later
* later retrieval. * retrieval.
* *
* @param req the current request object * @param req the current request object
* @param ac the current request context * @param ac the current request context
*
* @post DispatcherHelper.getRequestContext(req) == ac * @post DispatcherHelper.getRequestContext(req) == ac
*/ */
public static void setRequestContext(HttpServletRequest req, RequestContext ac) { public static void setRequestContext(HttpServletRequest req,
RequestContext ac) {
req.setAttribute(REQUEST_CONTEXT_ATTR, ac); req.setAttribute(REQUEST_CONTEXT_ATTR, ac);
} }
@ -219,20 +229,22 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Forwards the request from this resource to another resource at * Forwards the request from this resource to another resource at the
* the servlet-container level. This is a wrapper for * servlet-container level. This is a wrapper for
* javax.servlet.RequestDispatcher * javax.servlet.RequestDispatcher and is intended to hide the fact that you
* and is intended to hide the fact that you can't "forward" a request * can't "forward" a request once you've done an "include" in it.
* once you've done an "include" in it. *
* @param path the URL of the resource, relative to the webapp * @param path the URL of the resource, relative to the webapp root. For
* root. For example, if you request a JSP page with /context/foo/bar, * example, if you request a JSP page with /context/foo/bar, you
* you would call this method with path == /foo/bar. * would call this method with path == /foo/bar.
* @param req the current request * @param req the current request
* @param resp the current response * @param resp the current response
* @param sctx the current servlet context * @param sctx the current servlet context
* @exception java.io.IOException may be propagated from target resource *
* @exception javax.servlet.ServletException may be * @exception java.io.IOException may be propagated from target
* propagated from target resource * resource
* @exception javax.servlet.ServletException may be propagated from target
* resource
*/ */
public static void forwardRequestByPath(String path, public static void forwardRequestByPath(String path,
HttpServletRequest req, HttpServletRequest req,
@ -246,9 +258,11 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* Equivalent to <code>forwardRequestByPath(path, req, resp, * Equivalent to <code>forwardRequestByPath(path, req, resp,
* DispatcherHelper.getRequestContext(req).getServletContext())</code>. * DispatcherHelper.getRequestContext(req).getServletContext())</code>.
*
* @param path * @param path
* @param req * @param req
* @param resp * @param resp
*
* @throws java.io.IOException * @throws java.io.IOException
* @throws javax.servlet.ServletException * @throws javax.servlet.ServletException
*/ */
@ -256,28 +270,29 @@ public final class DispatcherHelper implements DispatcherConstants {
HttpServletRequest req, HttpServletRequest req,
HttpServletResponse resp) HttpServletResponse resp)
throws IOException, ServletException { throws IOException, ServletException {
ServletContext sctx = ServletContext sctx = DispatcherHelper.getRequestContext(req)
DispatcherHelper.getRequestContext(req).getServletContext(); .getServletContext();
forwardRequestByPath(path, req, resp, sctx); forwardRequestByPath(path, req, resp, sctx);
} }
/** /**
* Forwards the request from this resource to another resource at * Forwards the request from this resource to another resource at the JSP
* the JSP container level. This is a wrapper for * container level. This is a wrapper for <code>PageContext.forward</code>
* <code>PageContext.forward</code> and <code>PageContext.include</code> * and <code>PageContext.include</code> and is intended to transparently
* and is intended to transparently switch between "forward" and * switch between "forward" and "include" depending on whether or not an
* "include" depending on whether or not an include has already been * include has already been done on the request.
* done on the request.
* *
* @param path the URL of the resource, relative to the webapp * @param path the URL of the resource, relative to the webapp root.
* root. For example, if you request a JSP page with /context/foo/bar, * For example, if you request a JSP page with
* you would call this method with path == /foo/bar. * /context/foo/bar, you would call this method with path
* == /foo/bar.
* *
* @param pageContext the JSP page context * @param pageContext the JSP page context
* *
* @exception java.io.IOException may be propagated from target resource * @exception java.io.IOException may be propagated from target
* @exception javax.servlet.ServletException may be * resource
* propagated from target resource * @exception javax.servlet.ServletException may be propagated from target
* resource
*/ */
public static void forwardRequestByPath(String path, public static void forwardRequestByPath(String path,
PageContext pageContext) PageContext pageContext)
@ -300,18 +315,20 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Forwards the request from this resource to a servlet resource * Forwards the request from this resource to a servlet resource (named in
* (named in server.xml) at the servlet-container level. This is a * server.xml) at the servlet-container level. This is a wrapper for
* wrapper for javax.servlet.RequestDispatcher and is intended to hide * javax.servlet.RequestDispatcher and is intended to hide the fact that you
* the fact that you can't "forward" a request once you've done an * can't "forward" a request once you've done an "include" in it.
* "include" in it. *
* @param name the named servlet to forward to * @param name the named servlet to forward to
* @param req the current request * @param req the current request
* @param resp the current response * @param resp the current response
* @param sctx the current servlet context * @param sctx the current servlet context
* @exception java.io.IOException may be propagated from target resource *
* @exception javax.servlet.ServletException may be * @exception java.io.IOException may be propagated from target
* propagated from target resource * resource
* @exception javax.servlet.ServletException may be propagated from target
* resource
*/ */
public static void forwardRequestByName(String name, public static void forwardRequestByName(String name,
HttpServletRequest req, HttpServletRequest req,
@ -329,6 +346,7 @@ public final class DispatcherHelper implements DispatcherConstants {
* @param name * @param name
* @param req * @param req
* @param resp * @param resp
*
* @throws java.io.IOException * @throws java.io.IOException
* @throws javax.servlet.ServletException * @throws javax.servlet.ServletException
*/ */
@ -342,27 +360,38 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* Given the name of a resource in the file system that is missing an * Given the name of a resource in the file system that is missing an
* extension, picks an extension that matches. Serves a file * extension, picks an extension that matches. Serves a file with a
* with a <code>.jsp</code> extension first, if available. * <code>.jsp</code> extension first, if available. Otherwise picks any file
* Otherwise picks any file that matches. For directories, it tacks on * that matches. For directories, it tacks on the "index" filename plus the
* the "index" filename plus the extension. * extension.
* *
* <b><font color="red">Unsupported</font></b> * <b><font color="red">Unsupported</font></b>
* *
* @param abstractFile the extensionless file * @param abstractFile the extensionless file
* @param actx the current application context * @param actx the current application context
* @return a filename suffix (".jsp", "index.html", etc.) such *
* that (abstractFile.getAbsolutePath() + suffix) is a valid file * @return a filename suffix (".jsp", "index.html", etc.) such that
* in the filesystem * (abstractFile.getAbsolutePath() + suffix) is a valid file in the
* filesystem
* *
* @exception com.arsdigita.dispatcher.RedirectException if the * @exception com.arsdigita.dispatcher.RedirectException if the
* requested file is a directory and the original request URL does * requested
* not end with a trailing slash. * file is a
* @exception java.io.FileNotFoundException if no matching * directory
* file exists. * and the
* original
* request URL
* does not
* end with a
* trailing
* slash.
* @exception java.io.FileNotFoundException if no
* matching
* file
* exists.
* @throws com.arsdigita.dispatcher.DirectoryListingException * @throws com.arsdigita.dispatcher.DirectoryListingException
* @deprecated abstract URLs are no longer supported. Use * @deprecated abstract URLs are no longer supported. Use extensions when
* extensions when your file on disk has an extension. * your file on disk has an extension.
*/ */
public static String resolveAbstractFile(File abstractFile, public static String resolveAbstractFile(File abstractFile,
RequestContext actx) RequestContext actx)
@ -392,7 +421,6 @@ public final class DispatcherHelper implements DispatcherConstants {
dirToSearch = new File(abstractFile.getParent()); dirToSearch = new File(abstractFile.getParent());
} }
File filesInDir[] = dirToSearch.listFiles(); File filesInDir[] = dirToSearch.listFiles();
final String extensionSearchList[] = {".jsp"}; final String extensionSearchList[] = {".jsp"};
@ -444,18 +472,21 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* If the given servlet request is wrapped in one of our own classes, returns * If the given servlet request is wrapped in one of our own classes,
* the original (unwrapped) request object and stores a reference * returns the original (unwrapped) request object and stores a reference to
* to the request wrapper in the request attributes of the returned request. * the request wrapper in the request attributes of the returned request.
* Otherwise just returns the request object. * Otherwise just returns the request object.
* *
* @param req the servlet request * @param req the servlet request
* @return the original servlet request object, as created by *
* the servlet container. This can be used as a parameter for forward(). * @return the original servlet request object, as created by the servlet
* container. This can be used as a parameter for forward().
*/ */
public static HttpServletRequest restoreOriginalRequest(HttpServletRequest req) { public static HttpServletRequest restoreOriginalRequest(
HttpServletRequest req) {
if (req instanceof MultipartHttpServletRequest) { if (req instanceof MultipartHttpServletRequest) {
HttpServletRequest oldReq = (HttpServletRequest) req.getAttribute(ORIGINAL_REQUEST_ATTR); HttpServletRequest oldReq = (HttpServletRequest) req.getAttribute(
ORIGINAL_REQUEST_ATTR);
oldReq.setAttribute(WRAPPED_REQUEST_ATTR, req); oldReq.setAttribute(WRAPPED_REQUEST_ATTR, req);
req = oldReq; req = oldReq;
} }
@ -463,16 +494,17 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* If we've stored a reference to a request wrapper as a request * If we've stored a reference to a request wrapper as a request attribute
* attribute to the current servlet request, returns the wrapper object. * to the current servlet request, returns the wrapper object. Otherwise,
* Otherwise, returns the request object. * returns the request object.
* *
* @param req the current servlet request * @param req the current servlet request
*
* @return the previously created wrapper around the current servlet * @return the previously created wrapper around the current servlet
* request, if any; * request, if any; otherwise returns the request object.
* otherwise returns the request object.
*/ */
public static HttpServletRequest restoreRequestWrapper(HttpServletRequest req) { public static HttpServletRequest restoreRequestWrapper(
HttpServletRequest req) {
// switch back wrapped request if we're forwarded // switch back wrapped request if we're forwarded
// from somewhere else. // from somewhere else.
Object maybeWrappedReq = req.getAttribute(WRAPPED_REQUEST_ATTR); Object maybeWrappedReq = req.getAttribute(WRAPPED_REQUEST_ATTR);
@ -488,7 +520,9 @@ public final class DispatcherHelper implements DispatcherConstants {
* or restore the original wrapper if it was already wrapped. * or restore the original wrapper if it was already wrapped.
* *
* @param sreq * @param sreq
*
* @return * @return
*
* @throws java.io.IOException * @throws java.io.IOException
* @throws javax.servlet.ServletException * @throws javax.servlet.ServletException
*/ */
@ -501,15 +535,15 @@ public final class DispatcherHelper implements DispatcherConstants {
&& type.toLowerCase().startsWith("multipart")) { && type.toLowerCase().startsWith("multipart")) {
final HttpServletRequest orig = sreq; final HttpServletRequest orig = sreq;
final HttpServletRequest previous = final HttpServletRequest previous = DispatcherHelper
DispatcherHelper.restoreRequestWrapper(orig); .restoreRequestWrapper(orig);
if (previous instanceof MultipartHttpServletRequest) { if (previous instanceof MultipartHttpServletRequest) {
s_log.debug("Build new multipart request from previous " s_log.debug("Build new multipart request from previous "
+ previous + " and current " + orig); + previous + " and current " + orig);
MultipartHttpServletRequest previousmp = MultipartHttpServletRequest previousmp
(MultipartHttpServletRequest) previous; = (MultipartHttpServletRequest) previous;
sreq = new MultipartHttpServletRequest(previousmp, sreq = new MultipartHttpServletRequest(previousmp,
orig); orig);
@ -519,7 +553,8 @@ public final class DispatcherHelper implements DispatcherConstants {
s_log.debug("The main request is now " + sreq); s_log.debug("The main request is now " + sreq);
} else { } else {
s_log.debug("The request is a new multipart; wrapping the request " s_log.debug(
"The request is a new multipart; wrapping the request "
+ "object"); + "object");
try { try {
sreq = new MultipartHttpServletRequest(sreq); sreq = new MultipartHttpServletRequest(sreq);
@ -538,6 +573,7 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* Stores req as request attribute of oldReq. * Stores req as request attribute of oldReq.
*
* @param req the current servlet request (wrapper) * @param req the current servlet request (wrapper)
* @param oldReq the original servlet request * @param oldReq the original servlet request
*/ */
@ -547,19 +583,19 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Redirects the client to the given URL without rewriting it. Delegates * Redirects the client to the given URL without rewriting it. Delegates to
* to the sendExternalRedirect method. * the sendExternalRedirect method.
* *
* @throws java.io.IOException * @throws java.io.IOException
* @deprecated This method does not rewrite URLs. Use * @deprecated This method does not rewrite URLs. Use
* sendRedirect(HttpServletRequest, HttpServletResponse, String) for * sendRedirect(HttpServletRequest, HttpServletResponse, String) for
* redirects within this ACS or * redirects within this ACS or sendExternalRedirect(HttpServletResponse,
* sendExternalRedirect(HttpServletResponse, String) for redirects to * String) for redirects to sites outside this ACS.
* sites outside this ACS.
* *
* @param resp the current response * @param resp the current response
* @param url the destination URL for redirect * @param url the destination URL for redirect
**/ *
*/
public static void sendRedirect(HttpServletResponse resp, public static void sendRedirect(HttpServletResponse resp,
String url) String url)
throws IOException { throws IOException {
@ -570,12 +606,14 @@ public final class DispatcherHelper implements DispatcherConstants {
* Rewrites the given URL and redirects the client to the rewritten URL. * Rewrites the given URL and redirects the client to the rewritten URL.
* This method should be used for redirects within this ACS. * This method should be used for redirects within this ACS.
* *
* @param req the current request; used as a source for parameters * @param req the current request; used as a source for parameters for URL
* for URL rewriting * rewriting
* @param resp the current response * @param resp the current response
* @param url the destination URL for redirect * @param url the destination URL for redirect
*
* @throws java.io.IOException * @throws java.io.IOException
**/ *
*/
public static void sendRedirect(HttpServletRequest req, public static void sendRedirect(HttpServletRequest req,
HttpServletResponse resp, HttpServletResponse resp,
String url) String url)
@ -584,13 +622,15 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Redirects the client to the given URL without rewriting it. This * Redirects the client to the given URL without rewriting it. This method
* method should be used for redirects to sites outside this ACS. * should be used for redirects to sites outside this ACS.
* *
* @param resp the current response * @param resp the current response
* @param url the destination URL for redirect * @param url the destination URL for redirect
*
* @throws java.io.IOException * @throws java.io.IOException
**/ *
*/
public static void sendExternalRedirect(HttpServletResponse resp, public static void sendExternalRedirect(HttpServletResponse resp,
String url) String url)
throws IOException { throws IOException {
@ -634,7 +674,8 @@ public final class DispatcherHelper implements DispatcherConstants {
s_log.debug("Setting destination to " + destination); s_log.debug("Setting destination to " + destination);
} }
} else { } else {
final ParameterMap params = ParameterMap.fromString(url.substring(sep + 1)); final ParameterMap params = ParameterMap.fromString(url
.substring(sep + 1));
destination = URL.there(req, url.substring(0, sep), params); destination = URL.there(req, url.substring(0, sep), params);
if (s_log.isDebugEnabled()) { if (s_log.isDebugEnabled()) {
@ -655,11 +696,12 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* Adds a <code>ParameterProvider</code> to the URLRewriter engine. * Adds a <code>ParameterProvider</code> to the URLRewriter engine.
* <code>ParameterProvider</code>s are used when * <code>ParameterProvider</code>s are used when
* <code>encodeRedirectURL</code> and <code>encodeURL</code> are * <code>encodeRedirectURL</code> and <code>encodeURL</code> are called.
* called. They add global state parameters like the session ID (for * They add global state parameters like the session ID (for cookieless
* cookieless login) to URLs for links and redirects. * login) to URLs for links and redirects.
* *
* @param provider the parameter provider to add * @param provider the parameter provider to add
*
* @see com.arsdigita.util.URLRewriter#addParameterProvider * @see com.arsdigita.util.URLRewriter#addParameterProvider
* @deprecated use URLRewriter.addParameterProvider * @deprecated use URLRewriter.addParameterProvider
*/ */
@ -669,9 +711,11 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* Clears all parameter providers. * Clears all parameter providers.
*
* @deprecated use URLRewriter#clearParameterProviders() instead * @deprecated use URLRewriter#clearParameterProviders() instead
* @see com.arsdigita.util.URLRewriter#clearParameterProviders() * @see com.arsdigita.util.URLRewriter#clearParameterProviders()
**/ *
*/
public static void clearParameterProviders() { public static void clearParameterProviders() {
URLRewriter.clearParameterProviders(); URLRewriter.clearParameterProviders();
} }
@ -681,36 +725,42 @@ public final class DispatcherHelper implements DispatcherConstants {
* provider is set. * provider is set.
* *
* @return a set of Bebop parameter models. * @return a set of Bebop parameter models.
*
* @deprecated use URLRewriter.getGlobalModels instead * @deprecated use URLRewriter.getGlobalModels instead
* @see com.arsdigita.util.URLRewriter#getGlobalModels() * @see com.arsdigita.util.URLRewriter#getGlobalModels()
**/ *
*/
public static Set getGlobalModels() { public static Set getGlobalModels() {
return URLRewriter.getGlobalModels(); return URLRewriter.getGlobalModels();
} }
/** /**
* Returns the set of global URL parameters for the given request, or * Returns the set of global URL parameters for the given request, or the
* the empty set if no provider is set. * empty set if no provider is set.
* *
* @param req the current request * @param req the current request
*
* @return a Set of Bebop parameter data. * @return a Set of Bebop parameter data.
*
* @deprecated use URLRewriter.getGlobalParams instead * @deprecated use URLRewriter.getGlobalParams instead
**/ *
*/
public static Set getGlobalParams(HttpServletRequest req) { public static Set getGlobalParams(HttpServletRequest req) {
return URLRewriter.getGlobalParams(req); return URLRewriter.getGlobalParams(req);
} }
/** /**
* Prepares the given URL for a client link. If no providers are * Prepares the given URL for a client link. If no providers are set, has no
* set, has no effect. * effect.
* *
* @param url the target URL to prepare * @param url the target URL to prepare
* @return the prepared URL with global parameters added from *
* providers * @return the prepared URL with global parameters added from providers
* *
* @deprecated This method does not encode the servlet session ID. Use * @deprecated This method does not encode the servlet session ID. Use
* encodeURL(req, res, url) instead. * encodeURL(req, res, url) instead.
**/ *
*/
public static String prepareURL(String url, HttpServletRequest req) { public static String prepareURL(String url, HttpServletRequest req) {
return URLRewriter.prepareURL(url, req); return URLRewriter.prepareURL(url, req);
} }
@ -723,11 +773,14 @@ public final class DispatcherHelper implements DispatcherConstants {
* @param req the current request * @param req the current request
* @param resp the current response * @param resp the current response
* @param url the target URL (for a link) to encode * @param url the target URL (for a link) to encode
* @return the new URL, with extra URL variables added *
* from parameter providers * @return the new URL, with extra URL variables added from parameter
* providers
*
* @deprecated use URLRewriter.encodeURL instead * @deprecated use URLRewriter.encodeURL instead
* @see com.arsdigita.util.URLRewriter * @see com.arsdigita.util.URLRewriter
**/ *
*/
public static String encodeURL(HttpServletRequest req, public static String encodeURL(HttpServletRequest req,
HttpServletResponse resp, HttpServletResponse resp,
String url) { String url) {
@ -737,8 +790,8 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* Returns a global URL prefix for referencing static assets (images, CSS, * Returns a global URL prefix for referencing static assets (images, CSS,
* etc.) on disk in href attributes. This can be on the same server * etc.) on disk in href attributes. This can be on the same server
* ("/STATIC/") or a different server/port ("http://server:port/dir/"). * ("/STATIC/") or a different server/port ("http://server:port/dir/"). The
* The return value is guaranteed to end with a trailing slash. * return value is guaranteed to end with a trailing slash.
* *
* Usage example: * Usage example:
* <pre> * <pre>
@ -755,10 +808,11 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* sets the global URL prefix for referencing static assets (images, CSS, * sets the global URL prefix for referencing static assets (images, CSS,
* etc.) from user-agents in href attributes. * etc.) from user-agents in href attributes. Package visibility is
* Package visibility is intentional. * intentional.
* *
* @param s the static asset URL * @param s the static asset URL
*
* @pre s != null * @pre s != null
*/ */
static void setStaticURL(String s) { static void setStaticURL(String s) {
@ -773,13 +827,13 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* sets the webapp Context using the webappContext parameter in enterprise.init. * sets the webapp Context using the webappContext parameter in
* This is an optional parameter. If it is not specified, null will be used and * enterprise.init. This is an optional parameter. If it is not specified,
* the value of s_webappContext will be set when there is a request. * null will be used and the value of s_webappContext will be set when there
* Package visibility is intentional. * is a request. Package visibility is intentional.
* *
* @param webappCtx the webappContext specified in enterprise.init. * @param webappCtx the webappContext specified in enterprise.init. Normally
* Normally this wouldbe "/". * this wouldbe "/".
*/ */
static void setWebappContext(String webappCtx) { static void setWebappContext(String webappCtx) {
init(); init();
@ -796,12 +850,10 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* Gets the webapp Context using the following procedure: * Gets the webapp Context using the following procedure:
* *
* 1. If there is a request, get the value from the request. * 1. If there is a request, get the value from the request. 2. If there is
* 2. If there is no request, get the value saved from a previous * no request, get the value saved from a previous request. 3. If there is
* request. * no request or previous request, use the value specified by the
* 3. If there is no request or previous request, use the value * enterprise.init webappContext parameter. 4. Lastly, return null.
* specified by the enterprise.init webappContext parameter.
* 4. Lastly, return null.
* *
* @return * @return
*/ */
@ -821,7 +873,8 @@ public final class DispatcherHelper implements DispatcherConstants {
if (!s_webappCtx.equals(webappCtx)) { if (!s_webappCtx.equals(webappCtx)) {
s_log.warn( s_log.warn(
"webappContext changed. Expected='" + s_webappCtx "webappContext changed. Expected='" + s_webappCtx
+ "' found='" + webappCtx + "'.\nPerhaps the enterprise.init " + "' found='" + webappCtx
+ "'.\nPerhaps the enterprise.init "
+ "com.arsdigita.dispatcher.Initializer webappContext " + "com.arsdigita.dispatcher.Initializer webappContext "
+ "parameter is wrong."); + "parameter is wrong.");
// Save the webappCtx from the request for future use. // Save the webappCtx from the request for future use.
@ -838,22 +891,23 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Aborts all processing of the current request and treat it * Aborts all processing of the current request and treat it as successfully
* as successfully completed. We abort the request by percolating * completed. We abort the request by percolating an unchecked Error up
* an unchecked Error up through the call stack. Then the * through the call stack. Then the BaseDispatcherServlet.service method
* BaseDispatcherServlet.service method traps this and commits * traps this and commits whatever DML has already happened on the
* whatever DML has already happened on the transaction. * transaction.
* *
* @exception com.arsdigita.dispatcher.AbortRequestSignal Error thrown * @exception com.arsdigita.dispatcher.AbortRequestSignal Error thrown to
* to abort current request * abort current
* request
*/ */
public static void abortRequest() { public static void abortRequest() {
throw new AbortRequestSignal(); throw new AbortRequestSignal();
} }
/** /**
* Stores the HttpServletRequest in a ThreadLocal so that it can be * Stores the HttpServletRequest in a ThreadLocal so that it can be accessed
* accessed globally. * globally.
* *
* @param r * @param r
*/ */
@ -864,6 +918,7 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* Gets the current HttpServletRequest for this thread. * Gets the current HttpServletRequest for this thread.
*
* @return the current HttpServletRequest for this thread. * @return the current HttpServletRequest for this thread.
*/ */
public static HttpServletRequest getRequest() { public static HttpServletRequest getRequest() {
@ -871,7 +926,9 @@ public final class DispatcherHelper implements DispatcherConstants {
return (HttpServletRequest) s_request.get(); return (HttpServletRequest) s_request.get();
} }
/***************************************************/ /**
* ************************************************
*/
/* !!! Danger Will Robinson!!!! */ /* !!! Danger Will Robinson!!!! */
/* */ /* */
/* Don't go making changes to the cache headers */ /* Don't go making changes to the cache headers */
@ -879,10 +936,13 @@ public final class DispatcherHelper implements DispatcherConstants {
/* sections on caching in RFC 2616 (HTTP 1.1) */ /* sections on caching in RFC 2616 (HTTP 1.1) */
/* */ /* */
/* -- Daniel Berrange <berrange@redhat.com> */ /* -- Daniel Berrange <berrange@redhat.com> */
/***************************************************/
/** /**
* If no existing cache policy is set, then call * ************************************************
* cacheDisable to disable all caching of the response. */
/**
* If no existing cache policy is set, then call cacheDisable to disable all
* caching of the response.
*
* @param response * @param response
*/ */
public static void maybeCacheDisable(HttpServletResponse response) { public static void maybeCacheDisable(HttpServletResponse response) {
@ -931,8 +991,8 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* If no existing cache policy is set, then * If no existing cache policy is set, then call cacheForUser to enable
* call cacheForUser to enable caching for a user. * caching for a user.
* *
* @param response * @param response
*/ */
@ -943,9 +1003,9 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Allow caching of the response for this user only, as identified * Allow caching of the response for this user only, as identified by the
* by the Cookie header. The response will expire according * Cookie header. The response will expire according to the default age
* to the default age setting. * setting.
* *
* @param response * @param response
*/ */
@ -954,8 +1014,8 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* If no existing cache policy is set, then * If no existing cache policy is set, then call cacheForUser to enable
* call cacheForUser to enable caching for a user * caching for a user
* *
* @param response * @param response
* @param maxage the max time in second until this expires * @param maxage the max time in second until this expires
@ -968,8 +1028,8 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* If no existing cache policy is set, then * If no existing cache policy is set, then call cacheForUser to enable
* call cacheForUser to enable caching for a user * caching for a user
* *
* @param response * @param response
* @param expiry the time at which to expire * @param expiry the time at which to expire
@ -982,9 +1042,9 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Allow caching of the response for this user only, * Allow caching of the response for this user only, as identified by the
* as identified by the Cookie header. The response * Cookie header. The response will expire in 'age' seconds time.
* will expire in 'age' seconds time. *
* @param response * @param response
* @param maxage the max life of the response in seconds * @param maxage the max life of the response in seconds
*/ */
@ -1002,7 +1062,8 @@ public final class DispatcherHelper implements DispatcherConstants {
// For HTTP/1.1 user agents, we tell them only cache // For HTTP/1.1 user agents, we tell them only cache
// for the original person making the request // for the original person making the request
response.setHeader("Last-Modified", rfc1123_formatter.format(new Date())); response
.setHeader("Last-Modified", rfc1123_formatter.format(new Date()));
response.setHeader("Cache-Control", "private, max-age=" + maxage); response.setHeader("Cache-Control", "private, max-age=" + maxage);
// NB. THis line is delibrately *NOT* using the actual expiry date // NB. THis line is delibrately *NOT* using the actual expiry date
@ -1014,20 +1075,22 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Allowing caching of the response for this user only. * Allowing caching of the response for this user only. The response will
* The response will expire at time given in the expiry parameter * expire at time given in the expiry parameter
*
* @param response * @param response
* @param expiry time at which to expire * @param expiry time at which to expire
*/ */
public static void cacheForUser(HttpServletResponse response, public static void cacheForUser(HttpServletResponse response,
Date expiry) { Date expiry) {
cacheForUser(response, (int) ((expiry.getTime() - (new Date()).getTime()) / 1000l)); cacheForUser(response,
(int) ((expiry.getTime() - (new Date()).getTime()) / 1000l));
} }
/** /**
* If no existing cache policy is set, then call cacheForUser to enable * If no existing cache policy is set, then call cacheForUser to enable
* caching for the world. The response expiry will take the default * caching for the world. The response expiry will take the default age
* age setting. * setting.
* *
* @param response * @param response
*/ */
@ -1038,8 +1101,8 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Allow caching of this response for anyone in the world. * Allow caching of this response for anyone in the world. The response take
* The response take the default expiry time. * the default expiry time.
* *
* @param response * @param response
*/ */
@ -1062,8 +1125,8 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* If no existing cache policy is set, then call cacheForUser to * If no existing cache policy is set, then call cacheForUser to enable
* enable caching for the world. * caching for the world.
* *
* @param response * @param response
* @param expiry the time at which it will expire * @param expiry the time at which it will expire
@ -1076,9 +1139,9 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Allow caching of this response for anyone in the * Allow caching of this response for anyone in the world. The response will
* world. The response will expire at the current time * expire at the current time plus maxage seconds.
* plus maxage seconds. *
* @param response * @param response
* @param maxage time in seconds until this expires * @param maxage time in seconds until this expires
*/ */
@ -1104,15 +1167,16 @@ public final class DispatcherHelper implements DispatcherConstants {
} }
/** /**
* Allow caching of this response for anyone in the world. * Allow caching of this response for anyone in the world. THe response will
* THe response will expire at the time given. * expire at the time given.
* *
* @param response * @param response
* @param expiry * @param expiry
*/ */
public static void cacheForWorld(HttpServletResponse response, public static void cacheForWorld(HttpServletResponse response,
Date expiry) { Date expiry) {
cacheForWorld(response, (int) ((expiry.getTime() - (new Date()).getTime()) / 1000l)); cacheForWorld(response, (int) ((expiry.getTime() - (new Date())
.getTime()) / 1000l));
} }
/** /**
@ -1127,14 +1191,16 @@ public final class DispatcherHelper implements DispatcherConstants {
// Set the preferedLocale to the default locale (first entry in the // Set the preferedLocale to the default locale (first entry in the
// config parameter list) // config parameter list)
Locale preferedLocale = new Locale(kernelConfig.getDefaultLanguage(), "", ""); Locale preferedLocale
= new Locale(kernelConfig.getDefaultLanguage(), "", "");
// The ACCEPTED_LANGUAGES from the client // The ACCEPTED_LANGUAGES from the client
Enumeration locales = null; Enumeration locales = null;
// Try to get the RequestContext // Try to get the RequestContext
try { try {
locales = ((ServletRequest) DispatcherHelper.getRequest()).getLocales(); locales = ((ServletRequest) DispatcherHelper.getRequest())
.getLocales();
// For everey element in the enumerator // For everey element in the enumerator
while (locales.hasMoreElements()) { while (locales.hasMoreElements()) {
@ -1161,8 +1227,8 @@ public final class DispatcherHelper implements DispatcherConstants {
/** /**
* Encodes the given request into a return URL parameter. Returns * Encodes the given request into a return URL parameter. Returns
* <code>URLencode(returnURL)</code> where returnURL is * <code>URLencode(returnURL)</code> where returnURL is
* <code>returnURI?key=URLencode(val)&...</code>. The original parameter values are * <code>returnURI?key=URLencode(val)&...</code>. The original parameter
* doubly-encoded so that they are decoded appropriately. * values are doubly-encoded so that they are decoded appropriately.
* *
* *
* @param req the request to encode * @param req the request to encode