Search in sources :

Example 36 with Server

use of org.eclipse.jetty.server.Server in project jetty.project by eclipse.

the class JDK9HTTP2Server method main.

public static void main(String... args) throws Exception {
    Server server = new Server();
    HttpConfiguration httpsConfig = new HttpConfiguration();
    httpsConfig.setSecureScheme("https");
    httpsConfig.setSecurePort(8443);
    httpsConfig.setSendXPoweredBy(true);
    httpsConfig.setSendServerVersion(true);
    httpsConfig.addCustomizer(new SecureRequestCustomizer());
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath("src/test/resources/keystore.jks");
    sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
    sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
    sslContextFactory.setCipherComparator(HTTP2Cipher.COMPARATOR);
    HttpConnectionFactory http = new HttpConnectionFactory(httpsConfig);
    HTTP2ServerConnectionFactory h2 = new HTTP2ServerConnectionFactory(httpsConfig);
    ALPNServerConnectionFactory alpn = new ALPNServerConnectionFactory();
    alpn.setDefaultProtocol(http.getProtocol());
    SslConnectionFactory ssl = new SslConnectionFactory(sslContextFactory, alpn.getProtocol());
    ServerConnector http2Connector = new ServerConnector(server, ssl, alpn, h2, http);
    http2Connector.setPort(8443);
    server.addConnector(http2Connector);
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ALPNServerConnectionFactory(org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory)

Example 37 with Server

use of org.eclipse.jetty.server.Server in project jetty.project by eclipse.

the class JstlTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    // Setup Server
    server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.addConnector(connector);
    // Setup WebAppContext
    File testWebAppDir = MavenTestingUtils.getProjectDir("src/test/webapp");
    // Prepare WebApp libs
    File libDir = new File(testWebAppDir, "WEB-INF/lib");
    FS.ensureDirExists(libDir);
    File testTagLibDir = MavenTestingUtils.getProjectDir("src/test/taglibjar");
    JAR.create(testTagLibDir, new File(libDir, "testtaglib.jar"));
    // Configure WebAppContext
    Configuration.ClassList classlist = Configuration.ClassList.setServerDefault(server);
    classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
    WebAppContext context = new WebAppContext();
    context.setContextPath("/");
    File scratchDir = MavenTestingUtils.getTargetFile("tests/" + JstlTest.class.getSimpleName() + "-scratch");
    FS.ensureEmpty(scratchDir);
    JspConfig.init(context, testWebAppDir.toURI(), scratchDir);
    server.setHandler(context);
    // Start Server
    server.start();
    // Figure out Base URI
    String host = connector.getHost();
    if (host == null) {
        host = "localhost";
    }
    int port = connector.getLocalPort();
    baseUri = new URI(String.format("http://%s:%d/", host, port));
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) WebAppContext(org.eclipse.jetty.webapp.WebAppContext) Server(org.eclipse.jetty.server.Server) Configuration(org.eclipse.jetty.webapp.Configuration) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) URI(java.net.URI) BeforeClass(org.junit.BeforeClass)

Example 38 with Server

use of org.eclipse.jetty.server.Server in project jetty.project by eclipse.

the class ExampleServer method main.

public static void main(String[] args) throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(8080);
    server.setConnectors(new Connector[] { connector });
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");
    context.addServlet(HelloServlet.class, "/hello");
    context.addServlet(AsyncEchoServlet.class, "/echo/*");
    HandlerCollection handlers = new HandlerCollection();
    handlers.setHandlers(new Handler[] { context, new DefaultHandler() });
    server.setHandler(handlers);
    server.start();
    server.join();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 39 with Server

use of org.eclipse.jetty.server.Server in project jetty.project by eclipse.

the class FileServer method main.

public static void main(String[] args) throws Exception {
    // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
    // then a randomly available port will be assigned that you can either look in the logs for the port,
    // or programmatically obtain it for use in test cases.
    Server server = new Server(8080);
    // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
    // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
    ResourceHandler resource_handler = new ResourceHandler();
    // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
    // In this example it is the current directory but it can be configured to anything that the jvm has access to.
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[] { "index.html" });
    resource_handler.setResourceBase(".");
    // Add the ResourceHandler to the server.
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
    server.setHandler(handlers);
    // Start things up! By using the server.join() the server thread will join with the current thread.
    // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
    server.start();
    server.join();
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) Server(org.eclipse.jetty.server.Server) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 40 with Server

use of org.eclipse.jetty.server.Server in project jetty.project by eclipse.

the class RewriteServer method main.

public static void main(String[] args) throws Exception {
    Server server = new Server(8080);
    HttpConfiguration config = server.getConnectors()[0].getConnectionFactory(HttpConnectionFactory.class).getHttpConfiguration();
    RewriteCustomizer rewrite = new RewriteCustomizer();
    config.addCustomizer(rewrite);
    rewrite.addRule(new CompactPathRule());
    rewrite.addRule(new RewriteRegexRule("(.*)foo(.*)", "$1FOO$2"));
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);
    context.addServlet(DumpServlet.class, "/*");
    server.start();
    server.join();
}
Also used : CompactPathRule(org.eclipse.jetty.rewrite.handler.CompactPathRule) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) RewriteRegexRule(org.eclipse.jetty.rewrite.handler.RewriteRegexRule) RewriteCustomizer(org.eclipse.jetty.rewrite.RewriteCustomizer) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

Server (org.eclipse.jetty.server.Server)521 ServerConnector (org.eclipse.jetty.server.ServerConnector)209 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)129 Test (org.junit.Test)103 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)99 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)68 IOException (java.io.IOException)66 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)65 File (java.io.File)62 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)61 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)58 URI (java.net.URI)52 Before (org.junit.Before)50 BeforeClass (org.junit.BeforeClass)47 ServletException (javax.servlet.ServletException)44 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)43 LocalConnector (org.eclipse.jetty.server.LocalConnector)42 URL (java.net.URL)37 HttpServletRequest (javax.servlet.http.HttpServletRequest)33 HttpServletResponse (javax.servlet.http.HttpServletResponse)31