View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe;
12  
13  import java.io.IOException;
14  
15  import javax.servlet.ServletException;
16  
17  import org.apache.catalina.connector.Request;
18  import org.apache.catalina.connector.Response;
19  import org.apache.catalina.valves.ValveBase;
20  
21  import psiprobe.model.ApplicationSession;
22  import psiprobe.model.IpInfo;
23  
24  /**
25   * Valve which inserts the client's IP address into the session for Tomcat 9.0.
26   */
27  public class Tomcat90AgentValve extends ValveBase {
28  
29    /**
30     * Instantiates a new tomcat90 agent valve.
31     */
32    public Tomcat90AgentValve() {
33      super(true);
34    }
35  
36    @Override
37    public void invoke(Request request, Response response) throws IOException, ServletException {
38      getNext().invoke(request, response);
39  
40      if (request.getSession(false) != null) {
41        String ip = IpInfo.getClientAddress(request.getRequest());
42        // Explicit calls to ensure result not lost
43        request.getSession(false).setAttribute(ApplicationSession.LAST_ACCESSED_BY_IP, ip);
44        request.getSession(false).setAttribute(ApplicationSession.LAST_ACCESSED_LOCALE,
45            request.getLocale());
46      }
47    }
48  
49  }