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();
}
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();
}
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();
}
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 + ']');
}
Aggregations