{#========================================== Spincast Cookies plugin ==========================================#} {% extends "../../layout.html" %} {% block sectionClasses %}plugins hasBreadCrumb plugins-spincast-cookies {% endblock %} {% block meta_title %}Plugins - Spincast Cookies{% endblock %} {% block meta_description %}Spincast Cookies plugin provides functionalities to read and write cookies.{% endblock %} {% block scripts %} {% endblock %} {% block body %}

Overview

This plugin provides functionalities to read and write HTTP cookies. It mainly consists of an implementation of the request context add-on, CookiesRequestContextAddon, which is mounted as .cookies() on the default Spincast Request Context. The implementation class is SpincastCookiesRequestContextAddon.

The plugin also provides an implementation for the Cookie interface : CookieDefault.

Quick Examples

  • Using the .cookies() add-on from a Route Handler to get a cookie :

    public void myHandler(AppRequestContext context) {
        Cookie myCookie = context.cookies().getCookie("myCookie");
        //...
    }

  • Creating a cookie using the .cookies() add-on :

    public void myHandler(AppRequestContext context) {
        context.cookies().addCookie("myCookie", "someValue");
        //...
    }

  • Using the injected CookieFactory instance to create a cookie :

    public class SomeService {
    
        private final CookieFactory cookieFactory;
    
        @Inject
        public AppService(CookieFactory cookieFactory) {
            this.cookieFactory = cookieFactory;
        }
    
        protected CookieFactory getCookieFactory() {
            return this.cookieFactory;
        }
    
        public Cookie someServiceMethod() {
            Cookie cookie = getCookieFactory().createCookie("myCookie", "some value");
            return cookie;
        }
    }

Installation

If you use the spincast-default artifact, this plugin is already installed so you have nothing more to do!

If you start from scratch using the spincast-core artifact, you can use the plugin by adding this artifact to your project:

<dependency>
    <groupId>org.spincast</groupId>
    <artifactId>spincast-plugins-cookies</artifactId>
    <version>{{spincast.spincastCurrrentVersion}}</version>
</dependency>

You then install the plugin's Guice module, by passing it to the Guice.createInjector(...) method:

Injector guice = Guice.createInjector(
        new SpincastCoreGuiceModule(args),
        new SpincastCookiesPluginGuiceModule(AppRequestContext.class, AppWebsocketContext.class)
        // other modules...
        );

... or by using the install(...) method from your custom Guice module:

public class AppModule extends SpincastCoreGuiceModule {

    @Override
    protected void configure() {
        super.configure();
        install(new SpincastCookiesPluginGuiceModule(getRequestContextType(), 
                                                     getWebsocketContextType()));
        // other modules...
    }
    
    // ...
}

Javadoc

{% endblock %}