class CookiesSetter implements ICookiesSetter {

    @Override
    public void set(final Object response, final IObject environment) throws CookieSettingException {
        FullHttpResponse httpResponse = (FullHttpResponse) response;
        IField contextField;
        IField cookiesField;
        IFieldName cookieName;
        IFieldName cookieValue;
        IFieldName maxAgeFieldName;
        IFieldName cookiePath;
        try {
            contextField = IOC.resolve(Keys.getOrAdd(IField.class.getCanonicalName()), "context");
            cookiesField = IOC.resolve(Keys.getOrAdd(IField.class.getCanonicalName()), "cookies");
            cookieName = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), "name");
            cookieValue = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), "value");
            cookiePath = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), "path");
            maxAgeFieldName = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), "maxAge");
        } catch (ResolutionException e) {
            throw new CookieSettingException("Failed to resolve fieldName", e);
        }
        IObject context = null;
        List<IObject> cookies = null;
        try {
            context = contextField.in(environment, IObject.class);
        } catch (ReadValueException | InvalidArgumentException e) {
            throw new CookieSettingException("Failed to get context from environment", e);
        }
        try {
            cookies = cookiesField.in(context, List.class);
        } catch (ReadValueException | InvalidArgumentException e) {
            throw new CookieSettingException("Failed to get cookies from context", e);
        }
        List<Cookie> cookiesList = new ArrayList<>();
        for (IObject cookieObject : cookies) {
            try {
                Cookie cookie = new DefaultCookie(
                        cookieObject.getValue(cookieName).toString(),
                        cookieObject.getValue(cookieValue).toString());

                String path = (String) cookieObject.getValue(cookiePath);
                if (path != null) {
                    cookie.setPath(path);
                }

                Integer maxCookieAge = (Integer) cookieObject.getValue(maxAgeFieldName);
                if (maxCookieAge != null) {
                    cookie.setDiscard(false);
                    cookie.setMaxAge(maxCookieAge);
                }
                cookiesList.add(cookie);
            } catch (ReadValueException | InvalidArgumentException e) {
                throw new CookieSettingException("Failed to resolve cookie", e);
            }
        }
        httpResponse.headers().set(HttpHeaders.Names.SET_COOKIE,
                ServerCookieEncoder.encode(cookiesList));
    }

}