Search in sources :

Example 46 with ServerConnector

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

the class AttrEventTriggerTest method setUp.

@Before
public void setUp() throws Exception {
    File docRoot = new File("target/test-output/docroot/");
    docRoot.mkdirs();
    docRoot.deleteOnExit();
    System.setProperty("org.eclipse.jetty.util.log.DEBUG", "");
    _server = new Server();
    ServerConnector connector = new ServerConnector(_server);
    connector.setPort(0);
    _server.setConnectors(new Connector[] { connector });
    _handler = new TestHandler();
    _server.setHandler(_handler);
    MBeanContainer.resetUnique();
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    _mBeanContainer = new MBeanContainer(mBeanServer);
    _server.addBean(_mBeanContainer, true);
    _server.addBean(Log.getLog());
    _counter = _handler.getRequestCounter();
    _server.addBean(_counter);
    _server.start();
    startClient();
    _monitor = new JMXMonitor();
    int port = connector.getLocalPort();
    _requestUrl = "http://localhost:" + port + "/";
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) MBeanServer(javax.management.MBeanServer) Server(org.eclipse.jetty.server.Server) MBeanContainer(org.eclipse.jetty.jmx.MBeanContainer) File(java.io.File) MBeanServer(javax.management.MBeanServer) Before(org.junit.Before)

Example 47 with ServerConnector

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

the class ThreadStarvationTest method testDefaultServletSuccess.

@Test
@Slow
public void testDefaultServletSuccess() throws Exception {
    int maxThreads = 10;
    QueuedThreadPool threadPool = new QueuedThreadPool(maxThreads, maxThreads);
    threadPool.setDetailedDump(true);
    _server = new Server(threadPool);
    // Prepare a big file to download.
    File directory = MavenTestingUtils.getTargetTestingDir();
    Files.createDirectories(directory.toPath());
    String resourceName = "resource.bin";
    Path resourcePath = Paths.get(directory.getPath(), resourceName);
    try (OutputStream output = Files.newOutputStream(resourcePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
        byte[] chunk = new byte[1024];
        Arrays.fill(chunk, (byte) 'X');
        chunk[chunk.length - 2] = '\r';
        chunk[chunk.length - 1] = '\n';
        for (int i = 0; i < 256 * 1024; ++i) output.write(chunk);
    }
    final CountDownLatch writePending = new CountDownLatch(1);
    ServerConnector connector = new ServerConnector(_server, 0, 1) {

        @Override
        protected ChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException {
            return new SocketChannelEndPoint(channel, selectSet, key, getScheduler()) {

                @Override
                protected void onIncompleteFlush() {
                    super.onIncompleteFlush();
                    writePending.countDown();
                }
            };
        }
    };
    connector.setIdleTimeout(Long.MAX_VALUE);
    _server.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler(_server, "/");
    context.setResourceBase(directory.toURI().toString());
    context.addServlet(DefaultServlet.class, "/*").setAsyncSupported(false);
    _server.setHandler(context);
    _server.start();
    List<Socket> sockets = new ArrayList<>();
    for (int i = 0; i < maxThreads * 2; ++i) {
        Socket socket = new Socket("localhost", connector.getLocalPort());
        sockets.add(socket);
        OutputStream output = socket.getOutputStream();
        String request = "" + "GET /" + resourceName + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "\r\n";
        output.write(request.getBytes(StandardCharsets.UTF_8));
        output.flush();
        Thread.sleep(100);
    }
    // Wait for a the servlet to block.
    Assert.assertTrue(writePending.await(5, TimeUnit.SECONDS));
    long expected = Files.size(resourcePath);
    byte[] buffer = new byte[48 * 1024];
    List<Exchanger<Long>> totals = new ArrayList<>();
    for (Socket socket : sockets) {
        final Exchanger<Long> x = new Exchanger<>();
        totals.add(x);
        final InputStream input = socket.getInputStream();
        new Thread() {

            @Override
            public void run() {
                long total = 0;
                try {
                    // look for CRLFCRLF
                    StringBuilder header = new StringBuilder();
                    int state = 0;
                    while (state < 4 && header.length() < 2048) {
                        int ch = input.read();
                        if (ch < 0)
                            break;
                        header.append((char) ch);
                        switch(state) {
                            case 0:
                                if (ch == '\r')
                                    state = 1;
                                break;
                            case 1:
                                if (ch == '\n')
                                    state = 2;
                                else
                                    state = 0;
                                break;
                            case 2:
                                if (ch == '\r')
                                    state = 3;
                                else
                                    state = 0;
                                break;
                            case 3:
                                if (ch == '\n')
                                    state = 4;
                                else
                                    state = 0;
                                break;
                        }
                    }
                    while (total < expected) {
                        int read = input.read(buffer);
                        if (read < 0)
                            break;
                        total += read;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        x.exchange(total);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }
    for (Exchanger<Long> x : totals) {
        Long total = x.exchange(-1L, 10000, TimeUnit.SECONDS);
        Assert.assertEquals(expected, total.longValue());
    }
    // We could read everything, good.
    for (Socket socket : sockets) socket.close();
}
Also used : ManagedSelector(org.eclipse.jetty.io.ManagedSelector) SocketChannel(java.nio.channels.SocketChannel) Server(org.eclipse.jetty.server.Server) Exchanger(java.util.concurrent.Exchanger) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) ServerConnector(org.eclipse.jetty.server.ServerConnector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) DefaultServlet(org.eclipse.jetty.servlet.DefaultServlet) Path(java.nio.file.Path) SelectionKey(java.nio.channels.SelectionKey) InputStream(java.io.InputStream) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ChannelEndPoint(org.eclipse.jetty.io.ChannelEndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) File(java.io.File) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Socket(java.net.Socket) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 48 with ServerConnector

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

the class MemoryUsageTest method prepare.

@Before
public void prepare() throws Exception {
    server = new Server();
    connector = new ServerConnector(server);
    server.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler(server, "/", true, false);
    ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
    ServerEndpointConfig config = ServerEndpointConfig.Builder.create(BasicEchoEndpoint.class, "/").build();
    container.addEndpoint(config);
    server.start();
    client = ContainerProvider.getWebSocketContainer();
    server.addBean(client, true);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) BasicEchoEndpoint(org.eclipse.jetty.websocket.jsr356.server.samples.echo.BasicEchoEndpoint) Server(org.eclipse.jetty.server.Server) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Before(org.junit.Before)

Example 49 with ServerConnector

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

the class JsrBatchModeTest method prepare.

@Before
public void prepare() throws Exception {
    server = new Server();
    connector = new ServerConnector(server);
    server.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler(server, "/", true, false);
    ServerContainer container = WebSocketServerContainerInitializer.configureContext(context);
    ServerEndpointConfig config = ServerEndpointConfig.Builder.create(BasicEchoEndpoint.class, "/").build();
    container.addEndpoint(config);
    server.start();
    client = ContainerProvider.getWebSocketContainer();
    server.addBean(client, true);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) BasicEchoEndpoint(org.eclipse.jetty.websocket.jsr356.server.samples.echo.BasicEchoEndpoint) Server(org.eclipse.jetty.server.Server) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Before(org.junit.Before)

Example 50 with ServerConnector

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

the class StreamTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.addConnector(connector);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/");
    server.setHandler(context);
    ServerContainer wsContainer = WebSocketServerContainerInitializer.configureContext(context);
    // Prepare Server Side Output directory for uploaded files
    outputDir = MavenTestingUtils.getTargetTestingDir(StreamTest.class.getName());
    FS.ensureEmpty(outputDir);
    // Create Server Endpoint with output directory configuration
    ServerEndpointConfig config = ServerEndpointConfig.Builder.create(UploadSocket.class, "/upload/{filename}").configurator(new ServerUploadConfigurator(outputDir)).build();
    wsContainer.addEndpoint(config);
    server.start();
    String host = connector.getHost();
    if (host == null) {
        host = "localhost";
    }
    int port = connector.getLocalPort();
    serverUri = new URI(String.format("ws://%s:%d/", host, port));
    if (LOG.isDebugEnabled())
        LOG.debug("Server started on {}", serverUri);
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) URI(java.net.URI) ServerEndpoint(javax.websocket.server.ServerEndpoint) ClientEndpoint(javax.websocket.ClientEndpoint) BeforeClass(org.junit.BeforeClass)

Aggregations

ServerConnector (org.eclipse.jetty.server.ServerConnector)291 Server (org.eclipse.jetty.server.Server)213 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)91 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)83 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)67 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)64 SslConnectionFactory (org.eclipse.jetty.server.SslConnectionFactory)55 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)53 SecureRequestCustomizer (org.eclipse.jetty.server.SecureRequestCustomizer)45 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)45 URI (java.net.URI)37 Test (org.junit.Test)32 File (java.io.File)30 BeforeClass (org.junit.BeforeClass)29 IOException (java.io.IOException)27 Before (org.junit.Before)25 BeforeClass (org.testng.annotations.BeforeClass)22 ServletException (javax.servlet.ServletException)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)19 Connector (org.eclipse.jetty.server.Connector)19