This is an automated email from the ASF dual-hosted git repository. tv pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/turbine-core.git
commit 3ab027295f693b1d66e4be9846bc618f5ac07493 Author: Thomas Vandahl <[email protected]> AuthorDate: Tue Dec 16 21:55:59 2025 +0100 WIP: Refactor RunData --- .../services/rundata/DefaultTurbineRunData.java | 350 +-------------------- .../turbine/services/rundata/TurbineRunData.java | 11 +- .../services/velocity/TurbineVelocityService.java | 4 +- src/java/org/apache/turbine/util/RunData.java | 149 +++++++-- 4 files changed, 146 insertions(+), 368 deletions(-) diff --git a/src/java/org/apache/turbine/services/rundata/DefaultTurbineRunData.java b/src/java/org/apache/turbine/services/rundata/DefaultTurbineRunData.java index e97e0e65..891ba15c 100644 --- a/src/java/org/apache/turbine/services/rundata/DefaultTurbineRunData.java +++ b/src/java/org/apache/turbine/services/rundata/DefaultTurbineRunData.java @@ -45,12 +45,9 @@ import org.apache.turbine.services.TurbineServices; import org.apache.turbine.services.template.TemplateService; import org.apache.turbine.util.FormMessages; import org.apache.turbine.util.LocaleUtils; -import org.apache.turbine.util.ServerData; import org.apache.turbine.util.SystemError; import org.apache.turbine.util.template.TemplateInfo; -import jakarta.servlet.ServletConfig; -import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; @@ -97,7 +94,7 @@ public class DefaultTurbineRunData private String screen; /** The character encoding of template files. */ - private String templateEncoding; + private Charset templateEncoding; /** This is what will build the title of the document. */ private String title; @@ -111,9 +108,6 @@ public class DefaultTurbineRunData */ private PrintWriter out; - /** The HTTP charset. */ - private Charset charSet; - /** The HTTP content type to return. */ private String contentType = TurbineConstants.DEFAULT_HTML_CONTENT_TYPE; @@ -129,15 +123,6 @@ public class DefaultTurbineRunData /** JNDI Contexts. */ private Map<String, Context> jndiContexts; - /** @see #getRemoteAddr() */ - private String remoteAddr; - - /** @see #getRemoteHost() */ - private String remoteHost; - - /** @see #getUserAgent() */ - private String userAgent; - /** A holder for stack trace. */ private String stackTrace; @@ -229,19 +214,15 @@ public class DefaultTurbineRunData action = null; layout = null; screen = null; - templateEncoding = null; + templateEncoding = null; // FIXME This is never set title = null; outSet = false; out = null; - charSet = null; contentType = TurbineConstants.DEFAULT_HTML_CONTENT_TYPE; redirectURI = null; statusCode = HttpServletResponse.SC_OK; errors.clear(); jndiContexts = null; - remoteAddr = null; - remoteHost = null; - userAgent = null; stackTrace = null; stackTraceException = null; debugVariables.clear(); @@ -291,61 +272,6 @@ public class DefaultTurbineRunData return cookies; } - /** - * Gets the servlet request. - * - * @return the request. - */ - @Override - public HttpServletRequest getRequest() - { - return get(Turbine.class, HttpServletRequest.class); - } - - /** - * Gets the servlet response. - * - * @return the response. - */ - @Override - public HttpServletResponse getResponse() - { - return get(Turbine.class, HttpServletResponse.class); - } - - /** - * Gets the servlet session information. - * - * @return the session. - */ - @Override - public HttpSession getSession() - { - return getRequest().getSession(); - } - - /** - * Gets the servlet configuration used during servlet init. - * - * @return the configuration. - */ - @Override - public ServletConfig getServletConfig() - { - return get(Turbine.class, ServletConfig.class); - } - - /** - * Gets the servlet context used during servlet init. - * - * @return the context. - */ - @Override - public ServletContext getServletContext() - { - return get(Turbine.class, ServletContext.class); - } - /** * Gets the access control list. * @@ -454,33 +380,6 @@ public class DefaultTurbineRunData this.layout = layout; } - /** - * Convenience method for a template info that - * returns the layout template being used. - * - * @return a string. - */ - @Override - public String getLayoutTemplate() - { - return getTemplateInfo().getLayoutTemplate(); - } - - /** - * Modifies the layout template for the screen. This convenience - * method allows for a layout to be modified from within a - * template. For example; - * - * $data.setLayoutTemplate("NewLayout.vm") - * - * @param layout a layout template. - */ - @Override - public void setLayoutTemplate(String layout) - { - getTemplateInfo().setLayoutTemplate(layout); - } - /** * Whether or not a screen has been defined. * @@ -514,39 +413,13 @@ public class DefaultTurbineRunData this.screen = screen; } - /** - * Convenience method for a template info that - * returns the name of the template being used. - * - * @return a string. - */ - @Override - public String getScreenTemplate() - { - return getTemplateInfo().getScreenTemplate(); - } - - /** - * Sets the screen template for the request. For - * example; - * - * $data.setScreenTemplate("NewScreen.vm") - * - * @param screen a screen template. - */ - @Override - public void setScreenTemplate(String screen) - { - getTemplateInfo().setScreenTemplate(screen); - } - /** * Gets the character encoding to use for reading template files. * * @return the template encoding or null if not specified. */ @Override - public String getTemplateEncoding() + public Charset getTemplateCharset() { return templateEncoding; } @@ -557,7 +430,7 @@ public class DefaultTurbineRunData * @param encoding the template encoding. */ @Override - public void setTemplateEncoding(String encoding) + public void setTemplateCharset(Charset encoding) { templateEncoding = encoding; } @@ -727,20 +600,6 @@ public class DefaultTurbineRunData return (user != null); } - /** - * Gets the user. - * - * @param <T> a type extending {@link User} - * - * @return a user. - */ - @SuppressWarnings("unchecked") - @Override - public <T extends User> T getUser() - { - return (T)get(Turbine.class, User.class); - } - /** * Sets the user. * @@ -837,6 +696,7 @@ public class DefaultTurbineRunData if (locale == null) { locale = LocaleUtils.getDefaultLocale(); + setLocale(locale); } return locale; } @@ -852,8 +712,8 @@ public class DefaultTurbineRunData get(Turbine.class).put(Locale.class, locale); // propagate the locale to the parsers - ParameterParser parameters = get(Turbine.class, ParameterParser.class); - CookieParser cookies = get(Turbine.class, CookieParser.class); + ParameterParser parameters = getParameterParser(); + CookieParser cookies = getCookieParser(); if (parameters != null) { @@ -866,32 +726,6 @@ public class DefaultTurbineRunData } } - /** - * Gets the charset. If it has not already been defined with - * setCharSet(), then a property named "locale.default.charset" - * is checked from the Resource Service and returned. If this - * property is undefined, the default charset of the locale - * is returned. If the locale is undefined, null is returned. - * - * @return the name of the charset or null. - */ - @Override - public String getCharSet() - { - return getCharset().name(); - } - - /** - * Sets the charset. - * - * @param charSet the name of the new charset. - */ - @Override - public void setCharSet(String charSet) - { - setCharset(Charset.forName(charSet)); - } - /** * Gets the charset. If it has not already been defined with * setCharSet(), then a property named "locale.default.charset" @@ -904,12 +738,13 @@ public class DefaultTurbineRunData @Override public Charset getCharset() { - log.debug("getCharset()"); + Charset charSet = get(Turbine.class, Charset.class); if (charSet == null) { log.debug("Charset was null!"); charSet = LocaleUtils.getDefaultCharset(); + setCharset(charSet); } return charSet; @@ -924,7 +759,7 @@ public class DefaultTurbineRunData public void setCharset(Charset charSet) { log.debug("setCharset({})", charSet); - this.charSet = charSet; + get(Turbine.class).put(Charset.class, charSet); } /** @@ -943,19 +778,13 @@ public class DefaultTurbineRunData { if (StringUtils.isNotEmpty(contentType)) { - if (charSet == null) - { - if (contentType.startsWith("text/")) - { - return contentType + "; charset=" + LocaleUtils.getDefaultCharset(); - } - - return contentType; - } - else + if (contentType.startsWith("text/")) { + Charset charSet = getCharset(); return contentType + "; charset=" + charSet.name(); } + + return contentType; } return ""; @@ -1026,9 +855,7 @@ public class DefaultTurbineRunData @Override public SystemError[] getSystemErrors() { - SystemError[] result = new SystemError[errors.size()]; - errors.toArray(result); - return result; + return errors.toArray(new SystemError[0]); } /** @@ -1068,122 +895,6 @@ public class DefaultTurbineRunData this.jndiContexts = contexts; } - /** - * Gets the cached server scheme. - * - * @return a string. - */ - @Override - public String getServerScheme() - { - return getServerData().getServerScheme(); - } - - /** - * Gets the cached server name. - * - * @return a string. - */ - @Override - public String getServerName() - { - return getServerData().getServerName(); - } - - /** - * Gets the cached server port. - * - * @return an int. - */ - @Override - public int getServerPort() - { - return getServerData().getServerPort(); - } - - /** - * Gets the cached context path. - * - * @return a string. - */ - @Override - public String getContextPath() - { - return getServerData().getContextPath(); - } - - /** - * Gets the cached script name. - * - * @return a string. - */ - @Override - public String getScriptName() - { - return getServerData().getScriptName(); - } - - /** - * Gets the server data ofy the request. - * - * @return server data. - */ - @Override - public ServerData getServerData() - { - return get(Turbine.class, ServerData.class); - } - - /** - * Gets the IP address of the client that sent the request. - * - * @return a string. - */ - @Override - public String getRemoteAddr() - { - if (this.remoteAddr == null) - { - this.remoteAddr = this.getRequest().getRemoteAddr(); - } - - return this.remoteAddr; - } - - /** - * Gets the qualified name of the client that sent the request. - * - * @return a string. - */ - @Override - public String getRemoteHost() - { - if (this.remoteHost == null) - { - this.remoteHost = this.getRequest().getRemoteHost(); - } - - return this.remoteHost; - } - - /** - * Get the user agent for the request. The semantics here - * are muddled because RunData caches the value after the - * first invocation. This is different e.g. from getCharSet(). - * - * @return a string. - */ - @Override - public String getUserAgent() - { - if (StringUtils.isEmpty(userAgent)) - { - userAgent = this.getRequest().getHeader("User-Agent"); - } - - return userAgent; - } - /** * Pulls a user object from the session and increments the access * counter and sets the last access date for the object. @@ -1271,37 +982,6 @@ public class DefaultTurbineRunData return this.debugVariables; } - // ********************************************** - // Implementation of the TurbineRunData interface - // ********************************************** - - /** - * Gets the parameter parser without parsing the parameters. - * - * @return the parameter parser. - * TODO Does this method make sense? Pulling the parameter out of - * the run data object before setting a request (which happens - * only in getParameters() leads to the Parameter parser having - * no object and thus the default or even an undefined encoding - * instead of the actual request character encoding). - */ - @Override - public ParameterParser getParameterParser() - { - return get(Turbine.class, ParameterParser.class); - } - - /** - * Gets the cookie parser without parsing the cookies. - * - * @return the cookie parser. - */ - @Override - public CookieParser getCookieParser() - { - return get(Turbine.class, CookieParser.class); - } - // ******************** // Miscellaneous setters // ******************** diff --git a/src/java/org/apache/turbine/services/rundata/TurbineRunData.java b/src/java/org/apache/turbine/services/rundata/TurbineRunData.java index d0b194e8..6d4de758 100644 --- a/src/java/org/apache/turbine/services/rundata/TurbineRunData.java +++ b/src/java/org/apache/turbine/services/rundata/TurbineRunData.java @@ -22,6 +22,7 @@ package org.apache.turbine.services.rundata; import org.apache.fulcrum.parser.CookieParser; import org.apache.fulcrum.parser.ParameterParser; import org.apache.fulcrum.pool.Recyclable; +import org.apache.turbine.Turbine; import org.apache.turbine.util.RunData; /** @@ -50,12 +51,18 @@ public interface TurbineRunData * * @return the parameter parser. */ - ParameterParser getParameterParser(); + default ParameterParser getParameterParser() + { + return get(Turbine.class, ParameterParser.class); + } /** * Gets the cookie parser without parsing the cookies. * * @return the cookie parser. */ - CookieParser getCookieParser(); + default CookieParser getCookieParser() + { + return get(Turbine.class, CookieParser.class); + } } diff --git a/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java b/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java index d4a57b58..35be7d41 100644 --- a/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java +++ b/src/java/org/apache/turbine/services/velocity/TurbineVelocityService.java @@ -432,9 +432,9 @@ public class TurbineVelocityService Charset encoding = null; Object data = context.get(VelocityService.RUNDATA_KEY); - if (data != null && data instanceof RunData rd && rd.getTemplateEncoding() != null) + if (data != null && data instanceof RunData rd && rd.getTemplateCharset() != null) { - encoding = Charset.forName(rd.getTemplateEncoding()); + encoding = rd.getTemplateCharset(); } return encoding != null ? encoding : defaultInputEncoding; diff --git a/src/java/org/apache/turbine/util/RunData.java b/src/java/org/apache/turbine/util/RunData.java index 2b55d795..d13fe2f3 100644 --- a/src/java/org/apache/turbine/util/RunData.java +++ b/src/java/org/apache/turbine/util/RunData.java @@ -28,19 +28,21 @@ import java.util.Locale; import java.util.Map; import javax.naming.Context; -import jakarta.servlet.ServletConfig; -import jakarta.servlet.ServletContext; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import jakarta.servlet.http.HttpSession; import org.apache.fulcrum.parser.CookieParser; import org.apache.fulcrum.parser.ParameterParser; import org.apache.fulcrum.security.acl.AccessControlList; +import org.apache.turbine.Turbine; import org.apache.turbine.om.security.User; import org.apache.turbine.pipeline.PipelineData; import org.apache.turbine.util.template.TemplateInfo; +import jakarta.servlet.ServletConfig; +import jakarta.servlet.ServletContext; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + /** * RunData is an interface to run-time information that is passed * within Turbine. This provides the threading mechanism for the @@ -76,35 +78,50 @@ public interface RunData extends PipelineData * * @return the request. */ - HttpServletRequest getRequest(); + default HttpServletRequest getRequest() + { + return get(Turbine.class, HttpServletRequest.class); + } /** * Gets the servlet response. * * @return the resposne. */ - HttpServletResponse getResponse(); + default HttpServletResponse getResponse() + { + return get(Turbine.class, HttpServletResponse.class); + } /** * Gets the servlet session information. * * @return the session. */ - HttpSession getSession(); + default HttpSession getSession() + { + return getRequest().getSession(); + } /** * Gets the servlet configuration used during servlet init. * * @return the configuration. */ - ServletConfig getServletConfig(); + default ServletConfig getServletConfig() + { + return get(Turbine.class, ServletConfig.class); + } /** * Gets the servlet context used during servlet init. * * @return the context. */ - ServletContext getServletContext(); + default ServletContext getServletContext() + { + return get(Turbine.class, ServletContext.class); + } /** * Gets the access control list. @@ -173,7 +190,10 @@ public interface RunData extends PipelineData * * @return a string. */ - String getLayoutTemplate(); + default String getLayoutTemplate() + { + return getTemplateInfo().getLayoutTemplate(); + } /** * Modifies the layout template for the screen. This convenience @@ -184,7 +204,10 @@ public interface RunData extends PipelineData * * @param layout a layout template. */ - void setLayoutTemplate(String layout); + default void setLayoutTemplate(String layout) + { + getTemplateInfo().setLayoutTemplate(layout); + } /** * Whether or not a screen has been defined. @@ -213,7 +236,10 @@ public interface RunData extends PipelineData * * @return a string. */ - String getScreenTemplate(); + default String getScreenTemplate() + { + return getTemplateInfo().getScreenTemplate(); + } /** * Sets the screen template for the request. For @@ -223,21 +249,46 @@ public interface RunData extends PipelineData * * @param screen a screen template. */ - void setScreenTemplate(String screen); + default void setScreenTemplate(String screen) + { + getTemplateInfo().setScreenTemplate(screen); + } /** * Gets the character encoding to use for reading template files. * * @return the template encoding or null if not specified. */ - String getTemplateEncoding(); + Charset getTemplateCharset(); /** * Sets the character encoding to use for reading template files. * * @param encoding the template encoding. */ - void setTemplateEncoding(String encoding); + void setTemplateCharset(Charset encoding); + + /** + * Gets the character encoding to use for reading template files. + * + * @return the template encoding or null if not specified. + */ + @Deprecated + default String getTemplateEncoding() + { + return getTemplateCharset().name(); + } + + /** + * Sets the character encoding to use for reading template files. + * + * @param encoding the template encoding. + */ + @Deprecated + default void setTemplateEncoding(String encoding) + { + setTemplateCharset(Charset.forName(encoding)); + } /** * Gets the template info. Creates a new one if needed. @@ -332,7 +383,11 @@ public interface RunData extends PipelineData * * @return a user. */ - <T extends User> T getUser(); + @SuppressWarnings("unchecked") + default <T extends User> T getUser() + { + return (T)get(Turbine.class, User.class); + } /** * Sets the user. @@ -341,7 +396,10 @@ public interface RunData extends PipelineData * * @param <T> a type extending {@link User} */ - <T extends User> void setUser(T user); + default <T extends User> void setUser(T user) + { + get(Turbine.class).put(User.class, user); + } /** * Attempts to get the user from the session. If it does @@ -415,7 +473,10 @@ public interface RunData extends PipelineData * @return the name of the charset or null. */ @Deprecated - String getCharSet(); + default String getCharSet() + { + return getCharset().name(); + } /** * Sets the charset. @@ -423,7 +484,10 @@ public interface RunData extends PipelineData * @param charset the name of the new charset. */ @Deprecated - void setCharSet(String charset); + default void setCharSet(String charset) + { + setCharset(Charset.forName(charset)); + } /** * Gets the charset. If it has not already been defined with @@ -526,63 +590,90 @@ public interface RunData extends PipelineData * * @return a string. */ - String getServerScheme(); + default String getServerScheme() + { + return getServerData().getServerScheme(); + } /** * Gets the cached server name. * * @return a string. */ - String getServerName(); + default String getServerName() + { + return getServerData().getServerName(); + } /** * Gets the cached server port. * * @return an int. */ - int getServerPort(); + default int getServerPort() + { + return getServerData().getServerPort(); + } /** * Gets the cached context path. * * @return a string. */ - String getContextPath(); + default String getContextPath() + { + return getServerData().getContextPath(); + } /** * Gets the cached script name. * * @return a string. */ - String getScriptName(); + default String getScriptName() + { + return getServerData().getScriptName(); + } /** * Gets the server data used by the request. * * @return server data. */ - ServerData getServerData(); + default ServerData getServerData() + { + return get(Turbine.class, ServerData.class); + } /** * Gets the IP address of the client that sent the request. * * @return a string. */ - String getRemoteAddr(); + default String getRemoteAddr() + { + return getRequest().getRemoteAddr(); + } /** * Gets the qualified name of the client that sent the request. * * @return a string. */ - String getRemoteHost(); + default String getRemoteHost() + { + return getRequest().getRemoteHost(); + } /** * Get the user agent for the request. * * @return a string. */ - String getUserAgent(); + default String getUserAgent() + { + return getRequest().getHeader("User-Agent"); + } /** * Pulls a user object from the session and increments the access
