Java Code Examples for org.apache.catalina.connector.Request#getSession()

The following examples show how to use org.apache.catalina.connector.Request#getSession() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAccessLogValve.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    Object value = null;
    if (null != request) {
        HttpSession sess = request.getSession(false);
        if (null != sess) {
            value = sess.getAttribute(header);
        }
    } else {
        value = "??";
    }
    if (value != null) {
        if (value instanceof String) {
            buf.append((String) value);
        } else {
            buf.append(value.toString());
        }
    } else {
        buf.append('-');
    }
}
 
Example 2
Source File: AccessLogValve.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void addElement(StringBuilder buf, Date date, Request request,
        Response response, long time) {
    Object value = null;
    if (null != request) {
        HttpSession sess = request.getSession(false);
        if (null != sess) {
            value = sess.getAttribute(header);
        }
    } else {
        value = "??";
    }
    if (value != null) {
        if (value instanceof String) {
            buf.append((String) value);
        } else {
            buf.append(value.toString());
        }
    } else {
        buf.append('-');
    }
}
 
Example 3
Source File: CrossSubdomainSessionValve.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
public @Override void invoke(Request request, Response response) throws IOException, ServletException {

        // this will cause Request.doGetSession to create the session cookie if necessary
        request.getSession(true);

        // replace any Tomcat-generated session cookies with our own
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (SessionConfig.getSessionCookieName(null).equals(cookie.getName())) {
                    replaceCookie(request, response, cookie);
                }
            }
        }

        // process the next valve
        getNext().invoke(request, response);
    }
 
Example 4
Source File: AccessLogValve.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
@Override
public void addElement(StringBuilder buf, Date date, Request request,
        Response response, long time) {
    Object value = null;
    if (null != request) {
        HttpSession sess = request.getSession(false);
        if (null != sess) {
            value = sess.getAttribute(header);
        }
    } else {
        value = "??";
    }
    if (value != null) {
        if (value instanceof String) {
            buf.append((String) value);
        } else {
            buf.append(value.toString());
        }
    } else {
        buf.append('-');
    }
}
 
Example 5
Source File: ExtendedAccessLogValve.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void addElement(CharArrayWriter buf, Date date, Request request,
        Response response, long time) {
    HttpSession session = null;
    if (request != null) {
        session = request.getSession(false);
        if (session != null) {
            buf.append(wrap(session.getAttribute(attribute)));
        }
    }
}
 
Example 6
Source File: ExtendedAccessLogValve.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void addElement(StringBuilder buf, Date date, Request request,
        Response response, long time) {
    HttpSession session = null;
    if (request != null) {
        session = request.getSession(false);
        if (session != null) {
            buf.append(wrap(session.getAttribute(attribute)));
        }
    }
}
 
Example 7
Source File: CometConnectionManagerValve.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Register requests for tracking, whenever needed.
 *
 * @param request The servlet request to be processed
 * @param response The servlet response to be created
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
@Override
public void invoke(Request request, Response response)
    throws IOException, ServletException {
    // Perform the request
    getNext().invoke(request, response);

    if (request.isComet() && !response.isClosed()) {
        // Start tracking this connection, since this is a
        // begin event, and Comet mode is on
        HttpSession session = request.getSession(true);

        // Track the connection for webapp reload
        cometRequests.add(request);

        // Track the connection for session expiration
        synchronized (session) {
            Request[] requests = (Request[])
                    session.getAttribute(cometRequestsAttribute);
            if (requests == null) {
                requests = new Request[1];
                requests[0] = request;
                session.setAttribute(cometRequestsAttribute,
                        requests);
            } else {
                Request[] newRequests =
                    new Request[requests.length + 1];
                for (int i = 0; i < requests.length; i++) {
                    newRequests[i] = requests[i];
                }
                newRequests[requests.length] = request;
                session.setAttribute(cometRequestsAttribute, newRequests);
            }
        }
    }

}
 
Example 8
Source File: ExtendedAccessLogValve.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Override
public void addElement(StringBuilder buf, Date date, Request request,
        Response response, long time) {
    HttpSession session = null;
    if (request != null) {
        session = request.getSession(false);
        if (session != null) {
            buf.append(wrap(session.getAttribute(attribute)));
        }
    }
}
 
Example 9
Source File: CometConnectionManagerValve.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Register requests for tracking, whenever needed.
 *
 * @param request The servlet request to be processed
 * @param response The servlet response to be created
 *
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
@Override
public void invoke(Request request, Response response)
    throws IOException, ServletException {
    // Perform the request
    getNext().invoke(request, response);

    if (request.isComet() && !response.isClosed()) {
        // Start tracking this connection, since this is a
        // begin event, and Comet mode is on
        HttpSession session = request.getSession(true);

        // Track the connection for webapp reload
        cometRequests.add(request);

        // Track the connection for session expiration
        synchronized (session) {
            Request[] requests = (Request[])
                    session.getAttribute(cometRequestsAttribute);
            if (requests == null) {
                requests = new Request[1];
                requests[0] = request;
                session.setAttribute(cometRequestsAttribute,
                        requests);
            } else {
                Request[] newRequests =
                    new Request[requests.length + 1];
                for (int i = 0; i < requests.length; i++) {
                    newRequests[i] = requests[i];
                }
                newRequests[requests.length] = request;
                session.setAttribute(cometRequestsAttribute, newRequests);
            }
        }
    }

}
 
Example 10
Source File: TomcatRequestAuthenticator.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
protected String changeHttpSessionId(boolean create) {
    Request request = this.request;
    HttpSession session = request.getSession(false);
    if (session == null) {
        return request.getSession(true).getId();
    }
    if (!deployment.isTurnOffChangeSessionIdOnLogin()) return request.changeSessionId();
    else return session.getId();
}
 
Example 11
Source File: CrawlerSessionManagerValve.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void invoke(Request request, Response response) throws IOException, ServletException {

    boolean isBot = false;
    String sessionId = null;
    String clientIp = request.getRemoteAddr();
    String clientIdentifier = getClientIdentifier(request.getHost(), request.getContext(), clientIp);

    if (log.isDebugEnabled()) {
        log.debug(request.hashCode() + ": ClientIdentifier=" + clientIdentifier + ", RequestedSessionId="
                + request.getRequestedSessionId());
    }

    // If the incoming request has a valid session ID, no action is required
    if (request.getSession(false) == null) {

        // Is this a crawler - check the UA headers
        Enumeration<String> uaHeaders = request.getHeaders("user-agent");
        String uaHeader = null;
        if (uaHeaders.hasMoreElements()) {
            uaHeader = uaHeaders.nextElement();
        }

        // If more than one UA header - assume not a bot
        if (uaHeader != null && !uaHeaders.hasMoreElements()) {

            if (log.isDebugEnabled()) {
                log.debug(request.hashCode() + ": UserAgent=" + uaHeader);
            }

            if (uaPattern.matcher(uaHeader).matches()) {
                isBot = true;

                if (log.isDebugEnabled()) {
                    log.debug(request.hashCode() + ": Bot found. UserAgent=" + uaHeader);
                }
            }
        }

        if (ipPattern != null && ipPattern.matcher(clientIp).matches()) {
            isBot = true;

            if (log.isDebugEnabled()) {
                log.debug(request.hashCode() + ": Bot found. IP=" + clientIp);
            }
        }

        // If this is a bot, is the session ID known?
        if (isBot) {
            sessionId = clientIdSessionId.get(clientIdentifier);
            if (sessionId != null) {
                request.setRequestedSessionId(sessionId);
                if (log.isDebugEnabled()) {
                    log.debug(request.hashCode() + ": SessionID=" + sessionId);
                }
            }
        }
    }

    getNext().invoke(request, response);

    if (isBot) {
        if (sessionId == null) {
            // Has bot just created a session, if so make a note of it
            HttpSession s = request.getSession(false);
            if (s != null) {
                clientIdSessionId.put(clientIdentifier, s.getId());
                sessionIdClientId.put(s.getId(), clientIdentifier);
                // #valueUnbound() will be called on session expiration
                s.setAttribute(this.getClass().getName(),
                        new CrawlerHttpSessionBindingListener(clientIdSessionId, clientIdentifier));
                s.setMaxInactiveInterval(sessionInactiveInterval);

                if (log.isDebugEnabled()) {
                    log.debug(request.hashCode() + ": New bot session. SessionID=" + s.getId());
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug(
                        request.hashCode() + ": Bot session accessed. SessionID=" + sessionId);
            }
        }
    }
}
 
Example 12
Source File: CrawlerSessionManagerValve.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public void invoke(Request request, Response response) throws IOException,
        ServletException {

    boolean isBot = false;
    String sessionId = null;
    String clientIp = null;

    if (log.isDebugEnabled()) {
        log.debug(request.hashCode() + ": ClientIp=" +
                request.getRemoteAddr() + ", RequestedSessionId=" +
                request.getRequestedSessionId());
    }

    // If the incoming request has a valid session ID, no action is required
    if (request.getSession(false) == null) {

        // Is this a crawler - check the UA headers
        Enumeration<String> uaHeaders = request.getHeaders("user-agent");
        String uaHeader = null;
        if (uaHeaders.hasMoreElements()) {
            uaHeader = uaHeaders.nextElement();
        }

        // If more than one UA header - assume not a bot
        if (uaHeader != null && !uaHeaders.hasMoreElements()) {

            if (log.isDebugEnabled()) {
                log.debug(request.hashCode() + ": UserAgent=" + uaHeader);
            }

            if (uaPattern.matcher(uaHeader).matches()) {
                isBot = true;

                if (log.isDebugEnabled()) {
                    log.debug(request.hashCode() +
                            ": Bot found. UserAgent=" + uaHeader);
                }
            }
        }

        // If this is a bot, is the session ID known?
        if (isBot) {
            clientIp = request.getRemoteAddr();
            sessionId = clientIpSessionId.get(clientIp);
            if (sessionId != null) {
                request.setRequestedSessionId(sessionId);
                if (log.isDebugEnabled()) {
                    log.debug(request.hashCode() + ": SessionID=" +
                            sessionId);
                }
            }
        }
    }

    getNext().invoke(request, response);

    if (isBot) {
        if (sessionId == null) {
            // Has bot just created a session, if so make a note of it
            HttpSession s = request.getSession(false);
            if (s != null) {
                clientIpSessionId.put(clientIp, s.getId());
                sessionIdClientIp.put(s.getId(), clientIp);
                // #valueUnbound() will be called on session expiration
                s.setAttribute(this.getClass().getName(), this);
                s.setMaxInactiveInterval(sessionInactiveInterval);

                if (log.isDebugEnabled()) {
                    log.debug(request.hashCode() +
                            ": New bot session. SessionID=" + s.getId());
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug(request.hashCode() +
                        ": Bot session accessed. SessionID=" + sessionId);
            }
        }
    }
}
 
Example 13
Source File: CrawlerSessionManagerValve.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Override
public void invoke(Request request, Response response) throws IOException,
        ServletException {

    boolean isBot = false;
    String sessionId = null;
    String clientIp = null;

    if (log.isDebugEnabled()) {
        log.debug(request.hashCode() + ": ClientIp=" +
                request.getRemoteAddr() + ", RequestedSessionId=" +
                request.getRequestedSessionId());
    }

    // If the incoming request has a valid session ID, no action is required
    if (request.getSession(false) == null) {

        // Is this a crawler - check the UA headers
        Enumeration<String> uaHeaders = request.getHeaders("user-agent");
        String uaHeader = null;
        if (uaHeaders.hasMoreElements()) {
            uaHeader = uaHeaders.nextElement();
        }

        // If more than one UA header - assume not a bot
        if (uaHeader != null && !uaHeaders.hasMoreElements()) {

            if (log.isDebugEnabled()) {
                log.debug(request.hashCode() + ": UserAgent=" + uaHeader);
            }

            if (uaPattern.matcher(uaHeader).matches()) {
                isBot = true;

                if (log.isDebugEnabled()) {
                    log.debug(request.hashCode() +
                            ": Bot found. UserAgent=" + uaHeader);
                }
            }
        }

        // If this is a bot, is the session ID known?
        if (isBot) {
            clientIp = request.getRemoteAddr();
            sessionId = clientIpSessionId.get(clientIp);
            if (sessionId != null) {
                request.setRequestedSessionId(sessionId);
                if (log.isDebugEnabled()) {
                    log.debug(request.hashCode() + ": SessionID=" +
                            sessionId);
                }
            }
        }
    }

    getNext().invoke(request, response);

    if (isBot) {
        if (sessionId == null) {
            // Has bot just created a session, if so make a note of it
            HttpSession s = request.getSession(false);
            if (s != null) {
                clientIpSessionId.put(clientIp, s.getId());
                sessionIdClientIp.put(s.getId(), clientIp);
                // #valueUnbound() will be called on session expiration
                s.setAttribute(this.getClass().getName(), this);
                s.setMaxInactiveInterval(sessionInactiveInterval);

                if (log.isDebugEnabled()) {
                    log.debug(request.hashCode() +
                            ": New bot session. SessionID=" + s.getId());
                }
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug(request.hashCode() +
                        ": Bot session accessed. SessionID=" + sessionId);
            }
        }
    }
}