/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.jooby.internal.handlers;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import org.jooby.Cookie;
import org.jooby.FlashScope;
import org.jooby.Request;
import org.jooby.Response;
import org.jooby.Result;
import org.jooby.Route;

public class FlashScopeHandler implements Route.Filter {

  private String name;

  private Function<String, Map<String, String>> decoder;

  private Function<Map<String, String>, String> encoder;

  public FlashScopeHandler(final String cname, final Function<String, Map<String, String>> decoder,
      final Function<Map<String, String>, String> encoder) {
    this.name = cname;
    this.decoder = decoder;
    this.encoder = encoder;
  }

  @Override
  public void handle(final Request req, final Response rsp, final Route.Chain chain)
      throws Throwable {
    Optional<String> value = req.cookie(name).toOptional();
    Map<String, String> flashScope = value.map(decoder::apply)
        .orElseGet(HashMap::new);
    Map<String, String> copy = new HashMap<>(flashScope);

    req.set(FlashScope.NAME, flashScope);

    // wrap & proceed
    chain.next(req, new Response.Forwarding(rsp) {
      @Override
      public void send(final Result result) throws Throwable {
        // 1. no change detect
        if (flashScope.equals(copy)) {
          // 1.a. existing data available, discard
          if (flashScope.size() > 0) {
            rsp.cookie(new Cookie.Definition(name, "").maxAge(0));
          }
        } else {
          // 2. change detected
          if (flashScope.size() == 0) {
            // 2.a everything was removed from app logic
            rsp.cookie(new Cookie.Definition(name, "").maxAge(0));
          } else {
            // 2.b there is something to see in the next request
            rsp.cookie(name, encoder.apply(flashScope));
          }
        }
        // send
        super.send(result);
      }
    });

  }

}

@@ -1063,33 +1063,8 @@
 se;%0A
-import org.jooby.Result;%0A
 impo
@@ -1938,136 +1938,235 @@
     
-chain.next(req, new Response.Forwarding(rsp) %7B%0A      @Override%0A      public void send(final Result result) throws Throwable
+rsp.push(finalizeFlash(copy, flashScope));%0A%0A    chain.next(req, rsp);%0A  %7D%0A%0A  private Route.After finalizeFlash(final Map%3CString, String%3E initialScope,%0A      final Map%3CString, String%3E scope) %7B%0A    return (req, rsp, result) -%3E
  %7B%0A
-  
     
@@ -2192,36 +2192,29 @@
 etect%0A      
-  
 if (
-flashS
+s
 cope.equals(
@@ -2213,20 +2213,28 @@
 .equals(
+initialS
 cop
-y
+e
 )) %7B%0A   
@@ -2234,26 +2234,24 @@
 ) %7B%0A        
-  
 // 1.a. exis
@@ -2283,36 +2283,29 @@
 ard%0A        
-  
 if (
-flashS
+s
 cope.size() 
@@ -2307,26 +2307,24 @@
 ze() %3E 0) %7B%0A
-  
           rs
@@ -2380,30 +2380,26 @@
 ));%0A        
-  %7D%0A  
+%7D%0A
       %7D else
@@ -2393,34 +2393,32 @@
 %0A      %7D else %7B%0A
-  
         // 2. ch
@@ -2443,20 +2443,13 @@
     
-  
 if (
-flashS
+s
 cope
@@ -2460,26 +2460,24 @@
 e() == 0) %7B%0A
-  
           //
@@ -2521,34 +2521,32 @@
 logic%0A          
-  
 rsp.cookie(new C
@@ -2588,26 +2588,24 @@
 ));%0A        
-  
 %7D else %7B%0A   
@@ -2607,26 +2607,24 @@
 %7B%0A          
- 
- 
 // 2.b there
@@ -2666,34 +2666,32 @@
 quest%0A          
-  
 rsp.cookie(name,
@@ -2705,30 +2705,25 @@
 r.apply(
-flashS
+s
 cope));%0A
         
@@ -2710,26 +2710,24 @@
 ly(scope));%0A
-  
         %7D%0A  
@@ -2730,26 +2730,24 @@
 %7D%0A      
-  
 %7D%0A      
   // sen
@@ -2742,62 +2742,29 @@
     
-  // send%0A        super.send(
+return 
 result
-)
 ;%0A    
-  %7D%0A    %7D);%0A
+%7D;
 %0A  %7D

rewrite flash scope handler to new after processing