roller-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matt Raible <m...@raibledesigns.com>
Subject Re: Reading Cookies
Date Sat, 29 Mar 2014 14:43:16 GMT
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 <snoopdave@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 <matt@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 <snoopdave@gmail.com> wrote:
> >
> > > On Fri, Aug 10, 2012 at 11:10 AM, Sarwar Bhuiyan
> > > <sarwar.bhuiyan@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
> > >
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message