Search in sources :

Example 1 with Handler

use of org.mortbay.jetty.Handler in project maven-plugins by apache.

the class ProjectInfoReportUtilsTest method startJetty.

private void startJetty(boolean isSSL, boolean withAuth) throws Exception {
    jettyServer = new Server();
    jettyServer.setStopAtShutdown(true);
    Connector connector = (isSSL ? getSSLConnector() : getDefaultConnector());
    jettyServer.setConnectors(new Connector[] { connector });
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setResourceBase(getBasedir() + "/target/classes/");
    webapp.setServer(jettyServer);
    if (withAuth) {
        Constraint constraint = new Constraint();
        constraint.setName(Constraint.__BASIC_AUTH);
        constraint.setRoles(new String[] { "user", "admin" });
        constraint.setAuthenticate(true);
        ConstraintMapping cm = new ConstraintMapping();
        cm.setConstraint(constraint);
        cm.setPathSpec("/*");
        SecurityHandler sh = new SecurityHandler();
        sh.setUserRealm(new HashUserRealm("MyRealm", getBasedir() + "/src/test/resources/realm.properties"));
        sh.setConstraintMappings(new ConstraintMapping[] { cm });
        webapp.addHandler(sh);
    }
    DefaultHandler defaultHandler = new DefaultHandler();
    defaultHandler.setServer(jettyServer);
    Handler[] handlers = new Handler[2];
    handlers[0] = webapp;
    handlers[1] = defaultHandler;
    jettyServer.setHandlers(handlers);
    jettyServer.start();
    port = connector.getLocalPort();
}
Also used : Connector(org.mortbay.jetty.Connector) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) SslSocketConnector(org.mortbay.jetty.security.SslSocketConnector) WebAppContext(org.mortbay.jetty.webapp.WebAppContext) SecurityHandler(org.mortbay.jetty.security.SecurityHandler) ConstraintMapping(org.mortbay.jetty.security.ConstraintMapping) Server(org.mortbay.jetty.Server) Constraint(org.mortbay.jetty.security.Constraint) Handler(org.mortbay.jetty.Handler) SecurityHandler(org.mortbay.jetty.security.SecurityHandler) DefaultHandler(org.mortbay.jetty.handler.DefaultHandler) HashUserRealm(org.mortbay.jetty.security.HashUserRealm) DefaultHandler(org.mortbay.jetty.handler.DefaultHandler)

Example 2 with Handler

use of org.mortbay.jetty.Handler 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 3 with Handler

use of org.mortbay.jetty.Handler in project gradle by gradle.

the class JettyRun method finishConfigurationBeforeStart.

public void finishConfigurationBeforeStart() throws Exception {
    Handler[] handlers = getConfiguredContextHandlers();
    org.gradle.api.plugins.jetty.internal.JettyPluginServer plugin = getServer();
    Server server = (Server) plugin.getProxiedObject();
    HandlerCollection contexts = (HandlerCollection) server.getChildHandlerByClass(ContextHandlerCollection.class);
    if (contexts == null) {
        contexts = (HandlerCollection) server.getChildHandlerByClass(HandlerCollection.class);
    }
    for (int i = 0; (handlers != null) && (i < handlers.length); i++) {
        contexts.addHandler(handlers[i]);
    }
}
Also used : JettyPluginServer(org.gradle.api.plugins.jetty.internal.JettyPluginServer) Jetty6PluginServer(org.gradle.api.plugins.jetty.internal.Jetty6PluginServer) Server(org.mortbay.jetty.Server) JettyPluginServer(org.gradle.api.plugins.jetty.internal.JettyPluginServer) Handler(org.mortbay.jetty.Handler) ContextHandler(org.mortbay.jetty.handler.ContextHandler) HandlerCollection(org.mortbay.jetty.handler.HandlerCollection) ContextHandlerCollection(org.mortbay.jetty.handler.ContextHandlerCollection) ContextHandlerCollection(org.mortbay.jetty.handler.ContextHandlerCollection)

Example 4 with Handler

use of org.mortbay.jetty.Handler in project maven-plugins by apache.

the class SiteRunMojo method execute.

/**
     * @see org.apache.maven.plugin.AbstractMojo#execute()
     */
public void execute() throws MojoExecutionException, MojoFailureException {
    checkInputEncoding();
    Server server = new Server();
    server.setStopAtShutdown(true);
    Connector defaultConnector = getDefaultConnector();
    server.setConnectors(new Connector[] { defaultConnector });
    WebAppContext webapp = createWebApplication();
    webapp.setServer(server);
    DefaultHandler defaultHandler = new DefaultHandler();
    defaultHandler.setServer(server);
    Handler[] handlers = new Handler[2];
    handlers[0] = webapp;
    handlers[1] = defaultHandler;
    server.setHandlers(handlers);
    getLog().info("Starting Jetty on http://localhost:" + port + "/");
    try {
        server.start();
    } catch (Exception e) {
        throw new MojoExecutionException("Error executing Jetty: " + e.getMessage(), e);
    }
    // Watch it
    try {
        server.getThreadPool().join();
    } catch (InterruptedException e) {
        getLog().warn("Jetty was interrupted", e);
    }
}
Also used : Connector(org.mortbay.jetty.Connector) SelectChannelConnector(org.mortbay.jetty.nio.SelectChannelConnector) WebAppContext(org.mortbay.jetty.webapp.WebAppContext) Server(org.mortbay.jetty.Server) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Handler(org.mortbay.jetty.Handler) DefaultHandler(org.mortbay.jetty.handler.DefaultHandler) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DefaultHandler(org.mortbay.jetty.handler.DefaultHandler)

Aggregations

Handler (org.mortbay.jetty.Handler)4 Server (org.mortbay.jetty.Server)4 Connector (org.mortbay.jetty.Connector)3 SelectChannelConnector (org.mortbay.jetty.nio.SelectChannelConnector)3 IOException (java.io.IOException)2 ContextHandler (org.mortbay.jetty.handler.ContextHandler)2 DefaultHandler (org.mortbay.jetty.handler.DefaultHandler)2 WebAppContext (org.mortbay.jetty.webapp.WebAppContext)2 FileNotFoundException (java.io.FileNotFoundException)1 ServletContext (javax.servlet.ServletContext)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 Options (org.apache.openejb.loader.Options)1 ServiceException (org.apache.openejb.server.ServiceException)1 Jetty6PluginServer (org.gradle.api.plugins.jetty.internal.Jetty6PluginServer)1 JettyPluginServer (org.gradle.api.plugins.jetty.internal.JettyPluginServer)1 Request (org.mortbay.jetty.Request)1