After some testing, I noticed that this is working, but not as good as I'd
hoped. It seems that the cache is kicking in
and $cookies.getCookie("com.raibledesigns.theme") is only getting called
the first time the page loads. Subsequent requests get the page from the
cache, so a new cookie value is never read. I'm guessing there's no way to
disable the cache for a certain portion of the page? If not, is there
anyway to read cookies every time in a page?Thanks, Matt On Sat, Mar 29, 2014 at 8:43 AM, Matt Raible <[email protected]> wrote: > Thanks - using this I was able to create a CookieModel plugin: > > package org.apache.roller.cookies.plugins.pagemodel; > > import java.util.Map; > import javax.servlet.http.Cookie; > import javax.servlet.http.HttpServletRequest; > import javax.servlet.jsp.PageContext; > > import org.apache.commons.logging.Log; > import org.apache.commons.logging.LogFactory; > import org.apache.roller.weblogger.ui.core.RollerSession; > import org.apache.roller.weblogger.WebloggerException; > import org.apache.roller.weblogger.ui.rendering.model.Model; > > public class CookieModel implements Model { > private static Log log = LogFactory.getLog(CookieModel.class); > private HttpServletRequest request = null; > > public String getModelName() { > return "cookies"; > } > > public void init(Map params) throws WebloggerException { > PageContext context = (PageContext)params.get("pageContext"); > this.request = (HttpServletRequest) context.getRequest(); > } > > public String getCookie(String cookieName) { > Cookie[] cookies = request.getCookies(); > > for (int i = 0; i < cookies.length; i++) { > String name = cookies[i].getName(); > if (cookieName.equals(name)) { > return cookies[i].getValue(); > } > } > > log.warn("ERROR: cookie '" + cookieName + "' not found"); > return null; > } > } > > Then use it in my velocity templates with: > > $cookies.getCookie("com.raibledesigns.theme") > > To create it, I copied plugins/pluginmodel to plugins/cookieplugin: > > /Users/mraible/dev/roller/docs/examples/plugins/cookieplugin > > And I had to alter the build.xml quite a bit to get everything to compile: > > <project name="cookiemodel" default="dist"> > > <property name="buildlib" value="../../../../app/target" /> > > <target name="dist"> > <mkdir dir="build" /> > <mkdir dir="dist" /> > <javac srcdir="src" destdir="build"> > <classpath> > <pathelement path="${buildlib}/roller-classes.jar" /> > <pathelement > path="${user.home}/.m2/repository/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar" > /> > <pathelement > path="${user.home}/.m2/repository/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar" > /> > <pathelement > path="${user.home}/.m2/repository/javax/servlet/jsp-api/2.0/jsp-api-2.0.jar" > /> > </classpath> > </javac> > <jar basedir="build" destfile="dist/cookiemodel.jar" /> > </target> > > <target name="clean" > > <delete dir="build" /> > <delete dir="clean" /> > </target> > > </project> > > Cheers, > > Matt > > > > On Thu, Jan 16, 2014 at 2:24 PM, Dave <[email protected]> wrote: > >> Here's the new one: >> >> http://svn.apache.org/repos/asf/roller/trunk/docs/examples/plugins/pluginmodel/ >> >> >> On Thu, Jan 16, 2014 at 4:09 PM, Matt Raible <[email protected]> >> wrote: >> >> > I'm finally getting around to trying to implement reading cookies in my >> > templates. But the above link doesn't work. Got a new one? >> > >> > >> > On Fri, Aug 10, 2012 at 10:37 AM, Dave <[email protected]> wrote: >> > >> > > On Fri, Aug 10, 2012 at 11:10 AM, Sarwar Bhuiyan >> > > <[email protected]> wrote: >> > > > Something like this >> > > > >> > > >> > >> http://www.jajakarta.org/velocity/tools/velocity-tools-1.1/docs/view/CookieTool.html >> > > >> > > I don't think that will work since we setup Velocity internally within >> > > our own Servlets. >> > > >> > > I think the only way to do this without code changes to Roller is to >> > > add your own Page Model plugin. In that plugin, you would get the >> > > cookies and return them via a getter. There is an example of a page >> > > model plugin here: >> > > >> > > >> > > >> > >> http://svn.apache.org/repos/asf/roller/trunk/weblogger-docs/examples/plugins/pluginmodel/ >> > > >> > > - Dave >> > > >> > >> > >
