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)); }  @Override void set(final Object response, final IObject environment);  }
@Test public void testCookiesSetting() throws InvalidArgumentException, CookieSettingException, ReadValueException { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); CookiesSetter setter = new CookiesSetter(); IObject environment = new DSObject("{\n" + " \"context\": {\n" + " \"cookies\": [\n" + " {\n" + " \"name\": \"foo\", " + " \"value\": \"bar\"\n" + " },\n" + " {\n" + " \"name\": \"hello\", " + " \"value\": \"world\"\n" + " }\n" + " ]\n" + " }\n" + "}"); IObject context = new DSObject( " {" + " \"cookies\": [\n" + " {\n" + " \"name\": \"foo\", " + " \"value\": \"bar\"\n" + " },\n" + " {\n" + " \"name\": \"hello\", " + " \"value\": \"world\"\n" + " }\n" + " ]\n" + " }\n"); IObject cookie1 = new DSObject("{\n" + " \"name\": \"foo\", " + " \"value\": \"bar\"\n" + " }"); IObject cookie2 = new DSObject("{\n" + " \"name\": \"hello\", " + " \"value\": \"world\"\n" + " }"); List<String> cookiesGoodString = new ArrayList<>(2); cookiesGoodString.add("foo=bar"); cookiesGoodString.add("hello=world"); List<IObject> cookies = new ArrayList<>(2); cookies.add(cookie1); cookies.add(cookie2); when(field.in(environment, IObject.class)).thenReturn(context); when(field.in(context, List.class)).thenReturn(cookies); setter.set(response, environment); List<String> cookiesString = response.headers().getAll(HttpHeaders.Names.SET_COOKIE); for (String cookie : cookiesString) { assertEquals(cookiesGoodString.get(0), cookiesString.get(0)); } }
@Test public void testCookiesSettingWithTime_ShouldSetDiscard() throws InvalidArgumentException, CookieSettingException, ReadValueException { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); CookiesSetter setter = new CookiesSetter(); IObject environment = new DSObject("{\n" + " \"context\": {\n" + " \"cookies\": [\n" + " {\n" + " \"name\": \"foo\", " + " \"value\": \"bar\",\n" + " \"maxAge\": 12 \n" + " },\n" + " {\n" + " \"name\": \"hello\", " + " \"value\": \"world\"\n" + " }\n" + " ]\n" + " }\n" + "}"); IObject context = new DSObject( " {" + " \"cookies\": [\n" + " {\n" + " \"name\": \"foo\", " + " \"value\": \"bar\",\n" + " \"maxAge\": 12 \n" + " },\n" + " {\n" + " \"name\": \"hello\", " + " \"value\": \"world\"\n" + " }\n" + " ]\n" + " }\n"); IObject cookie1 = new DSObject("{\n" + " \"name\": \"foo\", " + " \"value\": \"bar\",\n" + " \"maxAge\": 12 \n" + " }"); IObject cookie2 = new DSObject("{\n" + " \"name\": \"hello\", " + " \"value\": \"world\"\n" + " }"); List<IObject> cookies = new ArrayList<>(2); cookies.add(cookie1); cookies.add(cookie2); when(field.in(environment, IObject.class)).thenReturn(context); when(field.in(context, List.class)).thenReturn(cookies); setter.set(response, environment); List<String> cookiesString = response.headers().getAll(HttpHeaders.Names.SET_COOKIE); assertTrue(cookiesString.get(0).lastIndexOf("Expires")>0); assertFalse(cookiesString.get(1).lastIndexOf("Expires")>0); }