Search in sources :

Example 1 with AbstractNetworkConnector

use of org.eclipse.jetty.server.AbstractNetworkConnector in project dropwizard by dropwizard.

the class SimpleServerFactoryTest method testBuild.

@Test
public void testBuild() throws Exception {
    environment.jersey().register(new TestResource());
    environment.admin().addTask(new TestTask());
    final Server server = http.build(environment);
    server.start();
    final int port = ((AbstractNetworkConnector) server.getConnectors()[0]).getLocalPort();
    assertThat(httpRequest("GET", "http://localhost:" + port + "/service/test")).isEqualTo("{\"hello\": \"World\"}");
    assertThat(httpRequest("POST", "http://localhost:" + port + "/secret/tasks/hello?name=test_user")).isEqualTo("Hello, test_user!");
    server.stop();
}
Also used : Server(org.eclipse.jetty.server.Server) AbstractNetworkConnector(org.eclipse.jetty.server.AbstractNetworkConnector) Test(org.junit.Test)

Example 2 with AbstractNetworkConnector

use of org.eclipse.jetty.server.AbstractNetworkConnector in project dropwizard by dropwizard.

the class DefaultServerFactoryTest method testGracefulShutdown.

@Test
public void testGracefulShutdown() throws Exception {
    CountDownLatch requestReceived = new CountDownLatch(1);
    CountDownLatch shutdownInvoked = new CountDownLatch(1);
    environment.jersey().register(new TestResource(requestReceived, shutdownInvoked));
    final ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
    final Server server = http.build(environment);
    ((AbstractNetworkConnector) server.getConnectors()[0]).setPort(0);
    ((AbstractNetworkConnector) server.getConnectors()[1]).setPort(0);
    ScheduledFuture<Void> cleanup = executor.schedule(() -> {
        if (!server.isStopped()) {
            server.stop();
        }
        executor.shutdownNow();
        return null;
    }, 5, TimeUnit.SECONDS);
    server.start();
    final int port = ((AbstractNetworkConnector) server.getConnectors()[0]).getLocalPort();
    Future<String> futureResult = executor.submit(() -> {
        URL url = new URL("http://localhost:" + port + "/app/test");
        URLConnection connection = url.openConnection();
        connection.connect();
        return CharStreams.toString(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
    });
    requestReceived.await(10, TimeUnit.SECONDS);
    Future<Void> serverStopped = executor.submit(() -> {
        server.stop();
        return null;
    });
    Connector[] connectors = server.getConnectors();
    assertThat(connectors).isNotEmpty();
    assertThat(connectors[0]).isInstanceOf(NetworkConnector.class);
    NetworkConnector connector = (NetworkConnector) connectors[0];
    // wait for server to close the connectors
    while (true) {
        if (!connector.isOpen()) {
            shutdownInvoked.countDown();
            break;
        }
        Thread.sleep(5);
    }
    String result = futureResult.get();
    assertThat(result).isEqualTo("test");
    serverStopped.get();
    // cancel the cleanup future since everything succeeded
    cleanup.cancel(false);
    executor.shutdownNow();
}
Also used : AbstractNetworkConnector(org.eclipse.jetty.server.AbstractNetworkConnector) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Connector(org.eclipse.jetty.server.Connector) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Server(org.eclipse.jetty.server.Server) InputStreamReader(java.io.InputStreamReader) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) URLConnection(java.net.URLConnection) AbstractNetworkConnector(org.eclipse.jetty.server.AbstractNetworkConnector) AbstractNetworkConnector(org.eclipse.jetty.server.AbstractNetworkConnector) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Test(org.junit.Test)

Example 3 with AbstractNetworkConnector

use of org.eclipse.jetty.server.AbstractNetworkConnector in project dropwizard by dropwizard.

the class ExceptionMapperBinderTest method testOverrideDefaultExceptionMapper.

@Test
public void testOverrideDefaultExceptionMapper() throws Exception {
    environment.jersey().register(new TestValidationResource());
    environment.jersey().register(new MyJerseyExceptionMapper());
    final Server server = http.build(environment);
    server.start();
    final int port = ((AbstractNetworkConnector) server.getConnectors()[0]).getLocalPort();
    assertThat(httpRequest("GET", "http://localhost:" + port + "/service/test")).isEqualTo("alright!");
    server.stop();
}
Also used : Server(org.eclipse.jetty.server.Server) AbstractNetworkConnector(org.eclipse.jetty.server.AbstractNetworkConnector) Test(org.junit.Test)

Example 4 with AbstractNetworkConnector

use of org.eclipse.jetty.server.AbstractNetworkConnector in project ignite by apache.

the class GridJettyRestProtocol method start.

/** {@inheritDoc} */
@SuppressWarnings("BusyWait")
@Override
public void start(GridRestProtocolHandler hnd) throws IgniteCheckedException {
    assert ctx.config().getConnectorConfiguration() != null;
    InetAddress locHost;
    try {
        locHost = U.resolveLocalHost(ctx.config().getLocalHost());
    } catch (IOException e) {
        throw new IgniteCheckedException("Failed to resolve local host to bind address: " + ctx.config().getLocalHost(), e);
    }
    System.setProperty(IGNITE_JETTY_HOST, locHost.getHostAddress());
    jettyHnd = new GridJettyRestHandler(hnd, new C1<String, Boolean>() {

        @Override
        public Boolean apply(String tok) {
            return F.isEmpty(secretKey) || authenticate(tok);
        }
    }, log);
    String jettyPath = config().getJettyPath();
    final URL cfgUrl;
    if (jettyPath == null) {
        cfgUrl = null;
        if (log.isDebugEnabled())
            log.debug("Jetty configuration file is not provided, using defaults.");
    } else {
        cfgUrl = U.resolveIgniteUrl(jettyPath);
        if (cfgUrl == null)
            throw new IgniteSpiException("Invalid Jetty configuration file: " + jettyPath);
        else if (log.isDebugEnabled())
            log.debug("Jetty configuration file: " + cfgUrl);
    }
    loadJettyConfiguration(cfgUrl);
    AbstractNetworkConnector connector = getJettyConnector();
    try {
        host = InetAddress.getByName(connector.getHost());
    } catch (UnknownHostException e) {
        throw new IgniteCheckedException("Failed to resolve Jetty host address: " + connector.getHost(), e);
    }
    int initPort = connector.getPort();
    int portRange = config().getPortRange();
    int lastPort = portRange == 0 ? initPort : initPort + portRange - 1;
    for (port = initPort; port <= lastPort; port++) {
        connector.setPort(port);
        if (startJetty()) {
            if (log.isInfoEnabled())
                log.info(startInfo());
            return;
        }
    }
    U.warn(log, "Failed to start Jetty REST server (possibly all ports in range are in use) " + "[firstPort=" + initPort + ", lastPort=" + lastPort + ']');
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UnknownHostException(java.net.UnknownHostException) IOException(java.io.IOException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) InetAddress(java.net.InetAddress) C1(org.apache.ignite.internal.util.typedef.C1) URL(java.net.URL) AbstractNetworkConnector(org.eclipse.jetty.server.AbstractNetworkConnector)

Aggregations

AbstractNetworkConnector (org.eclipse.jetty.server.AbstractNetworkConnector)4 Server (org.eclipse.jetty.server.Server)3 Test (org.junit.Test)3 URL (java.net.URL)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 InetAddress (java.net.InetAddress)1 URLConnection (java.net.URLConnection)1 UnknownHostException (java.net.UnknownHostException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 C1 (org.apache.ignite.internal.util.typedef.C1)1 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)1 Connector (org.eclipse.jetty.server.Connector)1 NetworkConnector (org.eclipse.jetty.server.NetworkConnector)1