Search in sources :

Example 11 with ExchangeCompletionListener

use of io.undertow.server.ExchangeCompletionListener in project undertow by undertow-io.

the class CrawlerBindingListener method handleRequest.

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    boolean isBot = false;
    String sessionId = null;
    String clientIp = null;
    ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    // If the incoming request has a valid session ID, no action is required
    if (src.getOriginalRequest().getSession(false) == null) {
        // Is this a crawler - check the UA headers
        HeaderValues userAgentHeaders = exchange.getRequestHeaders().get(Headers.USER_AGENT);
        if (userAgentHeaders != null) {
            Iterator<String> uaHeaders = userAgentHeaders.iterator();
            String uaHeader = null;
            if (uaHeaders.hasNext()) {
                uaHeader = uaHeaders.next();
            }
            // If more than one UA header - assume not a bot
            if (uaHeader != null && !uaHeaders.hasNext()) {
                if (uaPattern.matcher(uaHeader).matches()) {
                    isBot = true;
                    if (UndertowLogger.REQUEST_LOGGER.isDebugEnabled()) {
                        UndertowLogger.REQUEST_LOGGER.debug(exchange + ": Bot found. UserAgent=" + uaHeader);
                    }
                }
            }
            // If this is a bot, is the session ID known?
            if (isBot) {
                clientIp = src.getServletRequest().getRemoteAddr();
                sessionId = clientIpSessionId.get(clientIp);
                if (sessionId != null) {
                    src.setOverridenSessionId(sessionId);
                    if (UndertowLogger.REQUEST_LOGGER.isDebugEnabled()) {
                        UndertowLogger.REQUEST_LOGGER.debug(exchange + ": SessionID=" + sessionId);
                    }
                }
            }
        }
    }
    if (isBot) {
        final String finalSessionId = sessionId;
        final String finalClientId = clientIp;
        exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

            @Override
            public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
                try {
                    ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
                    if (finalSessionId == null) {
                        // Has bot just created a session, if so make a note of it
                        HttpSession s = src.getOriginalRequest().getSession(false);
                        if (s != null) {
                            clientIpSessionId.put(finalClientId, s.getId());
                            sessionIdClientIp.put(s.getId(), finalClientId);
                            // #valueUnbound() will be called on session expiration
                            s.setAttribute(SESSION_ATTRIBUTE_NAME, new CrawlerBindingListener(clientIpSessionId, sessionIdClientIp));
                            s.setMaxInactiveInterval(config.getSessionInactiveInterval());
                            if (UndertowLogger.REQUEST_LOGGER.isDebugEnabled()) {
                                UndertowLogger.REQUEST_LOGGER.debug(exchange + ": New bot session. SessionID=" + s.getId());
                            }
                        }
                    } else {
                        if (UndertowLogger.REQUEST_LOGGER.isDebugEnabled()) {
                            UndertowLogger.REQUEST_LOGGER.debug(exchange + ": Bot session accessed. SessionID=" + finalSessionId);
                        }
                    }
                } finally {
                    nextListener.proceed();
                }
            }
        });
    }
    next.handleRequest(exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HttpSession(javax.servlet.http.HttpSession) HeaderValues(io.undertow.util.HeaderValues) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener)

Example 12 with ExchangeCompletionListener

use of io.undertow.server.ExchangeCompletionListener in project undertow by undertow-io.

the class AsyncContextImpl method createListener.

@Override
public <T extends AsyncListener> T createListener(final Class<T> clazz) throws ServletException {
    try {
        InstanceFactory<T> factory = ((ServletContextImpl) this.servletRequest.getServletContext()).getDeployment().getDeploymentInfo().getClassIntrospecter().createInstanceFactory(clazz);
        final InstanceHandle<T> instance = factory.createInstance();
        exchange.addExchangeCompleteListener(new ExchangeCompletionListener() {

            @Override
            public void exchangeEvent(HttpServerExchange exchange, NextListener nextListener) {
                try {
                    instance.release();
                } finally {
                    nextListener.proceed();
                }
            }
        });
        return instance.getInstance();
    } catch (Exception e) {
        throw new ServletException(e);
    }
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) ServletException(javax.servlet.ServletException) ExchangeCompletionListener(io.undertow.server.ExchangeCompletionListener) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

ExchangeCompletionListener (io.undertow.server.ExchangeCompletionListener)12 HttpServerExchange (io.undertow.server.HttpServerExchange)12 HeaderValues (io.undertow.util.HeaderValues)2 SecurityContext (io.undertow.security.api.SecurityContext)1 HttpString (io.undertow.util.HttpString)1 IOException (java.io.IOException)1 Deque (java.util.Deque)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1 HttpSession (javax.servlet.http.HttpSession)1 XnioExecutor (org.xnio.XnioExecutor)1 StreamSinkConduit (org.xnio.conduits.StreamSinkConduit)1