package org.nutz.mvc.adaptor.injector;

import java.util.LinkedHashMap;
import java.util.Map;

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

import org.nutz.castor.Castors;
import org.nutz.mvc.adaptor.ParamInjector;

public class CookieInjector implements ParamInjector {

	private String name;
	
	private Class<?> type;
	
	public CookieInjector(String name, Class<?> type) {
		this.name = name;
		this.type = type;
	}
	
    public Object get(ServletContext sc,
					  HttpServletRequest req,
					  HttpServletResponse resp,
					  Object refer) {
		if ("_map".equals(name)) {
			Map<String, String> cookies = new LinkedHashMap<String, String>();
			for (Cookie cookie : req.getCookies()) {
				cookies.put(cookie.getName(), cookie.getValue());
			}
			return cookies;
		}
		for (Cookie cookie : req.getCookies()) {
			if (cookie.getName().equalsIgnoreCase(name))
				return Castors.me().castTo(cookie.getValue(), type);
		}
		return null;
	}

}

@@ -661,16 +661,130 @@
 efer) %7B%0A
+        Cookie%5B%5D _cookies = req.getCookies();%0A        if (_cookies == null)%0A            _cookies = new Cookie%5B0%5D;%0A
 %09%09if (%22_
@@ -898,32 +898,24 @@
 ookie : 
-req.getC
+_c
 ookies
-()
 ) %7B%0A%09%09%09%09
@@ -1015,32 +1015,24 @@
 ookie : 
-req.getC
+_c
 ookies
-()
 ) %7B%0A%09%09%09i

fix #1291