package com.radiadesign.catalina.session;

import org.apache.catalina.Session;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.logging.Logger;


public class RedisSessionHandlerValve extends ValveBase {
  private static Logger log = Logger.getLogger("RedisSessionHandlerValve");
  private RedisSessionManager manager;

  public void setRedisSessionManager(RedisSessionManager manager) {
    this.manager = manager;
  }

  @Override
  public void invoke(Request request, Response response) throws IOException, ServletException {
    try {
      getNext().invoke(request, response);
    } finally {
      final Session session = request.getSessionInternal(false);
      storeOrRemoveSession(session);
      manager.afterRequest();
    }
  }

  private void storeOrRemoveSession(Session session) {
    try {
      if (session != null) {
        if (session.isValid()) {
          log.fine("Request with session completed, saving session " + session.getId());
          if (session.getSession() != null) {
            log.fine("HTTP Session present, saving " + session.getId());
            manager.save(session);
          } else {
            log.fine("No HTTP Session present, Not saving " + session.getId());
          }
        } else {
          log.fine("HTTP Session has been invalidated, removing :" + session.getId());
          manager.remove(session);
        }
      }
    } catch (Exception e) {
      // Do nothing.
    }
  }
}

@@ -282,39 +282,86 @@
 on;%0A
+%0A
 import 
-java.util.logging.Logger
+org.apache.juli.logging.Log;%0Aimport org.apache.juli.logging.LogFactory
 ;%0A%0A%0A
@@ -432,21 +432,17 @@
 ate 
-static
+final
  Log
-ger
  log
@@ -451,23 +451,23 @@
  Log
-ger
+Factory
 .getLog
-ger(%22
+(
 Redi
@@ -478,21 +478,21 @@
 sion
-HandlerValve%22
+Manager.class
 );%0A 
@@ -1092,19 +1092,20 @@
     log.
-fin
+trac
 e(%22Reque
@@ -1222,35 +1222,36 @@
             log.
-fin
+trac
 e(%22HTTP Session 
@@ -1358,19 +1358,20 @@
     log.
-fin
+trac
 e(%22No HT
@@ -1466,19 +1466,20 @@
     log.
-fin
+trac
 e(%22HTTP 

Convert valve logging to org.apache.juli.logging.