You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@roller.apache.org by Matt Raible <ma...@raibledesigns.com> on 2014/01/16 22:09:47 UTC

Re: Reading Cookies

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 <sn...@gmail.com> wrote:

> On Fri, Aug 10, 2012 at 11:10 AM, Sarwar Bhuiyan
> <sa...@gmail.com> 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
>

Re: Reading Cookies

Posted by Matt Raible <ma...@raibledesigns.com>.
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 <ma...@raibledesigns.com> 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 <sn...@gmail.com> 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 <ma...@raibledesigns.com>
>> 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 <sn...@gmail.com> wrote:
>> >
>> > > On Fri, Aug 10, 2012 at 11:10 AM, Sarwar Bhuiyan
>> > > <sa...@gmail.com> 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
>> > >
>> >
>>
>
>

Re: Reading Cookies

Posted by Matt Raible <ma...@raibledesigns.com>.
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 <sn...@gmail.com> 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 <ma...@raibledesigns.com>
> 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 <sn...@gmail.com> wrote:
> >
> > > On Fri, Aug 10, 2012 at 11:10 AM, Sarwar Bhuiyan
> > > <sa...@gmail.com> 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
> > >
> >
>

Re: Reading Cookies

Posted by Dave <sn...@gmail.com>.
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 <ma...@raibledesigns.com> 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 <sn...@gmail.com> wrote:
>
> > On Fri, Aug 10, 2012 at 11:10 AM, Sarwar Bhuiyan
> > <sa...@gmail.com> 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
> >
>