package de.springbootbuch.webmvc;

import java.io.IOException;
import javax.inject.Provider;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Part of springbootbuch.de.
 *
 * @author Michael J. Simons
 * @author @rotnroll666
 */
@Component
public class DemoFilter implements Filter {
	private static final Logger LOG = LoggerFactory
		.getLogger(DemoFilter.class);
	
	private final Provider<ShoppingCart> shoppingCart;

	public DemoFilter(
		Provider<ShoppingCart> shoppingCart) {
		this.shoppingCart = shoppingCart;
	}
	
	// Init und Destroy der Übersicht 
	// halber nicht gezeigt
	
	@Override
	public void init(
		FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(
		ServletRequest request, 
		ServletResponse response, 
		FilterChain chain
	) throws IOException, ServletException {
		chain.doFilter(request, response);
		LOG.info(
			"Shopping cart is {} empty",
			shoppingCart.get()
				.getContent().isEmpty() ? "" : "not" 
		);
	}

	@Override
	public void destroy() {
	}
}
@@ -298,16 +298,62 @@
 sponse;%0A
+import javax.servlet.http.HttpServletRequest;%0A
 import o
@@ -1185,50 +1185,148 @@
 ;%0A%09%09
-LOG.info(%0A%09%09%09%22Shopping cart is %7B%7D empty
+if(request instanceof HttpServletRequest && ((HttpServletRequest)request).getSession(false) != null) %7B%0A%09%09%09LOG.info(%0A%09%09%09%09%22Request from %7B%7D
 %22,%0A
+%09
 %09%09%09s
@@ -1343,16 +1343,17 @@
 t.get()%0A
+%09
 %09%09%09%09.get
@@ -1388,19 +1388,24 @@
 not%22 %0A%09%09
+%09
 );%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Ov

Fix bug when no session scope