velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Bubna" <nbu...@gmail.com>
Subject Re: getting list of all context values
Date Wed, 18 Oct 2006 20:52:13 GMT
Oh, i should also mention that it's written for VelocityTools 1.3-dev,
so it doesn't currently declare that it implements the Configurable or
ViewTool interfaces.  To work with previous VelocityTools versions, it
will need to at least declare that it implements ViewTool.

On 10/18/06, Nathan Bubna <nbubna@gmail.com> wrote:
> On 10/18/06, Charles Harvey III <charlieh@alloy.com> wrote:
> > Ok.  I tried this out.  And it works... sort of.  There are definately
> > things missing and I can't figure out why.
> >
> > As in, I know for sure that I am doing:
> >
> >     request.setAttribute( "person", person );
> >
> > And "person" isn't in the list of $request.attributeNames.  I can't for the
> > life of me figure out why though.  Is there something I am missing?  This
> > is what is getting printed to the page for the $request.attributeNames:
> >
> > # javax.servlet.forward.request_uri
> > # javax.servlet.forward.context_path
> > # javax.servlet.forward.servlet_path
> > # javax.servlet.forward.path_info
> > # org.springframework.web.servlet.DispatcherServlet.THEME
> > # __acegi_session_integration_filter_applied
> > # org.springframework.web.servlet.DispatcherServlet.CONTEXT
> > # __acegi_filterSecurityInterceptor_filterApplied
> > # org.springframework.web.servlet.DispatcherServlet.LOCALE
>
> That's pretty odd.  Assuming you're sure that the setAttribute and the
> getAttributeNames are happening in the same request thread (not
> subsequent requests or some sort of secondary connection, like with
> ImportTool), then i'm really not sure why the person attribute would
> be removed.  It's not something that the VVS or VLS would do.
>
> > What is even stranger is that it works for the session.
> > $session.attributeNames:
> >
> > # website
> > # IS_USER_ONLINE_SESSION_BINDING_LISTENER
> >
> > Those are definately two values that I specifically put into the
> > session.  All
> > I can think of is that Spring is doing something to my objects and I guess I
> > have to go figure out what.
>
> might try checking out the exact class of the request object to see if
> it is being wrapped along the way
>
> $request.class.name
>
> Also, if you want to hack around, i hacked up a quick ContextTool.  I
> haven't tested it yet (and thus haven't committed it) and it relies on
> the addition of a new method to VelocityTools' ChainedContext class,
> but i figure you should be able to get it working without much
> modification.  Let me know what you think...
>
> /*
>  * Copyright 2006 The Apache Software Foundation.
>  *
>  * Licensed under the Apache License, Version 2.0 (the "License");
>  * you may not use this file except in compliance with the License.
>  * You may obtain a copy of the License at
>  *
>  *     http://www.apache.org/licenses/LICENSE-2.0
>  *
>  * Unless required by applicable law or agreed to in writing, software
>  * distributed under the License is distributed on an "AS IS" BASIS,
>  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>  * See the License for the specific language governing permissions and
>  * limitations under the License.
>  */
>
> package org.apache.velocity.tools.view.tools;
>
> import java.util.Arrays;
> import java.util.Enumeration;
> import java.util.HashSet;
> import java.util.Iterator;
> import java.util.Map;
> import java.util.Set;
> import javax.servlet.http.HttpServletRequest;
> import javax.servlet.http.HttpSession;
> import javax.servlet.ServletContext;
> import org.apache.velocity.context.AbstractContext;
> import org.apache.velocity.context.Context;
> import org.apache.velocity.tools.generic.ValueParser;
> import org.apache.velocity.tools.view.context.ChainedContext;
> import org.apache.velocity.tools.view.context.ViewContext;
>
> /**
>  * <p>View tool for convenient access to all available context keys
> and values.</p>
>  * <p><pre>
>  * Template example(s):
>  *  #foreach( $key in $context.keys )
>  *    $key = $context.get($key)
>  *  #end
>  *
>  * Toolbox configuration:
>  * &lt;tool&gt;
>  *   &lt;key&gt;context&lt;/key&gt;
>  *   &lt;scope&gt;request&lt;/scope&gt;
>  *   &lt;class&gt;org.apache.velocity.tools.view.tools.ContextTool&lt;/class&gt;
>  * &lt;/tool&gt;
>  * </pre></p>
>  *
>  * <p>This class is only designed for use as a request-scope tool.</p>
>  *
>  * @author <a href="mailto:nathan@esha.com">Nathan Bubna</a>
>  * @since VelocityTools 1.3
>  * @version $Id: ContextTool.java 385122 2006-03-11 18:37:42Z nbubna $
>  */
> public class ContextTool
> {
>     /**
>      * The key used for specifying whether to hide keys with '.' in them.
>      */
>     public static final String SAFE_MODE_KEY = "safe-mode";
>
>     protected ViewContext context;
>     protected HttpServletRequest request;
>     protected HttpSession session;
>     protected ServletContext application;
>
>     private boolean safeMode = true;
>
>
>     /**
>      * Looks for a safe-mode configuration setting. By default,
>      * safe-mode is true and thus keys with '.' in them are hidden.
>      */
>     public void configure(Map params)
>     {
>         if (params != null)
>         {
>             ValueParser parser = new ValueParser(params);
>             safeMode = parser.getBoolean(SAFE_MODE_KEY, true);
>         }
>     }
>
>     /**
>      * Initializes this instance for the current request.
>      *
>      * @param obj the ViewContext of the current request
>      */
>     public void init(Object obj)
>     {
>         this.context = (ViewContext)obj;
>         this.request = context.getRequest();
>         this.session = request.getSession(false);
>         this.application = context.getServletContext();
>     }
>
>
>     /**
>      * Return a {@link Set} of the available reference keys in the current
>      * context.
>      */
>     public Set getKeys()
>     {
>         Set keys = new HashSet();
>
>         // get the tool keys, if there is a toolbox
>         if (this.context instanceof ChainedContext)
>         {
>             Set toolKeys = ((ChainedContext)this.context).getToolKeys();
>             keys.addAll(toolKeys);
>         }
>
>         // recurse down the velocity context collecting keys
>         Context velctx = this.context.getVelocityContext();
>         while (velctx != null)
>         {
>             Object[] ctxKeys = velctx.getKeys();
>             keys.addAll(Arrays.asList(ctxKeys));
>             if (velctx instanceof AbstractContext)
>             {
>                 velctx = ((AbstractContext)velctx).getChainedContext();
>             }
>             else
>             {
>                 velctx = null;
>             }
>         }
>
>         // get request attribute keys
>         Enumeration e = request.getAttributeNames();
>         while (e.hasMoreElements())
>         {
>             keys.add(e.nextElement());
>         }
>
>         // get session attribute keys if we have a session
>         if (session != null)
>         {
>             e = session.getAttributeNames();
>             while (e.hasMoreElements())
>             {
>                 keys.add(e.nextElement());
>             }
>         }
>
>         // get request attribute keys
>         e = application.getAttributeNames();
>         while (e.hasMoreElements())
>         {
>             keys.add(e.nextElement());
>         }
>
>         // if we're in safe mode, remove keys that contain '.'
>         if (safeMode)
>         {
>             for (Iterator i = keys.iterator(); i.hasNext(); )
>             {
>                 String key = String.valueOf(i.next());
>                 if (key.indexOf('.') >= 0)
>                 {
>                     i.remove();
>                 }
>             }
>         }
>
>         // return the key set
>         return keys;
>     }
>
>     /**
>      * Retrieves the value for the specified reference name (aka context key).
>      */
>     public Object get(Object refName)
>     {
>         String key = String.valueOf(refName);
>         if (safeMode && key.indexOf('.') >= 0)
>         {
>             return null;
>         }
>         return this.context.getVelocityContext().get(key);
>     }
>
> }
>

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Mime
View raw message