/*
 * Copyright [2013] [ShopWiki]
 *
 * 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 com.shopwiki.vaadin;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.HttpServletRequestListener;

/**
 * Hideous hack so Vaadin can use Cookies
 *
 * @owner rstewart
 */
public abstract class CookieApplication extends Application implements HttpServletRequestListener {

    // Had these transient but then it could cause NPE's when deserializing a session.
    private ThreadLocal<HttpServletRequest> requests = new ThreadLocal<HttpServletRequest>();
    private ThreadLocal<HttpServletResponse> responses = new ThreadLocal<HttpServletResponse>();

    @Override
    public void onRequestStart(HttpServletRequest request, HttpServletResponse response) {
        requests.set(request);
        responses.set(response);
    }

    @Override
    public void onRequestEnd(HttpServletRequest request, HttpServletResponse response) {
        // do nothing
    }

    protected void addCookie(String name, String value) {
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(Integer.MAX_VALUE);
        responses.get().addCookie(cookie);
    }

    protected String getCookie(String name) {
        HttpServletRequest request = requests.get();
        if (request == null) {
            return null;
        }

        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return null;
        }

        for (Cookie cookie : cookies) {
            if (name.equals(cookie.getName())) {
                return cookie.getValue();
            }
        }

        return null;
    }

    protected void deteteCookie(String name) {
        Cookie cookie = new Cookie(name, "DELETE");
        cookie.setMaxAge(0);
        responses.get().addCookie(cookie);
    }
}

@@ -616,16 +616,79 @@
 aadin;%0A%0A
+import java.io.IOException;%0Aimport java.io.ObjectInputStream;%0A%0A
 import j
@@ -713,16 +713,16 @@
 Cookie;%0A
-
 import j
@@ -1086,19 +1086,14 @@
     
-// Had thes
+privat
 e tr
@@ -1104,67 +1104,89 @@
 ent 
-but then it could cause NPE's when deserializing a session.
+ThreadLocal%3CHttpServletRequest%3E requests = new ThreadLocal%3CHttpServletRequest%3E();
 %0A   
@@ -1186,32 +1186,42 @@
 ();%0A    private 
+transient 
 ThreadLocal%3CHttp
@@ -1225,38 +1225,40 @@
 ttpServletRe
-quest%3E request
+sponse%3E response
 s = new Thre
@@ -1278,37 +1278,265 @@
 ervletRe
-quest%3E();%0A    private
+sponse%3E();%0A%0A    // Need this because deserialization is stupid and sets transient fields to null.%0A    private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException %7B%0A        ois.defaultReadObject();%0A        requests = new
  ThreadL
@@ -1545,39 +1545,49 @@
 al%3CHttpServletRe
-sponse%3E
+quest%3E();%0A       
  responses = new
@@ -1618,24 +1618,30 @@
 Response%3E();
+%0A    %7D
 %0A%0A    @Overr

Put ThreadLocals in CookieApplication back to being transient WHY: They actually, shoudl be transient, but i needed to provide a readObject for serialization, to prevent them from being set to null