Search in sources :

Example 1 with ContextHandler

use of org.mortbay.jetty.handler.ContextHandler in project tomee by apache.

the class JettyHttpServer method init.

@Override
public void init(final Properties props) throws Exception {
    final Options options = new Options(props);
    port = options.get("port", 8080);
    // Create all the Jetty objects but dont' start them
    server = new Server();
    final Connector connector = new SelectChannelConnector();
    connector.setPort(port);
    server.setConnectors(new Connector[] { connector });
    final ContextHandler context = new ContextHandler();
    context.setContextPath("/");
    final ServletContext servletContext = context.getServletContext();
    server.setHandler(context);
    final Handler handler = new AbstractHandler() {

        @Override
        public void handle(final String target, final HttpServletRequest req, final HttpServletResponse res, final int dispatch) throws IOException, ServletException {
            try {
                ((Request) req).setHandled(true);
                final HttpRequest httpRequest = new ServletRequestAdapter(req, res, servletContext);
                final HttpResponse httpResponse = new ServletResponseAdapter(res);
                JettyHttpServer.this.listener.onMessage(httpRequest, httpResponse);
            } catch (IOException | ServletException e) {
                throw e;
            } catch (Exception e) {
                throw new ServletException(e);
            }
        }
    };
    final SessionHandler sessionHandler = new SessionHandler();
    final SessionManager sessionManager = new HashSessionManager();
    sessionManager.setIdManager(new HashSessionIdManager());
    sessionHandler.setSessionManager(sessionManager);
    sessionHandler.setHandler(handler);
    context.setHandler(sessionHandler);
}
Also used : SessionHandler(org.mortbay.jetty.servlet.SessionHandler) Options(org.apache.openejb.loader.Options) Connector(org.mortbay.jetty.Connector) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) Server(org.mortbay.jetty.Server) HashSessionManager(org.mortbay.jetty.servlet.HashSessionManager) SessionManager(org.mortbay.jetty.SessionManager) HashSessionManager(org.mortbay.jetty.servlet.HashSessionManager) Request(org.mortbay.jetty.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.mortbay.jetty.Handler) SessionHandler(org.mortbay.jetty.servlet.SessionHandler) ContextHandler(org.mortbay.jetty.handler.ContextHandler) AbstractHandler(org.mortbay.jetty.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HashSessionIdManager(org.mortbay.jetty.servlet.HashSessionIdManager) AbstractHandler(org.mortbay.jetty.handler.AbstractHandler) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ServiceException(org.apache.openejb.server.ServiceException) ContextHandler(org.mortbay.jetty.handler.ContextHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) ServletContext(javax.servlet.ServletContext)

Example 2 with ContextHandler

use of org.mortbay.jetty.handler.ContextHandler in project exhibitor by soabase.

the class ExhibitorMain method addSecurityFile.

private void addSecurityFile(HashUserRealm realm, String securityFile, Context root) throws Exception {
    // create a temp Jetty context to parse the security portion of the web.xml file
    /*
            TODO

            This code assumes far too much internal knowledge of Jetty. I don't know
            of simple way to parse the web.xml though and don't want to write it myself.
         */
    final URL url = new URL("file", null, securityFile);
    final WebXmlConfiguration webXmlConfiguration = new WebXmlConfiguration();
    WebAppContext context = new WebAppContext();
    context.setServer(server);
    webXmlConfiguration.setWebAppContext(context);
    ContextHandler contextHandler = new ContextHandler("/") {

        @Override
        protected void startContext() throws Exception {
            super.startContext();
            setServer(server);
            webXmlConfiguration.configure(url.toString());
        }
    };
    contextHandler.start();
    try {
        SecurityHandler securityHandler = webXmlConfiguration.getWebAppContext().getSecurityHandler();
        if (realm != null) {
            securityHandler.setUserRealm(realm);
        }
        root.setSecurityHandler(securityHandler);
    } finally {
        contextHandler.stop();
    }
}
Also used : ContextHandler(org.mortbay.jetty.handler.ContextHandler) WebAppContext(org.mortbay.jetty.webapp.WebAppContext) SecurityHandler(org.mortbay.jetty.security.SecurityHandler) WebXmlConfiguration(org.mortbay.jetty.webapp.WebXmlConfiguration) URL(java.net.URL)

Aggregations

ContextHandler (org.mortbay.jetty.handler.ContextHandler)2 IOException (java.io.IOException)1 URL (java.net.URL)1 ServletContext (javax.servlet.ServletContext)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Options (org.apache.openejb.loader.Options)1 ServiceException (org.apache.openejb.server.ServiceException)1 Connector (org.mortbay.jetty.Connector)1 Handler (org.mortbay.jetty.Handler)1 Request (org.mortbay.jetty.Request)1 Server (org.mortbay.jetty.Server)1 SessionManager (org.mortbay.jetty.SessionManager)1 AbstractHandler (org.mortbay.jetty.handler.AbstractHandler)1 SelectChannelConnector (org.mortbay.jetty.nio.SelectChannelConnector)1 SecurityHandler (org.mortbay.jetty.security.SecurityHandler)1 HashSessionIdManager (org.mortbay.jetty.servlet.HashSessionIdManager)1 HashSessionManager (org.mortbay.jetty.servlet.HashSessionManager)1 SessionHandler (org.mortbay.jetty.servlet.SessionHandler)1