package scm.hivemind.statefulservice;

import java.io.IOException;

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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.hivemind.Registry;
import org.apache.hivemind.servlet.HiveMindFilter;


/**
 * A servlet filter for setting up the 
 * {@link scm.hivemind.statefulservice.ClientStateStorage}
 * for services holding state across requests.
 * 
 * @author Marcus Schulte
 */
public class StatefulHivemindFilter implements Filter {

        /**
         * 
         */
        public StatefulHivemindFilter() {
                super();
        }
   

        public void doFilter(ServletRequest request, ServletResponse response,
                        FilterChain chain) throws IOException, ServletException {
                
                HttpServletRequest req = (HttpServletRequest) request;
                Registry registry = HiveMindFilter.getRegistry( req );
                ClientStateStorage stateStore = (ClientStateStorage) 
                                       registry.getService(ClientStateStorage.class);
                stateStore.provideSession( req.getSession() );
                chain.doFilter( request, response );
                
        }
        
        /* (non-Javadoc)
         * @see javax.servlet.Filter#destroy()
         */
        public void destroy() {
                        }
        /* (non-Javadoc)
         * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
         */
        public void init(FilterConfig filterConfig) throws ServletException {
        }
}