Java Code Examples for org.eclipse.jetty.server.Server#getChildHandlersByClass()

The following examples show how to use org.eclipse.jetty.server.Server#getChildHandlersByClass() . 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: MultiSessionAttributeAdapter.java    From brooklyn-server with Apache License 2.0 6 votes vote down vote up
private void invalidateAllSession(HttpSession preferredSession, HttpSession localSession) {
    Server server = ((Session)preferredSession).getSessionHandler().getServer();
    final Handler[] handlers = server.getChildHandlersByClass(SessionHandler.class);
    List<String> invalidatedSessions = new ArrayList<>();
    if (handlers!=null) {
        for (Handler h: handlers) {
            Session session = ((SessionHandler)h).getSession(preferredSession.getId());
            if (session!=null) {
                invalidatedSessions.add(session.getId());
                session.invalidate();
            }
        }
    }
    if(!invalidatedSessions.contains(localSession.getId())){
        localSession.invalidate();
    }
}
 
Example 2
Source File: JettyAutoConfigurationIT.java    From joinfaces with Apache License 2.0 5 votes vote down vote up
@Test
public void customize() {
	JettyServletWebServerFactory factory = new JettyServletWebServerFactory();

	this.jettyAutoConfiguration.jsfJettyFactoryCustomizer().customize(factory);

	Server server = ((JettyWebServer) factory.getWebServer()).getServer();

	Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class);
	WebAppContext webAppContext = (WebAppContext) childHandlersByClass[0];

	assertThat(webAppContext.getBaseResource().getResource("testJetty.txt").exists())
		.isTrue();
}
 
Example 3
Source File: MultiSessionAttributeAdapter.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
protected Handler[] getSessionHandlers() {
    Server srv = getServer();
    Handler[] handlers = null;
    if (srv!=null) {
        handlers = srv.getChildHandlersByClass(SessionHandler.class);
    }
    return handlers;
}
 
Example 4
Source File: MultiSessionAttributeAdapter.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private SessionHandler getPreferredJettyHandler(Session localSession, boolean allowHandlerThatDoesntHaveSession, boolean markAndReturnThisIfNoneFound) {
    SessionHandler localHandler = ((Session)localSession).getSessionHandler();
    Server server = localHandler.getServer();
    // NB: this can also be useful: ((DefaultSessionIdManager)localHandler.getSessionIdManager())
    
    if (server!=null) {
        Session sessionAtServerGlobalPreferredHandler = null;
        
        // does the server have a globally preferred handler
        SessionHandler preferredServerGlobalSessionHandler = getServerGlobalPreferredHandler(server);
        if (preferredServerGlobalSessionHandler!=null) {
            sessionAtServerGlobalPreferredHandler = preferredServerGlobalSessionHandler.getSession(localSession.getId());
            if (sessionAtServerGlobalPreferredHandler!=null && Boolean.TRUE.equals( sessionAtServerGlobalPreferredHandler.getAttribute(KEY_IS_PREFERRED)) ) {
                return preferredServerGlobalSessionHandler;
            }
        }

        Handler[] handlers = server.getChildHandlersByClass(SessionHandler.class);

        // if there is a session marked, use it, unless the server has a preferred session handler and it has an equivalent session
        // this way if a session is marked (from use in a context where we don't have a web request) it will be used
        SessionHandler preferredHandlerForMarkedSession = findPeerSessionMarkedPreferred(localSession.getId(), handlers);
        if (preferredHandlerForMarkedSession!=null) return preferredHandlerForMarkedSession;

        // nothing marked as preferred; if server global handler has a session, mark it as preferred
        // this way it will get found quickly on subsequent requests
        if (sessionAtServerGlobalPreferredHandler!=null) {
            sessionAtServerGlobalPreferredHandler.setAttribute(KEY_IS_PREFERRED, true);
            return preferredServerGlobalSessionHandler;
        }

        if (allowHandlerThatDoesntHaveSession && preferredServerGlobalSessionHandler!=null) {
            return preferredServerGlobalSessionHandler;
        }
        if (preferredServerGlobalSessionHandler==null) {
            preferredServerGlobalSessionHandler = findPreferredBundleHandler(localSession, server, handlers);
            if (preferredServerGlobalSessionHandler!=null) {
                // recurse
                return getPreferredJettyHandler(localSession, allowHandlerThatDoesntHaveSession, markAndReturnThisIfNoneFound);
            }
        }

        if (markAndReturnThisIfNoneFound) {
            // nothing detected as preferred ... let's mark this session as the preferred one
            markSessionAsPreferred(localSession, " (this is the handler that the request came in on)");
            return localHandler;
        }

    } else {
        log.warn("Could not find server for "+info(localSession));
    }
    return null;
}