001    /* 
002     * Copyright 2008-2009 the original author or authors.
003     * The contents of this file are subject to the Mozilla Public License
004     * Version 1.1 (the "License"); you may not use this file except in
005     * compliance with the License. You may obtain a copy of the License at
006     * http://www.mozilla.org/MPL/
007     *
008     * Software distributed under the License is distributed on an "AS IS"
009     * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
010     * License for the specific language governing rights and limitations
011     * under the License.
012     */
013     
014    package com.mtgi.analytics.servlet;
015    
016    import org.springframework.web.context.request.RequestContextHolder;
017    import org.springframework.web.context.request.RequestContextListener;
018    import org.springframework.web.context.request.ServletRequestAttributes;
019    
020    import com.mtgi.analytics.SessionContext;
021    
022    /**
023     * The default {@link SessionContext} implementation for web applications, which uses Spring request context
024     * to lookup user name and session ID.  Requires that the spring {@link RequestContextListener} is registered
025     * in the web application.
026     */
027    public class SpringSessionContext implements SessionContext {
028    
029            public String getContextSessionId() {
030                    ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
031                    return attributes == null ? null : attributes.getSessionId();
032            }
033    
034            public String getContextUserId() {
035                    ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
036                    return attributes == null ? null : attributes.getRequest().getRemoteUser();
037            }
038    
039    }