Search in sources :

Example 31 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project dropwizard by dropwizard.

the class ServletEnvironmentTest method setsSessionHandlers.

@Test
public void setsSessionHandlers() throws Exception {
    final SessionHandler sessionHandler = mock(SessionHandler.class);
    environment.setSessionHandler(sessionHandler);
    verify(handler).setSessionHandler(sessionHandler);
    verify(handler).setSessionsEnabled(true);
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) Test(org.junit.Test)

Example 32 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project jetty.project by eclipse.

the class AliasedConstraintTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    server = new Server();
    connector = new LocalConnector(server);
    server.setConnectors(new Connector[] { connector });
    ContextHandler context = new ContextHandler();
    SessionHandler session = new SessionHandler();
    TestLoginService loginService = new TestLoginService(TEST_REALM);
    loginService.putUser("user0", new Password("password"), new String[] {});
    loginService.putUser("user", new Password("password"), new String[] { "user" });
    loginService.putUser("user2", new Password("password"), new String[] { "user" });
    loginService.putUser("admin", new Password("password"), new String[] { "user", "administrator" });
    loginService.putUser("user3", new Password("password"), new String[] { "foo" });
    context.setContextPath("/ctx");
    context.setResourceBase(MavenTestingUtils.getTestResourceDir("docroot").getAbsolutePath());
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { context, new DefaultHandler() });
    server.setHandler(handlers);
    context.setHandler(session);
    // context.addAliasCheck(new AllowSymLinkAliasChecker());
    server.addBean(loginService);
    security = new ConstraintSecurityHandler();
    session.setHandler(security);
    ResourceHandler handler = new ResourceHandler();
    security.setHandler(handler);
    List<ConstraintMapping> constraints = new ArrayList<>();
    Constraint constraint0 = new Constraint();
    constraint0.setAuthenticate(true);
    constraint0.setName("forbid");
    ConstraintMapping mapping0 = new ConstraintMapping();
    mapping0.setPathSpec("/forbid/*");
    mapping0.setConstraint(constraint0);
    constraints.add(mapping0);
    Set<String> knownRoles = new HashSet<>();
    knownRoles.add("user");
    knownRoles.add("administrator");
    security.setConstraintMappings(constraints, knownRoles);
    server.start();
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) Constraint(org.eclipse.jetty.util.security.Constraint) LocalConnector(org.eclipse.jetty.server.LocalConnector) ArrayList(java.util.ArrayList) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) Matchers.containsString(org.hamcrest.Matchers.containsString) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Password(org.eclipse.jetty.util.security.Password) HashSet(java.util.HashSet) BeforeClass(org.junit.BeforeClass)

Example 33 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project jetty.project by eclipse.

the class Response method reset.

public void reset(boolean preserveCookies) {
    resetForForward();
    _status = 200;
    _reason = null;
    _contentLength = -1;
    List<HttpField> cookies = preserveCookies ? _fields.stream().filter(f -> f.getHeader() == HttpHeader.SET_COOKIE).collect(Collectors.toList()) : null;
    _fields.clear();
    String connection = _channel.getRequest().getHeader(HttpHeader.CONNECTION.asString());
    if (connection != null) {
        for (String value : StringUtil.csvSplit(null, connection, 0, connection.length())) {
            HttpHeaderValue cb = HttpHeaderValue.CACHE.get(value);
            if (cb != null) {
                switch(cb) {
                    case CLOSE:
                        _fields.put(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.toString());
                        break;
                    case KEEP_ALIVE:
                        if (HttpVersion.HTTP_1_0.is(_channel.getRequest().getProtocol()))
                            _fields.put(HttpHeader.CONNECTION, HttpHeaderValue.KEEP_ALIVE.toString());
                        break;
                    case TE:
                        _fields.put(HttpHeader.CONNECTION, HttpHeaderValue.TE.toString());
                        break;
                    default:
                }
            }
        }
    }
    if (preserveCookies)
        cookies.forEach(f -> _fields.add(f));
    else {
        Request request = getHttpChannel().getRequest();
        HttpSession session = request.getSession(false);
        if (session != null && session.isNew()) {
            SessionHandler sh = request.getSessionHandler();
            if (sh != null) {
                HttpCookie c = sh.getSessionCookie(session, request.getContextPath(), request.isSecure());
                if (c != null)
                    addCookie(c);
            }
        }
    }
}
Also used : HttpScheme(org.eclipse.jetty.http.HttpScheme) HttpVersion(org.eclipse.jetty.http.HttpVersion) CookieCompliance(org.eclipse.jetty.http.CookieCompliance) StringUtil(org.eclipse.jetty.util.StringUtil) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpURI(org.eclipse.jetty.http.HttpURI) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServletOutputStream(javax.servlet.ServletOutputStream) Locale(java.util.Locale) MetaData(org.eclipse.jetty.http.MetaData) IllegalSelectorException(java.nio.channels.IllegalSelectorException) HttpStatus(org.eclipse.jetty.http.HttpStatus) Cookie(javax.servlet.http.Cookie) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) HttpFields(org.eclipse.jetty.http.HttpFields) EnumSet(java.util.EnumSet) PrintWriter(java.io.PrintWriter) HttpSession(javax.servlet.http.HttpSession) HttpContent(org.eclipse.jetty.http.HttpContent) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) QuotedStringTokenizer(org.eclipse.jetty.util.QuotedStringTokenizer) Collection(java.util.Collection) RequestDispatcher(javax.servlet.RequestDispatcher) HttpCookie(org.eclipse.jetty.http.HttpCookie) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Collectors(java.util.stream.Collectors) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) List(java.util.List) Syntax(org.eclipse.jetty.http.Syntax) HttpField(org.eclipse.jetty.http.HttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) Log(org.eclipse.jetty.util.log.Log) HttpGenerator(org.eclipse.jetty.http.HttpGenerator) URIUtil(org.eclipse.jetty.util.URIUtil) Logger(org.eclipse.jetty.util.log.Logger) DateGenerator(org.eclipse.jetty.http.DateGenerator) Collections(java.util.Collections) MimeTypes(org.eclipse.jetty.http.MimeTypes) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) HttpField(org.eclipse.jetty.http.HttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) HttpSession(javax.servlet.http.HttpSession) HttpCookie(org.eclipse.jetty.http.HttpCookie) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue)

Example 34 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project jetty.project by eclipse.

the class DataConstraintsTest method startServer.

@Before
public void startServer() {
    _server = new Server();
    HttpConnectionFactory http = new HttpConnectionFactory();
    http.getHttpConfiguration().setSecurePort(9999);
    http.getHttpConfiguration().setSecureScheme("BWTP");
    _connector = new LocalConnector(_server, http);
    _connector.setIdleTimeout(300000);
    HttpConnectionFactory https = new HttpConnectionFactory();
    https.getHttpConfiguration().addCustomizer(new HttpConfiguration.Customizer() {

        @Override
        public void customize(Connector connector, HttpConfiguration channelConfig, Request request) {
            request.setScheme(HttpScheme.HTTPS.asString());
            request.setSecure(true);
        }
    });
    _connectorS = new LocalConnector(_server, https);
    _server.setConnectors(new Connector[] { _connector, _connectorS });
    ContextHandler _context = new ContextHandler();
    _session = new SessionHandler();
    _context.setContextPath("/ctx");
    _server.setHandler(_context);
    _context.setHandler(_session);
    _security = new ConstraintSecurityHandler();
    _session.setHandler(_security);
    _security.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            response.sendError(404);
        }
    });
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) LocalConnector(org.eclipse.jetty.server.LocalConnector) Connector(org.eclipse.jetty.server.Connector) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) LocalConnector(org.eclipse.jetty.server.LocalConnector) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Before(org.junit.Before)

Example 35 with SessionHandler

use of org.eclipse.jetty.server.session.SessionHandler in project jetty.project by eclipse.

the class SpecExampleConstraintTest method startServer.

@BeforeClass
public static void startServer() {
    _server = new Server();
    _connector = new LocalConnector(_server);
    _server.setConnectors(new Connector[] { _connector });
    ContextHandler _context = new ContextHandler();
    _session = new SessionHandler();
    TestLoginService _loginService = new TestLoginService(TEST_REALM);
    _loginService.putUser("fred", new Password("password"), IdentityService.NO_ROLES);
    _loginService.putUser("harry", new Password("password"), new String[] { "HOMEOWNER" });
    _loginService.putUser("chris", new Password("password"), new String[] { "CONTRACTOR" });
    _loginService.putUser("steven", new Password("password"), new String[] { "SALESCLERK" });
    _context.setContextPath("/ctx");
    _server.setHandler(_context);
    _context.setHandler(_session);
    _server.addBean(_loginService);
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) Server(org.eclipse.jetty.server.Server) LocalConnector(org.eclipse.jetty.server.LocalConnector) Password(org.eclipse.jetty.util.security.Password) BeforeClass(org.junit.BeforeClass)

Aggregations

SessionHandler (org.eclipse.jetty.server.session.SessionHandler)44 Server (org.eclipse.jetty.server.Server)13 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)11 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)10 Test (org.junit.Test)10 IOException (java.io.IOException)9 ServletException (javax.servlet.ServletException)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 HashSessionManager (org.eclipse.jetty.server.session.HashSessionManager)5 File (java.io.File)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpSession (javax.servlet.http.HttpSession)4 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)4 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)4 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)4 BeforeClass (org.junit.BeforeClass)4 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)3 SecurityHandler (org.eclipse.jetty.security.SecurityHandler)3 Handler (org.eclipse.jetty.server.Handler)3 LocalConnector (org.eclipse.jetty.server.LocalConnector)3